Skip to content
New issue

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

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

Already on GitHub? # to your account

WIP: Blocks in-game changes and example #380

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
9 changes: 6 additions & 3 deletions addons/block_code/drag_manager/drag_manager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ var drag: Drag = null


func _ready():
_picker = get_node(picker_path)
_block_canvas = get_node(block_canvas_path)
if picker_path:
_picker = get_node(picker_path)
if block_canvas_path:
_block_canvas = get_node(block_canvas_path)


func _process(_delta):
Expand Down Expand Up @@ -55,7 +57,8 @@ func drag_block(block: Block, copied_from: Block = null, offset: Vector2 = Vecto

drag = Drag.new(block, block_scope, offset, _block_canvas)
drag.set_snap_points(get_tree().get_nodes_in_group("snap_point"))
drag.add_delete_area(_picker.get_global_rect())
if _picker:
drag.add_delete_area(_picker.get_global_rect())
if block is ParameterBlock and block.spawned_by:
drag.add_delete_area(block.spawned_by.get_global_rect())
add_child(drag)
Expand Down
42 changes: 42 additions & 0 deletions addons/block_code/examples/demo_ingame/demo_ingame.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
extends Control

@onready var _context := BlockEditorContext.get_default()

@onready var block_code_node: BlockCode = %BlockCode
@onready var block_canvas: MarginContainer = %BlockCanvas
@onready var drag_manager: Control = %DragManager


# Called when the node enters the scene tree for the first time.
func _ready() -> void:
block_canvas.reconnect_block.connect(drag_manager.connect_block_canvas_signals)
drag_manager.block_dropped.connect(save_script)
drag_manager.block_modified.connect(save_script)
_context.block_code_node = block_code_node


func _input(event):
if event is InputEventMouseButton:
var mouse_event: InputEventMouseButton = event as InputEventMouseButton
if mouse_event.button_index == MOUSE_BUTTON_LEFT:
if mouse_event.pressed:
# Release focus
var focused_node := get_viewport().gui_get_focus_owner()
if focused_node:
focused_node.release_focus()
else:
drag_manager.drag_ended()


func save_script():
if _context.block_code_node == null:
print("No script loaded to save.")
return

var block_script: BlockScriptSerialization = _context.block_script
block_canvas.rebuild_ast_list()
block_canvas.rebuild_block_serialization_trees()
var generated_script = block_canvas.generate_script_from_current_window()
if generated_script != block_script.generated_script:
block_script.generated_script = generated_script
block_code_node._update_parent_script()
111 changes: 111 additions & 0 deletions addons/block_code/examples/demo_ingame/demo_ingame.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
[gd_scene load_steps=19 format=3 uid="uid://cytyr4s3vkoqm"]

[ext_resource type="Script" path="res://addons/block_code/examples/demo_ingame/demo_ingame.gd" id="1_6vqkq"]
[ext_resource type="PackedScene" uid="uid://c6vumewgnfquy" path="res://addons/block_code/ui/block_canvas/block_canvas.tscn" id="2_7wcyd"]
[ext_resource type="Texture2D" uid="uid://dk8joxmlxu82l" path="res://icon.png" id="2_saega"]
[ext_resource type="Script" uid="uid://cumxsbxj56dmk" path="res://addons/block_code/block_code_node/block_code.gd" id="3_com2k"]
[ext_resource type="Script" uid="uid://brx1cabdmgu38" path="res://addons/block_code/serialization/block_serialization_tree.gd" id="4_dmtl1"]
[ext_resource type="Script" uid="uid://u035wsjd5tpj" path="res://addons/block_code/serialization/block_serialization.gd" id="5_fjkgv"]
[ext_resource type="Script" uid="uid://tlsvrkc3fubl" path="res://addons/block_code/serialization/block_script_serialization.gd" id="6_nae1f"]
[ext_resource type="Script" uid="uid://duvgxorhfitb7" path="res://addons/block_code/serialization/value_block_serialization.gd" id="7_25a4o"]
[ext_resource type="Script" uid="uid://bwkinr8omutpw" path="res://addons/block_code/code_generation/variable_definition.gd" id="7_qrh5q"]
[ext_resource type="PackedScene" uid="uid://cph1k5cfximbf" path="res://addons/block_code/drag_manager/drag_manager.tscn" id="10_kgp86"]

[sub_resource type="Resource" id="Resource_7j0jm"]
script = ExtResource("5_fjkgv")
name = &"Node2D_change_rotation_degrees"
children = Array[ExtResource("5_fjkgv")]([])
arguments = {
"value": -1.0
}

[sub_resource type="Resource" id="Resource_p7e31"]
script = ExtResource("5_fjkgv")
name = &"Node2D_change_rotation_degrees"
children = Array[ExtResource("5_fjkgv")]([])
arguments = {
"value": 5.0
}

[sub_resource type="Resource" id="Resource_8vvuc"]
script = ExtResource("5_fjkgv")
name = &"process"
children = Array[ExtResource("5_fjkgv")]([SubResource("Resource_7j0jm"), SubResource("Resource_p7e31")])
arguments = {}

[sub_resource type="Resource" id="Resource_43nhb"]
script = ExtResource("4_dmtl1")
root = SubResource("Resource_8vvuc")
canvas_position = Vector2(75, 150)

[sub_resource type="Resource" id="Resource_xn35n"]
script = ExtResource("7_25a4o")
name = &"mouse_position"
arguments = {}

[sub_resource type="Resource" id="Resource_y3fve"]
script = ExtResource("5_fjkgv")
name = &"Node2D_set_position"
children = Array[ExtResource("5_fjkgv")]([])
arguments = {
"value": SubResource("Resource_xn35n")
}

[sub_resource type="Resource" id="Resource_k8cej"]
script = ExtResource("4_dmtl1")
root = SubResource("Resource_y3fve")
canvas_position = Vector2(125, 397)

[sub_resource type="Resource" id="Resource_ydoon"]
script = ExtResource("6_nae1f")
script_inherits = "Sprite2D"
block_serialization_trees = Array[ExtResource("4_dmtl1")]([SubResource("Resource_43nhb"), SubResource("Resource_k8cej")])
variables = Array[ExtResource("7_qrh5q")]([])
generated_script = "extends Sprite2D


func _process(delta):
rotation_degrees += -1
rotation_degrees += 5

"
version = 0

[node name="DemoInGame" type="Control"]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
mouse_filter = 1
script = ExtResource("1_6vqkq")

[node name="HSplitContainer" type="HSplitContainer" parent="."]
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2

[node name="BlockCanvas" parent="HSplitContainer" instance=ExtResource("2_7wcyd")]
unique_name_in_owner = true
layout_mode = 2

[node name="BoxContainer" type="BoxContainer" parent="HSplitContainer"]
layout_mode = 2
size_flags_horizontal = 3

[node name="Icon" type="Sprite2D" parent="HSplitContainer/BoxContainer"]
position = Vector2(290, 317)
texture = ExtResource("2_saega")

[node name="BlockCode" type="Node" parent="HSplitContainer/BoxContainer/Icon"]
unique_name_in_owner = true
script = ExtResource("3_com2k")
block_script = SubResource("Resource_ydoon")

[node name="DragManager" parent="." instance=ExtResource("10_kgp86")]
unique_name_in_owner = true
block_canvas_path = NodePath("../HSplitContainer/BlockCanvas")
9 changes: 5 additions & 4 deletions addons/block_code/ui/block_canvas/block_canvas.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const BlocksCatalog = preload("res://addons/block_code/code_generation/blocks_ca
const BlockCodePlugin = preload("res://addons/block_code/block_code_plugin.gd")
const BlockDefinition = preload("res://addons/block_code/code_generation/block_definition.gd")
const BlockTreeUtil = preload("res://addons/block_code/ui/block_tree_util.gd")
const DragManager = preload("res://addons/block_code/drag_manager/drag_manager.gd")
const ScriptGenerator = preload("res://addons/block_code/code_generation/script_generator.gd")
const Types = preload("res://addons/block_code/types/types.gd")
const Util = preload("res://addons/block_code/ui/util.gd")
Expand Down Expand Up @@ -37,8 +36,8 @@ const ZOOM_FACTOR: float = 1.1
@onready var _replace_block_code_button: Button = %ReplaceBlockCodeButton

@onready var _open_scene_icon = _open_scene_button.get_theme_icon("Load", "EditorIcons")
@onready var _icon_zoom_out := EditorInterface.get_editor_theme().get_icon("ZoomLess", "EditorIcons")
@onready var _icon_zoom_in := EditorInterface.get_editor_theme().get_icon("ZoomMore", "EditorIcons")
@onready var _icon_zoom_out := EditorInterface.get_editor_theme().get_icon("ZoomLess", "EditorIcons") if EditorInterface.has_method("get_editor_theme") else null
@onready var _icon_zoom_in := EditorInterface.get_editor_theme().get_icon("ZoomMore", "EditorIcons") if EditorInterface.has_method("get_editor_theme") else null

@onready var _mouse_override: Control = %MouseOverride
@onready var _zoom_buttons: HBoxContainer = %ZoomButtons
Expand Down Expand Up @@ -205,7 +204,9 @@ func set_child(n: Node):
func _on_context_changed():
clear_canvas()

var edited_node = EditorInterface.get_inspector().get_edited_object() as Node
var edited_node
if EditorInterface.has_method("get_inspector"):
edited_node = EditorInterface.get_inspector().get_edited_object() as Node

if _context.block_script != _current_block_script:
_window.position = Vector2(0, 0)
Expand Down