Skip to content

Commit 89beea9

Browse files
committed
BlockCanvas: Implement drag & drop the node's property from Inspector
Dragging the node's property from Inspector shows the object's type as "obj_property". So, allow the "obj_property" type in _can_drop_data() and handle it in _drop_data(). Then, implement getting the property's value as a Variable block with _drop_obj_property() in detail. https://phabricator.endlessm.com/T35649
1 parent 91396be commit 89beea9

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

addons/block_code/ui/block_canvas/block_canvas.gd

Lines changed: 32 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
@@ -75,6 +77,10 @@ func _can_drop_data(at_position: Vector2, data: Variant) -> bool:
7577
if typeof(data) != TYPE_DICTIONARY:
7678
return false
7779

80+
# Allow dropping property block
81+
if data.get("type", "") == "obj_property":
82+
return true
83+
7884
var nodes: Array = data.get("nodes", [])
7985
if nodes.size() != 1:
8086
return false
@@ -94,6 +100,8 @@ func _can_drop_data(at_position: Vector2, data: Variant) -> bool:
94100
func _drop_data(at_position: Vector2, data: Variant) -> void:
95101
if data["type"] == "nodes":
96102
_drop_node(at_position, data)
103+
elif data["type"] == "obj_property":
104+
_drop_obj_property(at_position, data)
97105

98106

99107
func _drop_node(at_position: Vector2, data: Variant) -> void:
@@ -113,6 +121,30 @@ func _drop_node(at_position: Vector2, data: Variant) -> void:
113121
reconnect_block.emit(block)
114122

115123

124+
func _drop_obj_property(at_position: Vector2, data: Variant) -> void:
125+
var object_name = str(data["object"]).get_slice(":", 0)
126+
var property_name = data["property"]
127+
var property_value = data["value"]
128+
129+
# Prepare a block to get the property's value
130+
var block_definition = (
131+
BlockDefinition
132+
. new(
133+
&"%s_get_%s" % [object_name, property_name],
134+
object_name,
135+
"The %s property" % property_name,
136+
"Variables",
137+
Types.BlockType.VALUE,
138+
typeof(property_value),
139+
"%s" % property_name.capitalize().to_lower(),
140+
"%s" % property_name,
141+
)
142+
)
143+
var block = _context.block_script.instantiate_block(block_definition)
144+
add_block(block, at_position)
145+
reconnect_block.emit(block)
146+
147+
116148
func add_block(block: Block, position: Vector2 = Vector2.ZERO) -> void:
117149
if block is EntryBlock:
118150
block.position = canvas_to_window(position).snapped(SNAP_GRID)

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