Skip to content

Commit 633e4a1

Browse files
authored
Merge pull request #132 from endlessm/test-serialization
Test serialization
2 parents 743d546 + 6ff5dbd commit 633e4a1

File tree

28 files changed

+346
-469
lines changed

28 files changed

+346
-469
lines changed

addons/block_code/block_code_plugin.gd

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,12 @@ const DISABLED_CLASSES := [
2424
"StatementBlock",
2525
"DragDropArea",
2626
"SnapPoint",
27-
"NodeBlockCanvas",
2827
"SerializedBlockTreeNodeArray",
2928
"SerializedBlockTreeNode",
3029
"SerializedBlock",
3130
"PackedSceneTreeNodeArray",
3231
"PackedSceneTreeNode",
3332
"BlockCanvas",
34-
"NodeCanvas",
35-
"NodeClass",
36-
"NodeClassList",
37-
"NodeData",
38-
"NodePreview",
39-
"NodeList",
4033
"CategoryFactory",
4134
"BlockCategoryDisplay",
4235
"BlockCategory",

addons/block_code/instruction_tree/instruction_tree.gd

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ class IDHandler:
4242
return unique_string
4343

4444

45-
func generate_text(root_node: TreeNode, start_depth: int = 0) -> String:
45+
static func generate_text(root_node: TreeNode, start_depth: int = 0) -> String:
4646
var out = PackedStringArray()
4747
generate_text_recursive(root_node, start_depth, out)
4848
return "".join(out)
4949

5050

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

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

5858
if node.next:
5959
generate_text_recursive(node.next, depth, out)
60+
61+
62+
static func generate_script_from_nodes(nodes: Array[Node], bsd: BlockScriptData) -> String:
63+
var entry_blocks_by_entry_statement: Dictionary = {}
64+
65+
for block in nodes:
66+
if !(block is Block):
67+
continue
68+
69+
if block is EntryBlock:
70+
var entry_statement = block.get_entry_statement()
71+
if not entry_blocks_by_entry_statement.has(entry_statement):
72+
entry_blocks_by_entry_statement[entry_statement] = []
73+
entry_blocks_by_entry_statement[entry_statement].append(block)
74+
75+
var script: String = ""
76+
77+
script += "extends %s\n\n" % bsd.script_inherits
78+
79+
for variable in bsd.variables:
80+
script += "var %s: %s\n\n" % [variable.var_name, type_string(variable.var_type)]
81+
82+
script += "\n"
83+
84+
var init_func = TreeNode.new("func _init():")
85+
86+
for entry_statement in entry_blocks_by_entry_statement:
87+
var entry_blocks: Array[EntryBlock]
88+
entry_blocks.assign(entry_blocks_by_entry_statement[entry_statement])
89+
script += _generate_script_from_entry_blocks(entry_statement, entry_blocks, init_func)
90+
91+
if init_func.children:
92+
script += generate_text(init_func)
93+
94+
return script
95+
96+
97+
static func _generate_script_from_entry_blocks(entry_statement: String, entry_blocks: Array[EntryBlock], init_func: TreeNode) -> String:
98+
var script = entry_statement + "\n"
99+
var signal_node: TreeNode
100+
var is_empty = true
101+
102+
InstructionTree.IDHandler.reset()
103+
104+
for entry_block in entry_blocks:
105+
var next_block := entry_block.bottom_snap.get_snapped_block()
106+
107+
if next_block != null:
108+
var instruction_node: TreeNode = next_block.get_instruction_node()
109+
var to_append := generate_text(instruction_node, 1)
110+
script += to_append
111+
script += "\n"
112+
is_empty = false
113+
114+
if signal_node == null and entry_block.signal_name:
115+
signal_node = TreeNode.new("{0}.connect(_on_{0})".format([entry_block.signal_name]))
116+
117+
if signal_node:
118+
init_func.add_child(signal_node)
119+
120+
if is_empty:
121+
script += "\tpass\n\n"
122+
123+
return script

addons/block_code/simple_nodes/simple_character/simple_character.gd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ static func get_custom_blocks() -> Array[Block]:
104104

105105
# Movement
106106
b = CategoryFactory.BLOCKS["statement_block"].instantiate()
107+
b.block_name = "simplecharacter_move"
107108
b.block_type = Types.BlockType.EXECUTE
108109
b.block_format = "Move with {player: OPTION} buttons as {kind: OPTION}"
109110
# TODO: delta here is assumed to be the parameter name of

addons/block_code/simple_nodes/simple_scoring/simple_scoring.gd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,15 @@ static func get_custom_blocks() -> Array[Block]:
5656

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

6566
b = CategoryFactory.BLOCKS["statement_block"].instantiate()
67+
b.block_name = "simplescoring_change_score"
6668
b.block_type = Types.BlockType.EXECUTE
6769
b.block_format = "Change player %s score by {score: INT}" % player
6870
b.statement = "score_%s += {score}" % _POSITIONS_FOR_PLAYER[player]

addons/block_code/ui/block_canvas/block_canvas.gd

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,3 +306,8 @@ func set_mouse_override(override: bool):
306306
else:
307307
_mouse_override.mouse_filter = Control.MOUSE_FILTER_IGNORE
308308
_mouse_override.mouse_default_cursor_shape = Control.CURSOR_ARROW
309+
310+
311+
func generate_script_from_current_window(bsd: BlockScriptData) -> String:
312+
# TODO: implement multiple windows
313+
return InstructionTree.generate_script_from_nodes(_window.get_children(), bsd)

addons/block_code/ui/block_canvas/block_canvas.tscn

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[ext_resource type="Script" path="res://addons/block_code/ui/block_canvas/block_canvas.gd" id="1_tk8h2"]
44
[ext_resource type="Texture2D" uid="uid://cmusxj1ppspnp" path="res://addons/block_code/block_code_node/block_code_node.svg" id="2_710vn"]
55

6-
[sub_resource type="Image" id="Image_0aray"]
6+
[sub_resource type="Image" id="Image_1nubg"]
77
data = {
88
"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),
99
"format": "RGBA8",
@@ -13,7 +13,7 @@ data = {
1313
}
1414

1515
[sub_resource type="ImageTexture" id="ImageTexture_jgo72"]
16-
image = SubResource("Image_0aray")
16+
image = SubResource("Image_1nubg")
1717

1818
[node name="BlockCanvas" type="MarginContainer"]
1919
anchors_preset = 15

addons/block_code/ui/block_canvas/node_block_canvas/node_block_canvas.gd

Lines changed: 0 additions & 71 deletions
This file was deleted.

addons/block_code/ui/block_canvas/node_block_canvas/node_block_canvas.tscn

Lines changed: 0 additions & 7 deletions
This file was deleted.

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,13 @@ signal modified
2121
@export var category: String
2222

2323
## The next block in the line of execution (can be null if end)
24-
@export var bottom_snap_path: NodePath
24+
@export var bottom_snap: SnapPoint
2525

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

29-
var bottom_snap: SnapPoint
30-
3129

3230
func _ready():
33-
bottom_snap = get_node_or_null(bottom_snap_path)
3431
mouse_filter = Control.MOUSE_FILTER_IGNORE
3532

3633

addons/block_code/ui/blocks/control_block/control_block.tscn

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
[ext_resource type="Script" path="res://addons/block_code/ui/blocks/control_block/control_block.gd" id="1_2hbir"]
44
[ext_resource type="PackedScene" uid="uid://b1oge52xhjqnu" path="res://addons/block_code/ui/blocks/utilities/snap_point/snap_point.tscn" id="3_nhryi"]
55

6-
[node name="ControlBlock" type="MarginContainer"]
6+
[node name="ControlBlock" type="MarginContainer" node_paths=PackedStringArray("bottom_snap")]
77
size_flags_horizontal = 0
88
mouse_filter = 2
99
script = ExtResource("1_2hbir")
1010
block_name = "control_block"
1111
label = "Control Block"
1212
color = Color(0.59979, 0.536348, 0.876215, 1)
13-
bottom_snap_path = NodePath("VBoxContainer/SnapPoint")
13+
bottom_snap = NodePath("VBoxContainer/SnapPoint")
1414

1515
[node name="VBoxContainer" type="VBoxContainer" parent="."]
1616
layout_mode = 2

0 commit comments

Comments
 (0)