Skip to content

Serialization: Reuse SerializedBlock resources #137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
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.
  • Loading branch information
manuq committed Jul 18, 2024
commit 01ab76c54c654b46ae04355fee13fa195a22a54a
29 changes: 18 additions & 11 deletions addons/block_code/ui/block_canvas/block_canvas.gd
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ var zoom: float:
get:
return _window.scale.x

var _undo_redo: EditorUndoRedoManager
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I would just pass this down in to build_tree rather than stashing it in the object. It's only called internally to this object, so there's no danger of it breaking another user.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I though about that, I did it like this because is also used in the build_tree() method that's called recursively. But yes it can be passed around to build_tree() as parameter.


signal reconnect_block(block: Block)
signal add_block_code
signal open_scene
Expand Down Expand Up @@ -170,24 +172,29 @@ func load_tree(parent: Node, node: SerializedBlockTreeNode):
load_tree(scene.get_node(c[0]), c[1])


func get_canvas_block_trees() -> SerializedBlockTreeNodeArray:
var block_trees := SerializedBlockTreeNodeArray.new()
func rebuild_block_trees(undo_redo):
_undo_redo = undo_redo
_current_bsd.block_trees.array = []
for c in _window.get_children():
block_trees.array.append(build_tree(c))

return block_trees
_current_bsd.block_trees.array.append(build_tree(c))


func build_tree(block: Block) -> SerializedBlockTreeNode:
var n = SerializedBlockTreeNode.new()
n.serialized_block = SerializedBlock.new(block.get_block_class(), block.get_serialized_props())
var path_child_pairs = []
block.update_resources(_undo_redo)

for snap in find_snaps(block):
for c in snap.get_children():
if c is Block: # Make sure to not include preview
n.path_child_pairs.append([block.get_path_to(snap), build_tree(c)])
for child in snap.get_children():
if not child is Block: # Make sure to not include preview
continue
path_child_pairs.append([block.get_path_to(snap), build_tree(child)])

if block.resource.path_child_pairs != path_child_pairs:
_undo_redo.add_undo_property(block.resource, "path_child_pairs", block.resource.path_child_pairs)
block.resource.path_child_pairs = path_child_pairs
_undo_redo.add_do_property(block.resource, "path_child_pairs", block.resource.path_child_pairs)

return n
return block.resource


func find_snaps(node: Node) -> Array:
Expand Down
16 changes: 16 additions & 0 deletions addons/block_code/ui/blocks/block/block.gd
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ signal modified
## The scope of the block (statement of matching entry block)
@export var scope: String = ""

## The resource containing the block properties and the snapped blocks
@export var resource: SerializedBlockTreeNode


func _ready():
mouse_filter = Control.MOUSE_FILTER_IGNORE
Expand Down Expand Up @@ -61,6 +64,19 @@ func get_instruction_node() -> InstructionTree.TreeNode:
return node


func update_resources(undo_redo: EditorUndoRedoManager):
if resource == null:
var serialized_block = SerializedBlock.new(get_block_class(), get_serialized_props())
resource = SerializedBlockTreeNode.new(serialized_block)
return

var serialized_props = get_serialized_props()
if serialized_props != resource.serialized_block.serialized_props:
undo_redo.add_undo_property(resource.serialized_block, "serialized_props", resource.serialized_block.serialized_props)
resource.serialized_block.serialized_props = serialized_props
undo_redo.add_do_property(resource.serialized_block, "serialized_props", resource.serialized_block.serialized_props)


# Override this method to add more serialized properties
func get_serialized_props() -> Array:
return serialize_props(["block_name", "label", "color", "block_type", "position", "scope"])
Expand Down
13 changes: 5 additions & 8 deletions addons/block_code/ui/main_panel.gd
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,14 @@ func save_script():
_current_block_code_node.block_script = block_script
undo_redo.add_do_property(_current_block_code_node, "block_script", _current_block_code_node.block_script)

undo_redo.add_undo_property(_current_block_code_node.block_script, "block_trees", _current_block_code_node.block_script.block_trees)
undo_redo.add_undo_property(_current_block_code_node.block_script, "generated_script", _current_block_code_node.block_script.generated_script)

var block_trees := _block_canvas.get_canvas_block_trees()
_block_canvas.rebuild_block_trees(undo_redo)
var generated_script = _block_canvas.generate_script_from_current_window(block_script)
block_script.block_trees = block_trees
block_script.generated_script = generated_script
if generated_script != block_script.generated_script:
undo_redo.add_undo_property(_current_block_code_node.block_script, "generated_script", _current_block_code_node.block_script.generated_script)
block_script.generated_script = generated_script
undo_redo.add_do_property(_current_block_code_node.block_script, "generated_script", _current_block_code_node.block_script.generated_script)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't be changing the generated_script property on our own here, and we should consistently use the var block_script: BlockScriptData we defined earlier:

Suggested change
if generated_script != block_script.generated_script:
undo_redo.add_undo_property(_current_block_code_node.block_script, "generated_script", _current_block_code_node.block_script.generated_script)
block_script.generated_script = generated_script
undo_redo.add_do_property(_current_block_code_node.block_script, "generated_script", _current_block_code_node.block_script.generated_script)
if generated_script != block_script.generated_script:
undo_redo.add_undo_property(block_script, "generated_script", block_script.generated_script)
undo_redo.add_do_property(block_script, "generated_script", generated_script)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out we're doing this in a lot of places. I included this and a bunch of similar fixes in #140.

block_script.version = Constants.CURRENT_DATA_VERSION

undo_redo.add_do_property(_current_block_code_node.block_script, "block_trees", block_trees)
undo_redo.add_do_property(_current_block_code_node.block_script, "generated_script", generated_script)
undo_redo.commit_action()


Expand Down