Skip to content

Commit

Permalink
BlockCanvas: Have object property's block from get_obj_property_block…
Browse files Browse the repository at this point in the history
…_definition

Since _drop_obj_property() generates object property blocks with the
same code in current _get_obj_property_block_definition(). Let's export
and reuse get_obj_property_block_definition() directly. It makes the
code more consistent.

https://phabricator.endlessm.com/T35649
  • Loading branch information
starnight committed Nov 8, 2024
1 parent e8ee69e commit ea8226b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 31 deletions.
4 changes: 2 additions & 2 deletions addons/block_code/serialization/block_script_serialization.gd
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func get_block_definition(block_name: String, arguments: Dictionary) -> BlockDef
return block_definition

var property_value = arguments.get("value", null)
block_definition = _get_obj_property_block_definition(block_name, property_value)
block_definition = get_obj_property_block_definition(block_name, property_value)
if block_definition != null:
return block_definition

Expand Down Expand Up @@ -138,7 +138,7 @@ func _get_parameter_block_definition(block_name: String, parameter_name: String)
return block_definition


func _get_obj_property_block_definition(block_name: String, property_value: Variant) -> BlockDefinition:
func get_obj_property_block_definition(block_name: String, property_value: Variant) -> BlockDefinition:
var split := block_name.split("_get_", false, 2)
var is_getter = true

Expand Down
37 changes: 8 additions & 29 deletions addons/block_code/ui/block_canvas/block_canvas.gd
Original file line number Diff line number Diff line change
Expand Up @@ -131,37 +131,16 @@ func _drop_obj_property(at_position: Vector2, data: Variant) -> void:
# Prepare a Variable block to set / get the property's value according to
# the modifier KEY_CTRL pressing.
var block_definition: BlockDefinition
var property_type = typeof(property_value)
var block_name: String
var arguments := {"value": 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},
)
)
block_name = &"%s_set_%s" % [object_name, property_name]
block_definition = _context.block_script.get_obj_property_block_definition(block_name, property_value)
block_definition.defaults = arguments
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_name = &"%s_get_%s" % [object_name, property_name]
block_definition = _context.block_script.get_obj_property_block_definition(block_name, property_value)

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

0 comments on commit ea8226b

Please sign in to comment.