Skip to content

Commit 11e00e1

Browse files
authored
Merge pull request #296 from endlessm/T35649-drop-object-property
BlockCanvas: Implement drag & drop the node's property from Inspector
2 parents 097d046 + c6ef1c1 commit 11e00e1

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

addons/block_code/ui/block_canvas/block_canvas.gd

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ extends MarginContainer
44
const ASTList = preload("res://addons/block_code/code_generation/ast_list.gd")
55
const BlockAST = preload("res://addons/block_code/code_generation/block_ast.gd")
66
const BlockCodePlugin = preload("res://addons/block_code/block_code_plugin.gd")
7+
const BlockDefinition = preload("res://addons/block_code/code_generation/block_definition.gd")
78
const BlockTreeUtil = preload("res://addons/block_code/ui/block_tree_util.gd")
89
const DragManager = preload("res://addons/block_code/drag_manager/drag_manager.gd")
910
const ScriptGenerator = preload("res://addons/block_code/code_generation/script_generator.gd")
11+
const Types = preload("res://addons/block_code/types/types.gd")
1012
const Util = preload("res://addons/block_code/ui/util.gd")
1113

1214
const EXTEND_MARGIN: float = 800
@@ -52,6 +54,8 @@ var zoom: float:
5254
get:
5355
return _window.scale.x
5456

57+
var _modifier_ctrl := false
58+
5559
signal reconnect_block(block: Block)
5660
signal add_block_code
5761
signal open_scene
@@ -75,6 +79,10 @@ func _can_drop_data(at_position: Vector2, data: Variant) -> bool:
7579
if typeof(data) != TYPE_DICTIONARY:
7680
return false
7781

82+
# Allow dropping property block
83+
if data.get("type", "") == "obj_property":
84+
return true
85+
7886
var nodes: Array = data.get("nodes", [])
7987
if nodes.size() != 1:
8088
return false
@@ -92,6 +100,13 @@ func _can_drop_data(at_position: Vector2, data: Variant) -> bool:
92100

93101

94102
func _drop_data(at_position: Vector2, data: Variant) -> void:
103+
if data["type"] == "nodes":
104+
_drop_node(at_position, data)
105+
elif data["type"] == "obj_property":
106+
_drop_obj_property(at_position, data)
107+
108+
109+
func _drop_node(at_position: Vector2, data: Variant) -> void:
95110
var abs_path: NodePath = data.get("nodes", []).pop_back()
96111
if abs_path == null:
97112
return
@@ -108,6 +123,51 @@ func _drop_data(at_position: Vector2, data: Variant) -> void:
108123
reconnect_block.emit(block)
109124

110125

126+
func _drop_obj_property(at_position: Vector2, data: Variant) -> void:
127+
var object_name = str(data["object"]).get_slice(":", 0)
128+
var property_name = data["property"]
129+
var property_value = data["value"]
130+
131+
# Prepare a Variable block to set / get the property's value according to
132+
# the modifier KEY_CTRL pressing.
133+
var block_definition: BlockDefinition
134+
var property_type = typeof(property_value)
135+
if _modifier_ctrl:
136+
var type_string: String = Types.VARIANT_TYPE_TO_STRING[property_type]
137+
block_definition = (
138+
BlockDefinition
139+
. new(
140+
&"%s_set_%s" % [object_name, property_name],
141+
object_name,
142+
"Set the %s property" % property_name,
143+
"Variables",
144+
Types.BlockType.STATEMENT,
145+
property_type,
146+
"set %s to {value: %s}" % [property_name.capitalize().to_lower(), type_string],
147+
"%s = {value}" % property_name,
148+
{"value": property_value},
149+
)
150+
)
151+
else:
152+
block_definition = (
153+
BlockDefinition
154+
. new(
155+
&"%s_get_%s" % [object_name, property_name],
156+
object_name,
157+
"The %s property" % property_name,
158+
"Variables",
159+
Types.BlockType.VALUE,
160+
property_type,
161+
"%s" % property_name.capitalize().to_lower(),
162+
"%s" % property_name,
163+
)
164+
)
165+
166+
var block = _context.block_script.instantiate_block(block_definition)
167+
add_block(block, at_position)
168+
reconnect_block.emit(block)
169+
170+
111171
func add_block(block: Block, position: Vector2 = Vector2.ZERO) -> void:
112172
if block is EntryBlock:
113173
block.position = canvas_to_window(position).snapped(SNAP_GRID)
@@ -373,6 +433,12 @@ func _on_replace_block_code_button_pressed():
373433
replace_block_code.emit()
374434

375435

436+
func _input(event):
437+
if event is InputEventKey:
438+
if event.keycode == KEY_CTRL:
439+
_modifier_ctrl = event.pressed
440+
441+
376442
func _gui_input(event):
377443
if event is InputEventKey:
378444
if event.keycode == KEY_SHIFT:

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy