From 565024f48c98833a2ae9fe4305f26c9083f4eb98 Mon Sep 17 00:00:00 2001 From: Jian-Hong Pan Date: Mon, 11 Nov 2024 18:23:21 +0800 Subject: [PATCH] BlockCanvas: Introduce get_property_(get|set)ter_block_definition for 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__" for getter block * "set_var_" for setter block Fixes: 89beea94eefa ("BlockCanvas: Implement drag & drop the node's property from Inspector") https://phabricator.endlessm.com/T35649 --- .../code_generation/blocks_catalog.gd | 17 +++++++- .../ui/block_canvas/block_canvas.gd | 43 ++++++------------- 2 files changed, 28 insertions(+), 32 deletions(-) diff --git a/addons/block_code/code_generation/blocks_catalog.gd b/addons/block_code/code_generation/blocks_catalog.gd index 4f8ca748..46cf3e36 100644 --- a/addons/block_code/code_generation/blocks_catalog.gd +++ b/addons/block_code/code_generation/blocks_catalog.gd @@ -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 diff --git a/addons/block_code/ui/block_canvas/block_canvas.gd b/addons/block_code/ui/block_canvas/block_canvas.gd index 2f4fcc51..7fb9f84c 100644 --- a/addons/block_code/ui/block_canvas/block_canvas.gd +++ b/addons/block_code/ui/block_canvas/block_canvas.gd @@ -3,6 +3,7 @@ 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") @@ -10,6 +11,7 @@ const DragManager = preload("res://addons/block_code/drag_manager/drag_manager.g 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) @@ -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)