1
1
@tool
2
2
extends MarginContainer
3
3
4
+ const OptionData = preload ("res://addons/block_code/code_generation/option_data.gd" )
4
5
const Types = preload ("res://addons/block_code/types/types.gd" )
5
6
6
7
signal modified
@@ -10,8 +11,9 @@ signal modified
10
11
11
12
@export var variant_type : Variant .Type = TYPE_STRING
12
13
@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
+
15
17
var default_value : Variant
16
18
17
19
@onready var _panel := % Panel
@@ -51,6 +53,13 @@ func set_raw_input(raw_input: Variant):
51
53
# Continue from here to reset the editor to default values
52
54
raw_input = null
53
55
56
+ if option_data :
57
+ _update_option_input (raw_input )
58
+ return
59
+
60
+ if raw_input == null :
61
+ raw_input = default_value
62
+
54
63
match variant_type :
55
64
TYPE_COLOR :
56
65
_color_input .color = raw_input
@@ -60,7 +69,7 @@ func set_raw_input(raw_input: Variant):
60
69
_x_line_edit .text = ("%.4f " % raw_input .x ).rstrip ("0" ).rstrip ("." ) if raw_input != null else ""
61
70
_y_line_edit .text = ("%.4f " % raw_input .y ).rstrip ("0" ).rstrip ("." ) if raw_input != null else ""
62
71
TYPE_BOOL :
63
- _bool_input_option .select (raw_input )
72
+ _bool_input_option .select (1 if raw_input else 0 )
64
73
TYPE_NIL :
65
74
_line_edit .text = raw_input if raw_input != null else ""
66
75
_ :
@@ -80,6 +89,9 @@ func get_raw_input() -> Variant:
80
89
if snapped_block :
81
90
return snapped_block
82
91
92
+ if option_data :
93
+ return _option_input .get_selected_metadata ()
94
+
83
95
match variant_type :
84
96
TYPE_COLOR :
85
97
return _color_input .color
@@ -99,31 +111,35 @@ func get_raw_input() -> Variant:
99
111
return _line_edit .text
100
112
101
113
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
104
116
105
117
if not is_node_ready ():
106
118
return
107
119
108
- _line_edit .placeholder_text = placeholder
120
+ # If options are being provided, you can't snap blocks.
121
+ snap_point .visible = not option_data
109
122
123
+ _update_option_input ()
110
124
111
- func _set_option (value : bool ) -> void :
112
- option = value
125
+
126
+ func _set_placeholder (new_placeholder : String ) -> void :
127
+ placeholder = new_placeholder
113
128
114
129
if not is_node_ready ():
115
130
return
116
131
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
119
135
120
136
121
137
func _ready ():
122
138
var stylebox = _panel .get_theme_stylebox ("panel" )
123
139
stylebox .bg_color = Color .WHITE
124
140
125
141
_set_placeholder (placeholder )
126
- _set_option ( option )
142
+ _set_option_data ( option_data )
127
143
128
144
snap_point .block_type = block_type
129
145
snap_point .variant_type = variant_type
@@ -182,7 +198,7 @@ func _on_y_line_edit_focus_exited():
182
198
func _update_visible_input ():
183
199
if snap_point .has_snapped_block ():
184
200
_switch_input (null )
185
- elif option :
201
+ elif option_data :
186
202
_switch_input (_option_input )
187
203
else :
188
204
match variant_type :
@@ -199,6 +215,44 @@ func _update_visible_input():
199
215
func _switch_input (node : Node ):
200
216
for c in _input_switcher .get_children ():
201
217
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 )
202
256
203
257
204
258
func _on_color_input_color_changed (color ):
0 commit comments