2023-01-05 13:25:55 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* option_button.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "option_button.h"
|
2020-02-21 23:26:13 +01:00
|
|
|
|
2023-08-03 10:38:20 +02:00
|
|
|
#include "core/os/keyboard.h"
|
2020-11-07 23:33:38 +01:00
|
|
|
#include "core/string/print_string.h"
|
2023-09-08 21:00:10 +02:00
|
|
|
#include "scene/theme/theme_db.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-01-28 23:03:45 +01:00
|
|
|
static const int NONE_SELECTED = -1;
|
|
|
|
|
2023-08-03 10:38:20 +02:00
|
|
|
void OptionButton::shortcut_input(const Ref<InputEvent> &p_event) {
|
|
|
|
ERR_FAIL_COND(p_event.is_null());
|
|
|
|
|
|
|
|
if (disable_shortcuts) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p_event->is_pressed() && !p_event->is_echo() && !is_disabled() && is_visible_in_tree() && popup->activate_item_by_event(p_event, false)) {
|
|
|
|
accept_event();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Button::shortcut_input(p_event);
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
Size2 OptionButton::get_minimum_size() const {
|
2022-03-19 02:15:55 +01:00
|
|
|
Size2 minsize;
|
|
|
|
if (fit_to_longest_item) {
|
|
|
|
minsize = _cached_size;
|
|
|
|
} else {
|
|
|
|
minsize = Button::get_minimum_size();
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-07-17 23:22:52 +02:00
|
|
|
if (has_theme_icon(SNAME("arrow"))) {
|
2024-05-16 09:46:24 +02:00
|
|
|
const Size2 padding = _get_largest_stylebox_size();
|
2022-08-31 14:02:40 +02:00
|
|
|
const Size2 arrow_size = theme_cache.arrow_icon->get_size();
|
2020-01-27 07:52:04 +01:00
|
|
|
|
|
|
|
Size2 content_size = minsize - padding;
|
2022-08-31 14:02:40 +02:00
|
|
|
content_size.width += arrow_size.width + MAX(0, theme_cache.h_separation);
|
2020-01-27 07:52:04 +01:00
|
|
|
content_size.height = MAX(content_size.height, arrow_size.height);
|
|
|
|
|
|
|
|
minsize = content_size + padding;
|
2019-12-28 16:03:04 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return minsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OptionButton::_notification(int p_what) {
|
2019-07-29 16:21:54 +02:00
|
|
|
switch (p_what) {
|
2022-08-31 14:02:40 +02:00
|
|
|
case NOTIFICATION_POSTINITIALIZE: {
|
2023-08-07 11:25:10 +02:00
|
|
|
_refresh_size_cache();
|
2022-08-31 14:02:40 +02:00
|
|
|
if (has_theme_icon(SNAME("arrow"))) {
|
|
|
|
if (is_layout_rtl()) {
|
|
|
|
_set_internal_margin(SIDE_LEFT, theme_cache.arrow_icon->get_width());
|
|
|
|
} else {
|
|
|
|
_set_internal_margin(SIDE_RIGHT, theme_cache.arrow_icon->get_width());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
2019-07-29 16:21:54 +02:00
|
|
|
case NOTIFICATION_DRAW: {
|
2021-07-17 23:22:52 +02:00
|
|
|
if (!has_theme_icon(SNAME("arrow"))) {
|
2019-07-29 16:21:54 +02:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2019-07-29 16:21:54 +02:00
|
|
|
|
|
|
|
RID ci = get_canvas_item();
|
|
|
|
Color clr = Color(1, 1, 1);
|
2022-08-31 14:02:40 +02:00
|
|
|
if (theme_cache.modulate_arrow) {
|
2019-07-29 16:21:54 +02:00
|
|
|
switch (get_draw_mode()) {
|
|
|
|
case DRAW_PRESSED:
|
2022-08-31 14:02:40 +02:00
|
|
|
clr = theme_cache.font_pressed_color;
|
2019-07-29 16:21:54 +02:00
|
|
|
break;
|
|
|
|
case DRAW_HOVER:
|
2022-08-31 14:02:40 +02:00
|
|
|
clr = theme_cache.font_hover_color;
|
2019-07-29 16:21:54 +02:00
|
|
|
break;
|
2022-08-11 14:15:04 +02:00
|
|
|
case DRAW_HOVER_PRESSED:
|
2022-08-31 14:02:40 +02:00
|
|
|
clr = theme_cache.font_hover_pressed_color;
|
2022-08-11 14:15:04 +02:00
|
|
|
break;
|
2019-07-29 16:21:54 +02:00
|
|
|
case DRAW_DISABLED:
|
2022-08-31 14:02:40 +02:00
|
|
|
clr = theme_cache.font_disabled_color;
|
2019-07-29 16:21:54 +02:00
|
|
|
break;
|
|
|
|
default:
|
2021-10-26 16:28:12 +02:00
|
|
|
if (has_focus()) {
|
2022-08-31 14:02:40 +02:00
|
|
|
clr = theme_cache.font_focus_color;
|
2021-10-26 16:28:12 +02:00
|
|
|
} else {
|
2022-08-31 14:02:40 +02:00
|
|
|
clr = theme_cache.font_color;
|
2021-10-26 16:28:12 +02:00
|
|
|
}
|
2019-07-29 16:21:54 +02:00
|
|
|
}
|
2017-12-25 16:24:10 +01:00
|
|
|
}
|
|
|
|
|
2019-07-29 16:21:54 +02:00
|
|
|
Size2 size = get_size();
|
2017-12-25 16:24:10 +01:00
|
|
|
|
2020-09-03 13:22:16 +02:00
|
|
|
Point2 ofs;
|
|
|
|
if (is_layout_rtl()) {
|
2022-08-31 14:02:40 +02:00
|
|
|
ofs = Point2(theme_cache.arrow_margin, int(Math::abs((size.height - theme_cache.arrow_icon->get_height()) / 2)));
|
2020-09-03 13:22:16 +02:00
|
|
|
} else {
|
2022-08-31 14:02:40 +02:00
|
|
|
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)));
|
2020-09-03 13:22:16 +02:00
|
|
|
}
|
2022-08-31 14:02:40 +02:00
|
|
|
theme_cache.arrow_icon->draw(ci, ofs, clr);
|
2019-07-29 16:21:54 +02:00
|
|
|
} break;
|
2022-02-15 18:06:48 +01:00
|
|
|
|
2024-01-23 22:29:45 +01:00
|
|
|
case NOTIFICATION_TRANSLATION_CHANGED:
|
2022-02-07 03:07:08 +01:00
|
|
|
case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
|
|
|
|
popup->set_layout_direction((Window::LayoutDirection)get_layout_direction());
|
|
|
|
[[fallthrough]];
|
|
|
|
}
|
2019-12-28 16:03:04 +01:00
|
|
|
case NOTIFICATION_THEME_CHANGED: {
|
2021-07-17 23:22:52 +02:00
|
|
|
if (has_theme_icon(SNAME("arrow"))) {
|
2020-09-03 13:22:16 +02:00
|
|
|
if (is_layout_rtl()) {
|
2022-08-31 14:02:40 +02:00
|
|
|
_set_internal_margin(SIDE_LEFT, theme_cache.arrow_icon->get_width());
|
2020-12-22 17:24:29 +01:00
|
|
|
_set_internal_margin(SIDE_RIGHT, 0.f);
|
2020-09-03 13:22:16 +02:00
|
|
|
} else {
|
2020-12-22 17:24:29 +01:00
|
|
|
_set_internal_margin(SIDE_LEFT, 0.f);
|
2022-08-31 14:02:40 +02:00
|
|
|
_set_internal_margin(SIDE_RIGHT, theme_cache.arrow_icon->get_width());
|
2020-09-03 13:22:16 +02:00
|
|
|
}
|
2019-12-28 16:03:04 +01:00
|
|
|
}
|
2022-03-19 02:15:55 +01:00
|
|
|
_refresh_size_cache();
|
2019-12-28 16:03:04 +01:00
|
|
|
} break;
|
2022-02-15 18:06:48 +01:00
|
|
|
|
2019-07-29 16:21:54 +02:00
|
|
|
case NOTIFICATION_VISIBILITY_CHANGED: {
|
|
|
|
if (!is_visible_in_tree()) {
|
|
|
|
popup->hide();
|
|
|
|
}
|
|
|
|
} break;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-11 16:13:16 +01:00
|
|
|
bool OptionButton::_set(const StringName &p_name, const Variant &p_value) {
|
2024-02-14 02:11:45 +01:00
|
|
|
int index;
|
|
|
|
const String sname = p_name;
|
2022-02-06 03:47:00 +01:00
|
|
|
|
2024-02-14 02:11:45 +01:00
|
|
|
if (property_helper.is_property_valid(sname, &index)) {
|
2021-12-11 16:13:16 +01:00
|
|
|
bool valid;
|
2024-02-14 02:11:45 +01:00
|
|
|
popup->set(sname.trim_prefix("popup/"), p_value, &valid);
|
2021-12-11 16:13:16 +01:00
|
|
|
|
2024-02-14 02:11:45 +01:00
|
|
|
if (index == current) {
|
2021-12-11 16:13:16 +01:00
|
|
|
// Force refreshing currently displayed item.
|
2022-01-28 23:03:45 +01:00
|
|
|
current = NONE_SELECTED;
|
2024-02-14 02:11:45 +01:00
|
|
|
_select(index, false);
|
2021-12-11 16:13:16 +01:00
|
|
|
}
|
|
|
|
|
2024-02-14 02:11:45 +01:00
|
|
|
const String property = sname.get_slice("/", 2);
|
2022-03-19 02:15:55 +01:00
|
|
|
if (property == "text" || property == "icon") {
|
2023-04-12 03:34:00 +02:00
|
|
|
_queue_update_size_cache();
|
2022-03-19 02:15:55 +01:00
|
|
|
}
|
|
|
|
|
2021-12-11 16:13:16 +01:00
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-12 02:53:35 +01:00
|
|
|
void OptionButton::_focused(int p_which) {
|
2021-07-17 23:22:52 +02:00
|
|
|
emit_signal(SNAME("item_focused"), p_which);
|
2018-01-12 02:53:35 +01:00
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void OptionButton::_selected(int p_which) {
|
2019-06-09 21:44:36 +02:00
|
|
|
_select(p_which, true);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void OptionButton::pressed() {
|
2022-08-23 09:38:51 +02:00
|
|
|
if (popup->is_visible()) {
|
|
|
|
popup->hide();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-02 08:46:37 +02:00
|
|
|
show_popup();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2019-06-11 20:43:37 +02:00
|
|
|
void OptionButton::add_icon_item(const Ref<Texture2D> &p_icon, const String &p_label, int p_id) {
|
2022-04-07 13:49:28 +02:00
|
|
|
bool first_selectable = !has_selectable_items();
|
2019-05-09 11:21:49 +02:00
|
|
|
popup->add_icon_radio_check_item(p_icon, p_label, p_id);
|
2022-04-07 13:49:28 +02:00
|
|
|
if (first_selectable) {
|
|
|
|
select(get_item_count() - 1);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2023-04-12 03:34:00 +02:00
|
|
|
_queue_update_size_cache();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2019-05-09 11:21:49 +02:00
|
|
|
void OptionButton::add_item(const String &p_label, int p_id) {
|
2022-04-07 13:49:28 +02:00
|
|
|
bool first_selectable = !has_selectable_items();
|
2019-05-09 11:21:49 +02:00
|
|
|
popup->add_radio_check_item(p_label, p_id);
|
2022-04-07 13:49:28 +02:00
|
|
|
if (first_selectable) {
|
|
|
|
select(get_item_count() - 1);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2023-04-12 03:34:00 +02:00
|
|
|
_queue_update_size_cache();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void OptionButton::set_item_text(int p_idx, const String &p_text) {
|
|
|
|
popup->set_item_text(p_idx, p_text);
|
2019-09-06 07:11:03 +02:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (current == p_idx) {
|
2019-09-06 07:11:03 +02:00
|
|
|
set_text(p_text);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2023-04-12 03:34:00 +02:00
|
|
|
_queue_update_size_cache();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2019-06-11 20:43:37 +02:00
|
|
|
void OptionButton::set_item_icon(int p_idx, const Ref<Texture2D> &p_icon) {
|
2017-03-05 16:44:50 +01:00
|
|
|
popup->set_item_icon(p_idx, p_icon);
|
2019-09-06 07:11:03 +02:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (current == p_idx) {
|
2019-09-06 07:11:03 +02:00
|
|
|
set_icon(p_icon);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2023-04-12 03:34:00 +02:00
|
|
|
_queue_update_size_cache();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2019-05-09 11:21:49 +02:00
|
|
|
void OptionButton::set_item_id(int p_idx, int p_id) {
|
|
|
|
popup->set_item_id(p_idx, p_id);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void OptionButton::set_item_metadata(int p_idx, const Variant &p_metadata) {
|
|
|
|
popup->set_item_metadata(p_idx, p_metadata);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2022-03-17 06:50:16 +01:00
|
|
|
void OptionButton::set_item_tooltip(int p_idx, const String &p_tooltip) {
|
|
|
|
popup->set_item_tooltip(p_idx, p_tooltip);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void OptionButton::set_item_disabled(int p_idx, bool p_disabled) {
|
|
|
|
popup->set_item_disabled(p_idx, p_disabled);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
String OptionButton::get_item_text(int p_idx) const {
|
|
|
|
return popup->get_item_text(p_idx);
|
|
|
|
}
|
|
|
|
|
2019-06-11 20:43:37 +02:00
|
|
|
Ref<Texture2D> OptionButton::get_item_icon(int p_idx) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
return popup->get_item_icon(p_idx);
|
|
|
|
}
|
|
|
|
|
2017-08-07 12:17:31 +02:00
|
|
|
int OptionButton::get_item_id(int p_idx) const {
|
2022-01-28 23:03:45 +01:00
|
|
|
if (p_idx == NONE_SELECTED) {
|
|
|
|
return NONE_SELECTED;
|
|
|
|
}
|
|
|
|
|
2017-08-07 12:17:31 +02:00
|
|
|
return popup->get_item_id(p_idx);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2019-01-28 16:31:24 +01:00
|
|
|
|
|
|
|
int OptionButton::get_item_index(int p_id) const {
|
|
|
|
return popup->get_item_index(p_id);
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
Variant OptionButton::get_item_metadata(int p_idx) const {
|
|
|
|
return popup->get_item_metadata(p_idx);
|
|
|
|
}
|
|
|
|
|
2022-03-17 06:50:16 +01:00
|
|
|
String OptionButton::get_item_tooltip(int p_idx) const {
|
|
|
|
return popup->get_item_tooltip(p_idx);
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
bool OptionButton::is_item_disabled(int p_idx) const {
|
|
|
|
return popup->is_item_disabled(p_idx);
|
|
|
|
}
|
|
|
|
|
2022-04-07 13:49:28 +02:00
|
|
|
bool OptionButton::is_item_separator(int p_idx) const {
|
|
|
|
return popup->is_item_separator(p_idx);
|
|
|
|
}
|
2021-12-11 16:13:16 +01:00
|
|
|
void OptionButton::set_item_count(int p_count) {
|
|
|
|
ERR_FAIL_COND(p_count < 0);
|
2022-02-06 03:47:00 +01:00
|
|
|
|
|
|
|
int count_old = get_item_count();
|
|
|
|
if (p_count == count_old) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-11 16:13:16 +01:00
|
|
|
popup->set_item_count(p_count);
|
2022-02-06 03:47:00 +01:00
|
|
|
|
|
|
|
if (p_count > count_old) {
|
|
|
|
for (int i = count_old; i < p_count; i++) {
|
|
|
|
popup->set_item_as_radio_checkable(i, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-09 17:52:11 +01:00
|
|
|
if (!initialized) {
|
|
|
|
if (queued_current != current) {
|
|
|
|
current = queued_current;
|
|
|
|
}
|
|
|
|
initialized = true;
|
|
|
|
}
|
|
|
|
|
2022-03-19 02:15:55 +01:00
|
|
|
_refresh_size_cache();
|
2021-12-11 16:13:16 +01:00
|
|
|
notify_property_list_changed();
|
|
|
|
}
|
|
|
|
|
2022-04-07 13:49:28 +02:00
|
|
|
bool OptionButton::has_selectable_items() const {
|
|
|
|
for (int i = 0; i < get_item_count(); i++) {
|
|
|
|
if (!is_item_disabled(i) && !is_item_separator(i)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
int OptionButton::get_selectable_item(bool p_from_last) const {
|
|
|
|
if (!p_from_last) {
|
|
|
|
for (int i = 0; i < get_item_count(); i++) {
|
|
|
|
if (!is_item_disabled(i) && !is_item_separator(i)) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2022-05-14 18:54:14 +02:00
|
|
|
for (int i = get_item_count() - 1; i >= 0; i--) {
|
2022-04-07 13:49:28 +02:00
|
|
|
if (!is_item_disabled(i) && !is_item_separator(i)) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int OptionButton::get_item_count() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
return popup->get_item_count();
|
|
|
|
}
|
|
|
|
|
2022-03-19 02:15:55 +01:00
|
|
|
void OptionButton::set_fit_to_longest_item(bool p_fit) {
|
|
|
|
if (p_fit == fit_to_longest_item) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fit_to_longest_item = p_fit;
|
|
|
|
|
|
|
|
_refresh_size_cache();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OptionButton::is_fit_to_longest_item() const {
|
|
|
|
return fit_to_longest_item;
|
|
|
|
}
|
|
|
|
|
2023-01-25 11:39:09 +01:00
|
|
|
void OptionButton::set_allow_reselect(bool p_allow) {
|
|
|
|
allow_reselect = p_allow;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OptionButton::get_allow_reselect() const {
|
|
|
|
return allow_reselect;
|
|
|
|
}
|
|
|
|
|
2022-04-07 13:49:28 +02:00
|
|
|
void OptionButton::add_separator(const String &p_text) {
|
|
|
|
popup->add_separator(p_text);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void OptionButton::clear() {
|
|
|
|
popup->clear();
|
|
|
|
set_text("");
|
2022-01-28 23:03:45 +01:00
|
|
|
current = NONE_SELECTED;
|
2022-03-19 02:15:55 +01:00
|
|
|
_refresh_size_cache();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-08-11 21:10:05 +02:00
|
|
|
void OptionButton::_select(int p_which, bool p_emit) {
|
2023-01-25 11:39:09 +01:00
|
|
|
if (p_which == current && !allow_reselect) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-01-28 23:03:45 +01:00
|
|
|
if (p_which == NONE_SELECTED) {
|
|
|
|
for (int i = 0; i < popup->get_item_count(); i++) {
|
|
|
|
popup->set_item_checked(i, false);
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-01-28 23:03:45 +01:00
|
|
|
current = NONE_SELECTED;
|
|
|
|
set_text("");
|
2022-02-06 03:47:00 +01:00
|
|
|
set_icon(nullptr);
|
2022-01-28 23:03:45 +01:00
|
|
|
} else {
|
|
|
|
ERR_FAIL_INDEX(p_which, popup->get_item_count());
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-01-28 23:03:45 +01:00
|
|
|
for (int i = 0; i < popup->get_item_count(); i++) {
|
|
|
|
popup->set_item_checked(i, i == p_which);
|
|
|
|
}
|
|
|
|
|
|
|
|
current = p_which;
|
|
|
|
set_text(popup->get_item_text(current));
|
|
|
|
set_icon(popup->get_item_icon(current));
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (is_inside_tree() && p_emit) {
|
2024-05-14 14:21:31 +02:00
|
|
|
emit_signal(SceneStringName(item_selected), current);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void OptionButton::_select_int(int p_which) {
|
2024-02-09 17:52:11 +01:00
|
|
|
if (p_which < NONE_SELECTED) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (p_which >= popup->get_item_count()) {
|
|
|
|
if (!initialized) {
|
|
|
|
queued_current = p_which;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
_select(p_which, false);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2022-03-19 02:15:55 +01:00
|
|
|
void OptionButton::_refresh_size_cache() {
|
|
|
|
cache_refresh_pending = false;
|
|
|
|
|
2023-07-15 05:10:23 +02:00
|
|
|
if (fit_to_longest_item) {
|
2023-08-07 11:25:10 +02:00
|
|
|
_cached_size = theme_cache.normal->get_minimum_size();
|
2023-07-15 05:10:23 +02:00
|
|
|
for (int i = 0; i < get_item_count(); i++) {
|
|
|
|
_cached_size = _cached_size.max(get_minimum_size_for_text_and_icon(popup->get_item_xl_text(i), get_item_icon(i)));
|
|
|
|
}
|
2022-03-19 02:15:55 +01:00
|
|
|
}
|
|
|
|
update_minimum_size();
|
|
|
|
}
|
|
|
|
|
2023-04-12 03:34:00 +02:00
|
|
|
void OptionButton::_queue_update_size_cache() {
|
2022-03-19 02:15:55 +01:00
|
|
|
if (cache_refresh_pending) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cache_refresh_pending = true;
|
|
|
|
|
2022-10-21 23:40:06 +02:00
|
|
|
callable_mp(this, &OptionButton::_refresh_size_cache).call_deferred();
|
2022-03-19 02:15:55 +01:00
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void OptionButton::select(int p_idx) {
|
2017-03-05 16:44:50 +01:00
|
|
|
_select(p_idx, false);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int OptionButton::get_selected() const {
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
2017-08-07 12:17:31 +02:00
|
|
|
int OptionButton::get_selected_id() const {
|
|
|
|
return get_item_id(current);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
Variant OptionButton::get_selected_metadata() const {
|
|
|
|
int idx = get_selected();
|
2020-05-14 16:41:43 +02:00
|
|
|
if (idx < 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return Variant();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
return get_item_metadata(current);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OptionButton::remove_item(int p_idx) {
|
|
|
|
popup->remove_item(p_idx);
|
2022-01-28 23:03:45 +01:00
|
|
|
if (current == p_idx) {
|
|
|
|
_select(NONE_SELECTED);
|
|
|
|
}
|
2023-04-12 03:34:00 +02:00
|
|
|
_queue_update_size_cache();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-12-25 16:24:10 +01:00
|
|
|
PopupMenu *OptionButton::get_popup() const {
|
|
|
|
return popup;
|
|
|
|
}
|
|
|
|
|
2022-10-02 08:46:37 +02:00
|
|
|
void OptionButton::show_popup() {
|
|
|
|
if (!get_viewport()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-25 21:49:56 +01:00
|
|
|
Rect2 rect = get_screen_rect();
|
|
|
|
rect.position.y += rect.size.height;
|
|
|
|
rect.size.height = 0;
|
|
|
|
popup->set_position(rect.position);
|
|
|
|
popup->set_size(rect.size);
|
2022-10-02 08:46:37 +02:00
|
|
|
|
|
|
|
// If not triggered by the mouse, start the popup with the checked item (or the first enabled one) focused.
|
|
|
|
if (current != NONE_SELECTED && !popup->is_item_disabled(current)) {
|
|
|
|
if (!_was_pressed_by_mouse()) {
|
|
|
|
popup->set_focused_item(current);
|
|
|
|
} else {
|
|
|
|
popup->scroll_to_item(current);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < popup->get_item_count(); i++) {
|
|
|
|
if (!popup->is_item_disabled(i)) {
|
|
|
|
if (!_was_pressed_by_mouse()) {
|
|
|
|
popup->set_focused_item(i);
|
|
|
|
} else {
|
|
|
|
popup->scroll_to_item(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
popup->popup();
|
|
|
|
}
|
|
|
|
|
2022-08-12 22:57:11 +02:00
|
|
|
void OptionButton::_validate_property(PropertyInfo &p_property) const {
|
|
|
|
if (p_property.name == "text" || p_property.name == "icon") {
|
|
|
|
p_property.usage = PROPERTY_USAGE_NONE;
|
2022-03-19 13:36:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void OptionButton::_bind_methods() {
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("add_item", "label", "id"), &OptionButton::add_item, DEFVAL(-1));
|
2018-05-08 07:35:28 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("add_icon_item", "texture", "label", "id"), &OptionButton::add_icon_item, DEFVAL(-1));
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_item_text", "idx", "text"), &OptionButton::set_item_text);
|
2017-08-09 13:19:41 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_item_icon", "idx", "texture"), &OptionButton::set_item_icon);
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_item_disabled", "idx", "disabled"), &OptionButton::set_item_disabled);
|
2017-08-07 12:17:31 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_item_id", "idx", "id"), &OptionButton::set_item_id);
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_item_metadata", "idx", "metadata"), &OptionButton::set_item_metadata);
|
2022-03-17 06:50:16 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_item_tooltip", "idx", "tooltip"), &OptionButton::set_item_tooltip);
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_item_text", "idx"), &OptionButton::get_item_text);
|
2017-08-09 13:19:41 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_item_icon", "idx"), &OptionButton::get_item_icon);
|
2017-08-07 12:17:31 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_item_id", "idx"), &OptionButton::get_item_id);
|
2019-01-28 16:31:24 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_item_index", "id"), &OptionButton::get_item_index);
|
2017-08-09 13:19:41 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_item_metadata", "idx"), &OptionButton::get_item_metadata);
|
2022-03-17 06:50:16 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_item_tooltip", "idx"), &OptionButton::get_item_tooltip);
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("is_item_disabled", "idx"), &OptionButton::is_item_disabled);
|
2022-04-07 13:49:28 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("is_item_separator", "idx"), &OptionButton::is_item_separator);
|
|
|
|
ClassDB::bind_method(D_METHOD("add_separator", "text"), &OptionButton::add_separator, DEFVAL(String()));
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("clear"), &OptionButton::clear);
|
|
|
|
ClassDB::bind_method(D_METHOD("select", "idx"), &OptionButton::select);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_selected"), &OptionButton::get_selected);
|
2017-08-07 12:17:31 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_selected_id"), &OptionButton::get_selected_id);
|
2017-08-09 13:19:41 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_selected_metadata"), &OptionButton::get_selected_metadata);
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("remove_item", "idx"), &OptionButton::remove_item);
|
2022-08-08 14:18:26 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("_select_int", "idx"), &OptionButton::_select_int);
|
2017-03-05 16:44:50 +01:00
|
|
|
|
2017-12-25 16:24:10 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_popup"), &OptionButton::get_popup);
|
2022-10-02 08:46:37 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("show_popup"), &OptionButton::show_popup);
|
2017-12-25 16:24:10 +01:00
|
|
|
|
2022-01-07 13:58:28 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_item_count", "count"), &OptionButton::set_item_count);
|
2021-12-11 16:13:16 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_item_count"), &OptionButton::get_item_count);
|
2022-04-07 13:49:28 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("has_selectable_items"), &OptionButton::has_selectable_items);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_selectable_item", "from_last"), &OptionButton::get_selectable_item, DEFVAL(false));
|
2022-03-19 02:15:55 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_fit_to_longest_item", "fit"), &OptionButton::set_fit_to_longest_item);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_fit_to_longest_item"), &OptionButton::is_fit_to_longest_item);
|
2023-01-25 11:39:09 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_allow_reselect", "allow"), &OptionButton::set_allow_reselect);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_allow_reselect"), &OptionButton::get_allow_reselect);
|
2023-08-03 10:38:20 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_disable_shortcuts", "disabled"), &OptionButton::set_disable_shortcuts);
|
2022-01-07 13:58:28 +01:00
|
|
|
|
2018-02-16 09:29:19 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "selected"), "_select_int", "get_selected");
|
2022-03-19 02:15:55 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fit_to_longest_item"), "set_fit_to_longest_item", "is_fit_to_longest_item");
|
2023-01-25 11:39:09 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_reselect"), "set_allow_reselect", "get_allow_reselect");
|
2024-02-09 17:52:11 +01:00
|
|
|
ADD_ARRAY_COUNT("Items", "item_count", "set_item_count", "get_item_count", "popup/item_");
|
2023-09-08 21:00:10 +02:00
|
|
|
|
2020-03-25 18:07:18 +01:00
|
|
|
ADD_SIGNAL(MethodInfo("item_selected", PropertyInfo(Variant::INT, "index")));
|
|
|
|
ADD_SIGNAL(MethodInfo("item_focused", PropertyInfo(Variant::INT, "index")));
|
2023-09-08 21:00:10 +02:00
|
|
|
|
|
|
|
BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, OptionButton, normal);
|
|
|
|
|
|
|
|
BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, OptionButton, font_color);
|
|
|
|
BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, OptionButton, font_focus_color);
|
|
|
|
BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, OptionButton, font_pressed_color);
|
|
|
|
BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, OptionButton, font_hover_color);
|
|
|
|
BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, OptionButton, font_hover_pressed_color);
|
|
|
|
BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, OptionButton, font_disabled_color);
|
|
|
|
|
|
|
|
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, OptionButton, h_separation);
|
|
|
|
|
|
|
|
BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, OptionButton, arrow_icon, "arrow");
|
|
|
|
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, OptionButton, arrow_margin);
|
|
|
|
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, OptionButton, modulate_arrow);
|
2024-02-14 02:11:45 +01:00
|
|
|
|
|
|
|
PopupMenu::Item defaults(true);
|
|
|
|
|
|
|
|
base_property_helper.set_prefix("popup/item_");
|
2024-05-09 14:19:16 +02:00
|
|
|
base_property_helper.set_array_length_getter(&OptionButton::get_item_count);
|
2024-02-14 02:11:45 +01:00
|
|
|
base_property_helper.register_property(PropertyInfo(Variant::STRING, "text"), defaults.text, &OptionButton::_dummy_setter, &OptionButton::get_item_text);
|
|
|
|
base_property_helper.register_property(PropertyInfo(Variant::OBJECT, "icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), defaults.icon, &OptionButton::_dummy_setter, &OptionButton::get_item_icon);
|
2024-08-20 12:38:33 +02:00
|
|
|
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);
|
2024-02-14 02:11:45 +01:00
|
|
|
base_property_helper.register_property(PropertyInfo(Variant::BOOL, "disabled"), defaults.disabled, &OptionButton::_dummy_setter, &OptionButton::is_item_disabled);
|
|
|
|
base_property_helper.register_property(PropertyInfo(Variant::BOOL, "separator"), defaults.separator, &OptionButton::_dummy_setter, &OptionButton::is_item_separator);
|
2024-07-03 09:39:18 +02:00
|
|
|
PropertyListHelper::register_base_helper(&base_property_helper);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2023-08-03 10:38:20 +02:00
|
|
|
void OptionButton::set_disable_shortcuts(bool p_disabled) {
|
|
|
|
disable_shortcuts = p_disabled;
|
|
|
|
}
|
|
|
|
|
2022-03-04 09:18:44 +01:00
|
|
|
OptionButton::OptionButton(const String &p_text) :
|
|
|
|
Button(p_text) {
|
2018-09-21 07:18:40 +02:00
|
|
|
set_toggle_mode(true);
|
2023-08-03 10:38:20 +02:00
|
|
|
set_process_shortcut_input(true);
|
2021-11-25 03:58:47 +01:00
|
|
|
set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT);
|
2020-09-03 13:22:16 +02:00
|
|
|
set_action_mode(ACTION_MODE_BUTTON_PRESS);
|
2017-12-25 16:24:10 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
popup = memnew(PopupMenu);
|
2014-02-10 02:10:30 +01:00
|
|
|
popup->hide();
|
2021-08-25 15:49:30 +02:00
|
|
|
add_child(popup, false, INTERNAL_MODE_FRONT);
|
2020-02-21 18:28:45 +01:00
|
|
|
popup->connect("index_pressed", callable_mp(this, &OptionButton::_selected));
|
|
|
|
popup->connect("id_focused", callable_mp(this, &OptionButton::_focused));
|
2022-07-28 22:56:41 +02:00
|
|
|
popup->connect("popup_hide", callable_mp((BaseButton *)this, &BaseButton::set_pressed).bind(false));
|
2024-02-14 02:11:45 +01:00
|
|
|
|
|
|
|
property_helper.setup_for_instance(base_property_helper, this);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
OptionButton::~OptionButton() {
|
|
|
|
}
|