8000 ParameterInput: Add a separate option data property · endlessm/godot-block-coding@1d8821c · GitHub
[go: up one dir, main page]

Skip to content

Commit 1d8821c

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 81b6456 commit 1d8821c

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
_:
@@ -80,6 +89,9 @@ func get_raw_input() -> Variant:
8089
if snapped_block:
8190
return snapped_block
8291

92+
if option_data:
93+
return _option_input.get_selected_metadata()
94+
8395
match variant_type:
8496
TYPE_COLOR:
8597
return _color_input.color
@@ -99,31 +111,35 @@ func get_raw_input() -> Variant:
99111
return _line_edit.text
100112

101113

102-
func _set_placeholder(new_placeholder: String) -> void:
103-
placeholder = new_placeholder
114+
func _set_option_data(new_option_data: OptionData) -> void:
115+
option_data = new_option_data
104116

105117
if not is_node_ready():
106118
return
107119

108-
_line_edit.placeholder_text = placeholder
120+
# If options are being provided, you can't snap blocks.
121+
snap_point.visible = not option_data
109122

123+
_update_option_input()
110124

111-
func _set_option(value: bool) -> void:
112-
option = value
125+
126+
func _set_placeholder(new_placeholder: String) -> void:
127+
placeholder = new_placeholder
113128

114129
if not is_node_ready():
115130
return
116131

117-
# If options are being provided, you can't snap blocks.
118-
snap_point.visible = not option
132+
_line_edit.placeholder_text = placeholder
133+
_input_switcher.tooltip_text = placeholder
134+
_option_input.tooltip_text = placeholder
119135

120136

121137
func _ready():
122138
var stylebox = _panel.get_theme_stylebox("panel")
123139
stylebox.bg_color = Color.WHITE
124140

125141
_set_placeholder(placeholder)
126-
_set_option(option)
142+
_set_option_data(option_data)
127143

128144
snap_point.block_type = block_type
129145
snap_point.variant_type = variant_type
@@ -182,7 +198,7 @@ func _on_y_line_edit_focus_exited():
182198
func _update_visible_input():
183199
if snap_point.has_snapped_block():
184200
_switch_input(null)
185-
elif option:
201+
elif option_data:
186202
_switch_input(_option_input)
187203
else:
188204
match variant_type:
@@ -199,6 +215,44 @@ func _update_visible_input():
199215
func _switch_input(node: Node):
200216
for c in _input_switcher.get_children():
201217
c.visible = c == node
218+
_panel.visible = node not in [_option_input]
219+
220+
221+
func _update_option_input(current_value: Variant = null):
222+
if not option_data:
223+
return
224+
225+
if current_value is OptionData:
226+
# Temporary hack: previously, the value was stored as an OptionData
227+
# object with a list of items and a "selected" property. Instead,
228+
# convert that value to the corresponding item.
229+
current_value = current_value.items[current_value.selected]
230+
231+
if current_value == null:
232+
current_value = _option_input.get_selected_metadata()
233+
234+
_option_input.clear()
235+
236+
var selected_item_index: int = -1
237+
238+
for item in option_data.items:
239+
var item_index = _option_input.item_count
240+
var option_label = item.capitalize() if item is String else str(item)
241+
_option_input.add_item(option_label)
242+
_option_input.set_item_tooltip(item_index, item)
243+
_option_input.set_item_metadata(item_index, item)
244+
if item == current_value:
245+
selected_item_index = item_index
246+
247+
if _option_input.item_count == 0:
248+
var item_index = _option_input.item_count
249+
_option_input.add_item("<%s>" % placeholder)
250+
_option_input.set_item_disabled(item_index, true)
251+
selected_item_index = item_index
252+
elif selected_item_index == -1:
253+
selected_item_index = option_data.selected
254+
255+
_option_input.select(selected_item_index)
202256

203257

204258
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
@@ -113,8 +113,16 @@ func _append_input_parameter(parameter: Dictionary, id: int):
113113
parameter_input.name = "ParameterInput%d" % id
114114
parameter_input.placeholder = parameter["name"]
115115
parameter_input.variant_type = parameter["type"]
116-
parameter_input.option = parameter["is_option"]
117-
parameter_input.default_value = default_value
116+
117+
if parameter["is_option"] and default_value is OptionData:
118+
var option_data := default_value as OptionData
119+
parameter_input.option_data = option_data
120+
if option_data.selected < option_data.items.size():
121+
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])
124+
else:
125+
parameter_input.default_value = default_value
118126

119127
parameter_input.modified.connect(func(): modified.emit())
120128

0 commit comments

Comments
 (0)
0