Skip to content

Commit c1a0f36

Browse files
committed
WIP
1 parent 38e7406 commit c1a0f36

File tree

5 files changed

+56
-60
lines changed

5 files changed

+56
-60
lines changed

addons/block_code/types/types.gd

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,45 @@ extends Node
33

44
enum BlockType {
55
NONE,
6-
EXECUTE,
76
ENTRY,
8-
# Parameters
9-
STRING,
10-
INT,
11-
FLOAT,
12-
VECTOR2,
13-
BOOL,
14-
COLOR,
15-
NODE
7+
EXECUTE,
8+
VALUE,
9+
}
10+
11+
const VARIANT_TYPE_TO_STRING: Dictionary = {
12+
TYPE_STRING: "STRING",
13+
TYPE_INT: "INT",
14+
TYPE_FLOAT: "FLOAT",
15+
TYPE_BOOL: "BOOL",
16+
TYPE_VECTOR2: "VECTOR2",
17+
TYPE_COLOR: "COLOR",
18+
}
19+
20+
const STRING_TO_VARIANT_TYPE: Dictionary = {
21+
"STRING": TYPE_STRING,
22+
"INT": TYPE_INT,
23+
"FLOAT": TYPE_FLOAT,
24+
"BOOL": TYPE_BOOL,
25+
"VECTOR2": TYPE_VECTOR2,
26+
"COLOR": TYPE_COLOR,
1627
}
1728

1829
const cast_relationships = [
19-
[BlockType.INT, BlockType.FLOAT, "float(%s)"],
20-
[BlockType.FLOAT, BlockType.INT, "int(%s)"],
21-
[BlockType.INT, BlockType.STRING, "str(%s)"],
22-
[BlockType.FLOAT, BlockType.STRING, "str(%s)"],
30+
[TYPE_INT, TYPE_FLOAT, "float(%s)"],
31+
[TYPE_FLOAT, TYPE_INT, "int(%s)"],
32+
[TYPE_INT, TYPE_STRING, "str(%s)"],
33+
[TYPE_FLOAT, TYPE_STRING, "str(%s)"],
2334
]
2435

2536
# Directed graph, edges are CastGraphEdge
2637
static var cast_graph: Dictionary
2738

2839

2940
class CastGraphEdge:
30-
var to: BlockType
41+
var to: Variant.Type
3142
var cast_format: String
3243

33-
func _init(p_to: BlockType, p_cast_format: String):
44+
func _init(p_to: Variant.Type, p_cast_format: String):
3445
to = p_to
3546
cast_format = p_cast_format
3647

@@ -56,7 +67,7 @@ static var dist: Dictionary
5667
const INT_MAX: int = 1000000000
5768

5869

59-
static func dijkstra(source: BlockType):
70+
static func dijkstra(source: Variant.Type):
6071
prev = {}
6172
dist = {}
6273

@@ -86,7 +97,7 @@ static func dijkstra(source: BlockType):
8697
queue.update_priority(v, alt)
8798

8899

89-
static func can_cast(type: BlockType, parent_type: BlockType) -> bool:
100+
static func can_cast(type: Variant.Type, parent_type: Variant.Type) -> bool:
90101
if type == parent_type:
91102
return true
92103

@@ -96,7 +107,7 @@ static func can_cast(type: BlockType, parent_type: BlockType) -> bool:
96107
return false
97108

98109

99-
static func cast(val: String, type: BlockType, parent_type: BlockType):
110+
static func cast(val: String, type: Variant.Type, parent_type: Variant.Type):
100111
if type == parent_type:
101112
return val
102113

addons/block_code/ui/blocks/parameter_block/parameter_block.gd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ extends Block
44

55
@export var block_format: String = ""
66
@export var statement: String = ""
7+
@export var variant_type: Variant.Type
78

89
@onready var _panel := $Panel
910
@onready var _hbox := %HBoxContainer
@@ -15,6 +16,7 @@ var param_input_strings: Dictionary # Only loaded from serialized
1516
func _ready():
1617
super()
1718

19+
block_type = Types.BlockType.VALUE
1820
var new_panel = _panel.get_theme_stylebox("panel").duplicate()
1921
new_panel.bg_color = color
2022
new_panel.border_color = color.darkened(0.2)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,12 @@ static func format_string(parent_block: Block, attach_to: Node, string: String)
100100
var split := param.split(": ")
101101
var param_name := split[0]
102102
var param_type_str := split[1]
103-
var param_type := Types.BlockType.get(param_type_str)
103+
var param_type: Variant.Type = Types.STRING_TO_VARIANT_TYPE[param_type_str]
104104

105105
var param_input: ParameterInput = preload("res://addons/block_code/ui/blocks/utilities/parameter_input/parameter_input.tscn").instantiate()
106106
param_input.name = "ParameterInput%d" % start # Unique path
107107
param_input.placeholder = param_name
108-
param_input.block_type = param_type
108+
param_input.variant_type = param_type
109109
param_input.block = parent_block
110110
param_input.text_modified.connect(func(): parent_block.modified.emit())
111111
attach_to.add_child(param_input)
@@ -115,7 +115,7 @@ static func format_string(parent_block: Block, attach_to: Node, string: String)
115115
var new_block: Block = preload("res://addons/block_code/ui/blocks/parameter_block/parameter_block.tscn").instantiate()
116116
new_block.block_format = param_name
117117
new_block.statement = param_name
118-
new_block.block_type = param_type
118+
new_block.variant_type = param_type
119119
new_block.color = parent_block.color
120120
param_input.block_type = Types.BlockType.NONE
121121
param_input.snap_point.block_type = Types.BlockType.NONE # Necessary because already called ready

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ signal text_modified
99

1010
@export var block_path: NodePath
1111

12-
@export var block_type: Types.BlockType = Types.BlockType.STRING
12+
@export var variant_type: Variant.Type = TYPE_STRING
13+
@export var block_type: Types.BlockType = Types.BlockType.VALUE
1314

1415
var block: Block
1516

@@ -50,16 +51,16 @@ func get_snapped_block() -> Block:
5051

5152

5253
func get_string() -> String:
53-
var snapped_block: Block = get_snapped_block()
54+
var snapped_block: ParameterBlock = get_snapped_block() as ParameterBlock
5455
if snapped_block:
5556
var generated_string = snapped_block.get_parameter_string()
56-
return Types.cast(generated_string, snapped_block.block_type, block_type)
57+
return Types.cast(generated_string, snapped_block.variant_type, variant_type)
5758

5859
var text: String = get_plain_text()
5960

60-
if block_type == Types.BlockType.STRING:
61+
if variant_type == TYPE_STRING:
6162
text = "'%s'" % text
62-
if block_type == Types.BlockType.VECTOR2:
63+
if variant_type == TYPE_VECTOR2:
6364
text = "Vector2(%s)" % text
6465

6566
return text

addons/block_code/ui/picker/categories/category_factory.gd

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,13 @@ static func get_general_categories() -> Array[BlockCategory]:
126126
signal_list.append(b)
127127

128128
b = BLOCKS["parameter_block"].instantiate()
129-
b.block_type = Types.BlockType.BOOL
129+
b.variant_type = TYPE_BOOL
130130
b.block_format = "Is in group {group: STRING}"
131131
b.statement = "is_in_group({group})"
132132
signal_list.append(b)
133133

134134
b = BLOCKS["parameter_block"].instantiate()
135-
b.block_type = Types.BlockType.BOOL
135+
b.variant_type = TYPE_BOOL
136136
b.block_format = "Is {node: NODE} in group {group: STRING}"
137137
b.statement = "{node}.is_in_group({group})"
138138
signal_list.append(b)
@@ -158,7 +158,7 @@ static func get_general_categories() -> Array[BlockCategory]:
158158
variable_list.append(b)
159159

160160
b = BLOCKS["parameter_block"].instantiate()
161-
b.block_type = Types.BlockType.INT
161+
b.variant_type = TYPE_INT
162162
b.block_format = "Get Int {var: STRING}"
163163
b.statement = "VAR_DICT[{var}]"
164164
variable_list.append(b)
@@ -174,31 +174,31 @@ static func get_general_categories() -> Array[BlockCategory]:
174174
var math_list: Array[Block] = []
175175

176176
b = BLOCKS["parameter_block"].instantiate()
177-
b.block_type = Types.BlockType.INT
177+
b.variant_type = TYPE_INT
178178
b.block_format = "{a: INT} + {b: INT}"
179179
b.statement = "({a} + {b})"
180180
math_list.append(b)
181181

182182
b = BLOCKS["parameter_block"].instantiate()
183-
b.block_type = Types.BlockType.INT
183+
b.variant_type = TYPE_INT
184184
b.block_format = "{a: INT} - {b: INT}"
185185
b.statement = "({a} - {b})"
186186
math_list.append(b)
187187

188188
b = BLOCKS["parameter_block"].instantiate()
189-
b.block_type = Types.BlockType.INT
189+
b.variant_type = TYPE_INT
190190
b.block_format = "{a: INT} * {b: INT}"
191191
b.statement = "({a} * {b})"
192192
math_list.append(b)
193193

194194
b = BLOCKS["parameter_block"].instantiate()
195-
b.block_type = Types.BlockType.INT
195+
b.variant_type = TYPE_INT
196196
b.block_format = "{a: INT} / {b: INT}"
197197
b.statement = "({a} / {b})"
198198
math_list.append(b)
199199

200200
b = BLOCKS["parameter_block"].instantiate()
201-
b.block_type = Types.BlockType.INT
201+
b.variant_type = TYPE_INT
202202
b.block_format = "{base: INT} ^ {exp: INT}"
203203
b.statement = "(pow({base}, {exp}))"
204204
math_list.append(b)
@@ -211,20 +211,20 @@ static func get_general_categories() -> Array[BlockCategory]:
211211

212212
for op in ["==", ">", "<", ">=", "<=", "!="]:
213213
b = BLOCKS["parameter_block"].instantiate()
214-
b.block_type = Types.BlockType.BOOL
214+
b.variant_type = TYPE_BOOL
215215
b.block_format = "{int1: INT} %s {int2: INT}" % op
216216
b.statement = "({int1} %s {int2})" % op
217217
logic_list.append(b)
218218

219219
for op in ["and", "or"]:
220220
b = BLOCKS["parameter_block"].instantiate()
221-
b.block_type = Types.BlockType.BOOL
221+
b.variant_type = TYPE_BOOL
222222
b.block_format = "{bool1: BOOL} %s {bool2: BOOL}" % op
223223
b.statement = "({bool1} %s {bool2})" % op
224224
logic_list.append(b)
225225

226226
b = BLOCKS["parameter_block"].instantiate()
227-
b.block_type = Types.BlockType.BOOL
227+
b.variant_type = TYPE_BOOL
228228
b.block_format = "Not {bool: BOOL}"
229229
b.statement = "(!{bool})"
230230
logic_list.append(b)
@@ -264,31 +264,13 @@ static func add_to_categories(main: Array[BlockCategory], addition: Array[BlockC
264264
return main
265265

266266

267-
static func built_in_type_to_block_type(type: Variant.Type):
268-
match type:
269-
TYPE_BOOL:
270-
return Types.BlockType.BOOL
271-
TYPE_INT:
272-
return Types.BlockType.INT
273-
TYPE_FLOAT:
274-
return Types.BlockType.FLOAT
275-
TYPE_STRING:
276-
return Types.BlockType.STRING
277-
TYPE_VECTOR2:
278-
return Types.BlockType.VECTOR2
279-
TYPE_COLOR:
280-
return Types.BlockType.COLOR
281-
282-
return null
283-
284-
285267
static func property_to_blocklist(property: Dictionary) -> Array[Block]:
286268
var block_list: Array[Block] = []
287269

288-
var block_type = built_in_type_to_block_type(property.type)
270+
var block_type = property.type
289271

290272
if block_type:
291-
var type_string: String = Types.BlockType.find_key(block_type)
273+
var type_string: String = Types.VARIANT_TYPE_TO_STRING[block_type]
292274

293275
var b = BLOCKS["statement_block"].instantiate()
294276
b.block_format = "Set %s to {value: %s}" % [property.name.capitalize(), type_string]
@@ -376,19 +358,19 @@ static func _get_input_blocks() -> Array[Block]:
376358

377359
for action: StringName in InputMap.get_actions():
378360
var block: Block = BLOCKS["parameter_block"].instantiate()
379-
block.block_type = Types.BlockType.BOOL
361+
block.variant_type = TYPE_BOOL
380362
block.block_format = "Is action %s pressed" % action
381363
block.statement = 'Input.is_action_pressed("%s")' % action
382364
block_list.append(block)
383365

384366
block = BLOCKS["parameter_block"].instantiate()
385-
block.block_type = Types.BlockType.BOOL
367+
block.variant_type = TYPE_BOOL
386368
block.block_format = "Is action %s just pressed" % action
387369
block.statement = 'Input.is_action_just_pressed("%s")' % action
388370
block_list.append(block)
389371

390372
block = BLOCKS["parameter_block"].instantiate()
391-
block.block_type = Types.BlockType.BOOL
373+
block.variant_type = TYPE_BOOL
392374
block.block_format = "Is action %s just released" % action
393375
block.statement = 'Input.is_action_just_released("%s")' % action
394376
block_list.append(block)

0 commit comments

Comments
 (0)