Skip to content

Commit 5d8c643

Browse files
committed
Add a widget to switch between BlockCode nodes
https://phabricator.endlessm.com/T35511
1 parent 4845a00 commit 5d8c643

File tree

4 files changed

+79
-31
lines changed

4 files changed

+79
-31
lines changed

addons/block_code/block_code_plugin.gd

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,19 @@ func _exit_tree():
9696

9797

9898
func _ready():
99+
connect("scene_changed", _on_scene_changed)
99100
editor_inspector.connect("edited_object_changed", _on_editor_inspector_edited_object_changed)
101+
_on_scene_changed(EditorInterface.get_edited_scene_root())
102+
_on_editor_inspector_edited_object_changed()
103+
104+
105+
func _on_scene_changed(scene_root: Node):
106+
BlockCodePlugin.main_panel.switch_scene(scene_root)
100107

101108

102109
func _on_editor_inspector_edited_object_changed():
103110
var block_code: BlockCode = editor_inspector.get_edited_object() as BlockCode
104-
if block_code:
105-
BlockCodePlugin.main_panel.switch_script(block_code)
111+
BlockCodePlugin.main_panel.switch_script(block_code)
106112

107113

108114
func _has_main_screen():

addons/block_code/ui/main_panel.gd

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ var eia: EditorInterfaceAccess
88
@onready var _block_canvas: BlockCanvas = %NodeBlockCanvas
99
@onready var _drag_manager: DragManager = %DragManager
1010
@onready var _title_bar: TitleBar = %TitleBar
11+
@onready var _editor_inspector: EditorInspector = EditorInterface.get_inspector()
1112

1213
var block_code_tab: Button
1314
var _current_bsd: BlockScriptData
1415
var _current_block_code_node: BlockCode
16+
var _scene_root: Node
17+
var _block_code_nodes: Array
1518

1619
var undo_redo: EditorUndoRedoManager
1720

@@ -32,17 +35,18 @@ func _on_button_pressed():
3235
_print_generated_script()
3336

3437

38+
func switch_scene(scene_root: Node):
39+
_title_bar.scene_selected(scene_root)
40+
41+
3542
func switch_script(block_code_node: BlockCode):
36-
var bsd = block_code_node.bsd
37-
if bsd:
38-
_current_bsd = bsd
39-
_current_block_code_node = block_code_node
40-
_picker.bsd_selected(bsd)
41-
_title_bar.bsd_selected(bsd)
42-
_block_canvas.bsd_selected(bsd)
43-
block_code_tab.pressed.emit()
44-
else:
45-
print("No block script attached.")
43+
var bsd = block_code_node.bsd if block_code_node else null
44+
_current_bsd = bsd
45+
_current_block_code_node = block_code_node
46+
_picker.bsd_selected(bsd)
47+
_title_bar.bsd_selected(bsd)
48+
_block_canvas.bsd_selected(bsd)
49+
block_code_tab.pressed.emit()
4650

4751

4852
func save_script():

addons/block_code/ui/title_bar/title_bar.gd

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,65 @@ extends MarginContainer
44

55
signal node_name_changed(node_name: String)
66

7-
@onready var _node_name := %NodeName
8-
@onready var _class_name := %ClassName
7+
@onready var _block_code_icon = load("res://addons/block_code/block_code_node/block_code_node.svg") as Texture2D
8+
@onready var _editor_inspector: EditorInspector = EditorInterface.get_inspector()
9+
@onready var _editor_selection: EditorSelection = EditorInterface.get_selection()
10+
@onready var _node_option_button: OptionButton = %NodeOptionButton
11+
12+
13+
func _ready():
14+
_node_option_button.connect("item_selected", _on_node_option_button_item_selected)
15+
16+
17+
func scene_selected(scene_root: Node):
18+
_update_node_option_button_options()
19+
var current_block_code = _editor_inspector.get_edited_object() as BlockCode
20+
if not current_block_code:
21+
bsd_selected(null)
922

1023

1124
func bsd_selected(bsd: BlockScriptData):
12-
_class_name.text = bsd.script_inherits
25+
# TODO: We should listen for property changes in all BlockCode nodes and
26+
# their parents. As a workaround for the UI displaying stale data,
27+
# we'll crudely update the list of BlockCode nodes whenever the
28+
# selection changes.
29+
30+
_update_node_option_button_options()
31+
32+
var select_index = _get_index_for_bsd(bsd)
33+
if _node_option_button.selected != select_index:
34+
_node_option_button.select(select_index)
35+
36+
37+
func _update_node_option_button_options():
38+
_node_option_button.clear()
39+
40+
var scene_root = EditorInterface.get_edited_scene_root()
41+
42+
if not scene_root:
43+
return
44+
45+
for block_code_node in scene_root.find_children("*", "BlockCode"):
46+
var node_item_index = _node_option_button.item_count
47+
var node_label = "{name} ({type})".format({"name": block_code_node.get_parent().name, "type": block_code_node.bsd.script_inherits})
48+
_node_option_button.add_item(node_label)
49+
_node_option_button.set_item_icon(node_item_index, _block_code_icon)
50+
_node_option_button.set_item_metadata(node_item_index, block_code_node)
1351

1452

15-
#func node_selected(node_data: NodeData):
16-
#_node_name.text = node_data.node_name
17-
#_class_name.text = node_data.node_class_name
53+
func _get_index_for_bsd(bsd: BlockScriptData) -> int:
54+
for index in range(_node_option_button.item_count):
55+
var block_code_node = _node_option_button.get_item_metadata(index)
56+
if block_code_node.bsd == bsd:
57+
return index
58+
return -1
1859

1960

20-
func _on_node_name_text_changed(new_text: String):
21-
#node_name_changed.emit(new_text)
22-
pass
61+
func _on_node_option_button_item_selected(index):
62+
var block_code_node = _node_option_button.get_item_metadata(index) as BlockCode
63+
# FIXME: We should clear the existing selection, but at the moment this
64+
# causes the new node to be deselected due to signal handlers being
65+
# called in the wrong order.
66+
#_editor_selection.clear()
67+
if block_code_node:
68+
EditorInterface.edit_node(block_code_node)

addons/block_code/ui/title_bar/title_bar.tscn

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,7 @@ layout_mode = 2
1414
[node name="HBoxContainer" type="HBoxContainer" parent="."]
1515
layout_mode = 2
1616

17-
[node name="NodeName" type="LineEdit" parent="HBoxContainer"]
17+
[node name="NodeOptionButton" type="OptionButton" parent="HBoxContainer"]
1818
unique_name_in_owner = true
19-
custom_minimum_size = Vector2(400, 0)
2019
layout_mode = 2
21-
editable = false
22-
23-
[node name="ClassName" type="Label" parent="HBoxContainer"]
24-
unique_name_in_owner = true
25-
layout_mode = 2
26-
theme_override_colors/font_color = Color(0.501407, 0.501406, 0.501406, 1)
27-
28-
[connection signal="text_changed" from="HBoxContainer/NodeName" to="." method="_on_node_name_text_changed"]
20+
size_flags_horizontal = 3

0 commit comments

Comments
 (0)