Skip to content

Commit

Permalink
Block resources: Dehardcode scene paths
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
manuq committed Jun 14, 2024
1 parent f01b3fd commit c884839
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 95 deletions.
27 changes: 19 additions & 8 deletions addons/block_code/ui/block_canvas/block_canvas.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,25 @@ const EXTEND_MARGIN: float = 800
@onready var _window: Control = %Window
@onready var _window_scroll: ScrollContainer = %WindowScroll

var _block_scenes_by_class = {}

signal reconnect_block(block: Block)


func _ready():
_populate_block_scenes_by_class()


func _populate_block_scenes_by_class():
for _class in ProjectSettings.get_global_class_list():
if not _class.base.ends_with("Block"):
continue
var _script = load(_class.path)
if not _script.has_method("get_scene_path"):
continue
_block_scenes_by_class[_class.class] = _script.get_scene_path()


func add_block(block: Block) -> void:
block.position.y += _window_scroll.scroll_vertical
_window.add_child(block)
Expand All @@ -34,14 +50,9 @@ func clear_canvas():
child.queue_free()


#func load_canvas():
#var save: SerializedBlockTreeNodeArray = ResourceLoader.load("user://test_canvas.tres")
#for tree in save.array:
#load_tree(_window, tree)


func load_tree(parent: Node, node: SerializedBlockTreeNode):
var scene: Block = load(node.serialized_block.block_path).instantiate()
var _block_scene_path = _block_scenes_by_class[node.serialized_block.block_class]
var scene: Block = load(_block_scene_path).instantiate()
for prop_pair in node.serialized_block.serialized_props:
scene.set(prop_pair[0], prop_pair[1])

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

func build_tree(block: Block) -> SerializedBlockTreeNode:
var n = SerializedBlockTreeNode.new()
n.serialized_block = SerializedBlock.new(block.get_scene_path(), block.get_serialized_props())
n.serialized_block = SerializedBlock.new(block.get_block_class(), block.get_serialized_props())

for snap in find_snaps(block):
for c in snap.get_children():
Expand Down
6 changes: 3 additions & 3 deletions addons/block_code/ui/block_canvas/serialized_block.gd
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
class_name SerializedBlock
extends Resource

@export var block_path: String
@export var block_class: StringName
@export var serialized_props: Array


func _init(p_block_path: String = "", p_serialized_props: Array = []):
block_path = p_block_path
func _init(p_block_class: StringName = "", p_serialized_props: Array = []):
block_class = p_block_class
serialized_props = p_serialized_props
6 changes: 5 additions & 1 deletion addons/block_code/ui/blocks/basic_block/basic_block.gd
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@ func _on_drag_drop_area_mouse_down():
_drag_started()


func get_scene_path():
static func get_block_class():
return "BasicBlock"


static func get_scene_path():
return "res://addons/block_code/ui/blocks/basic_block/basic_block.tscn"
8 changes: 6 additions & 2 deletions addons/block_code/ui/blocks/block/block.gd
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ func _ready():
bottom_snap = get_node_or_null(bottom_snap_path)


func get_scene_path():
return ""
static func get_block_class():
push_error("Unimplemented.")


static func get_scene_path():
push_error("Unimplemented.")


func _drag_started():
Expand Down
6 changes: 5 additions & 1 deletion addons/block_code/ui/blocks/control_block/control_block.gd
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ func get_serialized_props() -> Array:
return props


func get_scene_path():
static func get_block_class():
return "ControlBlock"


static func get_scene_path():
return "res://addons/block_code/ui/blocks/control_block/control_block.tscn"


Expand Down
6 changes: 5 additions & 1 deletion addons/block_code/ui/blocks/entry_block/entry_block.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ func _ready():
super()


func get_scene_path():
static func get_block_class():
return "EntryBlock"


static func get_scene_path():
return "res://addons/block_code/ui/blocks/entry_block/entry_block.tscn"


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ func get_parameter_string() -> String:
return formatted_statement


func get_scene_path():
static func get_block_class():
return "ParameterBlock"


static func get_scene_path():
return "res://addons/block_code/ui/blocks/parameter_block/parameter_block.tscn"


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ func get_serialized_props() -> Array:
return props


func get_scene_path():
static func get_block_class():
return "StatementBlock"


static func get_scene_path():
return "res://addons/block_code/ui/blocks/statement_block/statement_block.tscn"


Expand Down
4 changes: 2 additions & 2 deletions addons/block_code/ui/bsd_templates/default_blocktrees.tres
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

[sub_resource type="Resource" id="Resource_lonji"]
script = ExtResource("2_qtg7h")
block_path = "res://addons/block_code/ui/blocks/entry_block/entry_block.tscn"
block_class = &"EntryBlock"
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", {}]]

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

[sub_resource type="Resource" id="Resource_8uoy7"]
script = ExtResource("2_qtg7h")
block_path = "res://addons/block_code/ui/blocks/entry_block/entry_block.tscn"
block_class = &"EntryBlock"
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", {}]]

[sub_resource type="Resource" id="Resource_qsjc2"]
Expand Down
Loading

0 comments on commit c884839

Please sign in to comment.