2
0

link_button.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /**************************************************************************/
  2. /* link_button.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "link_button.h"
  31. #include "scene/theme/theme_db.h"
  32. void LinkButton::_shape() {
  33. Ref<Font> font = theme_cache.font;
  34. int font_size = theme_cache.font_size;
  35. text_buf->clear();
  36. if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
  37. text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  38. } else {
  39. text_buf->set_direction((TextServer::Direction)text_direction);
  40. }
  41. TS->shaped_text_set_bidi_override(text_buf->get_rid(), structured_text_parser(st_parser, st_args, xl_text));
  42. text_buf->add_string(xl_text, font, font_size, language);
  43. queue_accessibility_update();
  44. }
  45. void LinkButton::set_text(const String &p_text) {
  46. if (text == p_text) {
  47. return;
  48. }
  49. text = p_text;
  50. xl_text = atr(text);
  51. _shape();
  52. update_minimum_size();
  53. queue_redraw();
  54. }
  55. String LinkButton::get_text() const {
  56. return text;
  57. }
  58. void LinkButton::set_structured_text_bidi_override(TextServer::StructuredTextParser p_parser) {
  59. if (st_parser != p_parser) {
  60. st_parser = p_parser;
  61. _shape();
  62. queue_redraw();
  63. }
  64. }
  65. TextServer::StructuredTextParser LinkButton::get_structured_text_bidi_override() const {
  66. return st_parser;
  67. }
  68. void LinkButton::set_structured_text_bidi_override_options(Array p_args) {
  69. st_args = p_args;
  70. _shape();
  71. queue_redraw();
  72. }
  73. Array LinkButton::get_structured_text_bidi_override_options() const {
  74. return st_args;
  75. }
  76. void LinkButton::set_text_direction(Control::TextDirection p_text_direction) {
  77. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  78. if (text_direction != p_text_direction) {
  79. text_direction = p_text_direction;
  80. _shape();
  81. queue_redraw();
  82. }
  83. }
  84. Control::TextDirection LinkButton::get_text_direction() const {
  85. return text_direction;
  86. }
  87. void LinkButton::set_language(const String &p_language) {
  88. if (language != p_language) {
  89. language = p_language;
  90. _shape();
  91. queue_redraw();
  92. }
  93. }
  94. String LinkButton::get_language() const {
  95. return language;
  96. }
  97. void LinkButton::set_uri(const String &p_uri) {
  98. if (uri != p_uri) {
  99. uri = p_uri;
  100. queue_accessibility_update();
  101. }
  102. }
  103. String LinkButton::get_uri() const {
  104. return uri;
  105. }
  106. void LinkButton::set_underline_mode(UnderlineMode p_underline_mode) {
  107. if (underline_mode == p_underline_mode) {
  108. return;
  109. }
  110. underline_mode = p_underline_mode;
  111. queue_redraw();
  112. }
  113. LinkButton::UnderlineMode LinkButton::get_underline_mode() const {
  114. return underline_mode;
  115. }
  116. Ref<Font> LinkButton::get_button_font() const {
  117. return theme_cache.font;
  118. }
  119. void LinkButton::pressed() {
  120. if (uri.is_empty()) {
  121. return;
  122. }
  123. OS::get_singleton()->shell_open(uri);
  124. }
  125. Size2 LinkButton::get_minimum_size() const {
  126. return text_buf->get_size();
  127. }
  128. void LinkButton::_notification(int p_what) {
  129. switch (p_what) {
  130. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  131. RID ae = get_accessibility_element();
  132. ERR_FAIL_COND(ae.is_null());
  133. DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_LINK);
  134. const String &ac_name = get_accessibility_name();
  135. if (!xl_text.is_empty() && ac_name.is_empty()) {
  136. DisplayServer::get_singleton()->accessibility_update_set_name(ae, xl_text);
  137. } else if (xl_text.is_empty() && ac_name.is_empty() && !get_tooltip_text().is_empty()) {
  138. DisplayServer::get_singleton()->accessibility_update_set_name(ae, get_tooltip_text()); // Fall back to tooltip.
  139. }
  140. DisplayServer::get_singleton()->accessibility_update_set_url(ae, uri);
  141. } break;
  142. case NOTIFICATION_TRANSLATION_CHANGED: {
  143. xl_text = atr(text);
  144. _shape();
  145. update_minimum_size();
  146. queue_redraw();
  147. } break;
  148. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  149. queue_redraw();
  150. } break;
  151. case NOTIFICATION_THEME_CHANGED: {
  152. _shape();
  153. update_minimum_size();
  154. queue_redraw();
  155. } break;
  156. case NOTIFICATION_DRAW: {
  157. RID ci = get_canvas_item();
  158. Size2 size = get_size();
  159. Color color;
  160. bool do_underline = false;
  161. switch (get_draw_mode()) {
  162. case DRAW_NORMAL: {
  163. if (has_focus()) {
  164. color = theme_cache.font_focus_color;
  165. } else {
  166. color = theme_cache.font_color;
  167. }
  168. do_underline = underline_mode == UNDERLINE_MODE_ALWAYS;
  169. } break;
  170. case DRAW_HOVER_PRESSED:
  171. case DRAW_PRESSED: {
  172. if (has_theme_color(SNAME("font_pressed_color"))) {
  173. color = theme_cache.font_pressed_color;
  174. } else {
  175. color = theme_cache.font_color;
  176. }
  177. do_underline = underline_mode != UNDERLINE_MODE_NEVER;
  178. } break;
  179. case DRAW_HOVER: {
  180. color = theme_cache.font_hover_color;
  181. do_underline = underline_mode != UNDERLINE_MODE_NEVER;
  182. } break;
  183. case DRAW_DISABLED: {
  184. color = theme_cache.font_disabled_color;
  185. do_underline = underline_mode == UNDERLINE_MODE_ALWAYS;
  186. } break;
  187. }
  188. if (has_focus()) {
  189. Ref<StyleBox> style = theme_cache.focus;
  190. style->draw(ci, Rect2(Point2(), size));
  191. }
  192. int width = text_buf->get_line_width();
  193. Color font_outline_color = theme_cache.font_outline_color;
  194. int outline_size = theme_cache.outline_size;
  195. if (is_layout_rtl()) {
  196. if (outline_size > 0 && font_outline_color.a > 0) {
  197. text_buf->draw_outline(get_canvas_item(), Vector2(size.width - width, 0), outline_size, font_outline_color);
  198. }
  199. text_buf->draw(get_canvas_item(), Vector2(size.width - width, 0), color);
  200. } else {
  201. if (outline_size > 0 && font_outline_color.a > 0) {
  202. text_buf->draw_outline(get_canvas_item(), Vector2(0, 0), outline_size, font_outline_color);
  203. }
  204. text_buf->draw(get_canvas_item(), Vector2(0, 0), color);
  205. }
  206. if (do_underline) {
  207. int underline_spacing = theme_cache.underline_spacing + text_buf->get_line_underline_position();
  208. int y = text_buf->get_line_ascent() + underline_spacing;
  209. int underline_thickness = MAX(1, text_buf->get_line_underline_thickness());
  210. if (is_layout_rtl()) {
  211. draw_line(Vector2(size.width - width, y), Vector2(size.width, y), color, underline_thickness);
  212. } else {
  213. draw_line(Vector2(0, y), Vector2(width, y), color, underline_thickness);
  214. }
  215. }
  216. } break;
  217. }
  218. }
  219. void LinkButton::_bind_methods() {
  220. ClassDB::bind_method(D_METHOD("set_text", "text"), &LinkButton::set_text);
  221. ClassDB::bind_method(D_METHOD("get_text"), &LinkButton::get_text);
  222. ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &LinkButton::set_text_direction);
  223. ClassDB::bind_method(D_METHOD("get_text_direction"), &LinkButton::get_text_direction);
  224. ClassDB::bind_method(D_METHOD("set_language", "language"), &LinkButton::set_language);
  225. ClassDB::bind_method(D_METHOD("get_language"), &LinkButton::get_language);
  226. ClassDB::bind_method(D_METHOD("set_uri", "uri"), &LinkButton::set_uri);
  227. ClassDB::bind_method(D_METHOD("get_uri"), &LinkButton::get_uri);
  228. ClassDB::bind_method(D_METHOD("set_underline_mode", "underline_mode"), &LinkButton::set_underline_mode);
  229. ClassDB::bind_method(D_METHOD("get_underline_mode"), &LinkButton::get_underline_mode);
  230. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override", "parser"), &LinkButton::set_structured_text_bidi_override);
  231. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override"), &LinkButton::get_structured_text_bidi_override);
  232. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override_options", "args"), &LinkButton::set_structured_text_bidi_override_options);
  233. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override_options"), &LinkButton::get_structured_text_bidi_override_options);
  234. BIND_ENUM_CONSTANT(UNDERLINE_MODE_ALWAYS);
  235. BIND_ENUM_CONSTANT(UNDERLINE_MODE_ON_HOVER);
  236. BIND_ENUM_CONSTANT(UNDERLINE_MODE_NEVER);
  237. ADD_PROPERTY(PropertyInfo(Variant::STRING, "text"), "set_text", "get_text");
  238. ADD_PROPERTY(PropertyInfo(Variant::INT, "underline", PROPERTY_HINT_ENUM, "Always,On Hover,Never"), "set_underline_mode", "get_underline_mode");
  239. ADD_PROPERTY(PropertyInfo(Variant::STRING, "uri"), "set_uri", "get_uri");
  240. ADD_GROUP("BiDi", "");
  241. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_text_direction", "get_text_direction");
  242. ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID, ""), "set_language", "get_language");
  243. 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");
  244. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "structured_text_bidi_override_options"), "set_structured_text_bidi_override_options", "get_structured_text_bidi_override_options");
  245. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, LinkButton, focus);
  246. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LinkButton, font_color);
  247. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LinkButton, font_focus_color);
  248. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LinkButton, font_pressed_color);
  249. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LinkButton, font_hover_color);
  250. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LinkButton, font_hover_pressed_color);
  251. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LinkButton, font_disabled_color);
  252. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, LinkButton, font);
  253. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, LinkButton, font_size);
  254. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, LinkButton, outline_size);
  255. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LinkButton, font_outline_color);
  256. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, LinkButton, underline_spacing);
  257. }
  258. LinkButton::LinkButton(const String &p_text) {
  259. text_buf.instantiate();
  260. set_focus_mode(FOCUS_ACCESSIBILITY);
  261. set_default_cursor_shape(CURSOR_POINTING_HAND);
  262. set_text(p_text);
  263. }