Skip to content

Commit

Permalink
BlockCanvas: Introduce get_property_(get|set)ter_block_definition for…
Browse files Browse the repository at this point in the history
… drop object property

The object property's getter & setter blocks generating code is similar
to get_variable_(get|set)ter_block_definition(). So, reuse them within
get_property_(get|set)ter_block_definition() to generate property's
blocks including the description.

And, the block name will change to:
* "get_var_<property name>_<variant type code>" for getter block
* "set_var_<property name>" for setter block

Fixes: 89beea9 ("BlockCanvas: Implement drag & drop the node's property from Inspector")
https://phabricator.endlessm.com/T35649
  • Loading branch information
starnight committed Nov 13, 2024
1 parent be8f7f8 commit 565024f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 32 deletions.
17 changes: 16 additions & 1 deletion addons/block_code/code_generation/blocks_catalog.gd
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,21 @@ static func get_variable_setter_block_definition(variable: VariableDefinition) -
block_def.category = "Variables"
block_def.type = Types.BlockType.STATEMENT
block_def.display_template = "Set %s to {value: %s}" % [variable.var_name, type_string]
block_def.code_template = "%s = {value}" % [variable.var_name]
block_def.code_template = "%s = {value}" % variable.var_name

return block_def


static func get_property_getter_block_definition(variable: VariableDefinition) -> BlockDefinition:
var block_def := get_variable_getter_block_definition(variable)
# Getter block loses variant type information after close project.
# So, encode the variant type into block name for restoration.
block_def.name = "%s_%d" % [block_def.name, variable.var_type]
block_def.description = "The %s property" % variable.var_name
return block_def


static func get_property_setter_block_definition(variable: VariableDefinition) -> BlockDefinition:
var block_def := get_variable_setter_block_definition(variable)
block_def.description = "Set the %s property" % variable.var_name
return block_def
43 changes: 12 additions & 31 deletions addons/block_code/ui/block_canvas/block_canvas.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ extends MarginContainer

const ASTList = preload("res://addons/block_code/code_generation/ast_list.gd")
const BlockAST = preload("res://addons/block_code/code_generation/block_ast.gd")
const BlocksCatalog = preload("res://addons/block_code/code_generation/blocks_catalog.gd")
const BlockCodePlugin = preload("res://addons/block_code/block_code_plugin.gd")
const BlockDefinition = preload("res://addons/block_code/code_generation/block_definition.gd")
const BlockTreeUtil = preload("res://addons/block_code/ui/block_tree_util.gd")
const DragManager = preload("res://addons/block_code/drag_manager/drag_manager.gd")
const ScriptGenerator = preload("res://addons/block_code/code_generation/script_generator.gd")
const Types = preload("res://addons/block_code/types/types.gd")
const Util = preload("res://addons/block_code/ui/util.gd")
const VariableDefinition = preload("res://addons/block_code/code_generation/variable_definition.gd")

const EXTEND_MARGIN: float = 800
const BLOCK_AUTO_PLACE_MARGIN: Vector2 = Vector2(25, 8)
Expand Down Expand Up @@ -124,44 +126,23 @@ func _drop_node(at_position: Vector2, data: Variant) -> void:


func _drop_obj_property(at_position: Vector2, data: Variant) -> void:
var object_name = str(data["object"]).get_slice(":", 0)
var property_name = data["property"]
var property_value = data["value"]
var is_getter = !_modifier_ctrl

# Prepare a Variable block to set / get the property's value according to
# the modifier KEY_CTRL pressing.
var variable := VariableDefinition.new(property_name, typeof(property_value))
var block_definition: BlockDefinition
var property_type = typeof(property_value)
if _modifier_ctrl:
var type_string: String = Types.VARIANT_TYPE_TO_STRING[property_type]
block_definition = (
BlockDefinition
. new(
&"%s_set_%s" % [object_name, property_name],
object_name,
"Set the %s property" % property_name,
"Variables",
Types.BlockType.STATEMENT,
property_type,
"set %s to {value: %s}" % [property_name.capitalize().to_lower(), type_string],
"%s = {value}" % property_name,
{"value": property_value},
)
)
var parameter_values: Dictionary

parameter_values["value"] = property_value

if is_getter:
block_definition = BlocksCatalog.get_property_getter_block_definition(variable)
else:
block_definition = (
BlockDefinition
. new(
&"%s_get_%s" % [object_name, property_name],
object_name,
"The %s property" % property_name,
"Variables",
Types.BlockType.VALUE,
property_type,
"%s" % property_name.capitalize().to_lower(),
"%s" % property_name,
)
)
block_definition = BlocksCatalog.get_property_setter_block_definition(variable)
block_definition.defaults = parameter_values

var block = _context.block_script.instantiate_block(block_definition)
add_block(block, at_position)
Expand Down

0 comments on commit 565024f

Please sign in to comment.