Skip to content

Commit 4d131e5

Browse files
committed
Decouple block type from block variant
And use Variant.Type enum for the Value block variant. This means we stick to GDScript variants for the values represented by Value blocks: - A constant like "viewport width" - A property like "rotation" - A parameter like A in "A + B" - The resulting value of "A + B" The slots that can contain Value blocks also have a matching variant type now. Unfortunately there is no way to convert the native Variant.Type enum to string, so still we need dictionaries to go back and forth the string formatting. Previously there was a custom NODE type, although unused. This is now replaced by NODE_PATH, which is the same thing that Godot does in the PackedScene resource when exporting a property of type Node. Also: improve readability of drag manager and use a constant for the minimum slot distance.
1 parent 06392d9 commit 4d131e5

File tree

11 files changed

+114
-100
lines changed

11 files changed

+114
-100
lines changed

addons/block_code/drag_manager/drag_manager.gd

+33-28
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ signal block_modified
88
@export var picker_path: NodePath
99
@export var block_canvas_path: NodePath
1010

11+
const Constants = preload("res://addons/block_code/ui/constants.gd")
12+
1113
var drag_offset: Vector2
1214
var dragging: Block = null
1315

@@ -39,38 +41,41 @@ func _process(_delta):
3941
var closest_snap_point: SnapPoint = null
4042
var closest_dist: float = INF
4143
var snap_points: Array[Node] = get_tree().get_nodes_in_group("snap_point")
42-
for n in snap_points:
43-
if n is SnapPoint:
44-
var snap_point: SnapPoint = n as SnapPoint
45-
if snap_point.block == null:
46-
push_error("Warning: a snap point does not reference it's parent block.")
47-
continue
48-
if snap_point.block.on_canvas:
49-
if Types.can_cast(dragging.block_type, snap_point.block_type):
50-
var snap_global_pos: Vector2 = snap_point.get_global_rect().position
51-
var temp_dist: float = dragging_global_pos.distance_to(snap_global_pos)
52-
if temp_dist < closest_dist:
53-
# Check if any parent node is this node
54-
var is_child: bool = false
55-
var parent = snap_point
56-
while parent is SnapPoint:
57-
if parent.block == dragging:
58-
is_child = true
59-
60-
parent = parent.block.get_parent()
61-
62-
if not is_child:
63-
closest_dist = temp_dist
64-
closest_snap_point = snap_point
65-
66-
if closest_dist > 80.0:
67-
closest_snap_point = null
44+
for snap_point in snap_points:
45+
if not snap_point is SnapPoint:
46+
push_error('Warning: a node in group "snap_point"snap is not of class SnapPoint.')
47+
continue
48+
if snap_point.block == null:
49+
push_error("Warning: a snap point does not reference it's parent block.")
50+
continue
51+
if not snap_point.block.on_canvas:
52+
# We only snap to blocks on the canvas:
53+
continue
54+
if dragging.block_type != snap_point.block_type:
55+
# We only snap to the same block type:
56+
continue
57+
if dragging.block_type == Types.BlockType.VALUE and not Types.can_cast(dragging.variant_type, snap_point.variant_type):
58+
# We only snap Value blocks to snaps that can cast to same variant:
59+
continue
60+
var snap_global_pos: Vector2 = snap_point.get_global_rect().position
61+
var temp_dist: float = dragging_global_pos.distance_to(snap_global_pos)
62+
if temp_dist <= Constants.MINIMUM_SNAP_DISTANCE and temp_dist < closest_dist:
63+
# Check if any parent node is this node
64+
var is_child: bool = false
65+
var parent = snap_point
66+
while parent is SnapPoint:
67+
if parent.block == dragging:
68+
is_child = true
69+
70+
parent = parent.block.get_parent()
71+
72+
if not is_child:
73+
closest_dist = temp_dist
74+
closest_snap_point = snap_point
6875

6976
if closest_snap_point != previewing_snap_point:
7077
_update_preview(closest_snap_point)
7178

72-
# TODO: make sure dragging.block_type is the same as snap_point type
73-
7479

7580
func _update_preview(snap_point: SnapPoint):
7681
previewing_snap_point = snap_point

addons/block_code/types/types.gd

+33-18
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,49 @@ 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+
TYPE_NODE_PATH: "NODE_PATH",
19+
TYPE_NIL: "NIL",
20+
}
21+
22+
const STRING_TO_VARIANT_TYPE: Dictionary = {
23+
"STRING": TYPE_STRING,
24+
"INT": TYPE_INT,
25+
"FLOAT": TYPE_FLOAT,
26+
"BOOL": TYPE_BOOL,
27+
"VECTOR2": TYPE_VECTOR2,
28+
"COLOR": TYPE_COLOR,
29+
"NODE_PATH": TYPE_NODE_PATH,
30+
"NIL": TYPE_NIL,
1631
}
1732

1833
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)"],
34+
[TYPE_INT, TYPE_FLOAT, "float(%s)"],
35+
[TYPE_FLOAT, TYPE_INT, "int(%s)"],
36+
[TYPE_INT, TYPE_STRING, "str(%s)"],
37+
[TYPE_FLOAT, TYPE_STRING, "str(%s)"],
2338
]
2439

2540
# Directed graph, edges are CastGraphEdge
2641
static var cast_graph: Dictionary
2742

2843

2944
class CastGraphEdge:
30-
var to: BlockType
45+
var to: Variant.Type
3146
var cast_format: String
3247

33-
func _init(p_to: BlockType, p_cast_format: String):
48+
func _init(p_to: Variant.Type, p_cast_format: String):
3449
to = p_to
3550
cast_format = p_cast_format
3651

@@ -56,7 +71,7 @@ static var dist: Dictionary
5671
const INT_MAX: int = 1000000000
5772

5873

59-
static func dijkstra(source: BlockType):
74+
static func dijkstra(source: Variant.Type):
6075
prev = {}
6176
dist = {}
6277

@@ -86,7 +101,7 @@ static func dijkstra(source: BlockType):
86101
queue.update_priority(v, alt)
87102

88103

89-
static func can_cast(type: BlockType, parent_type: BlockType) -> bool:
104+
static func can_cast(type: Variant.Type, parent_type: Variant.Type) -> bool:
90105
if type == parent_type:
91106
return true
92107

@@ -96,7 +111,7 @@ static func can_cast(type: BlockType, parent_type: BlockType) -> bool:
96111
return false
97112

98113

99-
static func cast(val: String, type: BlockType, parent_type: BlockType):
114+
static func cast(val: String, type: Variant.Type, parent_type: Variant.Type):
100115
if type == parent_type:
101116
return val
102117

addons/block_code/ui/blocks/entry_block/entry_block.gd

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ extends StatementBlock
44

55

66
func _ready():
7+
block_type = Types.BlockType.ENTRY
78
super()
89

910

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

+4-1
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

+2
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

+3-3
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

+7-5
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

@@ -41,6 +42,7 @@ func _ready():
4142
block = get_node_or_null(block_path)
4243
snap_point.block = block
4344
snap_point.block_type = block_type
45+
snap_point.variant_type = variant_type
4446

4547
# Do something with block_type to restrict input
4648

@@ -50,16 +52,16 @@ func get_snapped_block() -> Block:
5052

5153

5254
func get_string() -> String:
53-
var snapped_block: Block = get_snapped_block()
55+
var snapped_block: ParameterBlock = get_snapped_block() as ParameterBlock
5456
if snapped_block:
5557
var generated_string = snapped_block.get_parameter_string()
56-
return Types.cast(generated_string, snapped_block.block_type, block_type)
58+
return Types.cast(generated_string, snapped_block.variant_type, variant_type)
5759

5860
var text: String = get_plain_text()
5961

60-
if block_type == Types.BlockType.STRING:
62+
if variant_type == TYPE_STRING:
6163
text = "'%s'" % text.replace("\\", "\\\\").replace("'", "\\'")
62-
if block_type == Types.BlockType.VECTOR2:
64+
elif variant_type == TYPE_VECTOR2:
6365
text = "Vector2(%s)" % text
6466

6567
return text

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

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ extends MarginContainer
66

77
@export var block_type: Types.BlockType = Types.BlockType.EXECUTE
88

9+
## Only used when block_type is a [enum Types.BlockType.VALUE].
10+
@export var variant_type: Variant.Type
11+
912
var block: Block
1013

1114

addons/block_code/ui/constants.gd

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ const KNOB_H = 5.0
66
const KNOB_Z = 5.0
77
const CONTROL_MARGIN = 20.0
88
const OUTLINE_WIDTH = 3.0
9+
const MINIMUM_SNAP_DISTANCE = 80.0

0 commit comments

Comments
 (0)