2023-01-05 12:25:55 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* link_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
|
|
|
|
2016-03-04 14:09:45 +00:00
|
|
|
#include "link_button.h"
|
2022-02-15 17:06:48 +00:00
|
|
|
|
2020-09-03 11:22:16 +00:00
|
|
|
#include "core/string/translation.h"
|
2023-09-08 19:00:10 +00:00
|
|
|
#include "scene/theme/theme_db.h"
|
2020-09-03 11:22:16 +00:00
|
|
|
|
|
|
|
void LinkButton::_shape() {
|
2022-08-31 12:02:40 +00:00
|
|
|
Ref<Font> font = theme_cache.font;
|
|
|
|
int font_size = theme_cache.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);
|
|
|
|
}
|
2021-08-27 00:45:35 +00:00
|
|
|
TS->shaped_text_set_bidi_override(text_buf->get_rid(), structured_text_parser(st_parser, st_args, xl_text));
|
2022-05-09 09:47:10 +00:00
|
|
|
text_buf->add_string(xl_text, font, font_size, language);
|
2020-09-03 11:22:16 +00:00
|
|
|
}
|
2016-03-04 14:09:45 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void LinkButton::set_text(const String &p_text) {
|
2021-08-27 00:45:35 +00:00
|
|
|
if (text == p_text) {
|
|
|
|
return;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
text = p_text;
|
2021-08-27 00:45:35 +00:00
|
|
|
xl_text = atr(text);
|
2020-09-03 11:22:16 +00:00
|
|
|
_shape();
|
2021-12-06 13:02:34 +00:00
|
|
|
update_minimum_size();
|
2022-08-13 21:21:24 +00:00
|
|
|
queue_redraw();
|
2016-03-04 14:09:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String LinkButton::get_text() const {
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2022-04-19 10:27:18 +00:00
|
|
|
void LinkButton::set_structured_text_bidi_override(TextServer::StructuredTextParser p_parser) {
|
2020-09-03 11:22:16 +00:00
|
|
|
if (st_parser != p_parser) {
|
|
|
|
st_parser = p_parser;
|
|
|
|
_shape();
|
2022-08-13 21:21:24 +00:00
|
|
|
queue_redraw();
|
2020-09-03 11:22:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-19 10:27:18 +00:00
|
|
|
TextServer::StructuredTextParser LinkButton::get_structured_text_bidi_override() const {
|
2020-09-03 11:22:16 +00:00
|
|
|
return st_parser;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LinkButton::set_structured_text_bidi_override_options(Array p_args) {
|
|
|
|
st_args = p_args;
|
|
|
|
_shape();
|
2022-08-13 21:21:24 +00:00
|
|
|
queue_redraw();
|
2020-09-03 11:22:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Array LinkButton::get_structured_text_bidi_override_options() const {
|
|
|
|
return st_args;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LinkButton::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();
|
2022-08-13 21:21:24 +00:00
|
|
|
queue_redraw();
|
2020-09-03 11:22:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Control::TextDirection LinkButton::get_text_direction() const {
|
|
|
|
return text_direction;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LinkButton::set_language(const String &p_language) {
|
|
|
|
if (language != p_language) {
|
|
|
|
language = p_language;
|
|
|
|
_shape();
|
2022-08-13 21:21:24 +00:00
|
|
|
queue_redraw();
|
2020-09-03 11:22:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String LinkButton::get_language() const {
|
|
|
|
return language;
|
|
|
|
}
|
|
|
|
|
2019-07-18 11:34:33 +00:00
|
|
|
void LinkButton::set_uri(const String &p_uri) {
|
|
|
|
uri = p_uri;
|
|
|
|
}
|
|
|
|
|
|
|
|
String LinkButton::get_uri() const {
|
|
|
|
return uri;
|
|
|
|
}
|
|
|
|
|
2016-03-04 14:09:45 +00:00
|
|
|
void LinkButton::set_underline_mode(UnderlineMode p_underline_mode) {
|
2022-03-16 07:50:48 +00:00
|
|
|
if (underline_mode == p_underline_mode) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
underline_mode = p_underline_mode;
|
2022-08-13 21:21:24 +00:00
|
|
|
queue_redraw();
|
2016-03-04 14:09:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LinkButton::UnderlineMode LinkButton::get_underline_mode() const {
|
|
|
|
return underline_mode;
|
|
|
|
}
|
|
|
|
|
2023-09-30 23:16:39 +00:00
|
|
|
Ref<Font> LinkButton::get_button_font() const {
|
|
|
|
return theme_cache.font;
|
|
|
|
}
|
|
|
|
|
2019-07-18 11:34:33 +00:00
|
|
|
void LinkButton::pressed() {
|
|
|
|
if (uri.is_empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
OS::get_singleton()->shell_open(uri);
|
|
|
|
}
|
|
|
|
|
2016-03-04 14:09:45 +00:00
|
|
|
Size2 LinkButton::get_minimum_size() const {
|
2020-09-03 11:22:16 +00:00
|
|
|
return text_buf->get_size();
|
2016-03-04 14:09:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LinkButton::_notification(int p_what) {
|
2017-03-05 15:44:50 +00:00
|
|
|
switch (p_what) {
|
2021-08-27 00:45:35 +00:00
|
|
|
case NOTIFICATION_TRANSLATION_CHANGED: {
|
|
|
|
xl_text = atr(text);
|
|
|
|
_shape();
|
2021-12-06 13:02:34 +00:00
|
|
|
update_minimum_size();
|
2022-08-13 21:21:24 +00:00
|
|
|
queue_redraw();
|
2021-08-27 00:45:35 +00:00
|
|
|
} break;
|
2022-02-15 17:06:48 +00:00
|
|
|
|
2020-09-03 11:22:16 +00:00
|
|
|
case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
|
2022-08-13 21:21:24 +00:00
|
|
|
queue_redraw();
|
2020-09-03 11:22:16 +00:00
|
|
|
} break;
|
2022-02-15 17:06:48 +00:00
|
|
|
|
2020-09-03 11:22:16 +00:00
|
|
|
case NOTIFICATION_THEME_CHANGED: {
|
|
|
|
_shape();
|
2021-12-06 13:02:34 +00:00
|
|
|
update_minimum_size();
|
2022-08-13 21:21:24 +00:00
|
|
|
queue_redraw();
|
2020-09-03 11:22:16 +00:00
|
|
|
} break;
|
2022-02-15 17:06:48 +00:00
|
|
|
|
2016-03-04 14:09:45 +00:00
|
|
|
case NOTIFICATION_DRAW: {
|
|
|
|
RID ci = get_canvas_item();
|
2017-03-05 15:44:50 +00:00
|
|
|
Size2 size = get_size();
|
2016-03-04 14:09:45 +00:00
|
|
|
Color color;
|
2017-03-05 15:44:50 +00:00
|
|
|
bool do_underline = false;
|
2016-03-04 14:09:45 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
switch (get_draw_mode()) {
|
2016-03-04 14:09:45 +00:00
|
|
|
case DRAW_NORMAL: {
|
2021-10-26 14:28:12 +00:00
|
|
|
if (has_focus()) {
|
2022-08-31 12:02:40 +00:00
|
|
|
color = theme_cache.font_focus_color;
|
2021-10-26 14:28:12 +00:00
|
|
|
} else {
|
2022-08-31 12:02:40 +00:00
|
|
|
color = theme_cache.font_color;
|
2021-10-26 14:28:12 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
do_underline = underline_mode == UNDERLINE_MODE_ALWAYS;
|
2016-03-04 14:09:45 +00:00
|
|
|
} break;
|
2018-10-05 23:06:32 +00:00
|
|
|
case DRAW_HOVER_PRESSED:
|
2016-03-04 14:09:45 +00:00
|
|
|
case DRAW_PRESSED: {
|
2021-07-17 21:22:52 +00:00
|
|
|
if (has_theme_color(SNAME("font_pressed_color"))) {
|
2022-08-31 12:02:40 +00:00
|
|
|
color = theme_cache.font_pressed_color;
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2022-08-31 12:02:40 +00:00
|
|
|
color = theme_cache.font_color;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-03-04 14:09:45 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
do_underline = underline_mode != UNDERLINE_MODE_NEVER;
|
2016-03-04 14:09:45 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
case DRAW_HOVER: {
|
2022-08-31 12:02:40 +00:00
|
|
|
color = theme_cache.font_hover_color;
|
2017-03-05 15:44:50 +00:00
|
|
|
do_underline = underline_mode != UNDERLINE_MODE_NEVER;
|
2016-03-04 14:09:45 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
case DRAW_DISABLED: {
|
2022-08-31 12:02:40 +00:00
|
|
|
color = theme_cache.font_disabled_color;
|
2017-03-05 15:44:50 +00:00
|
|
|
do_underline = underline_mode == UNDERLINE_MODE_ALWAYS;
|
2016-03-04 14:09:45 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (has_focus()) {
|
2022-08-31 12:02:40 +00:00
|
|
|
Ref<StyleBox> style = theme_cache.focus;
|
2017-03-05 15:44:50 +00:00
|
|
|
style->draw(ci, Rect2(Point2(), size));
|
2016-03-04 14:09:45 +00:00
|
|
|
}
|
|
|
|
|
2020-09-03 11:22:16 +00:00
|
|
|
int width = text_buf->get_line_width();
|
2016-03-04 14:09:45 +00:00
|
|
|
|
2022-08-31 12:02:40 +00:00
|
|
|
Color font_outline_color = theme_cache.font_outline_color;
|
|
|
|
int outline_size = theme_cache.outline_size;
|
2020-09-03 11:22:16 +00:00
|
|
|
if (is_layout_rtl()) {
|
2020-12-25 21:45:28 +00:00
|
|
|
if (outline_size > 0 && font_outline_color.a > 0) {
|
|
|
|
text_buf->draw_outline(get_canvas_item(), Vector2(size.width - width, 0), outline_size, font_outline_color);
|
|
|
|
}
|
2020-09-03 11:22:16 +00:00
|
|
|
text_buf->draw(get_canvas_item(), Vector2(size.width - width, 0), color);
|
|
|
|
} else {
|
2020-12-25 21:45:28 +00:00
|
|
|
if (outline_size > 0 && font_outline_color.a > 0) {
|
|
|
|
text_buf->draw_outline(get_canvas_item(), Vector2(0, 0), outline_size, font_outline_color);
|
|
|
|
}
|
2020-09-03 11:22:16 +00:00
|
|
|
text_buf->draw(get_canvas_item(), Vector2(0, 0), color);
|
|
|
|
}
|
2016-03-04 14:09:45 +00:00
|
|
|
|
|
|
|
if (do_underline) {
|
2022-08-31 12:02:40 +00:00
|
|
|
int underline_spacing = theme_cache.underline_spacing + text_buf->get_line_underline_position();
|
2020-09-03 11:22:16 +00:00
|
|
|
int y = text_buf->get_line_ascent() + underline_spacing;
|
2023-03-03 12:10:06 +00:00
|
|
|
int underline_thickness = MAX(1, text_buf->get_line_underline_thickness());
|
2016-03-04 14:09:45 +00:00
|
|
|
|
2020-09-03 11:22:16 +00:00
|
|
|
if (is_layout_rtl()) {
|
2023-03-03 12:10:06 +00:00
|
|
|
draw_line(Vector2(size.width - width, y), Vector2(size.width, y), color, underline_thickness);
|
2020-09-03 11:22:16 +00:00
|
|
|
} else {
|
2023-03-03 12:10:06 +00:00
|
|
|
draw_line(Vector2(0, y), Vector2(width, y), color, underline_thickness);
|
2020-09-03 11:22:16 +00:00
|
|
|
}
|
2016-03-04 14:09:45 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LinkButton::_bind_methods() {
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_text", "text"), &LinkButton::set_text);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_text"), &LinkButton::get_text);
|
2020-09-03 11:22:16 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &LinkButton::set_text_direction);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_text_direction"), &LinkButton::get_text_direction);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_language", "language"), &LinkButton::set_language);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_language"), &LinkButton::get_language);
|
2019-07-18 11:34:33 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_uri", "uri"), &LinkButton::set_uri);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_uri"), &LinkButton::get_uri);
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_underline_mode", "underline_mode"), &LinkButton::set_underline_mode);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_underline_mode"), &LinkButton::get_underline_mode);
|
2020-09-03 11:22:16 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override", "parser"), &LinkButton::set_structured_text_bidi_override);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override"), &LinkButton::get_structured_text_bidi_override);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override_options", "args"), &LinkButton::set_structured_text_bidi_override_options);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override_options"), &LinkButton::get_structured_text_bidi_override_options);
|
2016-03-04 14:09:45 +00:00
|
|
|
|
2017-08-20 15:45:01 +00:00
|
|
|
BIND_ENUM_CONSTANT(UNDERLINE_MODE_ALWAYS);
|
|
|
|
BIND_ENUM_CONSTANT(UNDERLINE_MODE_ON_HOVER);
|
|
|
|
BIND_ENUM_CONSTANT(UNDERLINE_MODE_NEVER);
|
2016-03-04 14:09:45 +00:00
|
|
|
|
2018-11-08 14:30:02 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "text"), "set_text", "get_text");
|
2022-05-09 09:47:10 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "underline", PROPERTY_HINT_ENUM, "Always,On Hover,Never"), "set_underline_mode", "get_underline_mode");
|
2019-07-18 11:34:33 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "uri"), "set_uri", "get_uri");
|
2022-05-09 09:47:10 +00:00
|
|
|
|
|
|
|
ADD_GROUP("BiDi", "");
|
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");
|
2021-09-23 11:08:50 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID, ""), "set_language", "get_language");
|
2020-09-03 11:22:16 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "structured_text_bidi_override", PROPERTY_HINT_ENUM, "Default,URI,File,Email,List,None,Custom"), "set_structured_text_bidi_override", "get_structured_text_bidi_override");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "structured_text_bidi_override_options"), "set_structured_text_bidi_override_options", "get_structured_text_bidi_override_options");
|
2023-09-08 19:00:10 +00:00
|
|
|
|
|
|
|
BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, LinkButton, focus);
|
|
|
|
|
|
|
|
BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LinkButton, font_color);
|
|
|
|
BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LinkButton, font_focus_color);
|
|
|
|
BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LinkButton, font_pressed_color);
|
|
|
|
BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LinkButton, font_hover_color);
|
|
|
|
BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LinkButton, font_hover_pressed_color);
|
|
|
|
BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LinkButton, font_disabled_color);
|
|
|
|
|
|
|
|
BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, LinkButton, font);
|
|
|
|
BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, LinkButton, font_size);
|
|
|
|
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, LinkButton, outline_size);
|
|
|
|
BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LinkButton, font_outline_color);
|
|
|
|
|
|
|
|
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, LinkButton, underline_spacing);
|
2016-03-04 14:09:45 +00:00
|
|
|
}
|
|
|
|
|
2022-03-04 08:18:44 +00:00
|
|
|
LinkButton::LinkButton(const String &p_text) {
|
2021-06-17 22:03:09 +00:00
|
|
|
text_buf.instantiate();
|
2020-11-30 04:28:45 +00:00
|
|
|
set_focus_mode(FOCUS_NONE);
|
2016-03-04 14:09:45 +00:00
|
|
|
set_default_cursor_shape(CURSOR_POINTING_HAND);
|
2022-03-04 08:18:44 +00:00
|
|
|
|
|
|
|
set_text(p_text);
|
2016-03-04 14:09:45 +00:00
|
|
|
}
|