Skip to content

Commit 6bea874

Browse files
committed
Redo categories and variables
1 parent 11a8d3e commit 6bea874

File tree

16 files changed

+230
-152
lines changed

16 files changed

+230
-152
lines changed

addons/block_code/drag_manager/drag_manager.gd

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,9 @@ func drag_block(block: Block, copied_from: Block = null):
243243

244244

245245
func copy_block(block: Block) -> Block:
246-
return CategoryFactory.construct_block_from_resource(block.block_resource) # use instantiation
246+
var new_block = CategoryFactory.construct_block_from_resource(block.block_resource)
247+
new_block.color = block.color
248+
return new_block # use instantiation
247249

248250

249251
func copy_picked_block_and_drag(block: Block):

addons/block_code/examples/pong_game/pong_game.tscn

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[gd_scene load_steps=19 format=3 uid="uid://tf7b8c64ecc0"]
1+
[gd_scene load_steps=22 format=3 uid="uid://tf7b8c64ecc0"]
22

33
[ext_resource type="PackedScene" uid="uid://cg8ibi18um3vg" path="res://addons/block_code/examples/pong_game/space.tscn" id="1_y56ac"]
44
[ext_resource type="Script" path="res://addons/block_code/simple_nodes/simple_character/simple_character.gd" id="3_mdrcv"]
@@ -9,59 +9,73 @@
99
[ext_resource type="Script" path="res://addons/block_code/block_script_data/block_script_data.gd" id="8_roq8v"]
1010
[ext_resource type="Script" path="res://addons/block_code/serialization/block_name_tree.gd" id="9_8q2wm"]
1111
[ext_resource type="PackedScene" uid="uid://c7l70grmkauij" path="res://addons/block_code/examples/pong_game/ball.tscn" id="9_xrqll"]
12+
[ext_resource type="Script" path="res://addons/block_code/ui/block_canvas/variable_resource.gd" id="12_4rr2k"]
1213
[ext_resource type="PackedScene" uid="uid://fhoapg3anjsu" path="res://addons/block_code/examples/pong_game/goal_area.tscn" id="12_nqmxu"]
1314
[ext_resource type="Script" path="res://addons/block_code/simple_nodes/simple_scoring/simple_scoring.gd" id="13_tg3yk"]
1415

15-
[sub_resource type="Resource" id="Resource_vvawh"]
16+
[sub_resource type="Resource" id="Resource_cbe3h"]
17+
script = ExtResource("8_dn27x")
18+
block_name = "set_var_test"
19+
children = Array[ExtResource("8_dn27x")]([])
20+
arguments = {
21+
"value": " world!"
22+
}
23+
24+
[sub_resource type="Resource" id="Resource_rv0b2"]
1625
script = ExtResource("8_268uy")
1726
block_name = "literal"
1827
arguments = {}
1928

20-
[sub_resource type="Resource" id="Resource_j0g38"]
29+
[sub_resource type="Resource" id="Resource_7mcfw"]
2130
script = ExtResource("8_268uy")
22-
block_name = "concat"
23-
arguments = {
24-
"string1": SubResource("Resource_vvawh"),
25-
"string2": " my name is Will"
26-
}
31+
block_name = "get_var_test"
32+
arguments = {}
2733

28-
[sub_resource type="Resource" id="Resource_sdkgq"]
29-
script = ExtResource("8_dn27x")
30-
block_name = "print"
31-
children = Array[ExtResource("8_dn27x")]([])
34+
[sub_resource type="Resource" id="Resource_neai5"]
35+
script = ExtResource("8_268uy")
36+
block_name = "concat"
3237
arguments = {
33-
"text": SubResource("Resource_j0g38")
38+
"string1": SubResource("Resource_rv0b2"),
39+
"string2": SubResource("Resource_7mcfw")
3440
}
3541

36-
[sub_resource type="Resource" id="Resource_exslj"]
42+
[sub_resource type="Resource" id="Resource_ixdpy"]
3743
script = ExtResource("8_dn27x")
3844
block_name = "print"
3945
children = Array[ExtResource("8_dn27x")]([])
4046
arguments = {
41-
"text": "test"
47+
"text": SubResource("Resource_neai5")
4248
}
4349

44-
[sub_resource type="Resource" id="Resource_lxr4w"]
50+
[sub_resource type="Resource" id="Resource_dpxw3"]
4551
script = ExtResource("8_dn27x")
4652
block_name = "ready"
47-
children = Array[ExtResource("8_dn27x")]([SubResource("Resource_sdkgq"), SubResource("Resource_exslj")])
53+
children = Array[ExtResource("8_dn27x")]([SubResource("Resource_cbe3h"), SubResource("Resource_ixdpy")])
4854
arguments = {}
4955

50-
[sub_resource type="Resource" id="Resource_ir6yh"]
56+
[sub_resource type="Resource" id="Resource_h0t3f"]
5157
script = ExtResource("9_8q2wm")
52-
root = SubResource("Resource_lxr4w")
53-
canvas_position = Vector2(50, 0)
58+
root = SubResource("Resource_dpxw3")
59+
canvas_position = Vector2(75, 50)
60+
61+
[sub_resource type="Resource" id="Resource_cjine"]
62+
script = ExtResource("12_4rr2k")
63+
var_name = "test"
64+
var_type = 4
5465

55-
[sub_resource type="Resource" id="Resource_np3yn"]
66+
[sub_resource type="Resource" id="Resource_k5ggu"]
5667
script = ExtResource("8_roq8v")
5768
script_inherits = "Node2D"
58-
block_name_trees = Array[ExtResource("9_8q2wm")]([SubResource("Resource_ir6yh")])
59-
variables = Array[Resource("res://addons/block_code/ui/block_canvas/variable_resource.gd")]([])
69+
block_name_trees = Array[ExtResource("9_8q2wm")]([SubResource("Resource_h0t3f")])
70+
variables = Array[ExtResource("12_4rr2k")]([SubResource("Resource_cjine")])
6071
generated_script = "extends Node2D
6172
73+
var test: String
74+
75+
6276
func _ready():
63-
print(((\"hello\") + (' my name is Will')))
64-
print(('test'))
77+
test = (' world!')
78+
print(((\"hello\") + (test)))
6579
6680
"
6781
version = 0
@@ -105,4 +119,4 @@ zoom = Vector2(0.6, 0.6)
105119

106120
[node name="BlockCode" type="Node" parent="."]
107121
script = ExtResource("7_2jilj")
108-
block_script = SubResource("Resource_np3yn")
122+
block_script = SubResource("Resource_k5ggu")

addons/block_code/serialization/block_resource.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ extends Resource
1414

1515
func _init(
1616
p_block_name: String = "",
17-
p_block_type: Types.BlockType = Types.BlockType.EXECUTE,
17+
p_block_type: Types.BlockType = Types.BlockType.STATEMENT,
1818
p_variant_type: Variant.Type = TYPE_NIL,
1919
p_block_format: String = "",
2020
p_statement: String = "",

addons/block_code/simple_nodes/simple_character/simple_character.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ static func get_custom_blocks() -> Array[Block]:
115115
# Movement
116116
b = CategoryFactory.BLOCKS["statement_block"].instantiate()
117117
b.block_name = "simplecharacter_move"
118-
b.block_type = Types.BlockType.EXECUTE
118+
b.block_type = Types.BlockType.STATEMENT
119119
b.block_format = "Move with {player: OPTION} buttons as {kind: OPTION}"
120120
# TODO: delta here is assumed to be the parameter name of
121121
# the _process or _physics_process method:

addons/block_code/simple_nodes/simple_scoring/simple_scoring.gd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ static func get_custom_blocks() -> Array[Block]:
6868
for player in _POSITIONS_FOR_PLAYER:
6969
b = CategoryFactory.BLOCKS["statement_block"].instantiate()
7070
b.block_name = "simplescoring_set_score"
71-
b.block_type = Types.BlockType.EXECUTE
71+
b.block_type = Types.BlockType.STATEMENT
7272
b.block_format = "Set player %s score to {score: INT}" % player
7373
b.statement = "score_%s = {score}" % _POSITIONS_FOR_PLAYER[player]
7474
b.category = "Info | Score"
7575
block_list.append(b)
7676

7777
b = CategoryFactory.BLOCKS["statement_block"].instantiate()
7878
b.block_name = "simplescoring_change_score"
79-
b.block_type = Types.BlockType.EXECUTE
79+
b.block_type = Types.BlockType.STATEMENT
8080
b.block_format = "Change player %s score by {score: INT}" % player
8181
b.statement = "score_%s += {score}" % _POSITIONS_FOR_PLAYER[player]
8282
b.category = "Info | Score"

addons/block_code/types/types.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ extends Node
44
enum BlockType {
55
NONE,
66
ENTRY,
7-
EXECUTE,
7+
STATEMENT,
88
VALUE,
99
}
1010

addons/block_code/ui/block_canvas/block_canvas.gd

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ const ZOOM_FACTOR: float = 1.1
3232

3333
var _current_bsd: BlockScriptData
3434
var _current_ast_list: ASTList
35+
var _block_resource_cache = {}
36+
var _block_category_cache = {}
3537
var _block_scenes_by_class = {}
3638
var _panning := false
3739
var zoom: float:
@@ -141,6 +143,21 @@ func bsd_selected(bsd: BlockScriptData):
141143

142144

143145
func _load_bsd(bsd: BlockScriptData):
146+
_block_resource_cache = {}
147+
_block_category_cache = {}
148+
149+
var custom_blocks := CategoryFactory.get_blocks_from_bsd(bsd)
150+
var custom_categories := CategoryFactory.get_categories_from_bsd(bsd)
151+
var block_list := CategoryFactory.get_general_blocks() + custom_blocks
152+
var category_list := CategoryFactory.get_categories(block_list, custom_categories)
153+
154+
# Load blocks and categories into cache so ASTs and UI can be created from names
155+
for block_resource in block_list:
156+
_block_resource_cache[block_resource.block_name] = block_resource
157+
for category in category_list:
158+
_block_category_cache[category.name] = category
159+
reload_variables(bsd.variables)
160+
144161
_current_ast_list = ASTList.new()
145162

146163
for name_tree in bsd.block_name_trees:
@@ -150,6 +167,12 @@ func _load_bsd(bsd: BlockScriptData):
150167
reload_ui_from_ast_list()
151168

152169

170+
func reload_variables(variables: Array[VariableResource]):
171+
var block_list := CategoryFactory.get_variable_blocks(variables)
172+
for block_resource in block_list:
173+
_block_resource_cache[block_resource.block_name] = block_resource
174+
175+
153176
func reload_ui_from_ast_list():
154177
for ast_pair in _current_ast_list.array:
155178
var root_block = ui_tree_from_ast_node(ast_pair.ast.root)
@@ -159,6 +182,8 @@ func reload_ui_from_ast_list():
159182

160183
func ui_tree_from_ast_node(ast_node: BlockAST.ASTNode) -> Block:
161184
var block: StatementBlock = CategoryFactory.construct_block_from_resource(ast_node.data)
185+
var category := get_cached_category_from_name(ast_node.data.category)
186+
block.color = category.color
162187
# Args
163188
for arg_name in ast_node.arguments:
164189
var argument = ast_node.arguments[arg_name]
@@ -189,6 +214,8 @@ func ui_tree_from_ast_node(ast_node: BlockAST.ASTNode) -> Block:
189214

190215
func ui_tree_from_ast_value_node(ast_value_node: BlockAST.ASTValueNode) -> Block:
191216
var block = CategoryFactory.construct_block_from_resource(ast_value_node.data)
217+
var category := get_cached_category_from_name(ast_value_node.data.category)
218+
block.color = category.color
192219
# Args
193220
for arg_name in ast_value_node.arguments:
194221
var argument = ast_value_node.arguments[arg_name]
@@ -215,6 +242,20 @@ func clear_canvas():
215242
child.queue_free()
216243

217244

245+
func get_cached_block_resource_from_name(block_name: String) -> BlockResource:
246+
if not block_name in _block_resource_cache:
247+
push_error("Block tried to load from name %s, but was not provided by block canvas cache." % block_name)
248+
249+
return _block_resource_cache[block_name]
250+
251+
252+
func get_cached_category_from_name(category_name: String) -> BlockCategory:
253+
if not category_name in _block_category_cache:
254+
push_error("Category tried to load from name %s, but was not provided by block canvas cache." % category_name)
255+
256+
return _block_category_cache[category_name]
257+
258+
218259
func ast_from_name_tree(tree: BlockNameTree) -> BlockAST:
219260
var ast: BlockAST = BlockAST.new()
220261
ast.root = ast_from_name_tree_recursive(tree.root)
@@ -223,8 +264,7 @@ func ast_from_name_tree(tree: BlockNameTree) -> BlockAST:
223264

224265
func ast_from_name_tree_recursive(node: BlockNameTreeNode):
225266
var ast_node := BlockAST.ASTNode.new()
226-
var block_resource: BlockResource = CategoryFactory.get_block_resource_from_name(node.block_name)
227-
ast_node.data = block_resource
267+
ast_node.data = get_cached_block_resource_from_name(node.block_name)
228268

229269
for arg_name in node.arguments:
230270
var argument = node.arguments[arg_name]
@@ -245,7 +285,7 @@ func ast_from_name_tree_recursive(node: BlockNameTreeNode):
245285

246286
func value_ast_from_value_name_tree_recursive(value_node: ValueBlockNameTreeNode) -> BlockAST.ASTValueNode:
247287
var ast_value_node := BlockAST.ASTValueNode.new()
248-
ast_value_node.data = CategoryFactory.get_block_resource_from_name(value_node.block_name)
288+
ast_value_node.data = get_cached_block_resource_from_name(value_node.block_name)
249289
# Args
250290
for arg_name in value_node.arguments:
251291
var argument = value_node.arguments[arg_name]
@@ -499,9 +539,12 @@ func generate_script_from_current_window() -> String:
499539
else:
500540
combined_entry_asts[statement].root.children.append_array(entry_ast.root.children)
501541

502-
var script := ""
542+
var script := "extends %s\n\n" % _current_bsd.script_inherits
543+
544+
for variable in _current_bsd.variables:
545+
script += "var %s: %s\n\n" % [variable.var_name, type_string(variable.var_type)]
503546

504-
script += "extends %s\n\n" % _current_bsd.script_inherits
547+
script += "\n"
505548

506549
for entry_statement in combined_entry_asts:
507550
var entry_ast: BlockAST = combined_entry_asts[entry_statement]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ signal modified
1515
@export var color: Color = Color(1., 1., 1.)
1616

1717
## Type of block to check if can be attached to snap point
18-
@export var block_type: Types.BlockType = Types.BlockType.EXECUTE
18+
@export var block_type: Types.BlockType = Types.BlockType.STATEMENT
1919

2020
## Category to add the block to
2121
@export var category: String

addons/block_code/ui/blocks/statement_block/statement_block.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var args_to_add_after_format: Dictionary # Only used when loading
1212
func _ready():
1313
super()
1414

15-
if block_type != Types.BlockType.EXECUTE:
15+
if block_type != Types.BlockType.STATEMENT:
1616
_background.show_top = false
1717
_background.color = color
1818

addons/block_code/ui/blocks/utilities/snap_point/snap_point.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
class_name SnapPoint
33
extends MarginContainer
44

5-
@export var block_type: Types.BlockType = Types.BlockType.EXECUTE
5+
@export var block_type: Types.BlockType = Types.BlockType.STATEMENT
66

77
@export var snapped_block: Block:
88
get:

addons/block_code/ui/main_panel.gd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,3 +266,4 @@ func _create_variable(variable: VariableResource):
266266
undo_redo.commit_action()
267267

268268
_picker.reload_variables(new_variables)
269+
_block_canvas.reload_variables(new_variables)

0 commit comments

Comments
 (0)