Skip to content

Commit 2714dde

Browse files
committed
BlockCanvas: Allow dragging and dropping nodes
When a node is dragged from the scene tree dock, dynamically create a get_node block for it and add it to the canvas. This is an alternative to using the get_node block from the picker and changing the drop down to the appropriate path. https://phabricator.endlessm.com/T35648
1 parent 4cc4b28 commit 2714dde

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

addons/block_code/ui/block_canvas/block_canvas.gd

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,45 @@ func _ready():
6060
_open_scene_button.icon = _open_scene_icon
6161

6262

63+
func _can_drop_data(at_position: Vector2, data: Variant) -> bool:
64+
if _context.block_code_node == null or _context.parent_node == null:
65+
return false
66+
if typeof(data) != TYPE_DICTIONARY:
67+
return false
68+
69+
var nodes: Array = data.get("nodes", [])
70+
if nodes.size() != 1:
71+
return false
72+
var abs_path: NodePath = nodes[0]
73+
74+
# Don't allow dropping BlockCode nodes or nodes that aren't part of the
75+
# edited scene.
76+
var node := get_tree().root.get_node(abs_path)
77+
if node is BlockCode or not Util.node_is_part_of_edited_scene(node):
78+
return false
79+
80+
# Don't allow dropping the BlockCode node's parent as that's already self.
81+
var parent_path: NodePath = _context.parent_node.get_path()
82+
return abs_path != parent_path
83+
84+
85+
func _drop_data(at_position: Vector2, data: Variant) -> void:
86+
var abs_path: NodePath = data.get("nodes", []).pop_back()
87+
if abs_path == null:
88+
return
89+
90+
# Figure out the best path to the node.
91+
var node := get_tree().root.get_node(abs_path)
92+
var node_path: NodePath = Util.node_scene_path(node, _context.parent_node)
93+
if node_path in [^"", ^"."]:
94+
return
95+
96+
var block = _context.block_script.instantiate_block_by_name(&"get_node")
97+
block.set_parameter_values_on_ready({"path": node_path})
98+
add_block(block, at_position)
99+
reconnect_block.emit(block)
100+
101+
63102
func add_block(block: Block, position: Vector2 = Vector2.ZERO) -> void:
64103
if block is EntryBlock:
65104
block.position = canvas_to_window(position).snapped(SNAP_GRID)

addons/block_code/ui/block_canvas/block_canvas.tscn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ script = ExtResource("1_tk8h2")
1515

1616
[node name="Panel" type="Panel" parent="."]
1717
layout_mode = 2
18+
mouse_filter = 2
1819

1920
[node name="WindowContainer" type="Control" parent="."]
2021
clip_contents = true
2122
layout_mode = 2
23+
mouse_filter = 2
2224

2325
[node name="Window" type="Control" parent="WindowContainer"]
2426
unique_name_in_owner = true

0 commit comments

Comments
 (0)