1 | /**************************************************************************/ |
2 | /* option_button.cpp */ |
3 | /**************************************************************************/ |
4 | /* This file is part of: */ |
5 | /* GODOT ENGINE */ |
6 | /* https://godotengine.org */ |
7 | /**************************************************************************/ |
8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
10 | /* */ |
11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
12 | /* a copy of this software and associated documentation files (the */ |
13 | /* "Software"), to deal in the Software without restriction, including */ |
14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
17 | /* the following conditions: */ |
18 | /* */ |
19 | /* The above copyright notice and this permission notice shall be */ |
20 | /* included in all copies or substantial portions of the Software. */ |
21 | /* */ |
22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
29 | /**************************************************************************/ |
30 | |
31 | #include "option_button.h" |
32 | |
33 | #include "core/os/keyboard.h" |
34 | #include "core/string/print_string.h" |
35 | #include "scene/theme/theme_db.h" |
36 | |
37 | static const int NONE_SELECTED = -1; |
38 | |
39 | void OptionButton::shortcut_input(const Ref<InputEvent> &p_event) { |
40 | ERR_FAIL_COND(p_event.is_null()); |
41 | |
42 | if (disable_shortcuts) { |
43 | return; |
44 | } |
45 | |
46 | if (p_event->is_pressed() && !p_event->is_echo() && !is_disabled() && is_visible_in_tree() && popup->activate_item_by_event(p_event, false)) { |
47 | accept_event(); |
48 | return; |
49 | } |
50 | |
51 | Button::shortcut_input(p_event); |
52 | } |
53 | |
54 | Size2 OptionButton::get_minimum_size() const { |
55 | Size2 minsize; |
56 | if (fit_to_longest_item) { |
57 | minsize = _cached_size; |
58 | } else { |
59 | minsize = Button::get_minimum_size(); |
60 | } |
61 | |
62 | if (has_theme_icon(SNAME("arrow" ))) { |
63 | const Size2 padding = theme_cache.normal->get_minimum_size(); |
64 | const Size2 arrow_size = theme_cache.arrow_icon->get_size(); |
65 | |
66 | Size2 content_size = minsize - padding; |
67 | content_size.width += arrow_size.width + MAX(0, theme_cache.h_separation); |
68 | content_size.height = MAX(content_size.height, arrow_size.height); |
69 | |
70 | minsize = content_size + padding; |
71 | } |
72 | |
73 | return minsize; |
74 | } |
75 | |
76 | void OptionButton::_notification(int p_what) { |
77 | switch (p_what) { |
78 | case NOTIFICATION_POSTINITIALIZE: { |
79 | _refresh_size_cache(); |
80 | if (has_theme_icon(SNAME("arrow" ))) { |
81 | if (is_layout_rtl()) { |
82 | _set_internal_margin(SIDE_LEFT, theme_cache.arrow_icon->get_width()); |
83 | } else { |
84 | _set_internal_margin(SIDE_RIGHT, theme_cache.arrow_icon->get_width()); |
85 | } |
86 | } |
87 | } break; |
88 | |
89 | case NOTIFICATION_DRAW: { |
90 | if (!has_theme_icon(SNAME("arrow" ))) { |
91 | return; |
92 | } |
93 | |
94 | RID ci = get_canvas_item(); |
95 | Color clr = Color(1, 1, 1); |
96 | if (theme_cache.modulate_arrow) { |
97 | switch (get_draw_mode()) { |
98 | case DRAW_PRESSED: |
99 | clr = theme_cache.font_pressed_color; |
100 | break; |
101 | case DRAW_HOVER: |
102 | clr = theme_cache.font_hover_color; |
103 | break; |
104 | case DRAW_HOVER_PRESSED: |
105 | clr = theme_cache.font_hover_pressed_color; |
106 | break; |
107 | case DRAW_DISABLED: |
108 | clr = theme_cache.font_disabled_color; |
109 | break; |
110 | default: |
111 | if (has_focus()) { |
112 | clr = theme_cache.font_focus_color; |
113 | } else { |
114 | clr = theme_cache.font_color; |
115 | } |
116 | } |
117 | } |
118 | |
119 | Size2 size = get_size(); |
120 | |
121 | Point2 ofs; |
122 | if (is_layout_rtl()) { |
123 | ofs = Point2(theme_cache.arrow_margin, int(Math::abs((size.height - theme_cache.arrow_icon->get_height()) / 2))); |
124 | } else { |
125 | ofs = Point2(size.width - theme_cache.arrow_icon->get_width() - theme_cache.arrow_margin, int(Math::abs((size.height - theme_cache.arrow_icon->get_height()) / 2))); |
126 | } |
127 | theme_cache.arrow_icon->draw(ci, ofs, clr); |
128 | } break; |
129 | |
130 | case NOTIFICATION_TRANSLATION_CHANGED: { |
131 | popup->set_auto_translate(is_auto_translating()); |
132 | [[fallthrough]]; |
133 | } |
134 | case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: { |
135 | popup->set_layout_direction((Window::LayoutDirection)get_layout_direction()); |
136 | [[fallthrough]]; |
137 | } |
138 | case NOTIFICATION_THEME_CHANGED: { |
139 | if (has_theme_icon(SNAME("arrow" ))) { |
140 | if (is_layout_rtl()) { |
141 | _set_internal_margin(SIDE_LEFT, theme_cache.arrow_icon->get_width()); |
142 | _set_internal_margin(SIDE_RIGHT, 0.f); |
143 | } else { |
144 | _set_internal_margin(SIDE_LEFT, 0.f); |
145 | _set_internal_margin(SIDE_RIGHT, theme_cache.arrow_icon->get_width()); |
146 | } |
147 | } |
148 | _refresh_size_cache(); |
149 | } break; |
150 | |
151 | case NOTIFICATION_VISIBILITY_CHANGED: { |
152 | if (!is_visible_in_tree()) { |
153 | popup->hide(); |
154 | } |
155 | } break; |
156 | } |
157 | } |
158 | |
159 | bool OptionButton::_set(const StringName &p_name, const Variant &p_value) { |
160 | Vector<String> components = String(p_name).split("/" , true, 2); |
161 | if (components.size() >= 2 && components[0] == "popup" ) { |
162 | String property = components[2]; |
163 | if (property != "text" && property != "icon" && property != "id" && property != "disabled" && property != "separator" ) { |
164 | return false; |
165 | } |
166 | |
167 | bool valid; |
168 | popup->set(String(p_name).trim_prefix("popup/" ), p_value, &valid); |
169 | |
170 | int idx = components[1].get_slice("_" , 1).to_int(); |
171 | if (idx == current) { |
172 | // Force refreshing currently displayed item. |
173 | current = NONE_SELECTED; |
174 | _select(idx, false); |
175 | } |
176 | |
177 | if (property == "text" || property == "icon" ) { |
178 | _queue_update_size_cache(); |
179 | } |
180 | |
181 | return valid; |
182 | } |
183 | return false; |
184 | } |
185 | |
186 | bool OptionButton::_get(const StringName &p_name, Variant &r_ret) const { |
187 | Vector<String> components = String(p_name).split("/" , true, 2); |
188 | if (components.size() >= 2 && components[0] == "popup" ) { |
189 | String property = components[2]; |
190 | if (property != "text" && property != "icon" && property != "id" && property != "disabled" && property != "separator" ) { |
191 | return false; |
192 | } |
193 | |
194 | bool valid; |
195 | r_ret = popup->get(String(p_name).trim_prefix("popup/" ), &valid); |
196 | return valid; |
197 | } |
198 | return false; |
199 | } |
200 | |
201 | void OptionButton::_get_property_list(List<PropertyInfo> *p_list) const { |
202 | for (int i = 0; i < popup->get_item_count(); i++) { |
203 | p_list->push_back(PropertyInfo(Variant::STRING, vformat("popup/item_%d/text" , i))); |
204 | |
205 | PropertyInfo pi = PropertyInfo(Variant::OBJECT, vformat("popup/item_%d/icon" , i), PROPERTY_HINT_RESOURCE_TYPE, "Texture2D" ); |
206 | pi.usage &= ~(popup->get_item_icon(i).is_null() ? PROPERTY_USAGE_STORAGE : 0); |
207 | p_list->push_back(pi); |
208 | |
209 | pi = PropertyInfo(Variant::INT, vformat("popup/item_%d/id" , i), PROPERTY_HINT_RANGE, "0,10,1,or_greater" ); |
210 | p_list->push_back(pi); |
211 | |
212 | pi = PropertyInfo(Variant::BOOL, vformat("popup/item_%d/disabled" , i)); |
213 | pi.usage &= ~(!popup->is_item_disabled(i) ? PROPERTY_USAGE_STORAGE : 0); |
214 | p_list->push_back(pi); |
215 | |
216 | pi = PropertyInfo(Variant::BOOL, vformat("popup/item_%d/separator" , i)); |
217 | pi.usage &= ~(!popup->is_item_separator(i) ? PROPERTY_USAGE_STORAGE : 0); |
218 | p_list->push_back(pi); |
219 | } |
220 | } |
221 | |
222 | void OptionButton::_focused(int p_which) { |
223 | emit_signal(SNAME("item_focused" ), p_which); |
224 | } |
225 | |
226 | void OptionButton::_selected(int p_which) { |
227 | _select(p_which, true); |
228 | } |
229 | |
230 | void OptionButton::pressed() { |
231 | if (popup->is_visible()) { |
232 | popup->hide(); |
233 | return; |
234 | } |
235 | |
236 | show_popup(); |
237 | } |
238 | |
239 | void OptionButton::add_icon_item(const Ref<Texture2D> &p_icon, const String &p_label, int p_id) { |
240 | bool first_selectable = !has_selectable_items(); |
241 | popup->add_icon_radio_check_item(p_icon, p_label, p_id); |
242 | if (first_selectable) { |
243 | select(get_item_count() - 1); |
244 | } |
245 | _queue_update_size_cache(); |
246 | } |
247 | |
248 | void OptionButton::add_item(const String &p_label, int p_id) { |
249 | bool first_selectable = !has_selectable_items(); |
250 | popup->add_radio_check_item(p_label, p_id); |
251 | if (first_selectable) { |
252 | select(get_item_count() - 1); |
253 | } |
254 | _queue_update_size_cache(); |
255 | } |
256 | |
257 | void OptionButton::set_item_text(int p_idx, const String &p_text) { |
258 | popup->set_item_text(p_idx, p_text); |
259 | |
260 | if (current == p_idx) { |
261 | set_text(p_text); |
262 | } |
263 | _queue_update_size_cache(); |
264 | } |
265 | |
266 | void OptionButton::set_item_icon(int p_idx, const Ref<Texture2D> &p_icon) { |
267 | popup->set_item_icon(p_idx, p_icon); |
268 | |
269 | if (current == p_idx) { |
270 | set_icon(p_icon); |
271 | } |
272 | _queue_update_size_cache(); |
273 | } |
274 | |
275 | void OptionButton::set_item_id(int p_idx, int p_id) { |
276 | popup->set_item_id(p_idx, p_id); |
277 | } |
278 | |
279 | void OptionButton::set_item_metadata(int p_idx, const Variant &p_metadata) { |
280 | popup->set_item_metadata(p_idx, p_metadata); |
281 | } |
282 | |
283 | void OptionButton::set_item_tooltip(int p_idx, const String &p_tooltip) { |
284 | popup->set_item_tooltip(p_idx, p_tooltip); |
285 | } |
286 | |
287 | void OptionButton::set_item_disabled(int p_idx, bool p_disabled) { |
288 | popup->set_item_disabled(p_idx, p_disabled); |
289 | } |
290 | |
291 | String OptionButton::get_item_text(int p_idx) const { |
292 | return popup->get_item_text(p_idx); |
293 | } |
294 | |
295 | Ref<Texture2D> OptionButton::get_item_icon(int p_idx) const { |
296 | return popup->get_item_icon(p_idx); |
297 | } |
298 | |
299 | int OptionButton::get_item_id(int p_idx) const { |
300 | if (p_idx == NONE_SELECTED) { |
301 | return NONE_SELECTED; |
302 | } |
303 | |
304 | return popup->get_item_id(p_idx); |
305 | } |
306 | |
307 | int OptionButton::get_item_index(int p_id) const { |
308 | return popup->get_item_index(p_id); |
309 | } |
310 | |
311 | Variant OptionButton::get_item_metadata(int p_idx) const { |
312 | return popup->get_item_metadata(p_idx); |
313 | } |
314 | |
315 | String OptionButton::get_item_tooltip(int p_idx) const { |
316 | return popup->get_item_tooltip(p_idx); |
317 | } |
318 | |
319 | bool OptionButton::is_item_disabled(int p_idx) const { |
320 | return popup->is_item_disabled(p_idx); |
321 | } |
322 | |
323 | bool OptionButton::is_item_separator(int p_idx) const { |
324 | return popup->is_item_separator(p_idx); |
325 | } |
326 | void OptionButton::set_item_count(int p_count) { |
327 | ERR_FAIL_COND(p_count < 0); |
328 | |
329 | int count_old = get_item_count(); |
330 | if (p_count == count_old) { |
331 | return; |
332 | } |
333 | |
334 | popup->set_item_count(p_count); |
335 | |
336 | if (p_count > count_old) { |
337 | for (int i = count_old; i < p_count; i++) { |
338 | popup->set_item_as_radio_checkable(i, true); |
339 | } |
340 | } |
341 | |
342 | _refresh_size_cache(); |
343 | notify_property_list_changed(); |
344 | } |
345 | |
346 | bool OptionButton::has_selectable_items() const { |
347 | for (int i = 0; i < get_item_count(); i++) { |
348 | if (!is_item_disabled(i) && !is_item_separator(i)) { |
349 | return true; |
350 | } |
351 | } |
352 | return false; |
353 | } |
354 | int OptionButton::get_selectable_item(bool p_from_last) const { |
355 | if (!p_from_last) { |
356 | for (int i = 0; i < get_item_count(); i++) { |
357 | if (!is_item_disabled(i) && !is_item_separator(i)) { |
358 | return i; |
359 | } |
360 | } |
361 | } else { |
362 | for (int i = get_item_count() - 1; i >= 0; i--) { |
363 | if (!is_item_disabled(i) && !is_item_separator(i)) { |
364 | return i; |
365 | } |
366 | } |
367 | } |
368 | return -1; |
369 | } |
370 | |
371 | int OptionButton::get_item_count() const { |
372 | return popup->get_item_count(); |
373 | } |
374 | |
375 | void OptionButton::set_fit_to_longest_item(bool p_fit) { |
376 | if (p_fit == fit_to_longest_item) { |
377 | return; |
378 | } |
379 | fit_to_longest_item = p_fit; |
380 | |
381 | _refresh_size_cache(); |
382 | } |
383 | |
384 | bool OptionButton::is_fit_to_longest_item() const { |
385 | return fit_to_longest_item; |
386 | } |
387 | |
388 | void OptionButton::set_allow_reselect(bool p_allow) { |
389 | allow_reselect = p_allow; |
390 | } |
391 | |
392 | bool OptionButton::get_allow_reselect() const { |
393 | return allow_reselect; |
394 | } |
395 | |
396 | void OptionButton::add_separator(const String &p_text) { |
397 | popup->add_separator(p_text); |
398 | } |
399 | |
400 | void OptionButton::clear() { |
401 | popup->clear(); |
402 | set_text("" ); |
403 | current = NONE_SELECTED; |
404 | _refresh_size_cache(); |
405 | } |
406 | |
407 | void OptionButton::_select(int p_which, bool p_emit) { |
408 | if (p_which == current && !allow_reselect) { |
409 | return; |
410 | } |
411 | |
412 | if (p_which == NONE_SELECTED) { |
413 | for (int i = 0; i < popup->get_item_count(); i++) { |
414 | popup->set_item_checked(i, false); |
415 | } |
416 | |
417 | current = NONE_SELECTED; |
418 | set_text("" ); |
419 | set_icon(nullptr); |
420 | } else { |
421 | ERR_FAIL_INDEX(p_which, popup->get_item_count()); |
422 | |
423 | for (int i = 0; i < popup->get_item_count(); i++) { |
424 | popup->set_item_checked(i, i == p_which); |
425 | } |
426 | |
427 | current = p_which; |
428 | set_text(popup->get_item_text(current)); |
429 | set_icon(popup->get_item_icon(current)); |
430 | } |
431 | |
432 | if (is_inside_tree() && p_emit) { |
433 | emit_signal(SNAME("item_selected" ), current); |
434 | } |
435 | } |
436 | |
437 | void OptionButton::_select_int(int p_which) { |
438 | if (p_which < NONE_SELECTED || p_which >= popup->get_item_count()) { |
439 | return; |
440 | } |
441 | _select(p_which, false); |
442 | } |
443 | |
444 | void OptionButton::_refresh_size_cache() { |
445 | cache_refresh_pending = false; |
446 | |
447 | if (fit_to_longest_item) { |
448 | _cached_size = theme_cache.normal->get_minimum_size(); |
449 | for (int i = 0; i < get_item_count(); i++) { |
450 | _cached_size = _cached_size.max(get_minimum_size_for_text_and_icon(popup->get_item_xl_text(i), get_item_icon(i))); |
451 | } |
452 | } |
453 | update_minimum_size(); |
454 | } |
455 | |
456 | void OptionButton::_queue_update_size_cache() { |
457 | if (cache_refresh_pending) { |
458 | return; |
459 | } |
460 | cache_refresh_pending = true; |
461 | |
462 | callable_mp(this, &OptionButton::_refresh_size_cache).call_deferred(); |
463 | } |
464 | |
465 | void OptionButton::select(int p_idx) { |
466 | _select(p_idx, false); |
467 | } |
468 | |
469 | int OptionButton::get_selected() const { |
470 | return current; |
471 | } |
472 | |
473 | int OptionButton::get_selected_id() const { |
474 | return get_item_id(current); |
475 | } |
476 | |
477 | Variant OptionButton::get_selected_metadata() const { |
478 | int idx = get_selected(); |
479 | if (idx < 0) { |
480 | return Variant(); |
481 | } |
482 | return get_item_metadata(current); |
483 | } |
484 | |
485 | void OptionButton::remove_item(int p_idx) { |
486 | popup->remove_item(p_idx); |
487 | if (current == p_idx) { |
488 | _select(NONE_SELECTED); |
489 | } |
490 | _queue_update_size_cache(); |
491 | } |
492 | |
493 | PopupMenu *OptionButton::() const { |
494 | return popup; |
495 | } |
496 | |
497 | void OptionButton::() { |
498 | if (!get_viewport()) { |
499 | return; |
500 | } |
501 | |
502 | Rect2 rect = get_screen_rect(); |
503 | rect.position.y += rect.size.height; |
504 | rect.size.height = 0; |
505 | popup->set_position(rect.position); |
506 | popup->set_size(rect.size); |
507 | |
508 | // If not triggered by the mouse, start the popup with the checked item (or the first enabled one) focused. |
509 | if (current != NONE_SELECTED && !popup->is_item_disabled(current)) { |
510 | if (!_was_pressed_by_mouse()) { |
511 | popup->set_focused_item(current); |
512 | } else { |
513 | popup->scroll_to_item(current); |
514 | } |
515 | } else { |
516 | for (int i = 0; i < popup->get_item_count(); i++) { |
517 | if (!popup->is_item_disabled(i)) { |
518 | if (!_was_pressed_by_mouse()) { |
519 | popup->set_focused_item(i); |
520 | } else { |
521 | popup->scroll_to_item(i); |
522 | } |
523 | |
524 | break; |
525 | } |
526 | } |
527 | } |
528 | |
529 | popup->popup(); |
530 | } |
531 | |
532 | void OptionButton::_validate_property(PropertyInfo &p_property) const { |
533 | if (p_property.name == "text" || p_property.name == "icon" ) { |
534 | p_property.usage = PROPERTY_USAGE_NONE; |
535 | } |
536 | } |
537 | |
538 | void OptionButton::_bind_methods() { |
539 | ClassDB::bind_method(D_METHOD("add_item" , "label" , "id" ), &OptionButton::add_item, DEFVAL(-1)); |
540 | ClassDB::bind_method(D_METHOD("add_icon_item" , "texture" , "label" , "id" ), &OptionButton::add_icon_item, DEFVAL(-1)); |
541 | ClassDB::bind_method(D_METHOD("set_item_text" , "idx" , "text" ), &OptionButton::set_item_text); |
542 | ClassDB::bind_method(D_METHOD("set_item_icon" , "idx" , "texture" ), &OptionButton::set_item_icon); |
543 | ClassDB::bind_method(D_METHOD("set_item_disabled" , "idx" , "disabled" ), &OptionButton::set_item_disabled); |
544 | ClassDB::bind_method(D_METHOD("set_item_id" , "idx" , "id" ), &OptionButton::set_item_id); |
545 | ClassDB::bind_method(D_METHOD("set_item_metadata" , "idx" , "metadata" ), &OptionButton::set_item_metadata); |
546 | ClassDB::bind_method(D_METHOD("set_item_tooltip" , "idx" , "tooltip" ), &OptionButton::set_item_tooltip); |
547 | ClassDB::bind_method(D_METHOD("get_item_text" , "idx" ), &OptionButton::get_item_text); |
548 | ClassDB::bind_method(D_METHOD("get_item_icon" , "idx" ), &OptionButton::get_item_icon); |
549 | ClassDB::bind_method(D_METHOD("get_item_id" , "idx" ), &OptionButton::get_item_id); |
550 | ClassDB::bind_method(D_METHOD("get_item_index" , "id" ), &OptionButton::get_item_index); |
551 | ClassDB::bind_method(D_METHOD("get_item_metadata" , "idx" ), &OptionButton::get_item_metadata); |
552 | ClassDB::bind_method(D_METHOD("get_item_tooltip" , "idx" ), &OptionButton::get_item_tooltip); |
553 | ClassDB::bind_method(D_METHOD("is_item_disabled" , "idx" ), &OptionButton::is_item_disabled); |
554 | ClassDB::bind_method(D_METHOD("is_item_separator" , "idx" ), &OptionButton::is_item_separator); |
555 | ClassDB::bind_method(D_METHOD("add_separator" , "text" ), &OptionButton::add_separator, DEFVAL(String())); |
556 | ClassDB::bind_method(D_METHOD("clear" ), &OptionButton::clear); |
557 | ClassDB::bind_method(D_METHOD("select" , "idx" ), &OptionButton::select); |
558 | ClassDB::bind_method(D_METHOD("get_selected" ), &OptionButton::get_selected); |
559 | ClassDB::bind_method(D_METHOD("get_selected_id" ), &OptionButton::get_selected_id); |
560 | ClassDB::bind_method(D_METHOD("get_selected_metadata" ), &OptionButton::get_selected_metadata); |
561 | ClassDB::bind_method(D_METHOD("remove_item" , "idx" ), &OptionButton::remove_item); |
562 | ClassDB::bind_method(D_METHOD("_select_int" , "idx" ), &OptionButton::_select_int); |
563 | |
564 | ClassDB::bind_method(D_METHOD("get_popup" ), &OptionButton::get_popup); |
565 | ClassDB::bind_method(D_METHOD("show_popup" ), &OptionButton::show_popup); |
566 | |
567 | ClassDB::bind_method(D_METHOD("set_item_count" , "count" ), &OptionButton::set_item_count); |
568 | ClassDB::bind_method(D_METHOD("get_item_count" ), &OptionButton::get_item_count); |
569 | ClassDB::bind_method(D_METHOD("has_selectable_items" ), &OptionButton::has_selectable_items); |
570 | ClassDB::bind_method(D_METHOD("get_selectable_item" , "from_last" ), &OptionButton::get_selectable_item, DEFVAL(false)); |
571 | ClassDB::bind_method(D_METHOD("set_fit_to_longest_item" , "fit" ), &OptionButton::set_fit_to_longest_item); |
572 | ClassDB::bind_method(D_METHOD("is_fit_to_longest_item" ), &OptionButton::is_fit_to_longest_item); |
573 | ClassDB::bind_method(D_METHOD("set_allow_reselect" , "allow" ), &OptionButton::set_allow_reselect); |
574 | ClassDB::bind_method(D_METHOD("get_allow_reselect" ), &OptionButton::get_allow_reselect); |
575 | ClassDB::bind_method(D_METHOD("set_disable_shortcuts" , "disabled" ), &OptionButton::set_disable_shortcuts); |
576 | |
577 | // "selected" property must come after "item_count", otherwise GH-10213 occurs. |
578 | ADD_ARRAY_COUNT("Items" , "item_count" , "set_item_count" , "get_item_count" , "popup/item_" ); |
579 | ADD_PROPERTY(PropertyInfo(Variant::INT, "selected" ), "_select_int" , "get_selected" ); |
580 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fit_to_longest_item" ), "set_fit_to_longest_item" , "is_fit_to_longest_item" ); |
581 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_reselect" ), "set_allow_reselect" , "get_allow_reselect" ); |
582 | |
583 | ADD_SIGNAL(MethodInfo("item_selected" , PropertyInfo(Variant::INT, "index" ))); |
584 | ADD_SIGNAL(MethodInfo("item_focused" , PropertyInfo(Variant::INT, "index" ))); |
585 | |
586 | BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, OptionButton, normal); |
587 | |
588 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, OptionButton, font_color); |
589 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, OptionButton, font_focus_color); |
590 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, OptionButton, font_pressed_color); |
591 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, OptionButton, font_hover_color); |
592 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, OptionButton, font_hover_pressed_color); |
593 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, OptionButton, font_disabled_color); |
594 | |
595 | BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, OptionButton, h_separation); |
596 | |
597 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, OptionButton, arrow_icon, "arrow" ); |
598 | BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, OptionButton, arrow_margin); |
599 | BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, OptionButton, modulate_arrow); |
600 | } |
601 | |
602 | void OptionButton::set_disable_shortcuts(bool p_disabled) { |
603 | disable_shortcuts = p_disabled; |
604 | } |
605 | |
606 | OptionButton::OptionButton(const String &p_text) : |
607 | Button(p_text) { |
608 | set_toggle_mode(true); |
609 | set_process_shortcut_input(true); |
610 | set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT); |
611 | set_action_mode(ACTION_MODE_BUTTON_PRESS); |
612 | |
613 | popup = memnew(PopupMenu); |
614 | popup->hide(); |
615 | add_child(popup, false, INTERNAL_MODE_FRONT); |
616 | popup->connect("index_pressed" , callable_mp(this, &OptionButton::_selected)); |
617 | popup->connect("id_focused" , callable_mp(this, &OptionButton::_focused)); |
618 | popup->connect("popup_hide" , callable_mp((BaseButton *)this, &BaseButton::set_pressed).bind(false)); |
619 | } |
620 | |
621 | OptionButton::~OptionButton() { |
622 | } |
623 | |