Skip to content

Commit e0052d8

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 a33019d commit e0052d8

File tree

3 files changed

+78
-14
lines changed

3 files changed

+78
-14
lines changed

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

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
@tool
22
extends MarginContainer
33

4+
const OptionData = preload("res://addons/block_code/code_generation/option_data.gd")
45
const Types = preload("res://addons/block_code/types/types.gd")
56

67
signal modified
@@ -10,8 +11,9 @@ signal modified
1011

1112
@export var variant_type: Variant.Type = TYPE_STRING
1213
@export var block_type: Types.BlockType = Types.BlockType.VALUE
13-
var option: bool = false:
14-
set = _set_option
14+
@export var option_data: OptionData:
15+
set = _set_option_data
16+
1517
var default_value: Variant
1618

1719
@onready var _panel := %Panel
@@ -51,6 +53,13 @@ func set_raw_input(raw_input: Variant):
5153
# Continue from here to reset the editor to default values
5254
raw_input = null
5355

56+
if option_data:
57+
_update_option_input(raw_input)
58+
return
59+
60+
if raw_input == null:
61+
raw_input = default_value
62+
5463
match variant_type:
5564
TYPE_COLOR:
5665
_color_input.color = raw_input
@@ -60,7 +69,7 @@ func set_raw_input(raw_input: Variant):
6069
_x_line_edit.text = ("%.4f" % raw_input.x).rstrip("0").rstrip(".") if raw_input != null else ""
6170
_y_line_edit.text = ("%.4f" % raw_input.y).rstrip("0").rstrip(".") if raw_input != null else ""
6271
TYPE_BOOL:
63-
_bool_input_option.select(raw_input)
72+
_bool_input_option.select(1 if raw_input else 0)
6473
TYPE_NIL:
6574
_line_edit.text = raw_input if raw_input != null else ""
6675
_:
@@ -76,6 +85,9 @@ func get_raw_input() -> Variant:
7685
if snapped_block:
7786
return snapped_block
7887

88+
if option_data:
89+
return _option_input.get_selected_metadata()
90+
7991
match variant_type:
8092
TYPE_COLOR:
8193
return _color_input.color
@@ -95,31 +107,35 @@ func get_raw_input() -> Variant:
95107
return _line_edit.text
96108

97109

98-
func _set_placeholder(new_placeholder: String) -> void:
99-
placeholder = new_placeholder
110+
func _set_option_data(new_option_data: OptionData) -> void:
111+
option_data = new_option_data
100112

101113
if not is_node_ready():
102114
return
103115

104-
_line_edit.placeholder_text = placeholder
116+
# If options are being provided, you can't snap blocks.
117+
snap_point.visible = not option_data
105118

119+
_update_option_input()
106120

107-
func _set_option(value: bool) -> void:
108-
option = value
121+
122+
func _set_placeholder(new_placeholder: String) -> void:
123+
placeholder = new_placeholder
109124

110125
if not is_node_ready():
111126
return
112127

113-
# If options are being provided, you can't snap blocks.
114-
snap_point.visible = not option
128+
_line_edit.placeholder_text = placeholder
129+
_input_switcher.tooltip_text = placeholder
130+
_option_input.tooltip_text = placeholder
115131

116132

117133
func _ready():
118134
var stylebox = _panel.get_theme_stylebox("panel")
119135
stylebox.bg_color = Color.WHITE
120136

121137
_set_placeholder(placeholder)
122-
_set_option(option)
138+
_set_option_data(option_data)
123139

124140
snap_point.block_type = block_type
125141
snap_point.variant_type = variant_type
@@ -178,7 +194,7 @@ func _on_y_line_edit_focus_exited():
178194
func _update_visible_input():
179195
if snap_point.has_snapped_block():
180196
_switch_input(null)
181-
elif option:
197+
elif option_data:
182198
_switch_input(_option_input)
183199
else:
184200
match variant_type:
@@ -195,6 +211,44 @@ func _update_visible_input():
195211
func _switch_input(node: Node):
196212
for c in _input_switcher.get_children():
197213
c.visible = c == node
214+
_panel.visible = node not in [_option_input]
215+
216+
217+
func _update_option_input(current_value: Variant = null):
218+
if not option_data:
219+
return
220+
221+
if current_value is OptionData:
222+
# Temporary hack: previously, the value was stored as an OptionValue
223+
# object with a list of items and a "selected" property. Instead,
224+
# convert that value to the corresponding item.
225+
current_value = current_value.items[current_value.selected]
226+
227+
if current_value == null:
228+
current_value = _option_input.get_selected_metadata()
229+
230+
_option_input.clear()
231+
232+
var selected_item_index: int = -1
233+
234+
for item in option_data.items:
235+
var item_index = _option_input.item_count
236+
var option_label = item.capitalize() if item is String else str(item)
237+
_option_input.add_item(option_label)
238+
_option_input.set_item_tooltip(item_index, item)
239+
_option_input.set_item_metadata(item_index, item)
240+
if item == current_value:
241+
selected_item_index = item_index
242+
243+
if _option_input.item_count == 0:
244+
var item_index = _option_input.item_count
245+
_option_input.add_item("<%s>" % placeholder)
246+
_option_input.set_item_disabled(item_index, true)
247+
selected_item_index = item_index
248+
elif selected_item_index == -1:
249+
selected_item_index = option_data.selected
250+
251+
_option_input.select(selected_item_index)
198252

199253

200254
func _on_color_input_color_changed(color):

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

Lines changed: 2 additions & 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
@@ -84,6 +85,7 @@ unique_name_in_owner = true
8485
visible = false
8586
custom_minimum_size = Vector2(40, 0)
8687
layout_mode = 2
88+
tooltip_text = "Parameter"
8789
theme_override_styles/normal = SubResource("StyleBoxEmpty_fjquj")
8890

8991
[node name="Vector2Input" type="MarginContainer" parent="InputSwitcher"]

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,16 @@ func _append_input_parameter(parameter: Dictionary, id: int):
112112
parameter_input.name = "ParameterInput%d" % id
113113
parameter_input.placeholder = parameter["name"]
114114
parameter_input.variant_type = parameter["type"]
115-
parameter_input.option = parameter["is_option"]
116-
parameter_input.default_value = default_value
115+
116+
if parameter["is_option"] and default_value is OptionData:
117+
var option_data := default_value as OptionData
118+
parameter_input.option_data = option_data
119+
if option_data.selected < option_data.items.size():
120+
parameter_input.default_value = option_data.items[option_data.selected]
121+
elif parameter["is_option"]:
122+
push_warning("The block parameter %s in %s appears to be an option, but no option data is provided" % [parameter_format, parent_block])
123+
else:
124+
parameter_input.default_value = default_value
117125

118126
parameter_input.modified.connect(modified.emit)
119127

0 commit comments

Comments
 (0)