Skip to content

Commit

Permalink
BlockCanvas: Reuse get_variable_(get|set)ter_block_definition for dro…
Browse files Browse the repository at this point in the history
…p object property

The object property's getter & setter blocks generating code is similar
to get_variable_(get|set)ter_block_definition(). So, just reuse them
directly.

And, the block name will change to "(get|set)_var_<property name>".

Fixes: 89beea9 ("BlockCanvas: Implement drag & drop the node's property from Inspector")
https://phabricator.endlessm.com/T35649
  • Loading branch information
starnight committed Nov 11, 2024
1 parent 73781f7 commit f8c6ce3
Showing 1 changed file with 7 additions and 30 deletions.
37 changes: 7 additions & 30 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,19 @@ 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"]

# 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},
)
)
block_definition = BlocksCatalog.get_variable_setter_block_definition(variable)
block_definition.defaults = {"value": property_value}
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_variable_getter_block_definition(variable)

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

0 comments on commit f8c6ce3

Please sign in to comment.