Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion addons/block_code/block_code_plugin.gd
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const DISABLED_CLASSES := [


func _enter_tree():
Types.init_cast_graph()
Types.init_types()

main_panel = MainPanel.instantiate()
main_panel.undo_redo = get_undo_redo()
Expand Down
7 changes: 6 additions & 1 deletion addons/block_code/drag_manager/drag_manager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ func _process(_delta):
push_error("Warning: a snap point does not reference it's parent block.")
continue
if snap_point.block.on_canvas:
if Types.can_cast(dragging.block_type, snap_point.block_type):
if dragging.block_type == snap_point.block_type:
# Don't snap uncastable value blocks
if dragging.block_type == Types.BlockType.VALUE:
if !Types.can_cast(dragging.variant_type, snap_point.variant_type):
continue

var snap_global_pos: Vector2 = snap_point.get_global_rect().position
var temp_dist: float = dragging_global_pos.distance_to(snap_global_pos)
if temp_dist < closest_dist:
Expand Down
44 changes: 23 additions & 21 deletions addons/block_code/types/types.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,44 @@ extends Node

enum BlockType {
NONE,
EXECUTE,
ENTRY,
# Parameters
STRING,
INT,
FLOAT,
VECTOR2,
BOOL,
COLOR,
NODE
EXECUTE,
VALUE,
}

const VARIANT_TYPE_TO_STRING: Dictionary = {
TYPE_STRING: "String",
TYPE_INT: "int",
TYPE_FLOAT: "float",
TYPE_BOOL: "bool",
TYPE_VECTOR2: "Vector2",
TYPE_COLOR: "Color",
}

const cast_relationships = [
[BlockType.INT, BlockType.FLOAT, "float(%s)"],
[BlockType.FLOAT, BlockType.INT, "int(%s)"],
[BlockType.INT, BlockType.STRING, "str(%s)"],
[BlockType.FLOAT, BlockType.STRING, "str(%s)"],
const CAST_RELATIONSHIPS = [
["int", "float", "float(%s)"],
["float", "int", "int(%s)"],
["int", "String", "str(%s)"],
["float", "String", "str(%s)"],
]

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


class CastGraphEdge:
var to: BlockType
var to: String
var cast_format: String

func _init(p_to: BlockType, p_cast_format: String):
func _init(p_to: String, p_cast_format: String):
to = p_to
cast_format = p_cast_format


static func init_cast_graph():
static func init_types():
cast_graph = {}

for rel in cast_relationships:
for rel in CAST_RELATIONSHIPS:
if not cast_graph.has(rel[0]):
cast_graph[rel[0]] = []

Expand All @@ -56,7 +58,7 @@ static var dist: Dictionary
const INT_MAX: int = 1000000000


static func dijkstra(source: BlockType):
static func dijkstra(source: String):
prev = {}
dist = {}

Expand Down Expand Up @@ -86,7 +88,7 @@ static func dijkstra(source: BlockType):
queue.update_priority(v, alt)


static func can_cast(type: BlockType, parent_type: BlockType) -> bool:
static func can_cast(type: String, parent_type: String) -> bool:
if type == parent_type:
return true

Expand All @@ -96,7 +98,7 @@ static func can_cast(type: BlockType, parent_type: BlockType) -> bool:
return false


static func cast(val: String, type: BlockType, parent_type: BlockType):
static func cast(val: String, type: String, parent_type: String):
if type == parent_type:
return val

Expand Down
5 changes: 4 additions & 1 deletion addons/block_code/ui/blocks/entry_block/entry_block.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@
script = ExtResource("2_3ik8h")
block_name = "entry_block"
label = "EntryBlock"
block_type = 2
block_type = 1

[node name="Background" parent="VBoxContainer/TopMarginContainer" index="0"]
show_top = false
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ extends Block

@export var block_format: String = ""
@export var statement: String = ""
@export var variant_type: String

@onready var _panel := $Panel
@onready var _hbox := %HBoxContainer
Expand Down Expand Up @@ -33,7 +34,7 @@ func _on_drag_drop_area_mouse_down():

func get_serialized_props() -> Array:
var props := super()
props.append_array(serialize_props(["block_format", "statement"]))
props.append_array(serialize_props(["variant_type", "block_format", "statement"]))

var _param_input_strings: Dictionary = {}
for pair in param_name_input_pairs:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[ext_resource type="Script" path="res://addons/block_code/ui/blocks/parameter_block/parameter_block.gd" id="1_0hajy"]
[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"]

[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_0afbg"]
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_d7xfk"]
bg_color = Color(1, 1, 1, 1)
border_width_left = 3
border_width_top = 3
Expand All @@ -26,7 +26,7 @@ block_type = 3
[node name="Panel" type="Panel" parent="."]
unique_name_in_owner = true
layout_mode = 2
theme_override_styles/panel = SubResource("StyleBoxFlat_0afbg")
theme_override_styles/panel = SubResource("StyleBoxFlat_d7xfk")

[node name="DragDropArea" parent="." instance=ExtResource("2_gy5co")]
layout_mode = 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,12 @@ static func format_string(parent_block: Block, attach_to: Node, string: String)

var split := param.split(": ")
var param_name := split[0]
var param_type_str := split[1]
var param_type := Types.BlockType.get(param_type_str)
var param_type := split[1]

var param_input: ParameterInput = preload("res://addons/block_code/ui/blocks/utilities/parameter_input/parameter_input.tscn").instantiate()
param_input.name = "ParameterInput%d" % start # Unique path
param_input.placeholder = param_name
param_input.block_type = param_type
param_input.variant_type = param_type
param_input.block = parent_block
param_input.text_modified.connect(func(): parent_block.modified.emit())
attach_to.add_child(param_input)
Expand All @@ -115,7 +114,7 @@ static func format_string(parent_block: Block, attach_to: Node, string: String)
var new_block: Block = preload("res://addons/block_code/ui/blocks/parameter_block/parameter_block.tscn").instantiate()
new_block.block_format = param_name
new_block.statement = param_name
new_block.block_type = param_type
new_block.variant_type = param_type
new_block.color = parent_block.color
param_input.block_type = Types.BlockType.NONE
param_input.snap_point.block_type = Types.BlockType.NONE # Necessary because already called ready
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ signal text_modified

@export var block_path: NodePath

@export var block_type: Types.BlockType = Types.BlockType.STRING
@export var block_type: Types.BlockType = Types.BlockType.VALUE
@export var variant_type: String = "String"

var block: Block

Expand Down Expand Up @@ -41,6 +42,7 @@ func _ready():
block = get_node_or_null(block_path)
snap_point.block = block
snap_point.block_type = block_type
snap_point.variant_type = variant_type

# Do something with block_type to restrict input

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


func get_string() -> String:
var snapped_block: Block = get_snapped_block()
var snapped_block: ParameterBlock = get_snapped_block() as ParameterBlock
if snapped_block:
var generated_string = snapped_block.get_parameter_string()
return Types.cast(generated_string, snapped_block.block_type, block_type)
return Types.cast(generated_string, snapped_block.variant_type, variant_type)

var text: String = get_plain_text()

if block_type == Types.BlockType.STRING:
if variant_type == "String":
text = "'%s'" % text
if block_type == Types.BlockType.VECTOR2:
if variant_type == "Vector2":
text = "Vector2(%s)" % text

return text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ extends MarginContainer
@export var block_path: NodePath

@export var block_type: Types.BlockType = Types.BlockType.EXECUTE
@export var variant_type: String = ""

var block: Block

Expand Down
Loading