2023-01-10 14:26:54 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* 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-04 23:50:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "button.h"
|
2019-04-05 12:06:16 +00:00
|
|
|
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/translation.h"
|
2017-03-05 15:44:50 +00:00
|
|
|
#include "servers/visual_server.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Size2 Button::get_minimum_size() const {
|
2017-03-05 15:44:50 +00:00
|
|
|
Size2 minsize = get_font("font")->get_string_size(xl_text);
|
2021-05-05 10:44:11 +00:00
|
|
|
if (clip_text) {
|
2017-03-05 15:44:50 +00:00
|
|
|
minsize.width = 0;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-08-20 16:41:14 +00:00
|
|
|
if (!expand_icon) {
|
|
|
|
Ref<Texture> _icon;
|
2021-05-05 10:44:11 +00:00
|
|
|
if (icon.is_null() && has_icon("icon")) {
|
2019-08-20 16:41:14 +00:00
|
|
|
_icon = Control::get_icon("icon");
|
2021-05-05 10:44:11 +00:00
|
|
|
} else {
|
2019-08-20 16:41:14 +00:00
|
|
|
_icon = icon;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2019-08-20 16:41:14 +00:00
|
|
|
|
|
|
|
if (!_icon.is_null()) {
|
|
|
|
minsize.height = MAX(minsize.height, _icon->get_height());
|
2022-02-07 18:56:27 +00:00
|
|
|
|
|
|
|
if (icon_align != ALIGN_CENTER) {
|
|
|
|
minsize.width += _icon->get_width();
|
|
|
|
if (xl_text != "") {
|
|
|
|
minsize.width += get_constant("hseparation");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
minsize.width = MAX(minsize.width, _icon->get_width());
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2019-08-20 16:41:14 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return get_stylebox("normal")->get_minimum_size() + minsize;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-12-11 05:03:32 +00:00
|
|
|
void Button::_set_internal_margin(Margin p_margin, float p_value) {
|
|
|
|
_internal_margin[p_margin] = p_value;
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void Button::_notification(int p_what) {
|
2019-08-20 16:41:14 +00:00
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_TRANSLATION_CHANGED: {
|
|
|
|
xl_text = tr(text);
|
|
|
|
minimum_size_changed();
|
|
|
|
update();
|
|
|
|
} break;
|
|
|
|
case NOTIFICATION_DRAW: {
|
|
|
|
RID ci = get_canvas_item();
|
|
|
|
Size2 size = get_size();
|
|
|
|
Color color;
|
|
|
|
Color color_icon(1, 1, 1, 1);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2019-08-20 16:41:14 +00:00
|
|
|
Ref<StyleBox> style = get_stylebox("normal");
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-08-20 16:41:14 +00:00
|
|
|
switch (get_draw_mode()) {
|
|
|
|
case DRAW_NORMAL: {
|
|
|
|
style = get_stylebox("normal");
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!flat) {
|
2019-08-20 16:41:14 +00:00
|
|
|
style->draw(ci, Rect2(Point2(0, 0), size));
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2021-10-26 13:32:15 +00:00
|
|
|
|
|
|
|
// Focus colors only take precedence over normal state.
|
|
|
|
if (has_focus()) {
|
|
|
|
color = get_color("font_color_focus");
|
|
|
|
if (has_color("icon_color_focus")) {
|
|
|
|
color_icon = get_color("icon_color_focus");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
color = get_color("font_color");
|
|
|
|
if (has_color("icon_color_normal")) {
|
|
|
|
color_icon = get_color("icon_color_normal");
|
|
|
|
}
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2019-08-20 16:41:14 +00:00
|
|
|
} break;
|
|
|
|
case DRAW_HOVER_PRESSED: {
|
|
|
|
if (has_stylebox("hover_pressed") && has_stylebox_override("hover_pressed")) {
|
|
|
|
style = get_stylebox("hover_pressed");
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!flat) {
|
2019-08-20 16:41:14 +00:00
|
|
|
style->draw(ci, Rect2(Point2(0, 0), size));
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
|
|
|
if (has_color("font_color_hover_pressed")) {
|
2019-08-20 16:41:14 +00:00
|
|
|
color = get_color("font_color_hover_pressed");
|
2021-05-05 10:44:11 +00:00
|
|
|
} else {
|
2019-08-20 16:41:14 +00:00
|
|
|
color = get_color("font_color");
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
|
|
|
if (has_color("icon_color_hover_pressed")) {
|
2019-08-20 16:41:14 +00:00
|
|
|
color_icon = get_color("icon_color_hover_pressed");
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2019-08-20 16:41:14 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
FALLTHROUGH;
|
|
|
|
}
|
|
|
|
case DRAW_PRESSED: {
|
|
|
|
style = get_stylebox("pressed");
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!flat) {
|
2018-09-23 23:56:30 +00:00
|
|
|
style->draw(ci, Rect2(Point2(0, 0), size));
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
|
|
|
if (has_color("font_color_pressed")) {
|
2019-08-20 16:41:14 +00:00
|
|
|
color = get_color("font_color_pressed");
|
2021-05-05 10:44:11 +00:00
|
|
|
} else {
|
2018-09-23 23:56:30 +00:00
|
|
|
color = get_color("font_color");
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
|
|
|
if (has_color("icon_color_pressed")) {
|
2019-08-20 16:41:14 +00:00
|
|
|
color_icon = get_color("icon_color_pressed");
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2018-09-23 23:56:30 +00:00
|
|
|
|
2019-08-20 16:41:14 +00:00
|
|
|
} break;
|
|
|
|
case DRAW_HOVER: {
|
|
|
|
style = get_stylebox("hover");
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!flat) {
|
2019-08-20 16:41:14 +00:00
|
|
|
style->draw(ci, Rect2(Point2(0, 0), size));
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2019-08-20 16:41:14 +00:00
|
|
|
color = get_color("font_color_hover");
|
2021-05-05 10:44:11 +00:00
|
|
|
if (has_color("icon_color_hover")) {
|
2019-08-20 16:41:14 +00:00
|
|
|
color_icon = get_color("icon_color_hover");
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2019-08-20 16:41:14 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
case DRAW_DISABLED: {
|
|
|
|
style = get_stylebox("disabled");
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!flat) {
|
2019-08-20 16:41:14 +00:00
|
|
|
style->draw(ci, Rect2(Point2(0, 0), size));
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2019-08-20 16:41:14 +00:00
|
|
|
color = get_color("font_color_disabled");
|
2021-05-05 10:44:11 +00:00
|
|
|
if (has_color("icon_color_disabled")) {
|
2019-08-20 16:41:14 +00:00
|
|
|
color_icon = get_color("icon_color_disabled");
|
2022-01-30 18:15:05 +00:00
|
|
|
} else {
|
|
|
|
color_icon.a = 0.4;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2019-08-20 16:41:14 +00:00
|
|
|
} break;
|
2018-09-23 23:56:30 +00:00
|
|
|
}
|
2015-06-08 13:36:11 +00:00
|
|
|
|
2019-08-20 16:41:14 +00:00
|
|
|
if (has_focus()) {
|
|
|
|
Ref<StyleBox> style2 = get_stylebox("focus");
|
|
|
|
style2->draw(ci, Rect2(Point2(), size));
|
|
|
|
}
|
2015-06-08 13:36:11 +00:00
|
|
|
|
2019-08-20 16:41:14 +00:00
|
|
|
Ref<Font> font = get_font("font");
|
|
|
|
Ref<Texture> _icon;
|
2021-05-05 10:44:11 +00:00
|
|
|
if (icon.is_null() && has_icon("icon")) {
|
2019-08-20 16:41:14 +00:00
|
|
|
_icon = Control::get_icon("icon");
|
2021-05-05 10:44:11 +00:00
|
|
|
} else {
|
2019-08-20 16:41:14 +00:00
|
|
|
_icon = icon;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-08-20 16:41:14 +00:00
|
|
|
Rect2 icon_region = Rect2();
|
|
|
|
if (!_icon.is_null()) {
|
|
|
|
int valign = size.height - style->get_minimum_size().y;
|
|
|
|
|
|
|
|
float icon_ofs_region = 0;
|
2022-02-07 18:56:27 +00:00
|
|
|
Point2 style_offset;
|
|
|
|
Size2 icon_size = _icon->get_size();
|
|
|
|
if (icon_align == ALIGN_LEFT) {
|
|
|
|
style_offset.x = style->get_margin(MARGIN_LEFT);
|
|
|
|
if (_internal_margin[MARGIN_LEFT] > 0) {
|
|
|
|
icon_ofs_region = _internal_margin[MARGIN_LEFT] + get_constant("hseparation");
|
|
|
|
}
|
|
|
|
} else if (icon_align == ALIGN_CENTER) {
|
|
|
|
style_offset.x = 0;
|
|
|
|
} else if (icon_align == ALIGN_RIGHT) {
|
|
|
|
style_offset.x = -style->get_margin(MARGIN_RIGHT);
|
|
|
|
if (_internal_margin[MARGIN_RIGHT] > 0) {
|
|
|
|
icon_ofs_region = -_internal_margin[MARGIN_RIGHT] - get_constant("hseparation");
|
|
|
|
}
|
2019-08-29 02:12:22 +00:00
|
|
|
}
|
2022-02-07 18:56:27 +00:00
|
|
|
style_offset.y = style->get_margin(MARGIN_TOP);
|
2019-08-20 16:41:14 +00:00
|
|
|
|
|
|
|
if (expand_icon) {
|
|
|
|
Size2 _size = get_size() - style->get_offset() * 2;
|
2022-05-10 20:27:48 +00:00
|
|
|
int icon_text_separation = text.empty() ? 0 : get_constant("h_separation");
|
|
|
|
_size.width -= icon_text_separation + icon_ofs_region;
|
2022-02-07 18:56:27 +00:00
|
|
|
if (!clip_text && icon_align != ALIGN_CENTER) {
|
2019-08-20 16:41:14 +00:00
|
|
|
_size.width -= get_font("font")->get_string_size(xl_text).width;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2019-11-09 19:14:52 +00:00
|
|
|
float icon_width = _icon->get_width() * _size.height / _icon->get_height();
|
2019-08-20 16:41:14 +00:00
|
|
|
float icon_height = _size.height;
|
|
|
|
|
|
|
|
if (icon_width > _size.width) {
|
|
|
|
icon_width = _size.width;
|
2019-11-09 22:06:46 +00:00
|
|
|
icon_height = _icon->get_height() * icon_width / _icon->get_width();
|
2019-08-20 16:41:14 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 18:56:27 +00:00
|
|
|
icon_size = Size2(icon_width, icon_height);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (icon_align == ALIGN_LEFT) {
|
|
|
|
icon_region = Rect2(style_offset + Point2(icon_ofs_region, Math::floor((valign - icon_size.y) * 0.5)), icon_size);
|
|
|
|
} else if (icon_align == ALIGN_CENTER) {
|
|
|
|
icon_region = Rect2(style_offset + Point2(icon_ofs_region + Math::floor((size.x - icon_size.x) * 0.5), Math::floor((valign - icon_size.y) * 0.5)), icon_size);
|
2017-12-11 06:37:29 +00:00
|
|
|
} else {
|
2022-02-07 18:56:27 +00:00
|
|
|
icon_region = Rect2(style_offset + Point2(icon_ofs_region + size.x - icon_size.x, Math::floor((valign - icon_size.y) * 0.5)), icon_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (icon_region.size.width > 0) {
|
|
|
|
draw_texture_rect_region(_icon, icon_region, Rect2(Point2(), _icon->get_size()), color_icon);
|
2017-12-11 06:37:29 +00:00
|
|
|
}
|
2019-08-20 16:41:14 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-08-20 16:41:14 +00:00
|
|
|
Point2 icon_ofs = !_icon.is_null() ? Point2(icon_region.size.width + get_constant("hseparation"), 0) : Point2();
|
2022-02-07 18:56:27 +00:00
|
|
|
if (align == ALIGN_CENTER && icon_align == ALIGN_CENTER) {
|
|
|
|
icon_ofs.x = 0;
|
|
|
|
}
|
2019-08-20 16:41:14 +00:00
|
|
|
int text_clip = size.width - style->get_minimum_size().width - icon_ofs.width;
|
2019-12-12 21:46:04 +00:00
|
|
|
if (_internal_margin[MARGIN_LEFT] > 0) {
|
|
|
|
text_clip -= _internal_margin[MARGIN_LEFT] + get_constant("hseparation");
|
|
|
|
}
|
|
|
|
if (_internal_margin[MARGIN_RIGHT] > 0) {
|
|
|
|
text_clip -= _internal_margin[MARGIN_RIGHT] + get_constant("hseparation");
|
|
|
|
}
|
|
|
|
|
2019-08-20 16:41:14 +00:00
|
|
|
Point2 text_ofs = (size - style->get_minimum_size() - icon_ofs - font->get_string_size(xl_text) - Point2(_internal_margin[MARGIN_RIGHT] - _internal_margin[MARGIN_LEFT], 0)) / 2.0;
|
|
|
|
|
|
|
|
switch (align) {
|
|
|
|
case ALIGN_LEFT: {
|
2022-02-07 18:56:27 +00:00
|
|
|
if (icon_align != ALIGN_LEFT) {
|
|
|
|
icon_ofs.x = 0;
|
|
|
|
}
|
2019-08-20 16:41:14 +00:00
|
|
|
if (_internal_margin[MARGIN_LEFT] > 0) {
|
|
|
|
text_ofs.x = style->get_margin(MARGIN_LEFT) + icon_ofs.x + _internal_margin[MARGIN_LEFT] + get_constant("hseparation");
|
|
|
|
} else {
|
|
|
|
text_ofs.x = style->get_margin(MARGIN_LEFT) + icon_ofs.x;
|
|
|
|
}
|
|
|
|
text_ofs.y += style->get_offset().y;
|
|
|
|
} break;
|
|
|
|
case ALIGN_CENTER: {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (text_ofs.x < 0) {
|
2019-08-20 16:41:14 +00:00
|
|
|
text_ofs.x = 0;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2022-02-07 18:56:27 +00:00
|
|
|
if (icon_align == ALIGN_LEFT) {
|
|
|
|
text_ofs += icon_ofs;
|
|
|
|
}
|
2019-08-20 16:41:14 +00:00
|
|
|
text_ofs += style->get_offset();
|
|
|
|
} break;
|
|
|
|
case ALIGN_RIGHT: {
|
2022-02-07 18:56:27 +00:00
|
|
|
int text_width = font->get_string_size(xl_text).x;
|
2019-08-20 16:41:14 +00:00
|
|
|
if (_internal_margin[MARGIN_RIGHT] > 0) {
|
2022-02-07 18:56:27 +00:00
|
|
|
text_ofs.x = size.x - style->get_margin(MARGIN_RIGHT) - text_width - _internal_margin[MARGIN_RIGHT] - get_constant("hseparation");
|
2019-08-20 16:41:14 +00:00
|
|
|
} else {
|
2022-02-07 18:56:27 +00:00
|
|
|
text_ofs.x = size.x - style->get_margin(MARGIN_RIGHT) - text_width;
|
2019-08-20 16:41:14 +00:00
|
|
|
}
|
|
|
|
text_ofs.y += style->get_offset().y;
|
2022-02-07 18:56:27 +00:00
|
|
|
if (icon_align == ALIGN_RIGHT) {
|
|
|
|
text_ofs.x -= icon_ofs.x;
|
|
|
|
}
|
2019-08-20 16:41:14 +00:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
|
|
|
text_ofs.y += font->get_ascent();
|
|
|
|
font->draw(ci, text_ofs.floor(), xl_text, color, clip_text ? text_clip : -1);
|
|
|
|
} break;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void Button::set_text(const String &p_text) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (text == p_text) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
text = p_text;
|
2017-08-18 20:29:15 +00:00
|
|
|
xl_text = tr(p_text);
|
2014-02-10 01:10:30 +00:00
|
|
|
update();
|
|
|
|
_change_notify("text");
|
|
|
|
minimum_size_changed();
|
|
|
|
}
|
|
|
|
String Button::get_text() const {
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void Button::set_icon(const Ref<Texture> &p_icon) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (icon == p_icon) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
icon = p_icon;
|
2014-02-10 01:10:30 +00:00
|
|
|
update();
|
|
|
|
_change_notify("icon");
|
|
|
|
minimum_size_changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<Texture> Button::get_icon() const {
|
|
|
|
return icon;
|
|
|
|
}
|
|
|
|
|
2019-08-20 16:41:14 +00:00
|
|
|
void Button::set_expand_icon(bool p_expand_icon) {
|
|
|
|
expand_icon = p_expand_icon;
|
|
|
|
update();
|
|
|
|
minimum_size_changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Button::is_expand_icon() const {
|
|
|
|
return expand_icon;
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void Button::set_flat(bool p_flat) {
|
2017-03-05 15:44:50 +00:00
|
|
|
flat = p_flat;
|
2014-02-10 01:10:30 +00:00
|
|
|
update();
|
|
|
|
_change_notify("flat");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Button::is_flat() const {
|
|
|
|
return flat;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::set_clip_text(bool p_clip_text) {
|
2017-03-05 15:44:50 +00:00
|
|
|
clip_text = p_clip_text;
|
2014-02-10 01:10:30 +00:00
|
|
|
update();
|
|
|
|
minimum_size_changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Button::get_clip_text() const {
|
|
|
|
return clip_text;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::set_text_align(TextAlign p_align) {
|
2017-03-05 15:44:50 +00:00
|
|
|
align = p_align;
|
2014-02-10 01:10:30 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
Button::TextAlign Button::get_text_align() const {
|
|
|
|
return align;
|
|
|
|
}
|
|
|
|
|
2022-02-07 18:56:27 +00:00
|
|
|
void Button::set_icon_align(TextAlign p_align) {
|
|
|
|
icon_align = p_align;
|
|
|
|
minimum_size_changed();
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
Button::TextAlign Button::get_icon_align() const {
|
|
|
|
return icon_align;
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void Button::_bind_methods() {
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_text", "text"), &Button::set_text);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_text"), &Button::get_text);
|
2017-08-09 11:19:41 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_button_icon", "texture"), &Button::set_icon);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_button_icon"), &Button::get_icon);
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_flat", "enabled"), &Button::set_flat);
|
2022-02-07 18:56:27 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("is_flat"), &Button::is_flat);
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_clip_text", "enabled"), &Button::set_clip_text);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_clip_text"), &Button::get_clip_text);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_text_align", "align"), &Button::set_text_align);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_text_align"), &Button::get_text_align);
|
2022-02-07 18:56:27 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_icon_align", "icon_align"), &Button::set_icon_align);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_icon_align"), &Button::get_icon_align);
|
2022-04-04 10:56:59 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_expand_icon", "enabled"), &Button::set_expand_icon);
|
2022-02-07 18:56:27 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("is_expand_icon"), &Button::is_expand_icon);
|
2017-03-05 15:44:50 +00:00
|
|
|
|
2017-08-20 15:45:01 +00:00
|
|
|
BIND_ENUM_CONSTANT(ALIGN_LEFT);
|
|
|
|
BIND_ENUM_CONSTANT(ALIGN_CENTER);
|
|
|
|
BIND_ENUM_CONSTANT(ALIGN_RIGHT);
|
2017-03-05 15:44:50 +00:00
|
|
|
|
2018-11-08 14:30:02 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT_INTL), "set_text", "get_text");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_button_icon", "get_button_icon");
|
2017-03-05 15:44:50 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flat"), "set_flat", "is_flat");
|
2018-11-08 14:30:02 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clip_text"), "set_clip_text", "get_clip_text");
|
2017-12-11 06:53:30 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "align", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_text_align", "get_text_align");
|
2022-02-07 18:56:27 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "icon_align", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_icon_align", "get_icon_align");
|
2019-08-20 16:41:14 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "expand_icon"), "set_expand_icon", "is_expand_icon");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Button::Button(const String &p_text) {
|
2017-03-05 15:44:50 +00:00
|
|
|
flat = false;
|
|
|
|
clip_text = false;
|
2019-08-20 16:41:14 +00:00
|
|
|
expand_icon = false;
|
2017-01-08 22:54:19 +00:00
|
|
|
set_mouse_filter(MOUSE_FILTER_STOP);
|
2014-02-10 01:10:30 +00:00
|
|
|
set_text(p_text);
|
2017-03-05 15:44:50 +00:00
|
|
|
align = ALIGN_CENTER;
|
2022-02-07 18:56:27 +00:00
|
|
|
icon_align = ALIGN_LEFT;
|
2017-12-11 05:03:32 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
_internal_margin[i] = 0;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Button::~Button() {
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|