Skip to content

Test serialization #132

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 7 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 0 additions & 7 deletions addons/block_code/block_code_plugin.gd
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,12 @@ const DISABLED_CLASSES := [
"StatementBlock",
"DragDropArea",
"SnapPoint",
"NodeBlockCanvas",
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 not worth the generalization, there is a single kind of canvas.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep, at one point I thought maybe we could have scripts that aren't attached to nodes, which is an interesting idea, but for now BlockCanvas has already started accumulating code that is reliant on attaching scripts to a node, so it makes sense to combine them.

"SerializedBlockTreeNodeArray",
"SerializedBlockTreeNode",
"SerializedBlock",
"PackedSceneTreeNodeArray",
"PackedSceneTreeNode",
"BlockCanvas",
"NodeCanvas",
"NodeClass",
"NodeClassList",
"NodeData",
"NodePreview",
"NodeList",
"CategoryFactory",
"BlockCategoryDisplay",
"BlockCategory",
Expand Down
68 changes: 66 additions & 2 deletions addons/block_code/instruction_tree/instruction_tree.gd
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ class IDHandler:
return unique_string


func generate_text(root_node: TreeNode, start_depth: int = 0) -> String:
static func generate_text(root_node: TreeNode, start_depth: int = 0) -> String:
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 yes, these should be static.

var out = PackedStringArray()
generate_text_recursive(root_node, start_depth, out)
return "".join(out)


func generate_text_recursive(node: TreeNode, depth: int, out: PackedStringArray):
static func generate_text_recursive(node: TreeNode, depth: int, out: PackedStringArray):
if node.data != "":
out.append("\t".repeat(depth) + node.data + "\n")

Expand All @@ -57,3 +57,67 @@ func generate_text_recursive(node: TreeNode, depth: int, out: PackedStringArray)

if node.next:
generate_text_recursive(node.next, depth, out)


static func generate_script_from_nodes(nodes: Array[Node], bsd: BlockScriptData) -> String:
var entry_blocks_by_entry_statement: Dictionary = {}

for block in nodes:
if !(block is Block):
continue

if block is EntryBlock:
var entry_statement = block.get_entry_statement()
if not entry_blocks_by_entry_statement.has(entry_statement):
entry_blocks_by_entry_statement[entry_statement] = []
entry_blocks_by_entry_statement[entry_statement].append(block)

var script: String = ""

script += "extends %s\n\n" % bsd.script_inherits

for variable in bsd.variables:
script += "var %s: %s\n\n" % [variable.var_name, type_string(variable.var_type)]

script += "\n"

var init_func = TreeNode.new("func _init():")

for entry_statement in entry_blocks_by_entry_statement:
var entry_blocks: Array[EntryBlock]
entry_blocks.assign(entry_blocks_by_entry_statement[entry_statement])
script += _generate_script_from_entry_blocks(entry_statement, entry_blocks, init_func)

if init_func.children:
script += generate_text(init_func)

return script


static func _generate_script_from_entry_blocks(entry_statement: String, entry_blocks: Array[EntryBlock], init_func: TreeNode) -> String:
var script = entry_statement + "\n"
var signal_node: TreeNode
var is_empty = true

InstructionTree.IDHandler.reset()

for entry_block in entry_blocks:
var next_block := entry_block.bottom_snap.get_snapped_block()

if next_block != null:
var instruction_node: TreeNode = next_block.get_instruction_node()
var to_append := generate_text(instruction_node, 1)
script += to_append
script += "\n"
is_empty = false

if signal_node == null and entry_block.signal_name:
signal_node = TreeNode.new("{0}.connect(_on_{0})".format([entry_block.signal_name]))

if signal_node:
init_func.add_child(signal_node)

if is_empty:
script += "\tpass\n\n"

return script
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ static func get_custom_blocks() -> Array[Block]:

# Movement
b = CategoryFactory.BLOCKS["statement_block"].instantiate()
b.block_name = "simplecharacter_move"
b.block_type = Types.BlockType.EXECUTE
b.block_format = "Move with {player: OPTION} buttons as {kind: OPTION}"
# TODO: delta here is assumed to be the parameter name of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@ static func get_custom_blocks() -> Array[Block]:

for player in _POSITIONS_FOR_PLAYER:
b = CategoryFactory.BLOCKS["statement_block"].instantiate()
b.block_name = "simplescoring_set_score"
b.block_type = Types.BlockType.EXECUTE
b.block_format = "Set player %s score to {score: INT}" % player
b.statement = "score_%s = {score}" % _POSITIONS_FOR_PLAYER[player]
b.category = "Info | Score"
block_list.append(b)

b = CategoryFactory.BLOCKS["statement_block"].instantiate()
b.block_name = "simplescoring_change_score"
b.block_type = Types.BlockType.EXECUTE
b.block_format = "Change player %s score by {score: INT}" % player
b.statement = "score_%s += {score}" % _POSITIONS_FOR_PLAYER[player]
Expand Down
5 changes: 5 additions & 0 deletions addons/block_code/ui/block_canvas/block_canvas.gd
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,8 @@ func set_mouse_override(override: bool):
else:
_mouse_override.mouse_filter = Control.MOUSE_FILTER_IGNORE
_mouse_override.mouse_default_cursor_shape = Control.CURSOR_ARROW


func generate_script_from_current_window(bsd: BlockScriptData) -> String:
# TODO: implement multiple windows
return InstructionTree.generate_script_from_nodes(_window.get_children(), bsd)
4 changes: 2 additions & 2 deletions addons/block_code/ui/block_canvas/block_canvas.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[ext_resource type="Script" path="res://addons/block_code/ui/block_canvas/block_canvas.gd" id="1_tk8h2"]
[ext_resource type="Texture2D" uid="uid://cmusxj1ppspnp" path="res://addons/block_code/block_code_node/block_code_node.svg" id="2_710vn"]

[sub_resource type="Image" id="Image_0aray"]
[sub_resource type="Image" id="Image_1nubg"]
data = {
"data": PackedByteArray(255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 255, 255, 0, 255, 94, 94, 127, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 255, 255, 0, 255, 93, 93, 255, 255, 94, 94, 127, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 255, 255, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 94, 94, 127, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 255, 255, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 94, 94, 127, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 93, 93, 231, 255, 94, 94, 54, 255, 94, 94, 57, 255, 93, 93, 233, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 231, 255, 94, 94, 54, 255, 94, 94, 57, 255, 93, 93, 233, 255, 93, 93, 255, 255, 93, 93, 255, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 97, 97, 42, 255, 255, 255, 0, 255, 255, 255, 0, 255, 97, 97, 42, 255, 93, 93, 233, 255, 93, 93, 232, 255, 93, 93, 41, 255, 255, 255, 0, 255, 255, 255, 0, 255, 97, 97, 42, 255, 93, 93, 233, 255, 93, 93, 232, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 96, 96, 45, 255, 93, 93, 44, 255, 255, 255, 0, 255, 97, 97, 42, 255, 97, 97, 42, 255, 255, 255, 0, 255, 96, 96, 45, 255, 93, 93, 44, 255, 255, 255, 0, 255, 97, 97, 42, 255, 97, 97, 42, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 96, 96, 45, 255, 93, 93, 235, 255, 94, 94, 234, 255, 95, 95, 43, 255, 255, 255, 0, 255, 255, 255, 0, 255, 96, 96, 45, 255, 93, 93, 235, 255, 94, 94, 234, 255, 95, 95, 43, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 93, 93, 235, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 233, 255, 95, 95, 59, 255, 96, 96, 61, 255, 93, 93, 235, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 233, 255, 95, 95, 59, 255, 96, 96, 61, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0),
"format": "RGBA8",
Expand All @@ -13,7 +13,7 @@ data = {
}

[sub_resource type="ImageTexture" id="ImageTexture_jgo72"]
image = SubResource("Image_0aray")
image = SubResource("Image_1nubg")

[node name="BlockCanvas" type="MarginContainer"]
anchors_preset = 15
Expand Down

This file was deleted.

This file was deleted.

5 changes: 1 addition & 4 deletions addons/block_code/ui/blocks/block/block.gd
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,13 @@ signal modified
@export var category: String

## The next block in the line of execution (can be null if end)
@export var bottom_snap_path: NodePath
@export var bottom_snap: SnapPoint
Copy link
Contributor

Choose a reason for hiding this comment

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

I was wondering about this. Thanks for doing it! 👍

Copy link
Member Author

Choose a reason for hiding this comment

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

I wouldn't have even noticed or bothered except it was making it a lot harder to test it without manually calling _ready().


## The scope of the block (statement of matching entry block)
@export var scope: String = ""

var bottom_snap: SnapPoint


func _ready():
bottom_snap = get_node_or_null(bottom_snap_path)
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 yes! no need to do this.

mouse_filter = Control.MOUSE_FILTER_IGNORE


Expand Down
4 changes: 2 additions & 2 deletions addons/block_code/ui/blocks/control_block/control_block.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
[ext_resource type="Script" path="res://addons/block_code/ui/blocks/control_block/control_block.gd" id="1_2hbir"]
[ext_resource type="PackedScene" uid="uid://b1oge52xhjqnu" path="res://addons/block_code/ui/blocks/utilities/snap_point/snap_point.tscn" id="3_nhryi"]

[node name="ControlBlock" type="MarginContainer"]
[node name="ControlBlock" type="MarginContainer" node_paths=PackedStringArray("bottom_snap")]
size_flags_horizontal = 0
mouse_filter = 2
script = ExtResource("1_2hbir")
block_name = "control_block"
label = "Control Block"
color = Color(0.59979, 0.536348, 0.876215, 1)
bottom_snap_path = NodePath("VBoxContainer/SnapPoint")
bottom_snap = NodePath("VBoxContainer/SnapPoint")

[node name="VBoxContainer" type="VBoxContainer" parent="."]
layout_mode = 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
[ext_resource type="PackedScene" uid="uid://c7puyxpqcq6xo" path="res://addons/block_code/ui/blocks/utilities/drag_drop_area/drag_drop_area.tscn" id="2_owgdx"]
[ext_resource type="PackedScene" uid="uid://b1oge52xhjqnu" path="res://addons/block_code/ui/blocks/utilities/snap_point/snap_point.tscn" id="3_5vaov"]

[node name="StatementBlock" type="MarginContainer"]
[node name="StatementBlock" type="MarginContainer" node_paths=PackedStringArray("bottom_snap")]
size_flags_horizontal = 0
mouse_filter = 2
script = ExtResource("1_6wvlf")
block_name = "statement_block"
label = "StatementBlock"
bottom_snap_path = NodePath("VBoxContainer/SnapPoint")
bottom_snap = NodePath("VBoxContainer/SnapPoint")

[node name="VBoxContainer" type="VBoxContainer" parent="."]
layout_mode = 2
Expand Down
8 changes: 4 additions & 4 deletions addons/block_code/ui/main_panel.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class_name MainPanel
extends Control

@onready var _picker: Picker = %Picker
@onready var _block_canvas: BlockCanvas = %NodeBlockCanvas
@onready var _block_canvas: BlockCanvas = %BlockCanvas
@onready var _drag_manager: DragManager = %DragManager
@onready var _title_bar: TitleBar = %TitleBar
@onready var _delete_node_button: Button = %DeleteNodeButton
Expand Down Expand Up @@ -192,7 +192,7 @@ func _on_collapse_button_pressed():
toggle_collapse()


func _on_node_block_canvas_add_block_code():
func _on_block_canvas_add_block_code():
var edited_node: Node = EditorInterface.get_inspector().get_edited_object() as Node
var scene_root: Node = EditorInterface.get_edited_scene_root()

Expand All @@ -214,7 +214,7 @@ func _on_node_block_canvas_add_block_code():
undo_redo.commit_action()


func _on_node_block_canvas_open_scene():
func _on_block_canvas_open_scene():
var edited_node: Node = EditorInterface.get_inspector().get_edited_object() as Node

if edited_node == null or edited_node.owner == null:
Expand All @@ -223,7 +223,7 @@ func _on_node_block_canvas_open_scene():
EditorInterface.open_scene_from_path(edited_node.scene_file_path)


func _on_node_block_canvas_replace_block_code():
func _on_block_canvas_replace_block_code():
var edited_node: Node = EditorInterface.get_inspector().get_edited_object() as Node
var scene_root: Node = EditorInterface.get_edited_scene_root()

Expand Down
Loading