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 _ :
@@ -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
121137func _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():
182198func _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():
199215func _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 OptionValue
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
204258func _on_color_input_color_changed (color ):
0 commit comments