Skip to content

Commit 3cf47d0

Browse files
authored
Merge pull request #137 from endlessm/improve-serialization
Serialization: Reuse SerializedBlock resources
2 parents 5ce3180 + 22ef121 commit 3cf47d0

File tree

3 files changed

+45
-28
lines changed

3 files changed

+45
-28
lines changed

addons/block_code/ui/block_canvas/block_canvas.gd

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,9 @@ func bsd_selected(bsd: BlockScriptData):
9898

9999
var edited_node = EditorInterface.get_inspector().get_edited_object() as Node
100100

101-
_window.position = Vector2(0, 0)
102-
zoom = 1
101+
if bsd != _current_bsd:
102+
_window.position = Vector2(0, 0)
103+
zoom = 1
103104

104105
_window.visible = false
105106
_zoom_label.visible = false
@@ -170,30 +171,35 @@ func load_tree(parent: Node, node: SerializedBlockTreeNode):
170171
load_tree(scene.get_node(c[0]), c[1])
171172

172173

173-
func get_canvas_block_trees() -> SerializedBlockTreeNodeArray:
174-
var block_trees := SerializedBlockTreeNodeArray.new()
174+
func rebuild_block_trees(undo_redo):
175+
var block_trees_array = []
175176
for c in _window.get_children():
176-
block_trees.array.append(build_tree(c))
177+
block_trees_array.append(build_tree(c, undo_redo))
178+
undo_redo.add_undo_property(_current_bsd.block_trees, "array", _current_bsd.block_trees.array)
179+
undo_redo.add_do_property(_current_bsd.block_trees, "array", block_trees_array)
177180

178-
return block_trees
179181

180-
181-
func build_tree(block: Block) -> SerializedBlockTreeNode:
182-
var n = SerializedBlockTreeNode.new()
183-
n.serialized_block = SerializedBlock.new(block.get_block_class(), block.get_serialized_props())
182+
func build_tree(block: Block, undo_redo: EditorUndoRedoManager) -> SerializedBlockTreeNode:
183+
var path_child_pairs = []
184+
block.update_resources(undo_redo)
184185

185186
for snap in find_snaps(block):
186-
for c in snap.get_children():
187-
if c is Block: # Make sure to not include preview
188-
n.path_child_pairs.append([block.get_path_to(snap), build_tree(c)])
187+
var snapped_block = snap.get_snapped_block()
188+
if snapped_block == null:
189+
continue
190+
path_child_pairs.append([block.get_path_to(snap), build_tree(snapped_block, undo_redo)])
191+
192+
if block.resource.path_child_pairs != path_child_pairs:
193+
undo_redo.add_undo_property(block.resource, "path_child_pairs", block.resource.path_child_pairs)
194+
undo_redo.add_do_property(block.resource, "path_child_pairs", path_child_pairs)
189195

190-
return n
196+
return block.resource
191197

192198

193-
func find_snaps(node: Node) -> Array:
194-
var snaps := []
199+
func find_snaps(node: Node) -> Array[SnapPoint]:
200+
var snaps: Array[SnapPoint]
195201

196-
if node.is_in_group("snap_point"):
202+
if node.is_in_group("snap_point") and node is SnapPoint:
197203
snaps.append(node)
198204
else:
199205
for c in node.get_children():

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

Lines changed: 15 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

3033
func _ready():
3134
mouse_filter = Control.MOUSE_FILTER_IGNORE
@@ -61,6 +64,18 @@ func get_instruction_node() -> InstructionTree.TreeNode:
6164
return node
6265

6366

67+
func update_resources(undo_redo: EditorUndoRedoManager):
68+
if resource == null:
69+
var serialized_block = SerializedBlock.new(get_block_class(), get_serialized_props())
70+
resource = SerializedBlockTreeNode.new(serialized_block)
71+
return
72+
73+
var serialized_props = get_serialized_props()
74+
if serialized_props != resource.serialized_block.serialized_props:
75+
undo_redo.add_undo_property(resource.serialized_block, "serialized_props", resource.serialized_block.serialized_props)
76+
undo_redo.add_do_property(resource.serialized_block, "serialized_props", serialized_props)
77+
78+
6479
# Override this method to add more serialized properties
6580
func get_serialized_props() -> Array:
6681
return serialize_props(["block_name", "label", "color", "block_type", "position", "scope"])

addons/block_code/ui/main_panel.gd

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,28 +128,24 @@ func save_script():
128128
var resource_path_split = block_script.resource_path.split("::", true, 1)
129129
var resource_scene = resource_path_split[0]
130130

131-
undo_redo.create_action("Modify %s's block code script" % _current_block_code_node.get_parent().name)
131+
undo_redo.create_action("Modify %s's block code script" % _current_block_code_node.get_parent().name, UndoRedo.MERGE_DISABLE, _current_block_code_node)
132132

133133
if resource_scene and resource_scene != scene_node.scene_file_path:
134134
# This resource is from another scene. Since the user is changing it
135135
# here, we'll make a copy for this scene rather than changing it in the
136136
# other scene file.
137137
undo_redo.add_undo_property(_current_block_code_node, "block_script", _current_block_code_node.block_script)
138138
block_script = block_script.duplicate(true)
139-
_current_block_code_node.block_script = block_script
140-
undo_redo.add_do_property(_current_block_code_node, "block_script", _current_block_code_node.block_script)
139+
undo_redo.add_do_property(_current_block_code_node, "block_script", block_script)
141140

142-
undo_redo.add_undo_property(_current_block_code_node.block_script, "block_trees", _current_block_code_node.block_script.block_trees)
143-
undo_redo.add_undo_property(_current_block_code_node.block_script, "generated_script", _current_block_code_node.block_script.generated_script)
144-
145-
var block_trees := _block_canvas.get_canvas_block_trees()
141+
_block_canvas.rebuild_block_trees(undo_redo)
146142
var generated_script = _block_canvas.generate_script_from_current_window(block_script)
147-
block_script.block_trees = block_trees
148-
block_script.generated_script = generated_script
143+
if generated_script != block_script.generated_script:
144+
undo_redo.add_undo_property(block_script, "generated_script", block_script.generated_script)
145+
undo_redo.add_do_property(block_script, "generated_script", generated_script)
146+
149147
block_script.version = Constants.CURRENT_DATA_VERSION
150148

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)
153149
undo_redo.commit_action()
154150

155151

0 commit comments

Comments
 (0)