Skip to content

Commit baf52cc

Browse files
committed
ParameterInput: Add a separate option data property
Instead of inferring that there is a list of options based on the current value, ParameterInput expects to be given a list of options, and get_raw_input and set_raw_input understand that the raw input is a value from the list. In addition, change TemplateEditor itself to determine the list of options for a given parameter. This option data is always from the parameter_defaults property, regardless of the block's current value. https://phabricator.endlessm.com/T35564
1 parent 0e9cd47 commit baf52cc

File tree

3 files changed

+61
-22
lines changed

3 files changed

+61
-22
lines changed

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

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ signal modified
1010

1111
@export var variant_type: Variant.Type = TYPE_STRING
1212
@export var block_type: Types.BlockType = Types.BlockType.VALUE
13-
var option: bool = false
13+
@export var option_data: OptionData:
14+
set = _set_option_data
15+
1416
var default_value: Variant
1517

1618
@onready var _panel := %Panel
@@ -42,16 +44,13 @@ var default_value: Variant
4244

4345

4446
func set_raw_input(raw_input):
45-
if option:
46-
_panel.visible = false
47-
_option_input.clear()
48-
var option_data: OptionData = raw_input as OptionData
49-
for item in option_data.items:
50-
_option_input.add_item(item.capitalize())
51-
_option_input.select(option_data.selected)
52-
47+
if option_data:
48+
_update_option_input(raw_input)
5349
return
5450

51+
if raw_input == null:
52+
raw_input = default_value
53+
5554
match variant_type:
5655
TYPE_COLOR:
5756
_color_input.color = raw_input
@@ -61,19 +60,16 @@ func set_raw_input(raw_input):
6160
_x_line_edit.text = ("%.4f" % raw_input.x).rstrip("0").rstrip(".")
6261
_y_line_edit.text = ("%.4f" % raw_input.y).rstrip("0").rstrip(".")
6362
TYPE_BOOL:
64-
_bool_input_option.select(raw_input)
63+
_bool_input_option.select(1 if raw_input else 0)
6564
TYPE_NIL:
66-
_line_edit.text = raw_input
65+
_line_edit.text = raw_input if raw_input else ""
6766
_:
68-
_line_edit.text = "" if raw_input == null else str(raw_input)
67+
_line_edit.text = str(raw_input) if raw_input else ""
6968

7069

7170
func get_raw_input():
72-
if option:
73-
var options: Array = []
74-
for i in _option_input.item_count:
75-
options.append(_option_input.get_item_text(i).to_snake_case())
76-
return OptionData.new(options, _option_input.selected)
71+
if option_data:
72+
return _option_input.get_selected_metadata()
7773

7874
match variant_type:
7975
TYPE_COLOR:
@@ -92,20 +88,32 @@ func get_raw_input():
9288
return _line_edit.text
9389

9490

91+
func _set_option_data(new_option_data: OptionData) -> void:
92+
option_data = new_option_data
93+
94+
if not is_node_ready():
95+
return
96+
97+
_update_option_input()
98+
99+
95100
func _set_placeholder(new_placeholder: String) -> void:
96101
placeholder = new_placeholder
97102

98103
if not is_node_ready():
99104
return
100105

101106
_line_edit.placeholder_text = placeholder
107+
_input_switcher.tooltip_text = placeholder
108+
_option_input.tooltip_text = placeholder
102109

103110

104111
func _ready():
105112
var stylebox = _panel.get_theme_stylebox("panel")
106113
stylebox.bg_color = Color.WHITE
107114

108115
_set_placeholder(placeholder)
116+
_set_option_data(option_data)
109117

110118
snap_point.block_type = block_type
111119
snap_point.variant_type = variant_type
@@ -132,8 +140,8 @@ func get_string() -> String:
132140

133141
var input = get_raw_input()
134142

135-
if option:
136-
return _option_input.get_item_text(_option_input.selected).to_snake_case()
143+
if option_data:
144+
return input
137145

138146
match variant_type:
139147
TYPE_STRING:
@@ -194,7 +202,7 @@ func _on_y_line_edit_focus_exited():
194202
func _update_visible_input():
195203
if snap_point.has_snapped_block():
196204
_switch_input(null)
197-
elif option:
205+
elif option_data:
198206
_switch_input(_option_input)
199207
else:
200208
match variant_type:
@@ -211,6 +219,29 @@ func _update_visible_input():
211219
func _switch_input(node: Node):
212220
for c in _input_switcher.get_children():
213221
c.visible = c == node
222+
_panel.visible = node not in [_option_input]
223+
224+
225+
func _update_option_input(current_value: Variant = null):
226+
if not option_data:
227+
return
228+
229+
if current_value == null:
230+
current_value = _option_input.get_selected_metadata()
231+
232+
_option_input.clear()
233+
234+
var selected_item_index: int = -1
235+
236+
for item in option_data.items:
237+
var item_index = _option_input.item_count
238+
var option_label = item.capitalize()
239+
_option_input.add_item(option_label)
240+
_option_input.set_item_metadata(item_index, item)
241+
if item == current_value:
242+
selected_item_index = item_index
243+
244+
_option_input.select(selected_item_index)
214245

215246

216247
func _on_color_input_color_changed(color):

addons/block_code/ui/blocks/utilities/parameter_input/parameter_input.tscn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ theme_override_styles/panel = SubResource("StyleBoxFlat_tn6h4")
4242
[node name="InputSwitcher" type="MarginContainer" parent="."]
4343
unique_name_in_owner = true
4444
layout_mode = 2
45+
tooltip_text = "Parameter"
4546
mouse_filter = 2
4647
theme_override_constants/margin_left = 0
4748
theme_override_constants/margin_top = 0

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,15 @@ func _append_input_parameter(parameter_format: String, id: int):
110110
parameter_input.name = "ParameterInput%d" % id
111111
parameter_input.placeholder = parameter["name"]
112112
parameter_input.variant_type = parameter["type"]
113-
parameter_input.option = parameter["is_option"]
114-
parameter_input.default_value = default_value
113+
114+
if parameter["is_option"] and default_value is OptionData:
115+
var option_data := default_value as OptionData
116+
parameter_input.option_data = option_data
117+
parameter_input.default_value = option_data.items[option_data.selected]
118+
elif parameter["is_option"]:
119+
push_warning("The block parameter %s in %s appears to be an option, but no option data is provided" % [parameter_format, parent_block])
120+
else:
121+
parameter_input.default_value = default_value
115122

116123
parameter_input.modified.connect(modified.emit)
117124

0 commit comments

Comments
 (0)