2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* button_array.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 21:01:57 +00:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-07 22:11:42 +00:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#include "button_array.h"
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool ButtonArray::_set(const StringName &p_name, const Variant &p_value) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
String n = String(p_name);
|
2014-02-10 01:10:30 +00:00
|
|
|
if (n.begins_with("button/")) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
String what = n.get_slicec('/', 1);
|
|
|
|
if (what == "count") {
|
|
|
|
int new_size = p_value;
|
|
|
|
if (new_size > 0 && buttons.size() == 0) {
|
|
|
|
selected = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (new_size < buttons.size()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
if (selected >= new_size)
|
|
|
|
selected = new_size - 1;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
buttons.resize(new_size);
|
|
|
|
_change_notify();
|
|
|
|
minimum_size_changed();
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if (what == "align") {
|
2014-02-10 01:10:30 +00:00
|
|
|
set_align(Align(p_value.operator int()));
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if (what == "selected") {
|
2014-02-10 01:10:30 +00:00
|
|
|
set_selected(p_value);
|
|
|
|
} else if (what == "min_button_size") {
|
|
|
|
min_button_size = p_value;
|
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
int idx = what.to_int();
|
|
|
|
ERR_FAIL_INDEX_V(idx, buttons.size(), false);
|
|
|
|
String f = n.get_slicec('/', 2);
|
|
|
|
if (f == "text") {
|
|
|
|
buttons[idx].text = p_value;
|
|
|
|
buttons[idx].xl_text = XL_MESSAGE(p_value);
|
|
|
|
} else if (f == "tooltip")
|
|
|
|
buttons[idx].tooltip = p_value;
|
|
|
|
else if (f == "icon")
|
|
|
|
buttons[idx].icon = p_value;
|
2014-02-10 01:10:30 +00:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
update();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool ButtonArray::_get(const StringName &p_name, Variant &r_ret) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
String n = String(p_name);
|
2014-02-10 01:10:30 +00:00
|
|
|
if (n.begins_with("button/")) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
String what = n.get_slicec('/', 1);
|
|
|
|
if (what == "count") {
|
|
|
|
r_ret = buttons.size();
|
|
|
|
} else if (what == "align") {
|
|
|
|
r_ret = get_align();
|
|
|
|
} else if (what == "selected") {
|
|
|
|
r_ret = get_selected();
|
|
|
|
} else if (what == "min_button_size") {
|
2014-02-10 01:10:30 +00:00
|
|
|
r_ret = min_button_size;
|
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
int idx = what.to_int();
|
|
|
|
ERR_FAIL_INDEX_V(idx, buttons.size(), false);
|
|
|
|
String f = n.get_slicec('/', 2);
|
|
|
|
if (f == "text")
|
|
|
|
r_ret = buttons[idx].text;
|
|
|
|
else if (f == "tooltip")
|
|
|
|
r_ret = buttons[idx].tooltip;
|
|
|
|
else if (f == "icon")
|
|
|
|
r_ret = buttons[idx].icon;
|
2014-02-10 01:10:30 +00:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
void ButtonArray::_get_property_list(List<PropertyInfo> *p_list) const {
|
|
|
|
|
|
|
|
p_list->push_back(PropertyInfo(Variant::INT, "button/count", PROPERTY_HINT_RANGE, "0,512,1"));
|
|
|
|
p_list->push_back(PropertyInfo(Variant::INT, "button/min_button_size", PROPERTY_HINT_RANGE, "0,1024,1"));
|
|
|
|
p_list->push_back(PropertyInfo(Variant::INT, "button/align", PROPERTY_HINT_ENUM, "Begin,Center,End,Fill,Expand"));
|
|
|
|
for (int i = 0; i < buttons.size(); i++) {
|
|
|
|
String base = "button/" + itos(i) + "/";
|
|
|
|
p_list->push_back(PropertyInfo(Variant::STRING, base + "text"));
|
|
|
|
p_list->push_back(PropertyInfo(Variant::STRING, base + "tooltip"));
|
|
|
|
p_list->push_back(PropertyInfo(Variant::OBJECT, base + "icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture"));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
if (buttons.size() > 0) {
|
|
|
|
p_list->push_back(PropertyInfo(Variant::INT, "button/selected", PROPERTY_HINT_RANGE, "0," + itos(buttons.size() - 1) + ",1"));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Size2 ButtonArray::get_minimum_size() const {
|
|
|
|
|
|
|
|
Ref<StyleBox> style_normal = get_stylebox("normal");
|
|
|
|
Ref<StyleBox> style_selected = get_stylebox("selected");
|
|
|
|
Ref<Font> font_normal = get_font("font");
|
|
|
|
Ref<Font> font_selected = get_font("font_selected");
|
|
|
|
int icon_sep = get_constant("icon_separator");
|
|
|
|
int button_sep = get_constant("button_separator");
|
|
|
|
|
|
|
|
Size2 minsize;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < buttons.size(); i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Ref<StyleBox> sb = i == selected ? style_selected : style_normal;
|
|
|
|
Ref<Font> f = i == selected ? font_selected : font_normal;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Size2 ms;
|
2017-02-07 23:38:36 +00:00
|
|
|
ms = f->get_string_size(buttons[i].xl_text);
|
2014-02-10 01:10:30 +00:00
|
|
|
if (buttons[i].icon.is_valid()) {
|
|
|
|
|
|
|
|
Size2 bs = buttons[i].icon->get_size();
|
2017-03-05 15:44:50 +00:00
|
|
|
ms.height = MAX(ms.height, bs.height);
|
|
|
|
ms.width += bs.width + icon_sep;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ms += sb->get_minimum_size();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
buttons[i]._ms_cache = ms[orientation];
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
minsize[orientation] += ms[orientation];
|
|
|
|
if (i > 0)
|
|
|
|
minsize[orientation] += button_sep;
|
|
|
|
minsize[!orientation] = MAX(minsize[!orientation], ms[!orientation]);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return minsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ButtonArray::_notification(int p_what) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_MOUSE_EXIT: {
|
|
|
|
hover = -1;
|
2016-10-12 21:54:04 +00:00
|
|
|
update();
|
2017-03-05 15:44:50 +00:00
|
|
|
} break;
|
|
|
|
case NOTIFICATION_READY: {
|
2016-10-12 21:54:04 +00:00
|
|
|
MethodInfo mi;
|
2017-03-05 15:44:50 +00:00
|
|
|
mi.name = "mouse_sub_enter";
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
add_user_signal(mi);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
} break;
|
2014-02-10 01:10:30 +00:00
|
|
|
case NOTIFICATION_DRAW: {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Size2 size = get_size();
|
|
|
|
Size2 minsize = get_combined_minimum_size();
|
2014-02-10 01:10:30 +00:00
|
|
|
Ref<StyleBox> style_normal = get_stylebox("normal");
|
|
|
|
Ref<StyleBox> style_selected = get_stylebox("selected");
|
|
|
|
Ref<StyleBox> style_focus = get_stylebox("focus");
|
|
|
|
Ref<StyleBox> style_hover = get_stylebox("hover");
|
|
|
|
Ref<Font> font_normal = get_font("font");
|
|
|
|
Ref<Font> font_selected = get_font("font_selected");
|
|
|
|
int icon_sep = get_constant("icon_separator");
|
|
|
|
int button_sep = get_constant("button_separator");
|
|
|
|
Color color_normal = get_color("font_color");
|
|
|
|
Color color_selected = get_color("font_color_selected");
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int sep = button_sep;
|
|
|
|
int ofs = 0;
|
|
|
|
int expand = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
switch (align) {
|
2014-02-10 01:10:30 +00:00
|
|
|
case ALIGN_BEGIN: {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ofs = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
} break;
|
|
|
|
case ALIGN_CENTER: {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ofs = Math::floor((size[orientation] - minsize[orientation]) / 2);
|
2014-02-10 01:10:30 +00:00
|
|
|
} break;
|
|
|
|
case ALIGN_END: {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ofs = Math::floor((size[orientation] - minsize[orientation]));
|
2014-02-10 01:10:30 +00:00
|
|
|
} break;
|
|
|
|
case ALIGN_FILL: {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (buttons.size() > 1)
|
|
|
|
sep += Math::floor((size[orientation] - minsize[orientation]) / (buttons.size() - 1.0));
|
|
|
|
ofs = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
} break;
|
|
|
|
case ALIGN_EXPAND_FILL: {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ofs = 0;
|
|
|
|
expand = size[orientation] - minsize[orientation];
|
2014-02-10 01:10:30 +00:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int op_size = orientation == VERTICAL ? size.width : size.height;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < buttons.size(); i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
int ms = buttons[i]._ms_cache;
|
2017-03-05 15:44:50 +00:00
|
|
|
int s = ms;
|
|
|
|
if (expand > 0) {
|
|
|
|
s += expand / buttons.size();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
if (min_button_size != -1 && s < min_button_size) {
|
2014-02-10 01:10:30 +00:00
|
|
|
s = min_button_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect2 r;
|
2017-03-05 15:44:50 +00:00
|
|
|
r.pos[orientation] = ofs;
|
|
|
|
r.pos[!orientation] = 0;
|
|
|
|
r.size[orientation] = s;
|
|
|
|
r.size[!orientation] = op_size;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Ref<Font> f;
|
|
|
|
Color c;
|
2016-10-12 21:54:04 +00:00
|
|
|
Point2 sbsize;
|
|
|
|
Point2 sbofs;
|
2017-03-05 15:44:50 +00:00
|
|
|
if (i == selected) {
|
|
|
|
draw_style_box(style_selected, r);
|
|
|
|
sbsize = style_selected->get_minimum_size();
|
|
|
|
sbofs = style_selected->get_offset();
|
|
|
|
f = font_selected;
|
|
|
|
c = color_selected;
|
2014-02-10 01:10:30 +00:00
|
|
|
if (has_focus())
|
2017-03-05 15:44:50 +00:00
|
|
|
draw_style_box(style_focus, r);
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
if (hover == i)
|
|
|
|
draw_style_box(style_hover, r);
|
2016-11-19 13:48:39 +00:00
|
|
|
else if (!flat)
|
2017-03-05 15:44:50 +00:00
|
|
|
draw_style_box(style_normal, r);
|
|
|
|
sbsize = style_normal->get_minimum_size();
|
|
|
|
sbofs = style_normal->get_offset();
|
|
|
|
f = font_normal;
|
|
|
|
c = color_normal;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-02-07 23:38:36 +00:00
|
|
|
Size2 ssize = f->get_string_size(buttons[i].xl_text);
|
2014-02-10 01:10:30 +00:00
|
|
|
if (buttons[i].icon.is_valid()) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ssize.x += buttons[i].icon->get_width();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
Point2 text_ofs = ((r.size - ssize - sbsize) / 2.0 + Point2(0, f->get_ascent())).floor() + sbofs;
|
2014-02-10 01:10:30 +00:00
|
|
|
if (buttons[i].icon.is_valid()) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
draw_texture(buttons[i].icon, r.pos + Point2(text_ofs.x, Math::floor((r.size.height - buttons[i].icon->get_height()) / 2.0)));
|
|
|
|
text_ofs.x += buttons[i].icon->get_width() + icon_sep;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
draw_string(f, text_ofs + r.pos, buttons[i].xl_text, c);
|
|
|
|
buttons[i]._pos_cache = ofs;
|
|
|
|
buttons[i]._size_cache = s;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ofs += s;
|
|
|
|
ofs += sep;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
void ButtonArray::_gui_input(const Ref<InputEvent> &p_event) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (
|
2017-05-20 15:38:03 +00:00
|
|
|
((orientation == HORIZONTAL && p_event->is_action("ui_left")) ||
|
|
|
|
(orientation == VERTICAL && p_event->is_action("ui_up"))) &&
|
|
|
|
p_event->is_pressed() && selected > 0) {
|
2017-03-05 15:44:50 +00:00
|
|
|
set_selected(selected - 1);
|
|
|
|
accept_event();
|
|
|
|
emit_signal("button_selected", selected);
|
|
|
|
return;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
2017-05-20 15:38:03 +00:00
|
|
|
((orientation == HORIZONTAL && p_event->is_action("ui_right")) ||
|
|
|
|
(orientation == VERTICAL && p_event->is_action("ui_down"))) &&
|
|
|
|
p_event->is_pressed() && selected < (buttons.size() - 1)) {
|
2017-03-05 15:44:50 +00:00
|
|
|
set_selected(selected + 1);
|
|
|
|
accept_event();
|
|
|
|
emit_signal("button_selected", selected);
|
|
|
|
return;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
Ref<InputEventMouseButton> mb = p_event;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
|
|
|
|
|
|
|
|
int ofs = orientation == HORIZONTAL ? mb->get_pos().x : mb->get_pos().y;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < buttons.size(); i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (ofs >= buttons[i]._pos_cache && ofs < buttons[i]._pos_cache + buttons[i]._size_cache) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
set_selected(i);
|
2017-03-05 15:44:50 +00:00
|
|
|
emit_signal("button_selected", i);
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
Ref<InputEventMouseMotion> mm = p_event;
|
|
|
|
|
|
|
|
if (mm.is_valid()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
int ofs = orientation == HORIZONTAL ? mm->get_pos().x : mm->get_pos().y;
|
2017-03-05 15:44:50 +00:00
|
|
|
int new_hover = -1;
|
|
|
|
for (int i = 0; i < buttons.size(); i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (ofs >= buttons[i]._pos_cache && ofs < buttons[i]._pos_cache + buttons[i]._size_cache) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
new_hover = i;
|
2014-02-10 01:10:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (new_hover != hover) {
|
|
|
|
hover = new_hover;
|
2014-02-10 01:10:30 +00:00
|
|
|
emit_signal("mouse_sub_enter");
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
String ButtonArray::get_tooltip(const Point2 &p_pos) const {
|
2016-10-14 17:11:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int ofs = orientation == HORIZONTAL ? p_pos.x : p_pos.y;
|
|
|
|
for (int i = 0; i < buttons.size(); i++) {
|
2016-10-14 17:11:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (ofs >= buttons[i]._pos_cache && ofs < buttons[i]._pos_cache + buttons[i]._size_cache)
|
2016-10-14 17:11:52 +00:00
|
|
|
return buttons[i].tooltip;
|
|
|
|
}
|
|
|
|
return Control::get_tooltip(p_pos);
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void ButtonArray::set_align(Align p_align) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
align = p_align;
|
2014-02-10 01:10:30 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
ButtonArray::Align ButtonArray::get_align() const {
|
|
|
|
|
|
|
|
return align;
|
|
|
|
}
|
|
|
|
|
2016-11-19 13:48:39 +00:00
|
|
|
void ButtonArray::set_flat(bool p_flat) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
flat = p_flat;
|
2016-11-19 13:48:39 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ButtonArray::is_flat() const {
|
|
|
|
|
|
|
|
return flat;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void ButtonArray::add_button(const String &p_text, const String &p_tooltip) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Button button;
|
2017-03-05 15:44:50 +00:00
|
|
|
button.text = p_text;
|
|
|
|
button.xl_text = XL_MESSAGE(p_text);
|
|
|
|
button.tooltip = p_tooltip;
|
2014-02-10 01:10:30 +00:00
|
|
|
buttons.push_back(button);
|
|
|
|
update();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (selected == -1)
|
|
|
|
selected = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
minimum_size_changed();
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void ButtonArray::add_icon_button(const Ref<Texture> &p_icon, const String &p_text, const String &p_tooltip) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Button button;
|
2017-03-05 15:44:50 +00:00
|
|
|
button.text = p_text;
|
|
|
|
button.xl_text = XL_MESSAGE(p_text);
|
|
|
|
button.icon = p_icon;
|
|
|
|
button.tooltip = p_tooltip;
|
2014-02-10 01:10:30 +00:00
|
|
|
buttons.push_back(button);
|
2017-03-05 15:44:50 +00:00
|
|
|
if (selected == -1)
|
|
|
|
selected = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void ButtonArray::set_button_text(int p_button, const String &p_text) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX(p_button, buttons.size());
|
|
|
|
buttons[p_button].text = p_text;
|
|
|
|
buttons[p_button].xl_text = XL_MESSAGE(p_text);
|
2014-02-10 01:10:30 +00:00
|
|
|
update();
|
|
|
|
minimum_size_changed();
|
|
|
|
}
|
2016-04-27 21:58:24 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void ButtonArray::set_button_tooltip(int p_button, const String &p_text) {
|
2016-10-14 17:11:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX(p_button, buttons.size());
|
|
|
|
buttons[p_button].tooltip = p_text;
|
2016-10-14 17:11:52 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void ButtonArray::set_button_icon(int p_button, const Ref<Texture> &p_icon) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX(p_button, buttons.size());
|
|
|
|
buttons[p_button].icon = p_icon;
|
2014-02-10 01:10:30 +00:00
|
|
|
update();
|
|
|
|
minimum_size_changed();
|
|
|
|
}
|
2016-04-27 21:58:24 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
String ButtonArray::get_button_text(int p_button) const {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_button, buttons.size(), "");
|
2014-02-10 01:10:30 +00:00
|
|
|
return buttons[p_button].text;
|
|
|
|
}
|
2016-04-27 21:58:24 +00:00
|
|
|
|
2016-10-14 17:11:52 +00:00
|
|
|
String ButtonArray::get_button_tooltip(int p_button) const {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_button, buttons.size(), "");
|
2016-10-14 17:11:52 +00:00
|
|
|
return buttons[p_button].tooltip;
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
Ref<Texture> ButtonArray::get_button_icon(int p_button) const {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_button, buttons.size(), Ref<Texture>());
|
2014-02-10 01:10:30 +00:00
|
|
|
return buttons[p_button].icon;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ButtonArray::get_selected() const {
|
|
|
|
|
|
|
|
return selected;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ButtonArray::get_hovered() const {
|
|
|
|
|
|
|
|
return hover;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ButtonArray::set_selected(int p_selected) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX(p_selected, buttons.size());
|
|
|
|
selected = p_selected;
|
2014-02-10 01:10:30 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ButtonArray::erase_button(int p_button) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX(p_button, buttons.size());
|
2014-02-10 01:10:30 +00:00
|
|
|
buttons.remove(p_button);
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_button >= selected)
|
2014-02-10 01:10:30 +00:00
|
|
|
selected--;
|
2017-03-05 15:44:50 +00:00
|
|
|
if (selected < 0)
|
|
|
|
selected = 0;
|
|
|
|
if (selected >= buttons.size())
|
|
|
|
selected = buttons.size() - 1;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void ButtonArray::clear() {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
buttons.clear();
|
2017-03-05 15:44:50 +00:00
|
|
|
selected = -1;
|
2014-02-10 01:10:30 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
int ButtonArray::get_button_count() const {
|
|
|
|
|
|
|
|
return buttons.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ButtonArray::get_translatable_strings(List<String> *p_strings) const {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < buttons.size(); i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
p_strings->push_back(buttons[i].text);
|
2016-10-14 17:11:52 +00:00
|
|
|
p_strings->push_back(buttons[i].tooltip);
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ButtonArray::_bind_methods() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("add_button", "text", "tooltip"), &ButtonArray::add_button, DEFVAL(""));
|
|
|
|
ClassDB::bind_method(D_METHOD("add_icon_button", "icon:Texture", "text", "tooltip"), &ButtonArray::add_icon_button, DEFVAL(""), DEFVAL(""));
|
|
|
|
ClassDB::bind_method(D_METHOD("set_button_text", "button_idx", "text"), &ButtonArray::set_button_text);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_button_tooltip", "button_idx", "text"), &ButtonArray::set_button_tooltip);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_button_icon", "button_idx", "icon:Texture"), &ButtonArray::set_button_icon);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_button_text", "button_idx"), &ButtonArray::get_button_text);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_button_tooltip", "button_idx"), &ButtonArray::get_button_tooltip);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_button_icon:Texture", "button_idx"), &ButtonArray::get_button_icon);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_button_count"), &ButtonArray::get_button_count);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_flat", "enabled"), &ButtonArray::set_flat);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_flat"), &ButtonArray::is_flat);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_selected"), &ButtonArray::get_selected);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_hovered"), &ButtonArray::get_hovered);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_selected", "button_idx"), &ButtonArray::set_selected);
|
|
|
|
ClassDB::bind_method(D_METHOD("erase_button", "button_idx"), &ButtonArray::erase_button);
|
|
|
|
ClassDB::bind_method(D_METHOD("clear"), &ButtonArray::clear);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("_gui_input"), &ButtonArray::_gui_input);
|
|
|
|
|
|
|
|
BIND_CONSTANT(ALIGN_BEGIN);
|
|
|
|
BIND_CONSTANT(ALIGN_CENTER);
|
|
|
|
BIND_CONSTANT(ALIGN_END);
|
|
|
|
BIND_CONSTANT(ALIGN_FILL);
|
|
|
|
BIND_CONSTANT(ALIGN_EXPAND_FILL);
|
|
|
|
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flat"), "set_flat", "is_flat");
|
|
|
|
|
|
|
|
ADD_SIGNAL(MethodInfo("button_selected", PropertyInfo(Variant::INT, "button_idx")));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ButtonArray::ButtonArray(Orientation p_orientation) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
orientation = p_orientation;
|
|
|
|
selected = -1;
|
2014-02-10 01:10:30 +00:00
|
|
|
set_focus_mode(FOCUS_ALL);
|
2017-03-05 15:44:50 +00:00
|
|
|
hover = -1;
|
|
|
|
flat = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
min_button_size = -1;
|
|
|
|
}
|