2023-01-10 15:26:54 +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"
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/print_string.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-05-16 21:47:46 +02:00
|
|
|
static const int NONE_SELECTED = -1;
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
Size2 OptionButton::get_minimum_size() const {
|
|
|
|
Size2 minsize = Button::get_minimum_size();
|
|
|
|
|
2019-12-28 16:03:04 +01:00
|
|
|
if (has_icon("arrow")) {
|
2020-01-27 07:52:04 +01:00
|
|
|
const Size2 padding = get_stylebox("normal")->get_minimum_size();
|
|
|
|
const Size2 arrow_size = Control::get_icon("arrow")->get_size();
|
|
|
|
|
|
|
|
Size2 content_size = minsize - padding;
|
|
|
|
content_size.width += arrow_size.width + get_constant("hseparation");
|
|
|
|
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) {
|
|
|
|
case NOTIFICATION_DRAW: {
|
2021-05-05 12:44:11 +02:00
|
|
|
if (!has_icon("arrow")) {
|
2019-07-29 16:21:54 +02:00
|
|
|
return;
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2019-07-29 16:21:54 +02:00
|
|
|
|
|
|
|
RID ci = get_canvas_item();
|
|
|
|
Ref<Texture> arrow = Control::get_icon("arrow");
|
|
|
|
Color clr = Color(1, 1, 1);
|
|
|
|
if (get_constant("modulate_arrow")) {
|
|
|
|
switch (get_draw_mode()) {
|
|
|
|
case DRAW_PRESSED:
|
|
|
|
clr = get_color("font_color_pressed");
|
|
|
|
break;
|
|
|
|
case DRAW_HOVER:
|
|
|
|
clr = get_color("font_color_hover");
|
|
|
|
break;
|
|
|
|
case DRAW_DISABLED:
|
|
|
|
clr = get_color("font_color_disabled");
|
|
|
|
break;
|
|
|
|
default:
|
2021-10-26 15:32:15 +02:00
|
|
|
if (has_focus()) {
|
|
|
|
clr = get_color("font_color_focus");
|
|
|
|
} else {
|
|
|
|
clr = get_color("font_color");
|
|
|
|
}
|
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
|
|
|
|
2019-07-29 16:21:54 +02:00
|
|
|
Point2 ofs(size.width - arrow->get_width() - get_constant("arrow_margin"), int(Math::abs((size.height - arrow->get_height()) / 2)));
|
|
|
|
arrow->draw(ci, ofs, clr);
|
|
|
|
} break;
|
2019-12-28 16:03:04 +01:00
|
|
|
case NOTIFICATION_THEME_CHANGED: {
|
|
|
|
if (has_icon("arrow")) {
|
|
|
|
_set_internal_margin(MARGIN_RIGHT, Control::get_icon("arrow")->get_width());
|
|
|
|
}
|
|
|
|
} break;
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-12 02:53:35 +01:00
|
|
|
void OptionButton::_focused(int p_which) {
|
|
|
|
emit_signal("item_focused", p_which);
|
|
|
|
}
|
|
|
|
|
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() {
|
2017-03-05 16:44:50 +01:00
|
|
|
Size2 size = get_size();
|
2019-04-23 03:28:38 +02:00
|
|
|
popup->set_global_position(get_global_position() + Size2(0, size.height * get_global_transform().get_scale().y));
|
2017-03-05 16:44:50 +01:00
|
|
|
popup->set_size(Size2(size.width, 0));
|
2019-04-23 03:28:38 +02:00
|
|
|
popup->set_scale(get_global_transform().get_scale());
|
2022-02-04 21:20:26 +01:00
|
|
|
|
2022-08-27 18:41:58 +02:00
|
|
|
// If not triggered by the mouse, start the popup with the checked item (or the first enabled one) focused.
|
|
|
|
if (!_was_pressed_by_mouse()) {
|
|
|
|
if (current > -1 && !popup->is_item_disabled(current)) {
|
|
|
|
popup->set_current_index(current);
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < popup->get_item_count(); i++) {
|
|
|
|
if (!popup->is_item_disabled(i)) {
|
|
|
|
popup->set_current_index(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-04 21:20:26 +01:00
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
popup->popup();
|
|
|
|
}
|
|
|
|
|
2019-05-09 11:21:49 +02:00
|
|
|
void OptionButton::add_icon_item(const Ref<Texture> &p_icon, const String &p_label, int p_id) {
|
|
|
|
popup->add_icon_radio_check_item(p_icon, p_label, p_id);
|
2021-05-05 12:44:11 +02:00
|
|
|
if (popup->get_item_count() == 1) {
|
2014-02-10 02:10:30 +01:00
|
|
|
select(0);
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2019-05-09 11:21:49 +02:00
|
|
|
void OptionButton::add_item(const String &p_label, int p_id) {
|
|
|
|
popup->add_radio_check_item(p_label, p_id);
|
2021-05-05 12:44:11 +02:00
|
|
|
if (popup->get_item_count() == 1) {
|
2016-03-09 00:00:52 +01:00
|
|
|
select(0);
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
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
|
|
|
|
2021-05-05 12:44:11 +02:00
|
|
|
if (current == p_idx) {
|
2019-09-06 07:11:03 +02:00
|
|
|
set_text(p_text);
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
void OptionButton::set_item_icon(int p_idx, const Ref<Texture> &p_icon) {
|
|
|
|
popup->set_item_icon(p_idx, p_icon);
|
2019-09-06 07:11:03 +02:00
|
|
|
|
2021-05-05 12:44:11 +02:00
|
|
|
if (current == p_idx) {
|
2019-09-06 07:11:03 +02:00
|
|
|
set_icon(p_icon);
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<Texture> OptionButton::get_item_icon(int p_idx) const {
|
|
|
|
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-05-16 21:47:46 +02: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);
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OptionButton::add_separator() {
|
|
|
|
popup->add_separator();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OptionButton::clear() {
|
|
|
|
popup->clear();
|
|
|
|
set_text("");
|
2022-05-16 21:47:46 +02:00
|
|
|
current = NONE_SELECTED;
|
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) {
|
2021-05-05 12:44:11 +02:00
|
|
|
if (p_which == current) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-05-16 21:47:46 +02: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-05-16 21:47:46 +02:00
|
|
|
current = NONE_SELECTED;
|
|
|
|
set_text("");
|
|
|
|
set_icon(NULL);
|
|
|
|
} else {
|
|
|
|
ERR_FAIL_INDEX(p_which, popup->get_item_count());
|
|
|
|
|
|
|
|
for (int i = 0; i < popup->get_item_count(); i++) {
|
|
|
|
popup->set_item_checked(i, i == p_which);
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-05-16 21:47:46 +02:00
|
|
|
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
|
|
|
|
2021-05-05 12:44:11 +02:00
|
|
|
if (is_inside_tree() && p_emit) {
|
2017-03-05 16:44:50 +01:00
|
|
|
emit_signal("item_selected", current);
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void OptionButton::_select_int(int p_which) {
|
2022-05-16 21:47:46 +02:00
|
|
|
if (p_which < NONE_SELECTED || p_which >= popup->get_item_count()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
_select(p_which, false);
|
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
|
|
|
}
|
2022-05-19 11:42:54 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
Variant OptionButton::get_selected_metadata() const {
|
|
|
|
int idx = get_selected();
|
2021-05-05 12:44:11 +02:00
|
|
|
if (idx < 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return Variant();
|
2021-05-05 12:44:11 +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-05-16 21:47:46 +02:00
|
|
|
if (current == p_idx) {
|
|
|
|
_select(NONE_SELECTED);
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-12-25 16:24:10 +01:00
|
|
|
PopupMenu *OptionButton::get_popup() const {
|
|
|
|
return popup;
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
Array OptionButton::_get_items() const {
|
|
|
|
Array items;
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < get_item_count(); i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
items.push_back(get_item_text(i));
|
|
|
|
items.push_back(get_item_icon(i));
|
|
|
|
items.push_back(is_item_disabled(i));
|
2017-08-07 12:17:31 +02:00
|
|
|
items.push_back(get_item_id(i));
|
2014-02-10 02:10:30 +01:00
|
|
|
items.push_back(get_item_metadata(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
return items;
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
void OptionButton::_set_items(const Array &p_items) {
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_FAIL_COND(p_items.size() % 5);
|
|
|
|
clear();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < p_items.size(); i += 5) {
|
|
|
|
String text = p_items[i + 0];
|
|
|
|
Ref<Texture> icon = p_items[i + 1];
|
|
|
|
bool disabled = p_items[i + 2];
|
|
|
|
int id = p_items[i + 3];
|
|
|
|
Variant meta = p_items[i + 4];
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int idx = get_item_count();
|
|
|
|
add_item(text, id);
|
|
|
|
set_item_icon(idx, icon);
|
|
|
|
set_item_disabled(idx, disabled);
|
|
|
|
set_item_metadata(idx, meta);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OptionButton::get_translatable_strings(List<String> *p_strings) const {
|
2019-07-01 12:59:42 +02:00
|
|
|
popup->get_translatable_strings(p_strings);
|
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("_selected"), &OptionButton::_selected);
|
2018-01-12 02:53:35 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("_focused"), &OptionButton::_focused);
|
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);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_item_count"), &OptionButton::get_item_count);
|
|
|
|
ClassDB::bind_method(D_METHOD("add_separator"), &OptionButton::add_separator);
|
|
|
|
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);
|
|
|
|
ClassDB::bind_method(D_METHOD("_select_int"), &OptionButton::_select_int);
|
|
|
|
|
2017-12-25 16:24:10 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_popup"), &OptionButton::get_popup);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("_set_items"), &OptionButton::_set_items);
|
|
|
|
ClassDB::bind_method(D_METHOD("_get_items"), &OptionButton::_get_items);
|
|
|
|
|
2018-01-11 23:35:12 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "items", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_items", "_get_items");
|
2019-12-28 16:03:04 +01:00
|
|
|
// "selected" property must come after "items", otherwise GH-10213 occurs.
|
2018-02-16 09:29:19 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "selected"), "_select_int", "get_selected");
|
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")));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
OptionButton::OptionButton() {
|
2017-12-25 16:24:10 +01:00
|
|
|
current = -1;
|
2018-09-21 07:18:40 +02:00
|
|
|
set_toggle_mode(true);
|
2017-12-25 16:24:10 +01:00
|
|
|
set_text_align(ALIGN_LEFT);
|
|
|
|
set_action_mode(ACTION_MODE_BUTTON_PRESS);
|
2019-12-28 16:03:04 +01:00
|
|
|
if (has_icon("arrow")) {
|
|
|
|
_set_internal_margin(MARGIN_RIGHT, Control::get_icon("arrow")->get_width());
|
|
|
|
}
|
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();
|
2017-12-25 16:24:10 +01:00
|
|
|
add_child(popup);
|
2017-11-22 09:43:40 +01:00
|
|
|
popup->set_pass_on_modal_close_click(false);
|
2019-04-23 03:28:38 +02:00
|
|
|
popup->set_notify_transform(true);
|
2019-05-11 18:32:53 +02:00
|
|
|
popup->set_allow_search(true);
|
2019-06-09 21:44:36 +02:00
|
|
|
popup->connect("index_pressed", this, "_selected");
|
2018-01-12 02:53:35 +01:00
|
|
|
popup->connect("id_focused", this, "_focused");
|
2018-09-21 07:18:40 +02:00
|
|
|
popup->connect("popup_hide", this, "set_pressed", varray(false));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
OptionButton::~OptionButton() {
|
|
|
|
}
|