Skip to content

Commit c99abcf

Browse files
committed
Use strings for types
1 parent c1a0f36 commit c99abcf

File tree

11 files changed

+197
-207
lines changed

11 files changed

+197
-207
lines changed

addons/block_code/block_code_plugin.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const DISABLED_CLASSES := [
4545

4646

4747
func _enter_tree():
48-
Types.init_cast_graph()
48+
Types.init_types()
4949

5050
main_panel = MainPanel.instantiate()
5151
main_panel.undo_redo = get_undo_redo()

addons/block_code/drag_manager/drag_manager.gd

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ func _process(_delta):
4646
push_error("Warning: a snap point does not reference it's parent block.")
4747
continue
4848
if snap_point.block.on_canvas:
49-
if Types.can_cast(dragging.block_type, snap_point.block_type):
49+
if dragging.block_type == snap_point.block_type:
50+
# Don't snap uncastable value blocks
51+
if dragging.block_type == Types.BlockType.VALUE:
52+
if !Types.can_cast(dragging.variant_type, snap_point.variant_type):
53+
continue
54+
5055
var snap_global_pos: Vector2 = snap_point.get_global_rect().position
5156
var temp_dist: float = dragging_global_pos.distance_to(snap_global_pos)
5257
if temp_dist < closest_dist:

addons/block_code/types/types.gd

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,47 +9,38 @@ enum BlockType {
99
}
1010

1111
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",
12+
TYPE_STRING: "String",
13+
TYPE_INT: "int",
14+
TYPE_FLOAT: "float",
15+
TYPE_BOOL: "bool",
16+
TYPE_VECTOR2: "Vector2",
17+
TYPE_COLOR: "Color",
1818
}
1919

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,
27-
}
28-
29-
const cast_relationships = [
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)"],
20+
const CAST_RELATIONSHIPS = [
21+
["int", "float", "float(%s)"],
22+
["float", "int", "int(%s)"],
23+
["int", "String", "str(%s)"],
24+
["float", "String", "str(%s)"],
3425
]
3526

3627
# Directed graph, edges are CastGraphEdge
3728
static var cast_graph: Dictionary
3829

3930

4031
class CastGraphEdge:
41-
var to: Variant.Type
32+
var to: String
4233
var cast_format: String
4334

44-
func _init(p_to: Variant.Type, p_cast_format: String):
35+
func _init(p_to: String, p_cast_format: String):
4536
to = p_to
4637
cast_format = p_cast_format
4738

4839

49-
static func init_cast_graph():
40+
static func init_types():
5041
cast_graph = {}
5142

52-
for rel in cast_relationships:
43+
for rel in CAST_RELATIONSHIPS:
5344
if not cast_graph.has(rel[0]):
5445
cast_graph[rel[0]] = []
5546

@@ -67,7 +58,7 @@ static var dist: Dictionary
6758
const INT_MAX: int = 1000000000
6859

6960

70-
static func dijkstra(source: Variant.Type):
61+
static func dijkstra(source: String):
7162
prev = {}
7263
dist = {}
7364

@@ -97,7 +88,7 @@ static func dijkstra(source: Variant.Type):
9788
queue.update_priority(v, alt)
9889

9990

100-
static func can_cast(type: Variant.Type, parent_type: Variant.Type) -> bool:
91+
static func can_cast(type: String, parent_type: String) -> bool:
10192
if type == parent_type:
10293
return true
10394

@@ -107,7 +98,7 @@ static func can_cast(type: Variant.Type, parent_type: Variant.Type) -> bool:
10798
return false
10899

109100

110-
static func cast(val: String, type: Variant.Type, parent_type: Variant.Type):
101+
static func cast(val: String, type: String, parent_type: String):
111102
if type == parent_type:
112103
return val
113104

addons/block_code/ui/blocks/entry_block/entry_block.tscn

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@
77
script = ExtResource("2_3ik8h")
88
block_name = "entry_block"
99
label = "EntryBlock"
10-
block_type = 2
10+
block_type = 1
11+
12+
[node name="Background" parent="VBoxContainer/TopMarginContainer" index="0"]
13+
show_top = false

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

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

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

99
@onready var _panel := $Panel
1010
@onready var _hbox := %HBoxContainer
@@ -16,7 +16,6 @@ var param_input_strings: Dictionary # Only loaded from serialized
1616
func _ready():
1717
super()
1818

19-
block_type = Types.BlockType.VALUE
2019
var new_panel = _panel.get_theme_stylebox("panel").duplicate()
2120
new_panel.bg_color = color
2221
new_panel.border_color = color.darkened(0.2)
@@ -35,7 +34,7 @@ func _on_drag_drop_area_mouse_down():
3534

3635
func get_serialized_props() -> Array:
3736
var props := super()
38-
props.append_array(serialize_props(["block_format", "statement"]))
37+
props.append_array(serialize_props(["variant_type", "block_format", "statement"]))
3938

4039
var _param_input_strings: Dictionary = {}
4140
for pair in param_name_input_pairs:

addons/block_code/ui/blocks/parameter_block/parameter_block.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/blocks/parameter_block/parameter_block.gd" id="1_0hajy"]
44
[ext_resource type="PackedScene" uid="uid://c7puyxpqcq6xo" path="res://addons/block_code/ui/blocks/utilities/drag_drop_area/drag_drop_area.tscn" id="2_gy5co"]
55

6-
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_0afbg"]
6+
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_d7xfk"]
77
bg_color = Color(1, 1, 1, 1)
88
border_width_left = 3
99
border_width_top = 3
@@ -26,7 +26,7 @@ block_type = 3
2626
[node name="Panel" type="Panel" parent="."]
2727
unique_name_in_owner = true
2828
layout_mode = 2
29-
theme_override_styles/panel = SubResource("StyleBoxFlat_0afbg")
29+
theme_override_styles/panel = SubResource("StyleBoxFlat_d7xfk")
3030

3131
[node name="DragDropArea" parent="." instance=ExtResource("2_gy5co")]
3232
layout_mode = 2

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ static func format_string(parent_block: Block, attach_to: Node, string: String)
9999

100100
var split := param.split(": ")
101101
var param_name := split[0]
102-
var param_type_str := split[1]
103-
var param_type: Variant.Type = Types.STRING_TO_VARIANT_TYPE[param_type_str]
102+
var param_type := split[1]
104103

105104
var param_input: ParameterInput = preload("res://addons/block_code/ui/blocks/utilities/parameter_input/parameter_input.tscn").instantiate()
106105
param_input.name = "ParameterInput%d" % start # Unique path

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

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

1010
@export var block_path: NodePath
1111

12-
@export var variant_type: Variant.Type = TYPE_STRING
1312
@export var block_type: Types.BlockType = Types.BlockType.VALUE
13+
@export var variant_type: String = "String"
1414

1515
var block: Block
1616

@@ -42,6 +42,7 @@ func _ready():
4242
block = get_node_or_null(block_path)
4343
snap_point.block = block
4444
snap_point.block_type = block_type
45+
snap_point.variant_type = variant_type
4546

4647
# Do something with block_type to restrict input
4748

@@ -58,9 +59,9 @@ func get_string() -> String:
5859

5960
var text: String = get_plain_text()
6061

61-
if variant_type == TYPE_STRING:
62+
if variant_type == "String":
6263
text = "'%s'" % text
63-
if variant_type == TYPE_VECTOR2:
64+
if variant_type == "Vector2":
6465
text = "Vector2(%s)" % text
6566

6667
return text

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ extends MarginContainer
55
@export var block_path: NodePath
66

77
@export var block_type: Types.BlockType = Types.BlockType.EXECUTE
8+
@export var variant_type: String = ""
89

910
var block: Block
1011

0 commit comments

Comments
 (0)