Skip to content

Commit c884839

Browse files
committed
Block resources: Dehardcode scene paths
Use the block class in the resources instead of the scene path. This decouples the block data from the block UI. Refactoring the scenes shouldn't break the block data.
1 parent f01b3fd commit c884839

File tree

10 files changed

+130
-95
lines changed

10 files changed

+130
-95
lines changed

addons/block_code/ui/block_canvas/block_canvas.gd

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,25 @@ const EXTEND_MARGIN: float = 800
77
@onready var _window: Control = %Window
88
@onready var _window_scroll: ScrollContainer = %WindowScroll
99

10+
var _block_scenes_by_class = {}
11+
1012
signal reconnect_block(block: Block)
1113

1214

15+
func _ready():
16+
_populate_block_scenes_by_class()
17+
18+
19+
func _populate_block_scenes_by_class():
20+
for _class in ProjectSettings.get_global_class_list():
21+
if not _class.base.ends_with("Block"):
22+
continue
23+
var _script = load(_class.path)
24+
if not _script.has_method("get_scene_path"):
25+
continue
26+
_block_scenes_by_class[_class.class] = _script.get_scene_path()
27+
28+
1329
func add_block(block: Block) -> void:
1430
block.position.y += _window_scroll.scroll_vertical
1531
_window.add_child(block)
@@ -34,14 +50,9 @@ func clear_canvas():
3450
child.queue_free()
3551

3652

37-
#func load_canvas():
38-
#var save: SerializedBlockTreeNodeArray = ResourceLoader.load("user://test_canvas.tres")
39-
#for tree in save.array:
40-
#load_tree(_window, tree)
41-
42-
4353
func load_tree(parent: Node, node: SerializedBlockTreeNode):
44-
var scene: Block = load(node.serialized_block.block_path).instantiate()
54+
var _block_scene_path = _block_scenes_by_class[node.serialized_block.block_class]
55+
var scene: Block = load(_block_scene_path).instantiate()
4556
for prop_pair in node.serialized_block.serialized_props:
4657
scene.set(prop_pair[0], prop_pair[1])
4758

@@ -65,7 +76,7 @@ func get_canvas_block_trees() -> SerializedBlockTreeNodeArray:
6576

6677
func build_tree(block: Block) -> SerializedBlockTreeNode:
6778
var n = SerializedBlockTreeNode.new()
68-
n.serialized_block = SerializedBlock.new(block.get_scene_path(), block.get_serialized_props())
79+
n.serialized_block = SerializedBlock.new(block.get_block_class(), block.get_serialized_props())
6980

7081
for snap in find_snaps(block):
7182
for c in snap.get_children():
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
class_name SerializedBlock
22
extends Resource
33

4-
@export var block_path: String
4+
@export var block_class: StringName
55
@export var serialized_props: Array
66

77

8-
func _init(p_block_path: String = "", p_serialized_props: Array = []):
9-
block_path = p_block_path
8+
func _init(p_block_class: StringName = "", p_serialized_props: Array = []):
9+
block_class = p_block_class
1010
serialized_props = p_serialized_props

addons/block_code/ui/blocks/basic_block/basic_block.gd

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,9 @@ func _on_drag_drop_area_mouse_down():
1717
_drag_started()
1818

1919

20-
func get_scene_path():
20+
static func get_block_class():
21+
return "BasicBlock"
22+
23+
24+
static func get_scene_path():
2125
return "res://addons/block_code/ui/blocks/basic_block/basic_block.tscn"

addons/block_code/ui/blocks/block/block.gd

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@ func _ready():
2929
bottom_snap = get_node_or_null(bottom_snap_path)
3030

3131

32-
func get_scene_path():
33-
return ""
32+
static func get_block_class():
33+
push_error("Unimplemented.")
34+
35+
36+
static func get_scene_path():
37+
push_error("Unimplemented.")
3438

3539

3640
func _drag_started():

addons/block_code/ui/blocks/control_block/control_block.gd

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ func get_serialized_props() -> Array:
8383
return props
8484

8585

86-
func get_scene_path():
86+
static func get_block_class():
87+
return "ControlBlock"
88+
89+
90+
static func get_scene_path():
8791
return "res://addons/block_code/ui/blocks/control_block/control_block.tscn"
8892

8993

addons/block_code/ui/blocks/entry_block/entry_block.gd

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ func _ready():
77
super()
88

99

10-
func get_scene_path():
10+
static func get_block_class():
11+
return "EntryBlock"
12+
13+
14+
static func get_scene_path():
1115
return "res://addons/block_code/ui/blocks/entry_block/entry_block.tscn"
1216

1317

addons/block_code/ui/blocks/parameter_block/parameter_block.gd

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ func get_parameter_string() -> String:
5454
return formatted_statement
5555

5656

57-
func get_scene_path():
57+
static func get_block_class():
58+
return "ParameterBlock"
59+
60+
61+
static func get_scene_path():
5862
return "res://addons/block_code/ui/blocks/parameter_block/parameter_block.tscn"
5963

6064

addons/block_code/ui/blocks/statement_block/statement_block.gd

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ func get_serialized_props() -> Array:
4242
return props
4343

4444

45-
func get_scene_path():
45+
static func get_block_class():
46+
return "StatementBlock"
47+
48+
49+
static func get_scene_path():
4650
return "res://addons/block_code/ui/blocks/statement_block/statement_block.tscn"
4751

4852

addons/block_code/ui/bsd_templates/default_blocktrees.tres

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
[sub_resource type="Resource" id="Resource_lonji"]
88
script = ExtResource("2_qtg7h")
9-
block_path = "res://addons/block_code/ui/blocks/entry_block/entry_block.tscn"
9+
block_class = &"EntryBlock"
1010
serialized_props = [["block_name", "ready_block"], ["label", "EntryBlock"], ["color", Color(0.980392, 0.34902, 0.337255, 1)], ["block_type", 2], ["position", Vector2(54, 47)], ["block_format", "On Ready"], ["statement", "func _ready():"], ["param_input_strings", {}]]
1111

1212
[sub_resource type="Resource" id="Resource_uxduk"]
@@ -16,7 +16,7 @@ path_child_pairs = []
1616

1717
[sub_resource type="Resource" id="Resource_8uoy7"]
1818
script = ExtResource("2_qtg7h")
19-
block_path = "res://addons/block_code/ui/blocks/entry_block/entry_block.tscn"
19+
block_class = &"EntryBlock"
2020
serialized_props = [["block_name", "process_block"], ["label", "EntryBlock"], ["color", Color(0.980392, 0.34902, 0.337255, 1)], ["block_type", 2], ["position", Vector2(525, 48)], ["block_format", "On Process"], ["statement", "func _process(delta):"], ["param_input_strings", {}]]
2121

2222
[sub_resource type="Resource" id="Resource_qsjc2"]

0 commit comments

Comments
 (0)