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

Add EntryBlock class #25

Merged
merged 1 commit into from
Jun 13, 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 @@ -9,65 +9,34 @@ func generate_script_from_current_window(script_inherits: String = ""):

var blocks := current_window.get_children()

var ready_nodes: Array[BasicBlock] = []
var process_nodes: Array[BasicBlock] = []
var physics_process_nodes: Array[BasicBlock] = []
var signal_nodes: Array[StatementBlock] = []
var entry_blocks: Array[EntryBlock] = []

for c in blocks:
if !(c is Block):
continue

match c.block_name:
"ready_block":
ready_nodes.append(c)
"process_block":
process_nodes.append(c)
"physics_process_block":
physics_process_nodes.append(c)
"signal_block":
signal_nodes.append(c)
if c is EntryBlock:
entry_blocks.append(c)

var script: String = ""

script += "extends %s\n\n" % script_inherits

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

var node_groups = [["func _ready():", ready_nodes], ["func _process(_delta):", process_nodes], ["func _physics_process(_delta):", physics_process_nodes]]
for entry_block in entry_blocks:
script += entry_block.get_entry_statement() + "\n"
Comment on lines +27 to +28
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes!


# Get signal entries
var signal_groups: Dictionary = {}
for signal_node in signal_nodes:
# Little bit hacky to get first param
var signal_name: String = signal_node.param_name_input_pairs[0][1].get_plain_text()
if signal_groups.has(signal_name):
signal_groups[signal_name].append(signal_node)
else:
signal_groups[signal_name] = [signal_node]

for signal_name in signal_groups:
node_groups.append(["func signal_%s():" % signal_name, signal_groups[signal_name]])

for section in node_groups:
script += section[0] + "\n"

var should_pass: bool = true
for block in section[1]:
if block.bottom_snap.get_snapped_block():
should_pass = false
break
var next_block := entry_block.bottom_snap.get_snapped_block()

if should_pass:
if next_block == null:
script += "\tpass\n"
else:
for block in section[1]:
var generator: InstructionTree = InstructionTree.new()
var instruction_node: InstructionTree.TreeNode = block.get_instruction_node()
var to_append := generator.generate_text(instruction_node, 1)
script += to_append
var generator: InstructionTree = InstructionTree.new()
var instruction_node: InstructionTree.TreeNode = next_block.get_instruction_node()
var to_append := generator.generate_text(instruction_node, 1)
script += to_append

#script += "\n\tsuper()\n\n"
script += "\n"

return script
24 changes: 24 additions & 0 deletions addons/block_code/ui/blocks/entry_block/entry_block.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
@tool
class_name EntryBlock
extends StatementBlock


func _ready():
super()


func get_scene_path():
return "res://addons/block_code/ui/blocks/entry_block/entry_block.tscn"


func get_entry_statement() -> String:
var formatted_statement := statement

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
10 changes: 10 additions & 0 deletions addons/block_code/ui/blocks/entry_block/entry_block.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[gd_scene load_steps=3 format=3 uid="uid://d2fibflv3ojys"]

[ext_resource type="PackedScene" uid="uid://c84vmg3odrtxt" path="res://addons/block_code/ui/blocks/statement_block/statement_block.tscn" id="1_byjbb"]
[ext_resource type="Script" path="res://addons/block_code/ui/blocks/entry_block/entry_block.gd" id="2_3ik8h"]

[node name="EntryBlock" instance=ExtResource("1_byjbb")]
script = ExtResource("2_3ik8h")
block_name = "entry_block"
label = "EntryBlock"
block_type = 2
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_vvwx0"]
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_0afbg"]
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_vvwx0")
theme_override_styles/panel = SubResource("StyleBoxFlat_0afbg")

[node name="DragDropArea" parent="." instance=ExtResource("2_gy5co")]
layout_mode = 2
Expand Down
32 changes: 17 additions & 15 deletions addons/block_code/ui/picker/categories/category_factory.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const BLOCKS: Dictionary = {
"control_block": preload("res://addons/block_code/ui/blocks/control_block/control_block.tscn"),
"parameter_block": preload("res://addons/block_code/ui/blocks/parameter_block/parameter_block.tscn"),
"statement_block": preload("res://addons/block_code/ui/blocks/statement_block/statement_block.tscn"),
"entry_block": preload("res://addons/block_code/ui/blocks/entry_block/entry_block.tscn"),
}


Expand All @@ -14,22 +15,22 @@ static func get_general_categories() -> Array[BlockCategory]:

# Entry
var entry_list: Array[Block] = []
b = BLOCKS["basic_block"].instantiate()
b = BLOCKS["entry_block"].instantiate()
b.block_name = "ready_block"
b.label = "On Ready"
b.block_type = Types.BlockType.ENTRY
b.block_format = "On Ready"
b.statement = "func _ready():"
entry_list.append(b)

b = BLOCKS["basic_block"].instantiate()
b = BLOCKS["entry_block"].instantiate()
b.block_name = "process_block"
b.label = "On Process"
b.block_type = Types.BlockType.ENTRY
b.block_format = "On Process"
b.statement = "func _process(delta):"
entry_list.append(b)

b = BLOCKS["basic_block"].instantiate()
b = BLOCKS["entry_block"].instantiate()
b.block_name = "physics_process_block"
b.label = "On Physics Process"
b.block_type = Types.BlockType.ENTRY
b.block_format = "On Physics Process"
b.statement = "func _physics_process(delta):"
entry_list.append(b)

var entry_cat: BlockCategory = BlockCategory.new("Entry", entry_list, Color("fa5956"))
Expand Down Expand Up @@ -64,20 +65,21 @@ static func get_general_categories() -> Array[BlockCategory]:
b.statement = "print({text})"
test_list.append(b)

b = BLOCKS["statement_block"].instantiate()
b.block_type = Types.BlockType.ENTRY
b = BLOCKS["entry_block"].instantiate()
b.block_format = "On body enter [body: NODE]"
b.statement = "func _on_body_enter(body):"
test_list.append(b)

var test_cat: BlockCategory = BlockCategory.new("Test", test_list, Color("9989df"))

# Signal
var signal_list: Array[Block] = []

b = BLOCKS["statement_block"].instantiate()
b.block_name = "signal_block"
b.block_type = Types.BlockType.ENTRY
b.block_format = "On signal {signal: STRING}"
b = BLOCKS["entry_block"].instantiate()
# HACK: make signals work with new entry nodes. NONE instead of STRING type allows
# plain text input for function name. Should revamp signals later
b.block_format = "On signal {signal: NONE}"
b.statement = "func signal_{signal}():"
signal_list.append(b)

b = BLOCKS["statement_block"].instantiate()
Expand Down
Loading
Loading