diff --git a/addons/block_code/block_code_plugin.gd b/addons/block_code/block_code_plugin.gd index c4370d6b..96b9b7ab 100644 --- a/addons/block_code/block_code_plugin.gd +++ b/addons/block_code/block_code_plugin.gd @@ -1,5 +1,6 @@ @tool extends EditorPlugin + const MainPanelScene := preload("res://addons/block_code/ui/main_panel.tscn") const MainPanel = preload("res://addons/block_code/ui/main_panel.gd") const Types = preload("res://addons/block_code/types/types.gd") @@ -93,16 +94,10 @@ func _exit_tree(): func _ready(): - connect("scene_changed", _on_scene_changed) editor_inspector.connect("edited_object_changed", _on_editor_inspector_edited_object_changed) - _on_scene_changed(EditorInterface.get_edited_scene_root()) _on_editor_inspector_edited_object_changed() -func _on_scene_changed(scene_root: Node): - main_panel.switch_scene(scene_root) - - func _on_editor_inspector_edited_object_changed(): var edited_object = editor_inspector.get_edited_object() #var edited_node = edited_object as Node diff --git a/addons/block_code/blocks/graphics/animationplayer_play.gd b/addons/block_code/blocks/graphics/animationplayer_play.gd new file mode 100644 index 00000000..63c603a2 --- /dev/null +++ b/addons/block_code/blocks/graphics/animationplayer_play.gd @@ -0,0 +1,15 @@ +@tool +extends BlockExtension + +const OptionData = preload("res://addons/block_code/code_generation/option_data.gd") + + +func get_defaults_for_node(context_node: Node) -> Dictionary: + var animation_player = context_node as AnimationPlayer + + if not animation_player: + return {} + + var animation_list = animation_player.get_animation_list() + + return {"animation": OptionData.new(animation_list)} diff --git a/addons/block_code/blocks/graphics/animationplayer_play.tres b/addons/block_code/blocks/graphics/animationplayer_play.tres index 2a2f373f..750111de 100644 --- a/addons/block_code/blocks/graphics/animationplayer_play.tres +++ b/addons/block_code/blocks/graphics/animationplayer_play.tres @@ -1,7 +1,13 @@ -[gd_resource type="Resource" load_steps=4 format=3 uid="uid://c5e1byehtxwc0"] +[gd_resource type="Resource" load_steps=6 format=3 uid="uid://c5e1byehtxwc0"] [ext_resource type="Script" path="res://addons/block_code/code_generation/block_definition.gd" id="1_emeuv"] [ext_resource type="Script" path="res://addons/block_code/code_generation/option_data.gd" id="1_xu43h"] +[ext_resource type="Script" path="res://addons/block_code/blocks/graphics/animationplayer_play.gd" id="2_7ymgi"] + +[sub_resource type="Resource" id="Resource_qpxn2"] +script = ExtResource("1_xu43h") +selected = 0 +items = [] [sub_resource type="Resource" id="Resource_vnp2w"] script = ExtResource("1_xu43h") @@ -16,14 +22,16 @@ description = "Play the animation." category = "Graphics | Animation" type = 2 variant_type = 0 -display_template = "Play {animation: STRING} {direction: OPTION}" -code_template = "if \"{direction}\" == \"ahead\": +display_template = "Play {animation: STRING} {direction: NIL}" +code_template = "if {direction} == \"ahead\": play({animation}) else: play_backwards({animation}) " defaults = { +"animation": SubResource("Resource_qpxn2"), "direction": SubResource("Resource_vnp2w") } signal_name = "" scope = "" +extension_script = ExtResource("2_7ymgi") diff --git a/addons/block_code/blocks/logic/compare.tres b/addons/block_code/blocks/logic/compare.tres index f657ba44..d4b4dc4f 100644 --- a/addons/block_code/blocks/logic/compare.tres +++ b/addons/block_code/blocks/logic/compare.tres @@ -16,8 +16,8 @@ description = "" category = "Logic | Comparison" type = 3 variant_type = 1 -display_template = "{float1: FLOAT} {op: OPTION} {float2: FLOAT}" -code_template = "{float1} {op} {float2}" +display_template = "{float1: FLOAT} {op: NIL} {float2: FLOAT}" +code_template = "{float1} {{op}} {float2}" defaults = { "float1": 1.0, "float2": 1.0, diff --git a/addons/block_code/blocks/sounds/pause_continue_sound.tres b/addons/block_code/blocks/sounds/pause_continue_sound.tres index 3cbfab88..17487bbc 100644 --- a/addons/block_code/blocks/sounds/pause_continue_sound.tres +++ b/addons/block_code/blocks/sounds/pause_continue_sound.tres @@ -6,7 +6,7 @@ [sub_resource type="Resource" id="Resource_lalgp"] script = ExtResource("1_ilhdq") selected = 0 -items = ["Pause", "Continue"] +items = ["pause", "continue"] [resource] script = ExtResource("1_q04gm") @@ -16,9 +16,9 @@ description = "Pause/Continue the audio stream" category = "Sounds" type = 2 variant_type = 0 -display_template = "{pause: OPTION} the sound {name: STRING}" +display_template = "{pause: NIL} the sound {name: STRING}" code_template = "var __sound_node = get_node({name}) -if \"{pause}\" == \"pause\": +if {pause} == \"pause\": __sound_node.stream_paused = true else: __sound_node.stream_paused = false diff --git a/addons/block_code/code_generation/block_ast.gd b/addons/block_code/code_generation/block_ast.gd index df35e354..a90657e2 100644 --- a/addons/block_code/code_generation/block_ast.gd +++ b/addons/block_code/code_generation/block_ast.gd @@ -44,31 +44,15 @@ class ASTNode: children = [] arguments = {} - func get_code_block() -> String: - var code_block: String = data.code_template # get multiline code_template from block definition - - # insert args - - # check if args match an overload in the resource - - for arg_name in arguments: - # Use parentheses to be safe - var argument = arguments[arg_name] - var code_string: String - if argument is ASTValueNode: - code_string = argument.get_code() - else: - code_string = BlockAST.raw_input_to_code_string(argument) - - code_block = code_block.replace("{%s}" % arg_name, code_string) - + func _get_code_block() -> String: + var code_block: String = BlockAST.format_code_template(data.code_template, arguments) return IDHandler.make_unique(code_block) func get_code(depth: int) -> String: var code: String = "" # append code block - var code_block := get_code_block() + var code_block := _get_code_block() code_block = code_block.indent("\t".repeat(depth)) code += code_block + "\n" @@ -91,21 +75,7 @@ class ASTValueNode: arguments = {} func get_code() -> String: - var code: String = data.code_template # get code_template from block definition - - # check if args match an overload in the resource - - for arg_name in arguments: - # Use parentheses to be safe - var argument = arguments[arg_name] - var code_string: String - if argument is ASTValueNode: - code_string = argument.get_code() - else: - code_string = BlockAST.raw_input_to_code_string(argument) - - code = code.replace("{%s}" % arg_name, code_string) - + var code: String = BlockAST.format_code_template(data.code_template, arguments) return IDHandler.make_unique("(%s)" % code) @@ -127,18 +97,42 @@ func to_string_recursive(node: ASTNode, depth: int) -> String: return string +static func format_code_template(code_template: String, arguments: Dictionary) -> String: + for argument_name in arguments: + # Use parentheses to be safe + var argument_value: Variant = arguments[argument_name] + var code_string: String + var raw_string: String + + if argument_value is OptionData: + # Temporary hack: previously, the value was stored as an OptionData + # object with a list of items and a "selected" property. If we are + # using an older block script where that is the case, convert the + # value to the value of its selected item. + # See also, ParameterInput._update_option_input. + argument_value = argument_value.items[argument_value.selected] + + if argument_value is ASTValueNode: + code_string = argument_value.get_code() + raw_string = code_string + else: + code_string = BlockAST.raw_input_to_code_string(argument_value) + raw_string = str(argument_value) + + code_template = code_template.replace("{{%s}}" % argument_name, raw_string) + code_template = code_template.replace("{%s}" % argument_name, code_string) + + return code_template + + static func raw_input_to_code_string(input) -> String: match typeof(input): TYPE_STRING: - return "'%s'" % input.replace("\\", "\\\\").replace("'", "\\'") + return "'%s'" % input.c_escape() TYPE_VECTOR2: return "Vector2%s" % str(input) TYPE_COLOR: return "Color%s" % str(input) - TYPE_OBJECT: - if input is OptionData: - var option_data := input as OptionData - return option_data.items[option_data.selected] _: return "%s" % input diff --git a/addons/block_code/code_generation/block_definition.gd b/addons/block_code/code_generation/block_definition.gd index 9f3bbb90..fe36c001 100644 --- a/addons/block_code/code_generation/block_definition.gd +++ b/addons/block_code/code_generation/block_definition.gd @@ -4,6 +4,8 @@ extends Resource const Types = preload("res://addons/block_code/types/types.gd") +const FORMAT_STRING_PATTERN = "\\[(?[^\\]]+)\\]|\\{(?[^}]+)\\}|(?