2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* button.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2021-01-01 19:13:46 +00:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 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. */
|
|
|
|
/*************************************************************************/
|
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
|
|
|
|
2020-11-07 22:33:38 +00:00
|
|
|
#include "core/string/translation.h"
|
2020-03-27 18:21:27 +00:00
|
|
|
#include "servers/rendering_server.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Size2 Button::get_minimum_size() const {
|
2020-09-03 11:22:16 +00:00
|
|
|
Size2 minsize = text_buf->get_size();
|
2020-05-14 14:41:43 +00:00
|
|
|
if (clip_text) {
|
2017-03-05 15:44:50 +00:00
|
|
|
minsize.width = 0;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-08-20 16:41:14 +00:00
|
|
|
if (!expand_icon) {
|
2019-06-11 18:43:37 +00:00
|
|
|
Ref<Texture2D> _icon;
|
2021-07-17 21:22:52 +00:00
|
|
|
if (icon.is_null() && has_theme_icon(SNAME("icon"))) {
|
|
|
|
_icon = Control::get_theme_icon(SNAME("icon"));
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2019-08-20 16:41:14 +00:00
|
|
|
_icon = icon;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2019-08-20 16:41:14 +00:00
|
|
|
|
|
|
|
if (!_icon.is_null()) {
|
|
|
|
minsize.height = MAX(minsize.height, _icon->get_height());
|
2019-10-25 03:29:10 +00:00
|
|
|
|
|
|
|
if (icon_align != ALIGN_CENTER) {
|
|
|
|
minsize.width += _icon->get_width();
|
|
|
|
if (xl_text != "") {
|
2021-07-17 21:22:52 +00:00
|
|
|
minsize.width += get_theme_constant(SNAME("hseparation"));
|
2019-10-25 03:29:10 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
minsize.width = MAX(minsize.width, _icon->get_width());
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2019-08-20 16:41:14 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2021-07-17 21:22:52 +00:00
|
|
|
Ref<Font> font = get_theme_font(SNAME("font"));
|
|
|
|
float font_height = font->get_height(get_theme_font_size(SNAME("font_size")));
|
2021-02-22 13:54:12 +00:00
|
|
|
|
|
|
|
minsize.height = MAX(font_height, minsize.height);
|
|
|
|
|
2021-07-17 21:22:52 +00:00
|
|
|
return get_theme_stylebox(SNAME("normal"))->get_minimum_size() + minsize;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 16:24:29 +00:00
|
|
|
void Button::_set_internal_margin(Side p_side, float p_value) {
|
|
|
|
_internal_margin[p_side] = p_value;
|
2017-12-11 05:03:32 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2020-09-03 11:22:16 +00:00
|
|
|
case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
|
|
|
|
update();
|
|
|
|
} break;
|
2019-08-20 16:41:14 +00:00
|
|
|
case NOTIFICATION_TRANSLATION_CHANGED: {
|
|
|
|
xl_text = tr(text);
|
2020-09-03 11:22:16 +00:00
|
|
|
_shape();
|
|
|
|
|
|
|
|
minimum_size_changed();
|
|
|
|
update();
|
|
|
|
} break;
|
|
|
|
case NOTIFICATION_THEME_CHANGED: {
|
|
|
|
_shape();
|
|
|
|
|
2019-08-20 16:41:14 +00:00
|
|
|
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
|
|
|
|
2021-07-17 21:22:52 +00:00
|
|
|
Ref<StyleBox> style = get_theme_stylebox(SNAME("normal"));
|
2020-09-03 11:22:16 +00:00
|
|
|
bool rtl = is_layout_rtl();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-08-20 16:41:14 +00:00
|
|
|
switch (get_draw_mode()) {
|
|
|
|
case DRAW_NORMAL: {
|
2021-07-17 21:22:52 +00:00
|
|
|
if (rtl && has_theme_stylebox(SNAME("normal_mirrored"))) {
|
|
|
|
style = get_theme_stylebox(SNAME("normal_mirrored"));
|
2020-09-03 11:22:16 +00:00
|
|
|
} else {
|
2021-07-17 21:22:52 +00:00
|
|
|
style = get_theme_stylebox(SNAME("normal"));
|
2020-09-03 11:22:16 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!flat) {
|
2019-08-20 16:41:14 +00:00
|
|
|
style->draw(ci, Rect2(Point2(0, 0), size));
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2021-07-17 21:22:52 +00:00
|
|
|
color = get_theme_color(SNAME("font_color"));
|
|
|
|
if (has_theme_color(SNAME("icon_normal_color"))) {
|
|
|
|
color_icon = get_theme_color(SNAME("icon_normal_color"));
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2019-08-20 16:41:14 +00:00
|
|
|
} break;
|
|
|
|
case DRAW_HOVER_PRESSED: {
|
2021-07-17 21:22:52 +00:00
|
|
|
if (has_theme_stylebox(SNAME("hover_pressed")) && has_theme_stylebox_override("hover_pressed")) {
|
|
|
|
if (rtl && has_theme_stylebox(SNAME("hover_pressed_mirrored"))) {
|
|
|
|
style = get_theme_stylebox(SNAME("hover_pressed_mirrored"));
|
2020-09-03 11:22:16 +00:00
|
|
|
} else {
|
2021-07-17 21:22:52 +00:00
|
|
|
style = get_theme_stylebox(SNAME("hover_pressed"));
|
2020-09-03 11:22:16 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!flat) {
|
2019-08-20 16:41:14 +00:00
|
|
|
style->draw(ci, Rect2(Point2(0, 0), size));
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2021-07-17 21:22:52 +00:00
|
|
|
if (has_theme_color(SNAME("font_hover_pressed_color"))) {
|
|
|
|
color = get_theme_color(SNAME("font_hover_pressed_color"));
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2021-07-17 21:22:52 +00:00
|
|
|
color = get_theme_color(SNAME("font_color"));
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2021-07-17 21:22:52 +00:00
|
|
|
if (has_theme_color(SNAME("icon_hover_pressed_color"))) {
|
|
|
|
color_icon = get_theme_color(SNAME("icon_hover_pressed_color"));
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2019-08-20 16:41:14 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2020-02-22 19:47:50 +00:00
|
|
|
[[fallthrough]];
|
2019-08-20 16:41:14 +00:00
|
|
|
}
|
|
|
|
case DRAW_PRESSED: {
|
2021-07-17 21:22:52 +00:00
|
|
|
if (rtl && has_theme_stylebox(SNAME("pressed_mirrored"))) {
|
|
|
|
style = get_theme_stylebox(SNAME("pressed_mirrored"));
|
2020-09-03 11:22:16 +00:00
|
|
|
} else {
|
2021-07-17 21:22:52 +00:00
|
|
|
style = get_theme_stylebox(SNAME("pressed"));
|
2020-09-03 11:22:16 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!flat) {
|
2018-09-23 23:56:30 +00:00
|
|
|
style->draw(ci, Rect2(Point2(0, 0), size));
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2021-07-17 21:22:52 +00:00
|
|
|
if (has_theme_color(SNAME("font_pressed_color"))) {
|
|
|
|
color = get_theme_color(SNAME("font_pressed_color"));
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2021-07-17 21:22:52 +00:00
|
|
|
color = get_theme_color(SNAME("font_color"));
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2021-07-17 21:22:52 +00:00
|
|
|
if (has_theme_color(SNAME("icon_pressed_color"))) {
|
|
|
|
color_icon = get_theme_color(SNAME("icon_pressed_color"));
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-09-23 23:56:30 +00:00
|
|
|
|
2019-08-20 16:41:14 +00:00
|
|
|
} break;
|
|
|
|
case DRAW_HOVER: {
|
2021-07-17 21:22:52 +00:00
|
|
|
if (rtl && has_theme_stylebox(SNAME("hover_mirrored"))) {
|
|
|
|
style = get_theme_stylebox(SNAME("hover_mirrored"));
|
2020-09-03 11:22:16 +00:00
|
|
|
} else {
|
2021-07-17 21:22:52 +00:00
|
|
|
style = get_theme_stylebox(SNAME("hover"));
|
2020-09-03 11:22:16 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!flat) {
|
2019-08-20 16:41:14 +00:00
|
|
|
style->draw(ci, Rect2(Point2(0, 0), size));
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2021-07-17 21:22:52 +00:00
|
|
|
color = get_theme_color(SNAME("font_hover_color"));
|
|
|
|
if (has_theme_color(SNAME("icon_hover_color"))) {
|
|
|
|
color_icon = get_theme_color(SNAME("icon_hover_color"));
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2019-08-20 16:41:14 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
case DRAW_DISABLED: {
|
2021-07-17 21:22:52 +00:00
|
|
|
if (rtl && has_theme_stylebox(SNAME("disabled_mirrored"))) {
|
|
|
|
style = get_theme_stylebox(SNAME("disabled_mirrored"));
|
2020-09-03 11:22:16 +00:00
|
|
|
} else {
|
2021-07-17 21:22:52 +00:00
|
|
|
style = get_theme_stylebox(SNAME("disabled"));
|
2020-09-03 11:22:16 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!flat) {
|
2019-08-20 16:41:14 +00:00
|
|
|
style->draw(ci, Rect2(Point2(0, 0), size));
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2021-07-17 21:22:52 +00:00
|
|
|
color = get_theme_color(SNAME("font_disabled_color"));
|
|
|
|
if (has_theme_color(SNAME("icon_disabled_color"))) {
|
|
|
|
color_icon = get_theme_color(SNAME("icon_disabled_color"));
|
2020-05-14 14:41:43 +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()) {
|
2021-07-17 21:22:52 +00:00
|
|
|
Ref<StyleBox> style2 = get_theme_stylebox(SNAME("focus"));
|
2019-08-20 16:41:14 +00:00
|
|
|
style2->draw(ci, Rect2(Point2(), size));
|
|
|
|
}
|
2015-06-08 13:36:11 +00:00
|
|
|
|
2019-06-11 18:43:37 +00:00
|
|
|
Ref<Texture2D> _icon;
|
2021-07-17 21:22:52 +00:00
|
|
|
if (icon.is_null() && has_theme_icon(SNAME("icon"))) {
|
|
|
|
_icon = Control::get_theme_icon(SNAME("icon"));
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2019-08-20 16:41:14 +00:00
|
|
|
_icon = icon;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-08-20 16:41:14 +00:00
|
|
|
Rect2 icon_region = Rect2();
|
2019-10-25 03:29:10 +00:00
|
|
|
TextAlign icon_align_rtl_checked = icon_align;
|
|
|
|
TextAlign align_rtl_checked = align;
|
|
|
|
// Swap icon and text alignment sides if right-to-left layout is set.
|
|
|
|
if (rtl) {
|
|
|
|
if (icon_align == ALIGN_RIGHT) {
|
|
|
|
icon_align_rtl_checked = ALIGN_LEFT;
|
|
|
|
} else if (icon_align == ALIGN_LEFT) {
|
|
|
|
icon_align_rtl_checked = ALIGN_RIGHT;
|
|
|
|
}
|
|
|
|
if (align == ALIGN_RIGHT) {
|
|
|
|
align_rtl_checked = ALIGN_LEFT;
|
|
|
|
} else if (align == ALIGN_LEFT) {
|
|
|
|
align_rtl_checked = ALIGN_RIGHT;
|
|
|
|
}
|
|
|
|
}
|
2019-08-20 16:41:14 +00:00
|
|
|
if (!_icon.is_null()) {
|
|
|
|
int valign = size.height - style->get_minimum_size().y;
|
|
|
|
if (is_disabled()) {
|
|
|
|
color_icon.a = 0.4;
|
|
|
|
}
|
|
|
|
|
2021-02-09 17:24:36 +00:00
|
|
|
float icon_ofs_region = 0.0;
|
2019-10-25 03:29:10 +00:00
|
|
|
Point2 style_offset;
|
|
|
|
Size2 icon_size = _icon->get_size();
|
|
|
|
if (icon_align_rtl_checked == ALIGN_LEFT) {
|
|
|
|
style_offset.x = style->get_margin(SIDE_LEFT);
|
2020-12-22 16:24:29 +00:00
|
|
|
if (_internal_margin[SIDE_LEFT] > 0) {
|
2021-07-17 21:22:52 +00:00
|
|
|
icon_ofs_region = _internal_margin[SIDE_LEFT] + get_theme_constant(SNAME("hseparation"));
|
2020-09-03 11:22:16 +00:00
|
|
|
}
|
2019-10-25 03:29:10 +00:00
|
|
|
} else if (icon_align_rtl_checked == ALIGN_CENTER) {
|
|
|
|
style_offset.x = 0.0;
|
|
|
|
} else if (icon_align_rtl_checked == ALIGN_RIGHT) {
|
|
|
|
style_offset.x = -style->get_margin(SIDE_RIGHT);
|
|
|
|
if (_internal_margin[SIDE_RIGHT] > 0) {
|
2021-07-17 21:22:52 +00:00
|
|
|
icon_ofs_region = -_internal_margin[SIDE_RIGHT] - get_theme_constant(SNAME("hseparation"));
|
2019-10-25 03:29:10 +00:00
|
|
|
}
|
2019-08-29 02:12:22 +00:00
|
|
|
}
|
2019-10-25 03:29:10 +00:00
|
|
|
style_offset.y = style->get_margin(SIDE_TOP);
|
2019-08-20 16:41:14 +00:00
|
|
|
|
|
|
|
if (expand_icon) {
|
|
|
|
Size2 _size = get_size() - style->get_offset() * 2;
|
2021-07-17 21:22:52 +00:00
|
|
|
_size.width -= get_theme_constant(SNAME("hseparation")) + icon_ofs_region;
|
2019-10-25 03:29:10 +00:00
|
|
|
if (!clip_text && icon_align_rtl_checked != ALIGN_CENTER) {
|
2020-09-03 11:22:16 +00:00
|
|
|
_size.width -= text_buf->get_size().width;
|
2020-05-14 14:41:43 +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
|
|
|
}
|
|
|
|
|
2019-10-25 03:29:10 +00:00
|
|
|
icon_size = Size2(icon_width, icon_height);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (icon_align_rtl_checked == 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_rtl_checked == 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 {
|
2019-10-25 03:29:10 +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
|
|
|
|
2021-07-17 21:22:52 +00:00
|
|
|
Point2 icon_ofs = !_icon.is_null() ? Point2(icon_region.size.width + get_theme_constant(SNAME("hseparation")), 0) : Point2();
|
2019-10-25 03:29:10 +00:00
|
|
|
if (align_rtl_checked == ALIGN_CENTER && icon_align_rtl_checked == ALIGN_CENTER) {
|
|
|
|
icon_ofs.x = 0.0;
|
|
|
|
}
|
2019-08-20 16:41:14 +00:00
|
|
|
int text_clip = size.width - style->get_minimum_size().width - icon_ofs.width;
|
2020-09-03 11:22:16 +00:00
|
|
|
text_buf->set_width(clip_text ? text_clip : -1);
|
|
|
|
|
|
|
|
int text_width = clip_text ? MIN(text_clip, text_buf->get_size().x) : text_buf->get_size().x;
|
|
|
|
|
2020-12-22 16:24:29 +00:00
|
|
|
if (_internal_margin[SIDE_LEFT] > 0) {
|
2021-07-17 21:22:52 +00:00
|
|
|
text_clip -= _internal_margin[SIDE_LEFT] + get_theme_constant(SNAME("hseparation"));
|
2019-12-12 21:46:04 +00:00
|
|
|
}
|
2020-12-22 16:24:29 +00:00
|
|
|
if (_internal_margin[SIDE_RIGHT] > 0) {
|
2021-07-17 21:22:52 +00:00
|
|
|
text_clip -= _internal_margin[SIDE_RIGHT] + get_theme_constant(SNAME("hseparation"));
|
2019-12-12 21:46:04 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 16:24:29 +00:00
|
|
|
Point2 text_ofs = (size - style->get_minimum_size() - icon_ofs - text_buf->get_size() - Point2(_internal_margin[SIDE_RIGHT] - _internal_margin[SIDE_LEFT], 0)) / 2.0;
|
2019-08-20 16:41:14 +00:00
|
|
|
|
2019-10-25 03:29:10 +00:00
|
|
|
switch (align_rtl_checked) {
|
2019-08-20 16:41:14 +00:00
|
|
|
case ALIGN_LEFT: {
|
2019-10-25 03:29:10 +00:00
|
|
|
if (icon_align_rtl_checked != ALIGN_LEFT) {
|
|
|
|
icon_ofs.x = 0.0;
|
|
|
|
}
|
|
|
|
if (_internal_margin[SIDE_LEFT] > 0) {
|
2021-07-17 21:22:52 +00:00
|
|
|
text_ofs.x = style->get_margin(SIDE_LEFT) + icon_ofs.x + _internal_margin[SIDE_LEFT] + get_theme_constant(SNAME("hseparation"));
|
2019-08-20 16:41:14 +00:00
|
|
|
} else {
|
2019-10-25 03:29:10 +00:00
|
|
|
text_ofs.x = style->get_margin(SIDE_LEFT) + icon_ofs.x;
|
2019-08-20 16:41:14 +00:00
|
|
|
}
|
|
|
|
text_ofs.y += style->get_offset().y;
|
|
|
|
} break;
|
|
|
|
case ALIGN_CENTER: {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (text_ofs.x < 0) {
|
2019-08-20 16:41:14 +00:00
|
|
|
text_ofs.x = 0;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2019-10-25 03:29:10 +00:00
|
|
|
if (icon_align_rtl_checked == ALIGN_LEFT) {
|
|
|
|
text_ofs += icon_ofs;
|
|
|
|
}
|
2019-08-20 16:41:14 +00:00
|
|
|
text_ofs += style->get_offset();
|
|
|
|
} break;
|
|
|
|
case ALIGN_RIGHT: {
|
2019-10-25 03:29:10 +00:00
|
|
|
if (_internal_margin[SIDE_RIGHT] > 0) {
|
2021-07-17 21:22:52 +00:00
|
|
|
text_ofs.x = size.x - style->get_margin(SIDE_RIGHT) - text_width - _internal_margin[SIDE_RIGHT] - get_theme_constant(SNAME("hseparation"));
|
2019-08-20 16:41:14 +00:00
|
|
|
} else {
|
2019-10-25 03:29:10 +00:00
|
|
|
text_ofs.x = size.x - style->get_margin(SIDE_RIGHT) - text_width;
|
2019-08-20 16:41:14 +00:00
|
|
|
}
|
|
|
|
text_ofs.y += style->get_offset().y;
|
2019-10-25 03:29:10 +00:00
|
|
|
if (icon_align_rtl_checked == ALIGN_RIGHT) {
|
|
|
|
text_ofs.x -= icon_ofs.x;
|
|
|
|
}
|
2019-08-20 16:41:14 +00:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
2021-07-17 21:22:52 +00:00
|
|
|
Color font_outline_color = get_theme_color(SNAME("font_outline_color"));
|
|
|
|
int outline_size = get_theme_constant(SNAME("outline_size"));
|
2020-12-08 13:11:45 +00:00
|
|
|
if (outline_size > 0 && font_outline_color.a > 0) {
|
2021-01-19 08:50:43 +00:00
|
|
|
text_buf->draw_outline(ci, text_ofs, outline_size, font_outline_color);
|
2020-09-03 11:22:16 +00:00
|
|
|
}
|
|
|
|
|
2021-01-19 08:50:43 +00:00
|
|
|
text_buf->draw(ci, text_ofs, color);
|
2019-08-20 16:41:14 +00:00
|
|
|
} break;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-03 11:22:16 +00:00
|
|
|
void Button::_shape() {
|
2021-07-17 21:22:52 +00:00
|
|
|
Ref<Font> font = get_theme_font(SNAME("font"));
|
|
|
|
int font_size = get_theme_font_size(SNAME("font_size"));
|
2020-09-03 11:22:16 +00:00
|
|
|
|
|
|
|
text_buf->clear();
|
|
|
|
if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
|
|
|
|
text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
|
|
|
|
} else {
|
|
|
|
text_buf->set_direction((TextServer::Direction)text_direction);
|
|
|
|
}
|
|
|
|
text_buf->add_string(xl_text, font, font_size, opentype_features, (language != "") ? language : TranslationServer::get_singleton()->get_tool_locale());
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void Button::set_text(const String &p_text) {
|
2020-09-03 11:22:16 +00:00
|
|
|
if (text != p_text) {
|
|
|
|
text = p_text;
|
|
|
|
xl_text = tr(text);
|
|
|
|
_shape();
|
|
|
|
|
|
|
|
update();
|
|
|
|
minimum_size_changed();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
String Button::get_text() const {
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2020-09-03 11:22:16 +00:00
|
|
|
void Button::set_text_direction(Control::TextDirection p_text_direction) {
|
|
|
|
ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
|
|
|
|
if (text_direction != p_text_direction) {
|
|
|
|
text_direction = p_text_direction;
|
|
|
|
_shape();
|
|
|
|
update();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2020-09-03 11:22:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Control::TextDirection Button::get_text_direction() const {
|
|
|
|
return text_direction;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::clear_opentype_features() {
|
|
|
|
opentype_features.clear();
|
|
|
|
_shape();
|
2014-02-10 01:10:30 +00:00
|
|
|
update();
|
2020-09-03 11:22:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Button::set_opentype_feature(const String &p_name, int p_value) {
|
|
|
|
int32_t tag = TS->name_to_tag(p_name);
|
|
|
|
if (!opentype_features.has(tag) || (int)opentype_features[tag] != p_value) {
|
|
|
|
opentype_features[tag] = p_value;
|
|
|
|
_shape();
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int Button::get_opentype_feature(const String &p_name) const {
|
|
|
|
int32_t tag = TS->name_to_tag(p_name);
|
|
|
|
if (!opentype_features.has(tag)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return opentype_features[tag];
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::set_language(const String &p_language) {
|
|
|
|
if (language != p_language) {
|
|
|
|
language = p_language;
|
|
|
|
_shape();
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String Button::get_language() const {
|
|
|
|
return language;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::set_icon(const Ref<Texture2D> &p_icon) {
|
|
|
|
if (icon != p_icon) {
|
|
|
|
icon = p_icon;
|
|
|
|
update();
|
|
|
|
minimum_size_changed();
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 18:43:37 +00:00
|
|
|
Ref<Texture2D> Button::get_icon() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
return icon;
|
|
|
|
}
|
|
|
|
|
2019-08-20 16:41:14 +00:00
|
|
|
void Button::set_expand_icon(bool p_expand_icon) {
|
2020-09-03 11:22:16 +00:00
|
|
|
if (expand_icon != p_expand_icon) {
|
|
|
|
expand_icon = p_expand_icon;
|
|
|
|
update();
|
|
|
|
minimum_size_changed();
|
|
|
|
}
|
2019-08-20 16:41:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Button::is_expand_icon() const {
|
|
|
|
return expand_icon;
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void Button::set_flat(bool p_flat) {
|
2020-09-03 11:22:16 +00:00
|
|
|
if (flat != p_flat) {
|
|
|
|
flat = p_flat;
|
|
|
|
update();
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Button::is_flat() const {
|
|
|
|
return flat;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::set_clip_text(bool p_clip_text) {
|
2020-09-03 11:22:16 +00:00
|
|
|
if (clip_text != p_clip_text) {
|
|
|
|
clip_text = p_clip_text;
|
|
|
|
update();
|
|
|
|
minimum_size_changed();
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Button::get_clip_text() const {
|
|
|
|
return clip_text;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::set_text_align(TextAlign p_align) {
|
2020-09-03 11:22:16 +00:00
|
|
|
if (align != p_align) {
|
|
|
|
align = p_align;
|
|
|
|
update();
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Button::TextAlign Button::get_text_align() const {
|
|
|
|
return align;
|
|
|
|
}
|
|
|
|
|
2019-10-25 03:29:10 +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;
|
|
|
|
}
|
|
|
|
|
2020-09-03 11:22:16 +00:00
|
|
|
bool Button::_set(const StringName &p_name, const Variant &p_value) {
|
|
|
|
String str = p_name;
|
|
|
|
if (str.begins_with("opentype_features/")) {
|
|
|
|
String name = str.get_slicec('/', 1);
|
|
|
|
int32_t tag = TS->name_to_tag(name);
|
|
|
|
double value = p_value;
|
|
|
|
if (value == -1) {
|
|
|
|
if (opentype_features.has(tag)) {
|
|
|
|
opentype_features.erase(tag);
|
|
|
|
_shape();
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ((double)opentype_features[tag] != value) {
|
|
|
|
opentype_features[tag] = value;
|
|
|
|
_shape();
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
2021-02-10 20:18:45 +00:00
|
|
|
notify_property_list_changed();
|
2020-09-03 11:22:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Button::_get(const StringName &p_name, Variant &r_ret) const {
|
|
|
|
String str = p_name;
|
|
|
|
if (str.begins_with("opentype_features/")) {
|
|
|
|
String name = str.get_slicec('/', 1);
|
|
|
|
int32_t tag = TS->name_to_tag(name);
|
|
|
|
if (opentype_features.has(tag)) {
|
|
|
|
r_ret = opentype_features[tag];
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
r_ret = -1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::_get_property_list(List<PropertyInfo> *p_list) const {
|
|
|
|
for (const Variant *ftr = opentype_features.next(nullptr); ftr != nullptr; ftr = opentype_features.next(ftr)) {
|
|
|
|
String name = TS->tag_to_name(*ftr);
|
|
|
|
p_list->push_back(PropertyInfo(Variant::FLOAT, "opentype_features/" + name));
|
|
|
|
}
|
|
|
|
p_list->push_back(PropertyInfo(Variant::NIL, "opentype_features/_new", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR));
|
|
|
|
}
|
|
|
|
|
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);
|
2020-09-03 11:22:16 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &Button::set_text_direction);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_text_direction"), &Button::get_text_direction);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_opentype_feature", "tag", "value"), &Button::set_opentype_feature);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_opentype_feature", "tag"), &Button::get_opentype_feature);
|
|
|
|
ClassDB::bind_method(D_METHOD("clear_opentype_features"), &Button::clear_opentype_features);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_language", "language"), &Button::set_language);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_language"), &Button::get_language);
|
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);
|
2019-10-25 03:29:10 +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);
|
2019-10-25 03:29:10 +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);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_expand_icon"), &Button::set_expand_icon);
|
|
|
|
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
|
|
|
|
2020-09-03 11:22:16 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_MULTILINE_TEXT, "", PROPERTY_USAGE_DEFAULT_INTL), "set_text", "get_text");
|
2021-05-22 02:30:58 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_text_direction", "get_text_direction");
|
2020-09-03 11:22:16 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "language"), "set_language", "get_language");
|
2019-06-11 18:43:37 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "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");
|
2019-10-25 03:29:10 +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) {
|
2021-06-17 22:03:09 +00:00
|
|
|
text_buf.instantiate();
|
2020-09-03 11:22:16 +00:00
|
|
|
text_buf->set_flags(TextServer::BREAK_MANDATORY);
|
|
|
|
|
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
|
|
|
Button::~Button() {
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|