Skip to content

Commit 496572d

Browse files
committed
Serialization: Reuse serialized resources
Add a resource property to blocks. The resource will be updated before building the tree. This mitigates having a huge diff in scenes with blocks for minimum changes like moving a block in the canvas.
1 parent 8a12028 commit 496572d

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

addons/block_code/ui/block_canvas/block_canvas.gd

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -168,24 +168,23 @@ func load_tree(parent: Node, node: SerializedBlockTreeNode):
168168
load_tree(scene.get_node(c[0]), c[1])
169169

170170

171-
func get_canvas_block_trees() -> SerializedBlockTreeNodeArray:
172-
var block_trees := SerializedBlockTreeNodeArray.new()
171+
func rebuild_block_trees():
172+
_current_bsd.block_trees.array = []
173173
for c in _window.get_children():
174-
block_trees.array.append(build_tree(c))
175-
176-
return block_trees
174+
_current_bsd.block_trees.array.append(build_tree(c))
177175

178176

179177
func build_tree(block: Block) -> SerializedBlockTreeNode:
180-
var n = SerializedBlockTreeNode.new()
181-
n.serialized_block = SerializedBlock.new(block.get_block_class(), block.get_serialized_props())
178+
block.update_resources()
179+
block.resource.path_child_pairs = []
182180

183181
for snap in find_snaps(block):
184-
for c in snap.get_children():
185-
if c is Block: # Make sure to not include preview
186-
n.path_child_pairs.append([block.get_path_to(snap), build_tree(c)])
182+
for child in snap.get_children():
183+
if not child is Block: # Make sure to not include preview
184+
continue
185+
block.resource.path_child_pairs.append([block.get_path_to(snap), build_tree(child)])
187186

188-
return n
187+
return block.resource
189188

190189

191190
func find_snaps(node: Node) -> Array:

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ signal modified
2626
## The scope of the block (statement of matching entry block)
2727
@export var scope: String = ""
2828

29+
## The resource containing the block properties and the snapped blocks
30+
@export var resource: SerializedBlockTreeNode
31+
2932
var bottom_snap: SnapPoint
3033

3134

@@ -64,6 +67,16 @@ func get_instruction_node() -> InstructionTree.TreeNode:
6467
return node
6568

6669

70+
func update_resources() -> bool:
71+
if resource == null:
72+
resource = SerializedBlockTreeNode.new()
73+
resource.serialized_block = SerializedBlock.new(get_block_class(), get_serialized_props())
74+
var old_serialized_props = resource.serialized_block.serialized_props #.duplicate(true)
75+
resource.serialized_block.serialized_props = get_serialized_props()
76+
var changed = old_serialized_props != resource.serialized_block.serialized_props
77+
return changed
78+
79+
6780
# Override this method to add more serialized properties
6881
func get_serialized_props() -> Array:
6982
return serialize_props(["block_name", "label", "color", "block_type", "position", "scope"])

addons/block_code/ui/main_panel.gd

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,13 @@ func save_script():
142142
undo_redo.add_undo_property(_current_block_code_node.block_script, "block_trees", _current_block_code_node.block_script.block_trees)
143143
undo_redo.add_undo_property(_current_block_code_node.block_script, "generated_script", _current_block_code_node.block_script.generated_script)
144144

145-
var block_trees := _block_canvas.get_canvas_block_trees()
145+
_block_canvas.rebuild_block_trees()
146146
var generated_script = _block_canvas.generate_script_from_current_window(block_script)
147-
block_script.block_trees = block_trees
148147
block_script.generated_script = generated_script
149148
block_script.version = Constants.CURRENT_DATA_VERSION
150149

151-
undo_redo.add_do_property(_current_block_code_node.block_script, "block_trees", block_trees)
152-
undo_redo.add_do_property(_current_block_code_node.block_script, "generated_script", generated_script)
150+
undo_redo.add_do_property(_current_block_code_node.block_script, "block_trees", _current_block_code_node.block_script.block_trees)
151+
undo_redo.add_do_property(_current_block_code_node.block_script, "generated_script", _current_block_code_node.block_script.generated_script)
153152
undo_redo.commit_action()
154153

155154

0 commit comments

Comments
 (0)