Debian Codegraph @

scene/gui/option_button.cpp

raw symbols SCIP
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 "scene/theme/theme_db.h"
34
35 static const int NONE_SELECTED = -1;
36
37 void OptionButton::shortcut_input(const Ref<InputEvent> &p_event) {
38 ERR_FAIL_COND(p_event.is_null());
39
40 if (disable_shortcuts) {
41 return;
42 }
43
44 if (p_event->is_pressed() && !p_event->is_echo() && !is_disabled() && is_visible_in_tree() && popup->activate_item_by_event(p_event, false)) {
45 accept_event();
46 return;
47 }
48
49 Button::shortcut_input(p_event);
50 }
51
52 Size2 OptionButton::get_minimum_size() const {
53 Size2 minsize;
54 if (fit_to_longest_item) {
55 minsize = _cached_size;
56 } else {
57 minsize = Button::get_minimum_size();
58 }
59
60 if (has_theme_icon(SNAME("arrow"))) {
61 const Size2 padding = _get_largest_stylebox_size();
62 const Size2 arrow_size = theme_cache.arrow_icon->get_size();
63
64 Size2 content_size = minsize - padding;
65 content_size.width += arrow_size.width + MAX(0, theme_cache.h_separation);
66 content_size.height = MAX(content_size.height, arrow_size.height);
67
68 minsize = content_size + padding;
69 }
70
71 return minsize;
72 }
73
74 void OptionButton::_notification(int p_what) {
75 switch (p_what) {
76 case NOTIFICATION_ACCESSIBILITY_UPDATE: {
77 RID ae = get_accessibility_element();
78 ERR_FAIL_COND(ae.is_null());
79
80 DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_BUTTON);
81 DisplayServer::get_singleton()->accessibility_update_set_popup_type(ae, DisplayServer::AccessibilityPopupType::POPUP_LIST);
82 } break;
83
84 case NOTIFICATION_POSTINITIALIZE: {
85 _refresh_size_cache();
86 if (has_theme_icon(SNAME("arrow"))) {
87 if (is_layout_rtl()) {
88 _set_internal_margin(SIDE_LEFT, theme_cache.arrow_icon->get_width());
89 } else {
90 _set_internal_margin(SIDE_RIGHT, theme_cache.arrow_icon->get_width());
91 }
92 }
93 } break;
94
95 case NOTIFICATION_DRAW: {
96 if (!has_theme_icon(SNAME("arrow"))) {
97 return;
98 }
99
100 RID ci = get_canvas_item();
101 Color clr = Color(1, 1, 1);
102 if (theme_cache.modulate_arrow) {
103 switch (get_draw_mode()) {
104 case DRAW_PRESSED:
105 clr = theme_cache.font_pressed_color;
106 break;
107 case DRAW_HOVER:
108 clr = theme_cache.font_hover_color;
109 break;
110 case DRAW_HOVER_PRESSED:
111 clr = theme_cache.font_hover_pressed_color;
112 break;
113 case DRAW_DISABLED:
114 clr = theme_cache.font_disabled_color;
115 break;
116 default:
117 if (has_focus(true)) {
118 clr = theme_cache.font_focus_color;
119 } else {
120 clr = theme_cache.font_color;
121 }
122 }
123 }
124
125 Size2 size = get_size();
126
127 Point2 ofs;
128 if (is_layout_rtl()) {
129 ofs = Point2(theme_cache.arrow_margin, int(Math::abs((size.height - theme_cache.arrow_icon->get_height()) / 2)));
130 } else {
131 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)));
132 }
133 theme_cache.arrow_icon->draw(ci, ofs, clr);
134 } break;
135
136 case NOTIFICATION_TRANSLATION_CHANGED:
137 case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
138 popup->set_layout_direction((Window::LayoutDirection)get_layout_direction());
139 [[fallthrough]];
140 }
141 case NOTIFICATION_THEME_CHANGED: {
142 if (has_theme_icon(SNAME("arrow"))) {
143 if (is_layout_rtl()) {
144 _set_internal_margin(SIDE_LEFT, theme_cache.arrow_icon->get_width());
145 _set_internal_margin(SIDE_RIGHT, 0.f);
146 } else {
147 _set_internal_margin(SIDE_LEFT, 0.f);
148 _set_internal_margin(SIDE_RIGHT, theme_cache.arrow_icon->get_width());
149 }
150 }
151 _refresh_size_cache();
152 } break;
153
154 case NOTIFICATION_VISIBILITY_CHANGED: {
155 if (!is_visible_in_tree()) {
156 popup->hide();
157 }
158 } break;
159 }
160 }
161
162 bool OptionButton::_set(const StringName &p_name, const Variant &p_value) {
163 int index;
164 const String sname = p_name;
165
166 if (property_helper.is_property_valid(sname, &index)) {
167 bool valid;
168 popup->set(sname.trim_prefix("popup/"), p_value, &valid);
169
170 if (index == current) {
171 // Force refreshing currently displayed item.
172 current = NONE_SELECTED;
173 _select(index, false);
174 }
175
176 const String property = sname.get_slicec('/', 2);
177 if (property == "text" || property == "icon") {
178 _queue_update_size_cache();
179 }
180
181 return valid;
182 }
183 return false;
184 }
185
186 void OptionButton::_focused(int p_which) {
187 emit_signal(SNAME("item_focused"), popup->get_item_index(p_which));
188 }
189
190 void OptionButton::_selected(int p_which) {
191 _select(p_which, true);
192 }
193
194 void OptionButton::pressed() {
195 if (popup->is_visible()) {
196 popup->hide();
197 return;
198 }
199
200 show_popup();
201 }
202
203 void OptionButton::add_icon_item(const Ref<Texture2D> &p_icon, const String &p_label, int p_id) {
204 bool first_selectable = !has_selectable_items();
205 popup->add_icon_radio_check_item(p_icon, p_label, p_id);
206 if (first_selectable) {
207 select(get_item_count() - 1);
208 }
209 _queue_update_size_cache();
210 }
211
212 void OptionButton::add_item(const String &p_label, int p_id) {
213 bool first_selectable = !has_selectable_items();
214 popup->add_radio_check_item(p_label, p_id);
215 if (first_selectable) {
216 select(get_item_count() - 1);
217 }
218 _queue_update_size_cache();
219 }
220
221 void OptionButton::set_item_text(int p_idx, const String &p_text) {
222 popup->set_item_text(p_idx, p_text);
223
224 if (current == p_idx) {
225 set_text(p_text);
226 }
227 _queue_update_size_cache();
228 }
229
230 void OptionButton::set_item_icon(int p_idx, const Ref<Texture2D> &p_icon) {
231 popup->set_item_icon(p_idx, p_icon);
232
233 if (current == p_idx) {
234 set_button_icon(p_icon);
235 }
236 _queue_update_size_cache();
237 }
238
239 void OptionButton::set_item_id(int p_idx, int p_id) {
240 popup->set_item_id(p_idx, p_id);
241 }
242
243 void OptionButton::set_item_metadata(int p_idx, const Variant &p_metadata) {
244 popup->set_item_metadata(p_idx, p_metadata);
245 }
246
247 void OptionButton::set_item_tooltip(int p_idx, const String &p_tooltip) {
248 popup->set_item_tooltip(p_idx, p_tooltip);
249 }
250
251 void OptionButton::set_item_auto_translate_mode(int p_idx, AutoTranslateMode p_mode) {
252 if (p_idx < 0) {
253 p_idx += get_item_count();
254 }
255 if (popup->get_item_auto_translate_mode(p_idx) == p_mode) {
256 return;
257 }
258 popup->set_item_auto_translate_mode(p_idx, p_mode);
259
260 if (current == p_idx) {
261 set_text(popup->get_item_text(p_idx));
262 }
263 _queue_update_size_cache();
264 }
265
266 void OptionButton::set_item_disabled(int p_idx, bool p_disabled) {
267 popup->set_item_disabled(p_idx, p_disabled);
268 }
269
270 String OptionButton::get_item_text(int p_idx) const {
271 return popup->get_item_text(p_idx);
272 }
273
274 Ref<Texture2D> OptionButton::get_item_icon(int p_idx) const {
275 return popup->get_item_icon(p_idx);
276 }
277
278 int OptionButton::get_item_id(int p_idx) const {
279 if (p_idx == NONE_SELECTED) {
280 return NONE_SELECTED;
281 }
282
283 return popup->get_item_id(p_idx);
284 }
285
286 int OptionButton::get_item_index(int p_id) const {
287 return popup->get_item_index(p_id);
288 }
289
290 Variant OptionButton::get_item_metadata(int p_idx) const {
291 return popup->get_item_metadata(p_idx);
292 }
293
294 String OptionButton::get_item_tooltip(int p_idx) const {
295 return popup->get_item_tooltip(p_idx);
296 }
297
298 Node::AutoTranslateMode OptionButton::get_item_auto_translate_mode(int p_idx) const {
299 return popup->get_item_auto_translate_mode(p_idx);
300 }
301
302 bool OptionButton::is_item_disabled(int p_idx) const {
303 return popup->is_item_disabled(p_idx);
304 }
305
306 bool OptionButton::is_item_separator(int p_idx) const {
307 return popup->is_item_separator(p_idx);
308 }
309 void OptionButton::set_item_count(int p_count) {
310 ERR_FAIL_COND(p_count < 0);
311
312 int count_old = get_item_count();
313 if (p_count == count_old) {
314 return;
315 }
316
317 if (current > p_count - 1) {
318 _select(p_count - 1, false);
319 }
320
321 popup->set_item_count(p_count);
322
323 if (p_count > count_old) {
324 for (int i = count_old; i < p_count; i++) {
325 popup->set_item_as_radio_checkable(i, true);
326 }
327 }
328
329 if (!initialized) {
330 if (queued_current != current) {
331 current = queued_current;
332 }
333 initialized = true;
334 }
335
336 _refresh_size_cache();
337 notify_property_list_changed();
338 }
339
340 bool OptionButton::has_selectable_items() const {
341 for (int i = 0; i < get_item_count(); i++) {
342 if (!is_item_disabled(i) && !is_item_separator(i)) {
343 return true;
344 }
345 }
346 return false;
347 }
348 int OptionButton::get_selectable_item(bool p_from_last) const {
349 if (!p_from_last) {
350 for (int i = 0; i < get_item_count(); i++) {
351 if (!is_item_disabled(i) && !is_item_separator(i)) {
352 return i;
353 }
354 }
355 } else {
356 for (int i = get_item_count() - 1; i >= 0; i--) {
357 if (!is_item_disabled(i) && !is_item_separator(i)) {
358 return i;
359 }
360 }
361 }
362 return -1;
363 }
364
365 int OptionButton::get_item_count() const {
366 return popup->get_item_count();
367 }
368
369 void OptionButton::set_fit_to_longest_item(bool p_fit) {
370 if (p_fit == fit_to_longest_item) {
371 return;
372 }
373 fit_to_longest_item = p_fit;
374
375 _refresh_size_cache();
376 }
377
378 bool OptionButton::is_fit_to_longest_item() const {
379 return fit_to_longest_item;
380 }
381
382 void OptionButton::set_allow_reselect(bool p_allow) {
383 allow_reselect = p_allow;
384 }
385
386 bool OptionButton::get_allow_reselect() const {
387 return allow_reselect;
388 }
389
390 void OptionButton::add_separator(const String &p_text) {
391 popup->add_separator(p_text);
392 }
393
394 void OptionButton::clear() {
395 popup->clear();
396 set_text("");
397 set_button_icon(Ref<Texture2D>());
398 current = NONE_SELECTED;
399 _refresh_size_cache();
400 }
401
402 void OptionButton::_select(int p_which, bool p_emit) {
403 if (p_which == current && !allow_reselect) {
404 return;
405 }
406
407 if (p_which == NONE_SELECTED) {
408 for (int i = 0; i < popup->get_item_count(); i++) {
409 popup->set_item_checked(i, false);
410 }
411
412 current = NONE_SELECTED;
413 set_text("");
414 set_button_icon(Ref<Texture2D>());
415 } else {
416 ERR_FAIL_INDEX(p_which, popup->get_item_count());
417
418 for (int i = 0; i < popup->get_item_count(); i++) {
419 popup->set_item_checked(i, i == p_which);
420 }
421
422 current = p_which;
423 set_text(popup->get_item_text(current));
424 set_button_icon(popup->get_item_icon(current));
425 }
426
427 if (is_inside_tree() && p_emit) {
428 emit_signal(SceneStringName(item_selected), current);
429 }
430 }
431
432 void OptionButton::_select_int(int p_which) {
433 if (p_which < NONE_SELECTED) {
434 return;
435 }
436 if (p_which >= popup->get_item_count()) {
437 if (!initialized) {
438 queued_current = p_which;
439 }
440 return;
441 }
442 _select(p_which, false);
443 }
444
445 void OptionButton::_refresh_size_cache() {
446 cache_refresh_pending = false;
447
448 if (fit_to_longest_item) {
449 _cached_size = theme_cache.normal->get_minimum_size();
450 for (int i = 0; i < get_item_count(); i++) {
451 _cached_size = _cached_size.max(get_minimum_size_for_text_and_icon(popup->get_item_xl_text(i), get_item_icon(i)));
452 }
453 }
454 update_minimum_size();
455 }
456
457 void OptionButton::_queue_update_size_cache() {
458 if (cache_refresh_pending) {
459 return;
460 }
461 cache_refresh_pending = true;
462
463 callable_mp(this, &OptionButton::_refresh_size_cache).call_deferred();
464 }
465
466 String OptionButton::_get_translated_text(const String &p_text) const {
467 if (0 <= current && current < popup->get_item_count()) {
468 AutoTranslateMode mode = popup->get_item_auto_translate_mode(current);
469 switch (mode) {
470 case AUTO_TRANSLATE_MODE_INHERIT: {
471 return atr(p_text);
472 } break;
473 case AUTO_TRANSLATE_MODE_ALWAYS: {
474 return tr(p_text);
475 } break;
476 case AUTO_TRANSLATE_MODE_DISABLED: {
477 return p_text;
478 } break;
479 }
480 ERR_FAIL_V_MSG(atr(p_text), "Unexpected auto translate mode: " + itos(mode));
481 }
482 return atr(p_text);
483 }
484
485 void OptionButton::select(int p_idx) {
486 _select(p_idx, false);
487 }
488
489 int OptionButton::get_selected() const {
490 return current;
491 }
492
493 int OptionButton::get_selected_id() const {
494 return get_item_id(current);
495 }
496
497 Variant OptionButton::get_selected_metadata() const {
498 int idx = get_selected();
499 if (idx < 0) {
500 return Variant();
501 }
502 return get_item_metadata(current);
503 }
504
505 void OptionButton::remove_item(int p_idx) {
506 popup->remove_item(p_idx);
507 if (current == p_idx) {
508 _select(NONE_SELECTED);
509 }
510 _queue_update_size_cache();
511 }
512
513 PopupMenu *OptionButton::get_popup() const {
514 return popup;
515 }
516
517 void OptionButton::show_popup() {
518 if (!get_viewport()) {
519 return;
520 }
521
522 // If not triggered by the mouse, start the popup with the checked item (or the first enabled one) focused.
523 if (current != NONE_SELECTED && !popup->is_item_disabled(current)) {
524 if (!_was_pressed_by_mouse()) {
525 popup->set_focused_item(current);
526 } else {
527 popup->scroll_to_item(current);
528 }
529 } else {
530 for (int i = 0; i < popup->get_item_count(); i++) {
531 if (!popup->is_item_disabled(i)) {
532 if (!_was_pressed_by_mouse()) {
533 popup->set_focused_item(i);
534 } else {
535 popup->scroll_to_item(i);
536 }
537
538 break;
539 }
540 }
541 }
542
543 Rect2 rect = get_screen_rect();
544 rect.position.y += rect.size.height;
545 if (get_viewport()->is_embedding_subwindows() && popup->get_force_native()) {
546 Transform2D xform = get_viewport()->get_popup_base_transform_native();
547 rect = xform.xform(rect);
548 }
549 rect.size.height = 0;
550 popup->set_min_size(Size2(0, 0));
551 popup->popup(rect);
552 }
553
554 void OptionButton::_validate_property(PropertyInfo &p_property) const {
555 if (p_property.name == "text" || p_property.name == "icon") {
556 p_property.usage = PROPERTY_USAGE_NONE;
557 }
558 }
559
560 void OptionButton::_bind_methods() {
561 ClassDB::bind_method(D_METHOD("add_item", "label", "id"), &OptionButton::add_item, DEFVAL(-1));
562 ClassDB::bind_method(D_METHOD("add_icon_item", "texture", "label", "id"), &OptionButton::add_icon_item, DEFVAL(-1));
563 ClassDB::bind_method(D_METHOD("set_item_text", "idx", "text"), &OptionButton::set_item_text);
564 ClassDB::bind_method(D_METHOD("set_item_icon", "idx", "texture"), &OptionButton::set_item_icon);
565 ClassDB::bind_method(D_METHOD("set_item_disabled", "idx", "disabled"), &OptionButton::set_item_disabled);
566 ClassDB::bind_method(D_METHOD("set_item_id", "idx", "id"), &OptionButton::set_item_id);
567 ClassDB::bind_method(D_METHOD("set_item_metadata", "idx", "metadata"), &OptionButton::set_item_metadata);
568 ClassDB::bind_method(D_METHOD("set_item_tooltip", "idx", "tooltip"), &OptionButton::set_item_tooltip);
569 ClassDB::bind_method(D_METHOD("set_item_auto_translate_mode", "idx", "mode"), &OptionButton::set_item_auto_translate_mode);
570 ClassDB::bind_method(D_METHOD("get_item_text", "idx"), &OptionButton::get_item_text);
571 ClassDB::bind_method(D_METHOD("get_item_icon", "idx"), &OptionButton::get_item_icon);
572 ClassDB::bind_method(D_METHOD("get_item_id", "idx"), &OptionButton::get_item_id);
573 ClassDB::bind_method(D_METHOD("get_item_index", "id"), &OptionButton::get_item_index);
574 ClassDB::bind_method(D_METHOD("get_item_metadata", "idx"), &OptionButton::get_item_metadata);
575 ClassDB::bind_method(D_METHOD("get_item_tooltip", "idx"), &OptionButton::get_item_tooltip);
576 ClassDB::bind_method(D_METHOD("get_item_auto_translate_mode", "idx"), &OptionButton::get_item_auto_translate_mode);
577 ClassDB::bind_method(D_METHOD("is_item_disabled", "idx"), &OptionButton::is_item_disabled);
578 ClassDB::bind_method(D_METHOD("is_item_separator", "idx"), &OptionButton::is_item_separator);
579 ClassDB::bind_method(D_METHOD("add_separator", "text"), &OptionButton::add_separator, DEFVAL(String()));
580 ClassDB::bind_method(D_METHOD("clear"), &OptionButton::clear);
581 ClassDB::bind_method(D_METHOD("select", "idx"), &OptionButton::select);
582 ClassDB::bind_method(D_METHOD("get_selected"), &OptionButton::get_selected);
583 ClassDB::bind_method(D_METHOD("get_selected_id"), &OptionButton::get_selected_id);
584 ClassDB::bind_method(D_METHOD("get_selected_metadata"), &OptionButton::get_selected_metadata);
585 ClassDB::bind_method(D_METHOD("remove_item", "idx"), &OptionButton::remove_item);
586 ClassDB::bind_method(D_METHOD("_select_int", "idx"), &OptionButton::_select_int);
587
588 ClassDB::bind_method(D_METHOD("get_popup"), &OptionButton::get_popup);
589 ClassDB::bind_method(D_METHOD("show_popup"), &OptionButton::show_popup);
590
591 ClassDB::bind_method(D_METHOD("set_item_count", "count"), &OptionButton::set_item_count);
592 ClassDB::bind_method(D_METHOD("get_item_count"), &OptionButton::get_item_count);
593 ClassDB::bind_method(D_METHOD("has_selectable_items"), &OptionButton::has_selectable_items);
594 ClassDB::bind_method(D_METHOD("get_selectable_item", "from_last"), &OptionButton::get_selectable_item, DEFVAL(false));
595 ClassDB::bind_method(D_METHOD("set_fit_to_longest_item", "fit"), &OptionButton::set_fit_to_longest_item);
596 ClassDB::bind_method(D_METHOD("is_fit_to_longest_item"), &OptionButton::is_fit_to_longest_item);
597 ClassDB::bind_method(D_METHOD("set_allow_reselect", "allow"), &OptionButton::set_allow_reselect);
598 ClassDB::bind_method(D_METHOD("get_allow_reselect"), &OptionButton::get_allow_reselect);
599 ClassDB::bind_method(D_METHOD("set_disable_shortcuts", "disabled"), &OptionButton::set_disable_shortcuts);
600
601 ADD_PROPERTY(PropertyInfo(Variant::INT, "selected"), "_select_int", "get_selected");
602 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fit_to_longest_item"), "set_fit_to_longest_item", "is_fit_to_longest_item");
603 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_reselect"), "set_allow_reselect", "get_allow_reselect");
604 ADD_ARRAY_COUNT("Items", "item_count", "set_item_count", "get_item_count", "popup/item_");
605
606 ADD_SIGNAL(MethodInfo("item_selected", PropertyInfo(Variant::INT, "index")));
607 ADD_SIGNAL(MethodInfo("item_focused", PropertyInfo(Variant::INT, "index")));
608
609 BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, OptionButton, normal);
610
611 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, OptionButton, font_color);
612 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, OptionButton, font_focus_color);
613 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, OptionButton, font_pressed_color);
614 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, OptionButton, font_hover_color);
615 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, OptionButton, font_hover_pressed_color);
616 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, OptionButton, font_disabled_color);
617
618 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, OptionButton, h_separation);
619
620 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, OptionButton, arrow_icon, "arrow");
621 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, OptionButton, arrow_margin);
622 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, OptionButton, modulate_arrow);
623
624 PopupMenu::Item defaults(true);
625
626 base_property_helper.set_prefix("popup/item_");
627 base_property_helper.set_array_length_getter(&OptionButton::get_item_count);
628 base_property_helper.register_property(PropertyInfo(Variant::STRING, "text"), defaults.text, &OptionButton::_dummy_setter, &OptionButton::get_item_text);
629 base_property_helper.register_property(PropertyInfo(Variant::OBJECT, "icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), defaults.icon, &OptionButton::_dummy_setter, &OptionButton::get_item_icon);
630 base_property_helper.register_property(PropertyInfo(Variant::INT, "id", PROPERTY_HINT_RANGE, "0,10,1,or_greater", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_STORE_IF_NULL), defaults.id, &OptionButton::_dummy_setter, &OptionButton::get_item_id);
631 base_property_helper.register_property(PropertyInfo(Variant::BOOL, "disabled"), defaults.disabled, &OptionButton::_dummy_setter, &OptionButton::is_item_disabled);
632 base_property_helper.register_property(PropertyInfo(Variant::BOOL, "separator"), defaults.separator, &OptionButton::_dummy_setter, &OptionButton::is_item_separator);
633 PropertyListHelper::register_base_helper(&base_property_helper);
634
635 ADD_CLASS_DEPENDENCY("PopupMenu");
636 }
637
638 void OptionButton::set_disable_shortcuts(bool p_disabled) {
639 disable_shortcuts = p_disabled;
640 }
641
642 #ifdef TOOLS_ENABLED
643 PackedStringArray OptionButton::get_configuration_warnings() const {
644 PackedStringArray warnings = Button::get_configuration_warnings();
645 warnings.append_array(popup->get_configuration_warnings());
646 return warnings;
647 }
648 #endif
649
650 OptionButton::OptionButton(const String &p_text) :
651 Button(p_text) {
652 set_toggle_mode(true);
653 set_process_shortcut_input(true);
654 set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT);
655 set_action_mode(ACTION_MODE_BUTTON_PRESS);
656
657 popup = memnew(PopupMenu);
658 popup->set_shrink_width(false);
659 popup->hide();
660 add_child(popup, false, INTERNAL_MODE_FRONT);
661 popup->connect("index_pressed", callable_mp(this, &OptionButton::_selected));
662 popup->connect("id_focused", callable_mp(this, &OptionButton::_focused));
663 popup->connect("popup_hide", callable_mp((BaseButton *)this, &BaseButton::set_pressed).bind(false));
664
665 property_helper.setup_for_instance(base_property_helper, this);
666 }
667
668 OptionButton::~OptionButton() {
669 }