Skip to content

Commit 43dd08b

Browse files
committed
Update selection in undo actions
When adding a BlockCode node, select the new node as part of the undo / redo action. This is consistent with the behaviour of other actions in Godot. https://phabricator.endlessm.com/T35541
1 parent b5754fc commit 43dd08b

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

addons/block_code/ui/main_panel.gd

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ var _current_block_code_node: BlockCode
1919
var _block_code_nodes: Array
2020
var _collapsed: bool = false
2121

22-
var undo_redo: EditorUndoRedoManager
22+
var undo_redo: EditorUndoRedoManager:
23+
set(value):
24+
if undo_redo:
25+
undo_redo.version_changed.disconnect(_on_undo_redo_version_changed)
26+
undo_redo = value
27+
if undo_redo:
28+
undo_redo.version_changed.connect(_on_undo_redo_version_changed)
2329

2430

2531
func _ready():
@@ -28,12 +34,10 @@ func _ready():
2834
_drag_manager.block_dropped.connect(save_script)
2935
_drag_manager.block_modified.connect(save_script)
3036

31-
# Setup block scripting environment
32-
undo_redo.version_changed.connect(_on_undo_redo_version_changed)
33-
3437
if not _delete_node_button.icon:
3538
_delete_node_button.icon = _icon_delete
36-
_collapse_button.icon = _icon_collapse
39+
if not _collapse_button.icon:
40+
_collapse_button.icon = _icon_collapse
3741

3842

3943
func _on_undo_redo_version_changed():
@@ -188,15 +192,13 @@ func _on_node_block_canvas_add_block_code():
188192

189193
undo_redo.add_do_method(edited_node, "add_child", block_code, true)
190194
undo_redo.add_do_property(block_code, "owner", scene_root)
195+
undo_redo.add_do_method(self, "_select_block_code_node", edited_node)
191196
undo_redo.add_do_reference(block_code)
192197
undo_redo.add_undo_method(edited_node, "remove_child", block_code)
193198
undo_redo.add_undo_property(block_code, "owner", null)
194199

195200
undo_redo.commit_action()
196201

197-
EditorInterface.get_selection().clear()
198-
EditorInterface.get_selection().add_node(block_code)
199-
200202

201203
func _on_node_block_canvas_open_scene():
202204
var edited_node: Node = EditorInterface.get_inspector().get_edited_object() as Node
@@ -213,12 +215,27 @@ func _on_node_block_canvas_replace_block_code():
213215

214216
undo_redo.create_action("Replace block code %s" % edited_node.name, UndoRedo.MERGE_DISABLE, scene_root)
215217

218+
# FIXME: When this is undone, the new state is not correctly shown in the
219+
# editor due to an issue in Godot:
220+
# <https://github.com/godotengine/godot/issues/45915>
221+
# Ideally this should fix itself in a future version of Godot.
222+
216223
undo_redo.add_do_method(scene_root, "set_editable_instance", edited_node, true)
224+
undo_redo.add_do_method(self, "_select_block_code_node", edited_node)
217225
undo_redo.add_undo_method(scene_root, "set_editable_instance", edited_node, false)
218226

219227
undo_redo.commit_action()
220228

229+
230+
func _select_block_code_node(edited_node: Node):
221231
var block_code_nodes = BlockCodePlugin.list_block_code_for_node(edited_node)
222-
#
232+
if block_code_nodes.size() > 0:
233+
_set_selection([block_code_nodes.pop_front()])
234+
else:
235+
_set_selection([edited_node])
236+
237+
238+
func _set_selection(nodes: Array[Node]):
223239
EditorInterface.get_selection().clear()
224-
EditorInterface.get_selection().add_node(block_code_nodes.pop_front() if block_code_nodes.size() > 0 else edited_node)
240+
for node in nodes:
241+
EditorInterface.get_selection().add_node(node)

0 commit comments

Comments
 (0)