Skip to content

Commit a808f67

Browse files
committed
Select parent node when a BlockCode is selected
In order for a node to be selected on the 2D canvas while its associated BlockCode is being edited, automatically select the current BlockCode node's parent. This requires some changes in BlockCodePlugin to deal with cases when multiple nodes are selected in the Scene panel and in the Inspector panel. https://phabricator.endlessm.com/T35541
1 parent 875a7c8 commit a808f67

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

addons/block_code/block_code_plugin.gd

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ static var main_panel: MainPanel
77
static var block_code_button: Button
88

99
var editor_inspector: EditorInspector
10+
var editor_selection: EditorSelection
1011

1112
var selected_block_code_node: BlockCode
1213

@@ -51,6 +52,7 @@ func _enter_tree():
5152
Types.init_cast_graph()
5253

5354
editor_inspector = EditorInterface.get_inspector()
55+
editor_selection = EditorInterface.get_selection()
5456

5557
main_panel = MainPanel.instantiate()
5658
main_panel.undo_redo = get_undo_redo()
@@ -101,25 +103,42 @@ func _ready():
101103

102104

103105
func _on_scene_changed(scene_root: Node):
104-
BlockCodePlugin.main_panel.switch_scene(scene_root)
106+
main_panel.switch_scene(scene_root)
105107
_on_editor_inspector_edited_object_changed()
106108

107109

108110
func _on_editor_inspector_edited_object_changed():
109-
var edited_node = editor_inspector.get_edited_object() as Node
111+
var edited_object = editor_inspector.get_edited_object()
112+
#var edited_node = edited_object as Node
113+
var selected_nodes = editor_selection.get_selected_nodes()
114+
115+
if edited_object is BlockCode and selected_nodes.has(edited_object):
116+
# If a BlockCode node is being edited, and it was explicitly selected
117+
# (as opposed to edited in the Inspector alone), select its parent node
118+
# as well. This provides a clearer indication of what is being edited.
119+
# Changing the selection will cause edited_object_changed to fire again,
120+
# so we will return early to avoid duplicate work.
121+
var parent_node = edited_object.get_parent()
122+
if parent_node:
123+
EditorInterface.get_selection().add_node.call_deferred(parent_node)
124+
make_bottom_panel_item_visible(main_panel)
125+
return
126+
127+
if edited_object and edited_object.get_class() == "MultiNodeEdit":
128+
# If multiple nodes are shown in the inspector, we will find the first
129+
# BlockCode node in the list of selected nodes and use that. This
130+
# occurs when the user selects multiple items in the Scene panel, or
131+
# when we select the parent of a BlockCode node.
132+
edited_object = selected_nodes.filter(func(node): return node is BlockCode).pop_front()
110133

111134
# We will edit either the selected node (if it is a BlockCode node) or
112-
# the first BlockCode child of that node.
113-
selected_block_code_node = list_block_code_for_node(edited_node).pop_front()
135+
# the first BlockCode child of that node. Keep track of the block code node
136+
# being edited so we know to monitor for changes from EditorInspector.
137+
selected_block_code_node = list_block_code_for_node(edited_object as Node).pop_front()
114138
if not is_block_code_editable(selected_block_code_node):
115139
selected_block_code_node = null
116140

117-
BlockCodePlugin.main_panel.switch_block_code_node(selected_block_code_node)
118-
if edited_node is BlockCode:
119-
# If the user explicitly chose a BlockCode node, show the Block Code
120-
# editor. We only do this for the BlockCode node itself, rather than
121-
# nodes containing BlockCode, to avoid conflicts with other panels.
122-
make_bottom_panel_item_visible(main_panel)
141+
main_panel.switch_block_code_node(selected_block_code_node)
123142

124143

125144
static func is_block_code_editable(block_code: BlockCode) -> bool:

addons/block_code/ui/title_bar/title_bar.gd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,8 @@ func _get_index_for_bsd(bsd: BlockScriptData) -> int:
6363

6464
func _on_node_option_button_item_selected(index):
6565
var block_code_node = _node_option_button.get_item_metadata(index) as BlockCode
66+
var parent_node = block_code_node.get_parent() as Node
6667
_editor_selection.clear()
6768
_editor_selection.add_node(block_code_node)
69+
if parent_node:
70+
_editor_selection.add_node(parent_node)

0 commit comments

Comments
 (0)