Skip to content

Commit d9f104a

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. It uses the const input parameter as the implementation to hold the resource file's path informaiton. https://phabricator.endlessm.com/T35712
1 parent 8851d72 commit d9f104a

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

addons/block_code/code_generation/blocks_catalog.gd

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,3 +302,17 @@ static func get_property_setter_block_definition(variable: VariableDefinition) -
302302
var block_def := get_variable_setter_block_definition(variable)
303303
block_def.description = "Set the %s property" % variable.var_name
304304
return block_def
305+
306+
307+
static func get_resource_block_definition(file_name: String) -> BlockDefinition:
308+
var block_def := BlockDefinition.new()
309+
310+
block_def.name = &"get_resource_%s" % file_name.replace(".", "_")
311+
block_def.description = "The full resource path of '%s'" % file_name
312+
block_def.category = "Variables"
313+
block_def.type = Types.BlockType.VALUE
314+
block_def.variant_type = TYPE_STRING
315+
block_def.display_template = "%s {const file_path: STRING}" % file_name
316+
block_def.code_template = "{file_path}"
317+
318+
return block_def

addons/block_code/serialization/block_script_serialization.gd

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ func get_block_definition(block_name: String, arguments: Dictionary) -> BlockDef
8686
var block_definition: BlockDefinition
8787

8888
if len(split) > 1:
89-
return _get_parameter_block_definition(split[0], split[1])
89+
block_definition = _get_parameter_block_definition(split[0], split[1])
90+
if block_definition:
91+
return block_definition
9092

9193
block_definition = _get_base_block_definition(block_name)
9294
if block_definition != null:
@@ -97,6 +99,11 @@ func get_block_definition(block_name: String, arguments: Dictionary) -> BlockDef
9799
if block_definition != null:
98100
return block_definition
99101

102+
var file_path = arguments.get("file_path", "")
103+
block_definition = _get_resource_block_definition(block_name, file_path)
104+
if block_definition != null:
105+
return block_definition
106+
100107
# FIXME: This is a workaround for old-style output block references.
101108
# These were generated ahead of time using a block name that has
102109
# a "_" before the parameter name. Now, these parameter blocks
@@ -181,6 +188,19 @@ func _get_parent_node_property_info(property_name: String) -> Dictionary:
181188
return {}
182189

183190

191+
func _get_resource_block_definition(block_name: String, file_path: String) -> BlockDefinition:
192+
if not block_name.begins_with("get_resource_"):
193+
return null
194+
195+
if file_path == "":
196+
return null
197+
198+
var file_name = file_path.get_file()
199+
var block_definition := BlocksCatalog.get_resource_block_definition(file_name)
200+
201+
return block_definition
202+
203+
184204
func _update_block_definitions():
185205
_available_blocks.clear()
186206
_available_blocks.append_array(_get_inherited_block_definitions())

addons/block_code/ui/block_canvas/block_canvas.gd

Lines changed: 32 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,32 @@ 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+
var bias = 10
161+
162+
for file_path in resource_files:
163+
# Prepare a Variable block getting the file's resource path.
164+
var block_definition = _res_file_block_def(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+
174+
func _res_file_block_def(file_path: String) -> BlockDefinition:
175+
var file_name = file_path.get_file()
176+
177+
var block_definition := BlocksCatalog.get_resource_block_definition(file_name)
178+
block_definition.defaults = {"file_path": file_path}
179+
180+
return block_definition
181+
182+
151183
func add_block(block: Block, position: Vector2 = Vector2.ZERO) -> void:
152184
if block is EntryBlock:
153185
block.position = canvas_to_window(position).snapped(SNAP_GRID)

0 commit comments

Comments
 (0)