Skip to content

Commit

Permalink
BlockCanvas: Implement drag & drop resource files from the filesystem…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
starnight committed Nov 19, 2024
1 parent 8851d72 commit d9f104a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
14 changes: 14 additions & 0 deletions addons/block_code/code_generation/blocks_catalog.gd
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,17 @@ static func get_property_setter_block_definition(variable: VariableDefinition) -
var block_def := get_variable_setter_block_definition(variable)
block_def.description = "Set the %s property" % variable.var_name
return block_def


static func get_resource_block_definition(file_name: String) -> BlockDefinition:
var block_def := BlockDefinition.new()

block_def.name = &"get_resource_%s" % file_name.replace(".", "_")
block_def.description = "The full resource path of '%s'" % file_name
block_def.category = "Variables"
block_def.type = Types.BlockType.VALUE
block_def.variant_type = TYPE_STRING
block_def.display_template = "%s {const file_path: STRING}" % file_name
block_def.code_template = "{file_path}"

return block_def
22 changes: 21 additions & 1 deletion addons/block_code/serialization/block_script_serialization.gd
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ func get_block_definition(block_name: String, arguments: Dictionary) -> BlockDef
var block_definition: BlockDefinition

if len(split) > 1:
return _get_parameter_block_definition(split[0], split[1])
block_definition = _get_parameter_block_definition(split[0], split[1])
if block_definition:
return block_definition

block_definition = _get_base_block_definition(block_name)
if block_definition != null:
Expand All @@ -97,6 +99,11 @@ func get_block_definition(block_name: String, arguments: Dictionary) -> BlockDef
if block_definition != null:
return block_definition

var file_path = arguments.get("file_path", "")
block_definition = _get_resource_block_definition(block_name, file_path)
if block_definition != null:
return block_definition

# FIXME: This is a workaround for old-style output block references.
# These were generated ahead of time using a block name that has
# a "_" before the parameter name. Now, these parameter blocks
Expand Down Expand Up @@ -181,6 +188,19 @@ func _get_parent_node_property_info(property_name: String) -> Dictionary:
return {}


func _get_resource_block_definition(block_name: String, file_path: String) -> BlockDefinition:
if not block_name.begins_with("get_resource_"):
return null

if file_path == "":
return null

var file_name = file_path.get_file()
var block_definition := BlocksCatalog.get_resource_block_definition(file_name)

return block_definition


func _update_block_definitions():
_available_blocks.clear()
_available_blocks.append_array(_get_inherited_block_definitions())
Expand Down
32 changes: 32 additions & 0 deletions addons/block_code/ui/block_canvas/block_canvas.gd
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ func _can_drop_data(at_position: Vector2, data: Variant) -> bool:
return false
return true

# Allow dropping resource file
if data.get("type", "") == "files":
return true

var nodes: Array = data.get("nodes", [])
if nodes.size() != 1:
return false
Expand All @@ -108,6 +112,8 @@ func _drop_data(at_position: Vector2, data: Variant) -> void:
_drop_node(at_position, data)
elif data["type"] == "obj_property":
_drop_obj_property(at_position, data)
elif data["type"] == "files":
_drop_files(at_position, data)


func _drop_node(at_position: Vector2, data: Variant) -> void:
Expand Down Expand Up @@ -148,6 +154,32 @@ func _drop_obj_property(at_position: Vector2, data: Variant) -> void:
reconnect_block.emit(block)


func _drop_files(at_position: Vector2, data: Variant) -> void:
var resource_files = data["files"]
var next_position = at_position
var bias = 10

for file_path in resource_files:
# Prepare a Variable block getting the file's resource path.
var block_definition = _res_file_block_def(file_path)
var block = _context.block_script.instantiate_block(block_definition)
add_block(block, next_position)
reconnect_block.emit(block)

# Shift next block's position a little bit to avoid overlap totally.
next_position.x += bias
next_position.y += bias


func _res_file_block_def(file_path: String) -> BlockDefinition:
var file_name = file_path.get_file()

var block_definition := BlocksCatalog.get_resource_block_definition(file_name)
block_definition.defaults = {"file_path": file_path}

return block_definition


func add_block(block: Block, position: Vector2 = Vector2.ZERO) -> void:
if block is EntryBlock:
block.position = canvas_to_window(position).snapped(SNAP_GRID)
Expand Down

0 comments on commit d9f104a

Please sign in to comment.