Skip to content

Commit 60c692e

Browse files
committed
TemplateEditor: Remove the OPTION parameter type
Instead, we infer that a parameter has a set of options by the existence of an OptionData as its default value. In addition, add a way for a code template to include a plain, unquoted string by using `{{parameter}}`. Previously this happened automatically for any parameter whose value was an OptionData. https://phabricator.endlessm.com/T35564
1 parent 2068594 commit 60c692e

File tree

7 files changed

+30
-22
lines changed

7 files changed

+30
-22
lines changed

addons/block_code/blocks/graphics/animationplayer_play.tres

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
[gd_resource type="Resource" load_steps=5 format=3 uid="uid://c5e1byehtxwc0"]
1+
[gd_resource type="Resource" load_steps=6 format=3 uid="uid://c5e1byehtxwc0"]
22

33
[ext_resource type="Script" path="res://addons/block_code/code_generation/block_definition.gd" id="1_emeuv"]
44
[ext_resource type="Script" path="res://addons/block_code/code_generation/option_data.gd" id="1_xu43h"]
55
[ext_resource type="Script" path="res://addons/block_code/blocks/graphics/animationplayer_play.gd" id="2_7ymgi"]
66

7+
[sub_resource type="Resource" id="Resource_qpxn2"]
8+
script = ExtResource("1_xu43h")
9+
selected = 0
10+
items = []
11+
712
[sub_resource type="Resource" id="Resource_vnp2w"]
813
script = ExtResource("1_xu43h")
914
selected = 0
@@ -17,13 +22,14 @@ description = "Play the animation."
1722
category = "Graphics | Animation"
1823
type = 2
1924
variant_type = 0
20-
display_template = "Play {animation: OPTION} {direction: OPTION}"
21-
code_template = "if \"{direction}\" == \"ahead\":
22-
play(\"{animation}\")
25+
display_template = "Play {animation: STRING} {direction: NIL}"
26+
code_template = "if {direction} == \"ahead\":
27+
play({animation})
2328
else:
24-
play_backwards(\"{animation}\")
29+
play_backwards({animation})
2530
"
2631
defaults = {
32+
"animation": SubResource("Resource_qpxn2"),
2733
"direction": SubResource("Resource_vnp2w")
2834
}
2935
signal_name = ""

addons/block_code/blocks/logic/compare.tres

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ description = ""
1616
category = "Logic | Comparison"
1717
type = 3
1818
variant_type = 1
19-
display_template = "{float1: FLOAT} {op: OPTION} {float2: FLOAT}"
20-
code_template = "{float1} {op} {float2}"
19+
display_template = "{float1: FLOAT} {op: NIL} {float2: FLOAT}"
20+
code_template = "{float1} {{op}} {float2}"
2121
defaults = {
2222
"float1": 1.0,
2323
"float2": 1.0,

addons/block_code/blocks/sounds/pause_continue_sound.tres

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
[sub_resource type="Resource" id="Resource_lalgp"]
77
script = ExtResource("1_ilhdq")
88
selected = 0
9-
items = ["Pause", "Continue"]
9+
items = ["pause", "continue"]
1010

1111
[resource]
1212
script = ExtResource("1_q04gm")
@@ -16,9 +16,9 @@ description = "Pause/Continue the audio stream"
1616
category = "Sounds"
1717
type = 2
1818
variant_type = 0
19-
display_template = "{pause: OPTION} the sound {name: STRING}"
19+
display_template = "{pause: NIL} the sound {name: STRING}"
2020
code_template = "var __sound_node = get_node({name})
21-
if \"{pause}\" == \"pause\":
21+
if {pause} == \"pause\":
2222
__sound_node.stream_paused = true
2323
else:
2424
__sound_node.stream_paused = false

addons/block_code/code_generation/block_ast.gd

+9-5
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,15 @@ class ASTNode:
5555
# Use parentheses to be safe
5656
var argument = arguments[arg_name]
5757
var code_string: String
58+
var raw_string: String
5859
if argument is ASTValueNode:
5960
code_string = argument.get_code()
61+
raw_string = code_string
6062
else:
6163
code_string = BlockAST.raw_input_to_code_string(argument)
64+
raw_string = str(argument)
6265

66+
code_block = code_block.replace("{{%s}}" % arg_name, raw_string)
6367
code_block = code_block.replace("{%s}" % arg_name, code_string)
6468

6569
return IDHandler.make_unique(code_block)
@@ -99,11 +103,15 @@ class ASTValueNode:
99103
# Use parentheses to be safe
100104
var argument = arguments[arg_name]
101105
var code_string: String
106+
var raw_string: String
102107
if argument is ASTValueNode:
103108
code_string = argument.get_code()
109+
raw_string = code_string
104110
else:
105111
code_string = BlockAST.raw_input_to_code_string(argument)
112+
raw_string = str(argument)
106113

114+
code = code.replace("{{%s}}" % arg_name, raw_string)
107115
code = code.replace("{%s}" % arg_name, code_string)
108116

109117
return IDHandler.make_unique("(%s)" % code)
@@ -130,15 +138,11 @@ func to_string_recursive(node: ASTNode, depth: int) -> String:
130138
static func raw_input_to_code_string(input) -> String:
131139
match typeof(input):
132140
TYPE_STRING:
133-
return "'%s'" % input.replace("\\", "\\\\").replace("'", "\\'")
141+
return "'%s'" % input.c_escape()
134142
TYPE_VECTOR2:
135143
return "Vector2%s" % str(input)
136144
TYPE_COLOR:
137145
return "Color%s" % str(input)
138-
TYPE_OBJECT:
139-
if input is OptionData:
140-
var option_data := input as OptionData
141-
return option_data.items[option_data.selected]
142146
_:
143147
return "%s" % input
144148

addons/block_code/code_generation/blocks_catalog.gd

+2-2
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ static func _setup_input_block():
198198
"Input",
199199
Types.BlockType.VALUE,
200200
TYPE_BOOL,
201-
"Is action {action_name: OPTION} {action: OPTION}",
202-
'Input.is_action_{action}("{action_name}")',
201+
"Is action {action_name: STRING_NAME} {action: NIL}",
202+
"Input.is_action_{{action}}('{{action_name}}')",
203203
{"action_name": OptionData.new(inputmap_actions), "action": OptionData.new(["pressed", "just_pressed", "just_released"])},
204204
)
205205
)

addons/block_code/simple_nodes/simple_character/simple_character.gd

+2-2
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ static func setup_custom_blocks():
133133
block_definition.target_node_class = _class_name
134134
block_definition.category = "Input"
135135
block_definition.type = Types.BlockType.STATEMENT
136-
block_definition.display_template = "Move with {player: OPTION} buttons as {kind: OPTION}"
136+
block_definition.display_template = "Move with {player: NIL} buttons as {kind: NIL}"
137137
block_definition.description = """Move the character using the “Player 1” or “Player 2” controls as configured in Godot.
138138
139139
“Top-down” enables the character to move in both x (vertical) and y (horizontal) dimensions, as if the camera is above the character, looking down. No gravity is added.
@@ -143,7 +143,7 @@ static func setup_custom_blocks():
143143
“Spaceship” uses the left/right controls to turn the character and up/down controls to go forward or backward."""
144144
# TODO: delta here is assumed to be the parameter name of
145145
# the _process or _physics_process method:
146-
block_definition.code_template = 'move_with_player_buttons("{player}", "{kind}", delta)'
146+
block_definition.code_template = "move_with_player_buttons({player}, {kind}, delta)"
147147
block_definition.defaults = {
148148
"player": OptionData.new(["player_1", "player_2"]),
149149
"kind": OptionData.new(["top-down", "platformer", "spaceship"]),

addons/block_code/ui/blocks/utilities/template_editor/template_editor.gd

+1-3
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,11 @@ func _append_input_parameter(parameter: Dictionary, id: int):
114114
parameter_input.placeholder = parameter["name"]
115115
parameter_input.variant_type = parameter["type"]
116116

117-
if parameter["is_option"] and default_value is OptionData:
117+
if default_value is OptionData:
118118
var option_data := default_value as OptionData
119119
parameter_input.option_data = option_data
120120
if option_data.selected < option_data.items.size():
121121
parameter_input.default_value = option_data.items[option_data.selected]
122-
elif parameter["is_option"]:
123-
push_warning("The block parameter %s in %s appears to be an option, but no option data is provided" % [parameter_format, parent_block])
124122
else:
125123
parameter_input.default_value = default_value
126124

0 commit comments

Comments
 (0)