Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Differentiate parameter input based on type #33

Merged
merged 7 commits into from
Jun 19, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ func get_custom_class():
return "SimpleCharacter"


static func get_base_class():
return "CharacterBody2D"


static func get_exposed_properties() -> Array[String]:
return ["position"]

Expand Down
11 changes: 11 additions & 0 deletions addons/block_code/types/types.gd
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const cast_relationships = [
[TYPE_FLOAT, TYPE_INT, "int(%s)"],
[TYPE_INT, TYPE_STRING, "str(%s)"],
[TYPE_FLOAT, TYPE_STRING, "str(%s)"],
[TYPE_COLOR, TYPE_STRING, "str(%s)"],
]

# Directed graph, edges are CastGraphEdge
Expand Down Expand Up @@ -169,3 +170,13 @@ class PriorityQueue:
if found_pair:
found_pair[1] = priority
_sort()


# Global classes
class OptionData:
var selected: int
var items: Array

func _init(p_items: Array = [], p_selected: int = 0):
items = p_items
selected = p_selected
9 changes: 5 additions & 4 deletions addons/block_code/ui/blocks/control_block/control_block.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const Constants = preload("res://addons/block_code/ui/constants.gd")

@export var block_formats: Array = []
@export var statements: Array = []
@export var defaults: Dictionary = {}

var snaps: Array
var param_name_input_pairs_array: Array
Expand All @@ -25,7 +26,7 @@ func _ready():
if param_input_strings_array:
for i in param_name_input_pairs_array.size():
for pair in param_name_input_pairs_array[i]:
pair[1].set_plain_text(param_input_strings_array[i][pair[0]])
pair[1].set_raw_input(param_input_strings_array[i][pair[0]])


func _on_drag_drop_area_mouse_down():
Expand Down Expand Up @@ -68,14 +69,14 @@ func get_instruction_node() -> InstructionTree.TreeNode:

func get_serialized_props() -> Array:
var props := super()
props.append_array(serialize_props(["block_formats", "statements"]))
props.append_array(serialize_props(["block_formats", "statements", "defaults"]))

var _param_input_strings_array = []
for param_name_input_pairs in param_name_input_pairs_array:
var _param_input_strings: Dictionary = {}

for pair in param_name_input_pairs:
_param_input_strings[pair[0]] = pair[1].get_plain_text()
_param_input_strings[pair[0]] = pair[1].get_raw_input()

_param_input_strings_array.append(_param_input_strings)

Expand Down Expand Up @@ -134,7 +135,7 @@ func format():
row_hbox.mouse_filter = Control.MOUSE_FILTER_IGNORE
row_hbox_container.add_child(row_hbox)

param_name_input_pairs_array.append(StatementBlock.format_string(self, row_hbox, block_formats[i]))
param_name_input_pairs_array.append(StatementBlock.format_string(self, row_hbox, block_formats[i], defaults))

%Rows.add_child(row)

Expand Down
2 changes: 2 additions & 0 deletions addons/block_code/ui/blocks/entry_block/entry_block.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

[node name="EntryBlock" instance=ExtResource("1_byjbb")]
script = ExtResource("2_3ik8h")
defaults = null
block_name = "entry_block"
label = "EntryBlock"
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 @@ -5,6 +5,7 @@ extends Block
@export var block_format: String = ""
@export var statement: String = ""
@export var variant_type: Variant.Type
@export var defaults: Dictionary = {}

@onready var _panel := $Panel
@onready var _hbox := %HBoxContainer
Expand All @@ -26,7 +27,7 @@ func _ready():

if param_input_strings:
for pair in param_name_input_pairs:
pair[1].set_plain_text(param_input_strings[pair[0]])
pair[1].set_raw_input(param_input_strings[pair[0]])


func _on_drag_drop_area_mouse_down():
Expand All @@ -35,11 +36,11 @@ func _on_drag_drop_area_mouse_down():

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

var _param_input_strings: Dictionary = {}
for pair in param_name_input_pairs:
_param_input_strings[pair[0]] = pair[1].get_plain_text()
_param_input_strings[pair[0]] = pair[1].get_raw_input()

props.append(["param_input_strings", _param_input_strings])

Expand All @@ -65,4 +66,4 @@ static func get_scene_path():


func format():
param_name_input_pairs = StatementBlock.format_string(self, %HBoxContainer, block_format)
param_name_input_pairs = StatementBlock.format_string(self, %HBoxContainer, block_format, defaults)
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_dbera"]
bg_color = Color(1, 1, 1, 1)
border_width_left = 3
border_width_top = 3
Expand All @@ -19,14 +19,15 @@ offset_right = 16.0
offset_bottom = 8.0
size_flags_horizontal = 0
script = ExtResource("1_0hajy")
defaults = null
block_name = "parameter_block"
label = "Param"
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_dbera")

[node name="DragDropArea" parent="." instance=ExtResource("2_gy5co")]
layout_mode = 2
Expand Down
34 changes: 26 additions & 8 deletions addons/block_code/ui/blocks/statement_block/statement_block.gd
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 defaults: Dictionary = {}

@onready var _background := %Background
@onready var _hbox := %HBoxContainer
Expand All @@ -23,7 +24,7 @@ func _ready():

if param_input_strings:
for pair in param_name_input_pairs:
pair[1].set_plain_text(param_input_strings[pair[0]])
pair[1].set_raw_input(param_input_strings[pair[0]])


func _on_drag_drop_area_mouse_down():
Expand All @@ -32,11 +33,11 @@ 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(["block_format", "statement", "defaults"]))

var _param_input_strings: Dictionary = {}
for pair in param_name_input_pairs:
_param_input_strings[pair[0]] = pair[1].get_plain_text()
_param_input_strings[pair[0]] = pair[1].get_raw_input()

props.append(["param_input_strings", _param_input_strings])
return props
Expand Down Expand Up @@ -75,10 +76,10 @@ func get_instruction_node() -> InstructionTree.TreeNode:


func format():
param_name_input_pairs = format_string(self, %HBoxContainer, block_format)
param_name_input_pairs = format_string(self, %HBoxContainer, block_format, defaults)


static func format_string(parent_block: Block, attach_to: Node, string: String) -> Array:
static func format_string(parent_block: Block, attach_to: Node, string: String, _defaults: Dictionary) -> Array:
var _param_name_input_pairs = []
var regex = RegEx.new()
regex.compile("\\[([^\\]]+)\\]|\\{([^}]+)\\}") # Capture things of format {test} or [test]
Expand All @@ -100,15 +101,32 @@ 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: Variant.Type = Types.STRING_TO_VARIANT_TYPE[param_type_str]

var param_type = null
var option := false
if param_type_str == "OPTION": # Easy way to specify dropdown option
option = true
else:
param_type = Types.STRING_TO_VARIANT_TYPE[param_type_str]

var param_default = null
if _defaults.has(param_name):
param_default = _defaults[param_name]

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.variant_type = param_type
if param_type:
param_input.variant_type = param_type
elif option:
param_input.option = true
param_input.block = parent_block
param_input.text_modified.connect(func(): parent_block.modified.emit())
param_input.modified.connect(func(): parent_block.modified.emit())

attach_to.add_child(param_input)
if param_default:
param_input.set_raw_input(param_default)

_param_name_input_pairs.append([param_name, param_input])

if copy_block:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
[node name="StatementBlock" type="MarginContainer"]
size_flags_horizontal = 0
script = ExtResource("1_6wvlf")
defaults = null
block_name = "statement_block"
label = "StatementBlock"
bottom_snap_path = NodePath("VBoxContainer/SnapPoint")
Expand Down
Loading
Loading