Skip to content

Commit

Permalink
Add Undo/Redo for choice and condition buttons. (dialogic-godot#2065)
Browse files Browse the repository at this point in the history
* Add add_event_undoable

* Add undo/redo for condition and choice node buttons
  • Loading branch information
Atlinx authored Feb 6, 2024
1 parent 592f934 commit 5e8c3ca
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -305,14 +305,7 @@ func _on_timeline_area_drag_completed(type:int, index:int, data:Variant) -> void
var resource :DialogicEvent = data.duplicate()
resource._load_custom_defaults()

TimelineUndoRedo.create_action("[D] Add "+resource.event_name+" event.")
if resource.can_contain_events:
TimelineUndoRedo.add_do_method(add_event_with_end_branch.bind(resource, index, true, true))
TimelineUndoRedo.add_undo_method(delete_events_at_index.bind(index, 2))
else:
TimelineUndoRedo.add_do_method(add_event_node.bind(resource, index, true, true))
TimelineUndoRedo.add_undo_method(delete_events_at_index.bind(index, 1))
TimelineUndoRedo.commit_action()
add_event_undoable(resource, index)

elif type == %TimelineArea.DragTypes.EXISTING_EVENTS:
if not (len(data) == 1 and data[0].get_index()+1 == index):
Expand All @@ -327,7 +320,6 @@ func _on_timeline_area_drag_completed(type:int, index:int, data:Variant) -> void

#region CREATING THE TIMELINE
################################################################################

# Adding an event to the timeline
func add_event_node(event_resource:DialogicEvent, at_index:int = -1, auto_select: bool = false, indent: bool = false) -> Control:
if event_resource is DialogicEndBranchEvent:
Expand Down Expand Up @@ -386,6 +378,17 @@ func add_event_with_end_branch(resource, at_index:int=-1, auto_select:bool = fal
var event := add_event_node(resource, at_index, auto_select, indent)
create_end_branch_event(at_index+1, event)


## Adds an event (either single nodes or with end branches) to the timeline with UndoRedo support
func add_event_undoable(event_resource: DialogicEvent, at_index: int = -1):
TimelineUndoRedo.create_action("[D] Add "+event_resource.event_name+" event.")
if event_resource.can_contain_events:
TimelineUndoRedo.add_do_method(add_event_with_end_branch.bind(event_resource, at_index, true, true))
TimelineUndoRedo.add_undo_method(delete_events_at_index.bind(at_index, 2))
else:
TimelineUndoRedo.add_do_method(add_event_node.bind(event_resource, at_index, true, true))
TimelineUndoRedo.add_undo_method(delete_events_at_index.bind(at_index, 1))
TimelineUndoRedo.commit_action()
#endregion


Expand Down Expand Up @@ -667,14 +670,7 @@ func _add_event_button_pressed(event_resource:DialogicEvent, force_resource := f

resource.created_by_button = true

TimelineUndoRedo.create_action("[D] Add "+event_resource.event_name+" event.")
if event_resource.can_contain_events:
TimelineUndoRedo.add_do_method(add_event_with_end_branch.bind(resource, at_index, true, true))
TimelineUndoRedo.add_undo_method(delete_events_at_index.bind(at_index, 2))
else:
TimelineUndoRedo.add_do_method(add_event_node.bind(resource, at_index, true, true))
TimelineUndoRedo.add_undo_method(delete_events_at_index.bind(at_index, 1))
TimelineUndoRedo.commit_action()
add_event_undoable(resource, at_index)

resource.created_by_button = false

Expand Down
5 changes: 4 additions & 1 deletion addons/dialogic/Modules/Choice/ui_choice_end.gd
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@ func _on_add_choice_pressed() -> void:
if timeline:
var resource = DialogicChoiceEvent.new()
resource.created_by_button = true
timeline.add_event_with_end_branch(resource, get_parent().get_index()+1)
timeline.add_event_undoable(resource, get_parent().get_index()+1)
timeline.indent_events()
timeline.something_changed()
# Prevent focusing on future redos
resource.created_by_button = false
6 changes: 4 additions & 2 deletions addons/dialogic/Modules/Condition/ui_condition_end.gd
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ func add_elif():
if timeline:
var resource = DialogicConditionEvent.new()
resource.condition_type = DialogicConditionEvent.ConditionTypes.ELIF
timeline.add_event_with_end_branch(resource, get_parent().get_index()+1)
timeline.add_event_undoable(resource, get_parent().get_index()+1)
timeline.indent_events()
timeline.something_changed()

func add_else():
var timeline = find_parent('VisualEditor')
if timeline:
var resource = DialogicConditionEvent.new()
resource.condition_type = DialogicConditionEvent.ConditionTypes.ELSE
timeline.add_event_with_end_branch(resource, get_parent().get_index()+1)
timeline.add_event_undoable(resource, get_parent().get_index()+1)
timeline.indent_events()
timeline.something_changed()

0 comments on commit 5e8c3ca

Please sign in to comment.