Skip to content

Commit a0df3fd

Browse files
committed
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_<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
1 parent be8f7f8 commit a0df3fd

File tree

2 files changed

+29
-32
lines changed

2 files changed

+29
-32
lines changed

addons/block_code/code_generation/blocks_catalog.gd

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,21 @@ static func get_variable_setter_block_definition(variable: VariableDefinition) -
287287
block_def.category = "Variables"
288288
block_def.type = Types.BlockType.STATEMENT
289289
block_def.display_template = "Set %s to {value: %s}" % [variable.var_name, type_string]
290-
block_def.code_template = "%s = {value}" % [variable.var_name]
290+
block_def.code_template = "%s = {value}" % variable.var_name
291291

292292
return block_def
293+
294+
295+
static func get_property_getter_block_definition(variable: VariableDefinition) -> BlockDefinition:
296+
var block_def := get_variable_getter_block_definition(variable)
297+
# Getter block loses variant type information after close project.
298+
# So, encode the variant type into block name for restoration.
299+
block_def.name = "%s_%d" % [block_def.name, variable.var_type]
300+
block_def.description = "The %s property" % variable.var_name
301+
return block_def
302+
303+
304+
static func get_property_setter_block_definition(variable: VariableDefinition) -> BlockDefinition:
305+
var block_def := get_variable_setter_block_definition(variable)
306+
block_def.description = "Set the %s property" % variable.var_name
307+
return block_def

addons/block_code/ui/block_canvas/block_canvas.gd

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ extends MarginContainer
33

44
const ASTList = preload("res://addons/block_code/code_generation/ast_list.gd")
55
const BlockAST = preload("res://addons/block_code/code_generation/block_ast.gd")
6+
const BlocksCatalog = preload("res://addons/block_code/code_generation/blocks_catalog.gd")
67
const BlockCodePlugin = preload("res://addons/block_code/block_code_plugin.gd")
78
const BlockDefinition = preload("res://addons/block_code/code_generation/block_definition.gd")
89
const BlockTreeUtil = preload("res://addons/block_code/ui/block_tree_util.gd")
910
const DragManager = preload("res://addons/block_code/drag_manager/drag_manager.gd")
1011
const ScriptGenerator = preload("res://addons/block_code/code_generation/script_generator.gd")
1112
const Types = preload("res://addons/block_code/types/types.gd")
1213
const Util = preload("res://addons/block_code/ui/util.gd")
14+
const VariableDefinition = preload("res://addons/block_code/code_generation/variable_definition.gd")
1315

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

125127

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

131133
# Prepare a Variable block to set / get the property's value according to
132134
# the modifier KEY_CTRL pressing.
135+
var variable := VariableDefinition.new(property_name, typeof(property_value))
133136
var block_definition: BlockDefinition
134-
var property_type = typeof(property_value)
135-
if _modifier_ctrl:
136-
var type_string: String = Types.VARIANT_TYPE_TO_STRING[property_type]
137-
block_definition = (
138-
BlockDefinition
139-
. new(
140-
&"%s_set_%s" % [object_name, property_name],
141-
object_name,
142-
"Set the %s property" % property_name,
143-
"Variables",
144-
Types.BlockType.STATEMENT,
145-
property_type,
146-
"set %s to {value: %s}" % [property_name.capitalize().to_lower(), type_string],
147-
"%s = {value}" % property_name,
148-
{"value": property_value},
149-
)
150-
)
137+
var parameter_values: Dictionary
138+
139+
parameter_values["value"] = property_value
140+
141+
if is_getter:
142+
block_definition = BlocksCatalog.get_property_getter_block_definition(variable)
151143
else:
152-
block_definition = (
153-
BlockDefinition
154-
. new(
155-
&"%s_get_%s" % [object_name, property_name],
156-
object_name,
157-
"The %s property" % property_name,
158-
"Variables",
159-
Types.BlockType.VALUE,
160-
property_type,
161-
"%s" % property_name.capitalize().to_lower(),
162-
"%s" % property_name,
163-
)
164-
)
144+
block_definition = BlocksCatalog.get_property_setter_block_definition(variable)
145+
block_definition.defaults = parameter_values
165146

166147
var block = _context.block_script.instantiate_block(block_definition)
167148
add_block(block, at_position)
@@ -335,6 +316,7 @@ func build_ast(block: Block) -> BlockAST.ASTNode:
335316

336317
var parameter_values := block.get_parameter_values()
337318

319+
print(block)
338320
for arg_name in parameter_values:
339321
var arg_value = parameter_values[arg_name]
340322
if arg_value is Block:

0 commit comments

Comments
 (0)