11@tool
22extends MarginContainer
33
4+ const OptionData = preload ("res://addons/block_code/code_generation/option_data.gd" )
45const Types = preload ("res://addons/block_code/types/types.gd" )
56
67signal 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+
1517var 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
117133func _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():
178194func _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():
195211func _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
200254func _on_color_input_color_changed (color ):
0 commit comments