Skip to content

Commit 6b7c9d2

Browse files
committed
BlockCanvas: Implement drag & drop resource files from the filesystem dock
Dragging the resources from the filesystem dock shows the object's type as "files". So, allow the "files" type in _can_drop_data() and handle it in _drop_data(). Then, implement getting the resource's file path as a Variable block with _drop_files() in detail. And, because Godot allows dragging multiple files at the same time, this implemention parses the "files"' paths, then generates the corresponding block definition with get_resource_block_definition() and sets default "file_path" for each file's path. Besides, to avoid the dropped blocks overlap with each others on the canvas, each block biases its position a little bit automatically. https://phabricator.endlessm.com/T35712
1 parent c8b7692 commit 6b7c9d2

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

addons/block_code/ui/block_canvas/block_canvas.gd

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ func _can_drop_data(at_position: Vector2, data: Variant) -> bool:
8787
return false
8888
return true
8989

90+
# Allow dropping resource file
91+
if data.get("type", "") == "files":
92+
return true
93+
9094
var nodes: Array = data.get("nodes", [])
9195
if nodes.size() != 1:
9296
return false
@@ -108,6 +112,8 @@ func _drop_data(at_position: Vector2, data: Variant) -> void:
108112
_drop_node(at_position, data)
109113
elif data["type"] == "obj_property":
110114
_drop_obj_property(at_position, data)
115+
elif data["type"] == "files":
116+
_drop_files(at_position, data)
111117

112118

113119
func _drop_node(at_position: Vector2, data: Variant) -> void:
@@ -148,6 +154,23 @@ func _drop_obj_property(at_position: Vector2, data: Variant) -> void:
148154
reconnect_block.emit(block)
149155

150156

157+
func _drop_files(at_position: Vector2, data: Variant) -> void:
158+
var resource_files = data["files"]
159+
var next_position = at_position
160+
const bias = 20
161+
162+
for file_path in resource_files:
163+
# Prepare a Variable block getting the file's resource path.
164+
var block_definition = BlocksCatalog.get_resource_block_definition(file_path)
165+
var block = _context.block_script.instantiate_block(block_definition)
166+
add_block(block, next_position)
167+
reconnect_block.emit(block)
168+
169+
# Shift next block's position a little bit to avoid overlap totally.
170+
next_position.x += bias
171+
next_position.y += bias
172+
173+
151174
func add_block(block: Block, position: Vector2 = Vector2.ZERO) -> void:
152175
if block is EntryBlock:
153176
block.position = canvas_to_window(position).snapped(SNAP_GRID)

0 commit comments

Comments
 (0)