Skip to content

Commit

Permalink
Merge branch 'main' into block-classes
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanmccall authored Jun 19, 2024
2 parents 2886d40 + f98cdc0 commit 1164e21
Show file tree
Hide file tree
Showing 17 changed files with 483 additions and 75 deletions.
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ func generate_script_from_current_window(script_inherits: String = ""):

script += "var VAR_DICT := {}\n\n"

var init_func = InstructionTree.TreeNode.new("func _init():")

for entry_block in entry_blocks:
script += entry_block.get_entry_statement() + "\n"

Expand All @@ -39,4 +41,10 @@ func generate_script_from_current_window(script_inherits: String = ""):

script += "\n"

if entry_block.signal_name:
init_func.add_child(InstructionTree.TreeNode.new("{0}.connect(_on_{0})".format([entry_block.signal_name])))

if init_func.children:
script += InstructionTree.new().generate_text(init_func)

return script
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
13 changes: 9 additions & 4 deletions addons/block_code/ui/blocks/entry_block/entry_block.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
class_name EntryBlock
extends StatementBlock

## if non-empty, this block defines a callback that will be connected to the signal with this name
@export var signal_name: String


func _ready():
block_type = Types.BlockType.ENTRY
Expand All @@ -22,8 +25,10 @@ func get_entry_statement() -> String:
for pair in param_name_input_pairs:
formatted_statement = formatted_statement.replace("{%s}" % pair[0], pair[1].get_string())

# One line, should not have \n
if formatted_statement.find("\n") != -1:
push_error("Entry block has multiline statement.")

return formatted_statement


func get_serialized_props() -> Array:
var props := super()
props.append_array(serialize_props(["signal_name"]))
return props
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"]))
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

0 comments on commit 1164e21

Please sign in to comment.