Skip to content

Commit db25ba8

Browse files
authored
Merge pull request #44 from endlessm/add-node-switcher
Add a widget to switch between BlockCode nodes
2 parents a79ef8a + 50f92e8 commit db25ba8

File tree

7 files changed

+121
-32
lines changed

7 files changed

+121
-32
lines changed

addons/block_code/block_code_plugin.gd

+8-2
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/block_canvas/block_canvas.gd

+19
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const EXTEND_MARGIN: float = 800
66

77
@onready var _window: Control = %Window
88
@onready var _window_scroll: ScrollContainer = %WindowScroll
9+
@onready var _choose_block_code_label: Label = %ChooseBlockCodeLabel
10+
@onready var _create_block_code_label: Label = %CreateBlockCodeLabel
911

1012
var _block_scenes_by_class = {}
1113

@@ -41,10 +43,27 @@ func set_child(n: Node):
4143
func bsd_selected(bsd: BlockScriptData):
4244
clear_canvas()
4345

46+
_choose_block_code_label.visible = false
47+
_create_block_code_label.visible = false
48+
49+
if not bsd and scene_has_bsd_nodes():
50+
_choose_block_code_label.visible = true
51+
return
52+
elif not bsd and not scene_has_bsd_nodes():
53+
_create_block_code_label.visible = true
54+
return
55+
4456
for tree in bsd.block_trees.array:
4557
load_tree(_window, tree)
4658

4759

60+
func scene_has_bsd_nodes() -> bool:
61+
var scene_root = EditorInterface.get_edited_scene_root()
62+
if not scene_root:
63+
return false
64+
return scene_root.find_children("*", "BlockCode").size() > 0
65+
66+
4867
func clear_canvas():
4968
for child in _window.get_children():
5069
child.queue_free()

addons/block_code/ui/block_canvas/block_canvas.tscn

+14
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,17 @@ unique_name_in_owner = true
2424
layout_mode = 2
2525
size_flags_horizontal = 3
2626
mouse_filter = 1
27+
28+
[node name="ChooseBlockCodeLabel" type="Label" parent="."]
29+
unique_name_in_owner = true
30+
visible = false
31+
layout_mode = 2
32+
text = "Choose a BlockCode node in the inspector."
33+
horizontal_alignment = 1
34+
35+
[node name="CreateBlockCodeLabel" type="Label" parent="."]
36+
unique_name_in_owner = true
37+
visible = false
38+
layout_mode = 2
39+
text = "First, add a BlockCode node to the scene."
40+
horizontal_alignment = 1

addons/block_code/ui/main_panel.gd

+14-10
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/picker/picker.gd

+9-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ signal block_picked(block: Block)
88

99

1010
func bsd_selected(bsd: BlockScriptData):
11+
if not bsd:
12+
reset_picker()
13+
return
14+
1115
for class_dict in ProjectSettings.get_global_class_list():
1216
if class_dict.class == bsd.script_inherits:
1317
var script = load(class_dict.path)
@@ -19,10 +23,14 @@ func bsd_selected(bsd: BlockScriptData):
1923
init_picker(CategoryFactory.get_inherited_categories(bsd.script_inherits))
2024

2125

22-
func init_picker(extra_blocks: Array[BlockCategory] = []):
26+
func reset_picker():
2327
for c in _block_list.get_children():
2428
c.queue_free()
2529

30+
31+
func init_picker(extra_blocks: Array[BlockCategory] = []):
32+
reset_picker()
33+
2634
var block_categories := CategoryFactory.get_general_categories()
2735

2836
if extra_blocks.size() > 0:

addons/block_code/ui/title_bar/title_bar.gd

+55-9
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": scene_root.get_path_to(block_code_node).get_concatenated_names(), "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

+2-10
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)