link_button.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. text_buf->set_width(-1);
  37. if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
  38. text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  39. } else {
  40. text_buf->set_direction((TextServer::Direction)text_direction);
  41. }
  42. TS->shaped_text_set_bidi_override(text_buf->get_rid(), structured_text_parser(st_parser, st_args, xl_text));
  43. const String &lang = language.is_empty() ? _get_locale() : language;
  44. text_buf->add_string(xl_text, font, font_size, lang);
  45. text_buf->set_text_overrun_behavior(overrun_behavior);
  46. text_buf->set_ellipsis_char(el_char);
  47. queue_accessibility_update();
  48. }
  49. void LinkButton::set_text(const String &p_text) {
  50. if (text == p_text) {
  51. return;
  52. }
  53. text = p_text;
  54. xl_text = atr(text);
  55. _shape();
  56. update_minimum_size();
  57. queue_redraw();
  58. }
  59. String LinkButton::get_text() const {
  60. return text;
  61. }
  62. void LinkButton::set_text_overrun_behavior(TextServer::OverrunBehavior p_behavior) {
  63. if (overrun_behavior != p_behavior) {
  64. overrun_behavior = p_behavior;
  65. _shape();
  66. update_minimum_size();
  67. queue_redraw();
  68. }
  69. }
  70. TextServer::OverrunBehavior LinkButton::get_text_overrun_behavior() const {
  71. return overrun_behavior;
  72. }
  73. void LinkButton::set_structured_text_bidi_override(TextServer::StructuredTextParser p_parser) {
  74. if (st_parser != p_parser) {
  75. st_parser = p_parser;
  76. _shape();
  77. queue_redraw();
  78. }
  79. }
  80. void LinkButton::set_ellipsis_char(const String &p_char) {
  81. String c = p_char;
  82. if (c.length() > 1) {
  83. WARN_PRINT("Ellipsis must be exactly one character long (" + itos(c.length()) + " characters given).");
  84. c = c.left(1);
  85. }
  86. if (el_char == c) {
  87. return;
  88. }
  89. el_char = c;
  90. if (overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) {
  91. _shape();
  92. queue_redraw();
  93. update_minimum_size();
  94. }
  95. }
  96. String LinkButton::get_ellipsis_char() const {
  97. return el_char;
  98. }
  99. TextServer::StructuredTextParser LinkButton::get_structured_text_bidi_override() const {
  100. return st_parser;
  101. }
  102. void LinkButton::set_structured_text_bidi_override_options(const Array &p_args) {
  103. st_args = Array(p_args);
  104. _shape();
  105. queue_redraw();
  106. }
  107. Array LinkButton::get_structured_text_bidi_override_options() const {
  108. return Array(st_args);
  109. }
  110. void LinkButton::set_text_direction(Control::TextDirection p_text_direction) {
  111. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  112. if (text_direction != p_text_direction) {
  113. text_direction = p_text_direction;
  114. _shape();
  115. queue_redraw();
  116. }
  117. }
  118. Control::TextDirection LinkButton::get_text_direction() const {
  119. return text_direction;
  120. }
  121. void LinkButton::set_language(const String &p_language) {
  122. if (language != p_language) {
  123. language = p_language;
  124. _shape();
  125. queue_redraw();
  126. }
  127. }
  128. String LinkButton::get_language() const {
  129. return language;
  130. }
  131. void LinkButton::set_uri(const String &p_uri) {
  132. if (uri != p_uri) {
  133. uri = p_uri;
  134. queue_accessibility_update();
  135. }
  136. }
  137. String LinkButton::get_uri() const {
  138. return uri;
  139. }
  140. void LinkButton::set_underline_mode(UnderlineMode p_underline_mode) {
  141. if (underline_mode == p_underline_mode) {
  142. return;
  143. }
  144. underline_mode = p_underline_mode;
  145. queue_redraw();
  146. }
  147. LinkButton::UnderlineMode LinkButton::get_underline_mode() const {
  148. return underline_mode;
  149. }
  150. Ref<Font> LinkButton::get_button_font() const {
  151. return theme_cache.font;
  152. }
  153. int LinkButton::get_button_font_size() const {
  154. return theme_cache.font_size;
  155. }
  156. void LinkButton::pressed() {
  157. if (uri.is_empty()) {
  158. return;
  159. }
  160. OS::get_singleton()->shell_open(uri);
  161. }
  162. Size2 LinkButton::get_minimum_size() const {
  163. Size2 minsize = text_buf->get_size();
  164. if (overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) {
  165. minsize.width = 0;
  166. }
  167. return minsize;
  168. }
  169. Control::CursorShape LinkButton::get_cursor_shape(const Point2 &p_pos) const {
  170. return is_disabled() ? CURSOR_ARROW : get_default_cursor_shape();
  171. }
  172. void LinkButton::_notification(int p_what) {
  173. switch (p_what) {
  174. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  175. RID ae = get_accessibility_element();
  176. ERR_FAIL_COND(ae.is_null());
  177. DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_LINK);
  178. const String &ac_name = get_accessibility_name();
  179. if (!xl_text.is_empty() && ac_name.is_empty()) {
  180. DisplayServer::get_singleton()->accessibility_update_set_name(ae, xl_text);
  181. } else if (!xl_text.is_empty() && !ac_name.is_empty() && ac_name != xl_text) {
  182. DisplayServer::get_singleton()->accessibility_update_set_name(ae, ac_name + ": " + xl_text);
  183. } else if (xl_text.is_empty() && ac_name.is_empty() && !get_tooltip_text().is_empty()) {
  184. DisplayServer::get_singleton()->accessibility_update_set_name(ae, get_tooltip_text()); // Fall back to tooltip.
  185. }
  186. DisplayServer::get_singleton()->accessibility_update_set_url(ae, uri);
  187. } break;
  188. case NOTIFICATION_TRANSLATION_CHANGED: {
  189. xl_text = atr(text);
  190. _shape();
  191. update_minimum_size();
  192. queue_redraw();
  193. } break;
  194. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  195. queue_redraw();
  196. } break;
  197. case NOTIFICATION_RESIZED:
  198. case NOTIFICATION_THEME_CHANGED: {
  199. _shape();
  200. update_minimum_size();
  201. queue_redraw();
  202. } break;
  203. case NOTIFICATION_DRAW: {
  204. RID ci = get_canvas_item();
  205. Size2 size = get_size();
  206. Color color;
  207. bool do_underline = false;
  208. switch (get_draw_mode()) {
  209. case DRAW_NORMAL: {
  210. if (has_focus(true)) {
  211. color = theme_cache.font_focus_color;
  212. } else {
  213. color = theme_cache.font_color;
  214. }
  215. do_underline = underline_mode == UNDERLINE_MODE_ALWAYS;
  216. } break;
  217. case DRAW_HOVER_PRESSED:
  218. case DRAW_PRESSED: {
  219. if (has_theme_color(SNAME("font_pressed_color"))) {
  220. color = theme_cache.font_pressed_color;
  221. } else {
  222. color = theme_cache.font_color;
  223. }
  224. do_underline = underline_mode != UNDERLINE_MODE_NEVER;
  225. } break;
  226. case DRAW_HOVER: {
  227. color = theme_cache.font_hover_color;
  228. do_underline = underline_mode != UNDERLINE_MODE_NEVER;
  229. } break;
  230. case DRAW_DISABLED: {
  231. color = theme_cache.font_disabled_color;
  232. do_underline = underline_mode == UNDERLINE_MODE_ALWAYS;
  233. } break;
  234. }
  235. if (has_focus(true)) {
  236. Ref<StyleBox> style = theme_cache.focus;
  237. style->draw(ci, Rect2(Point2(), size));
  238. }
  239. if (overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) {
  240. text_buf->set_width(MAX(1.0f, size.width));
  241. }
  242. int width = text_buf->get_line_width();
  243. Color font_outline_color = theme_cache.font_outline_color;
  244. int outline_size = theme_cache.outline_size;
  245. if (is_layout_rtl()) {
  246. if (outline_size > 0 && font_outline_color.a > 0) {
  247. text_buf->draw_outline(get_canvas_item(), Vector2(size.width - width, 0), outline_size, font_outline_color);
  248. }
  249. text_buf->draw(get_canvas_item(), Vector2(size.width - width, 0), color);
  250. } else {
  251. if (outline_size > 0 && font_outline_color.a > 0) {
  252. text_buf->draw_outline(get_canvas_item(), Vector2(0, 0), outline_size, font_outline_color);
  253. }
  254. text_buf->draw(get_canvas_item(), Vector2(0, 0), color);
  255. }
  256. if (do_underline) {
  257. int underline_spacing = theme_cache.underline_spacing + text_buf->get_line_underline_position();
  258. int y = text_buf->get_line_ascent() + underline_spacing;
  259. int underline_thickness = MAX(1, text_buf->get_line_underline_thickness());
  260. if (is_layout_rtl()) {
  261. draw_line(Vector2(size.width - width, y), Vector2(size.width, y), color, underline_thickness);
  262. } else {
  263. draw_line(Vector2(0, y), Vector2(width, y), color, underline_thickness);
  264. }
  265. }
  266. } break;
  267. }
  268. }
  269. void LinkButton::_bind_methods() {
  270. ClassDB::bind_method(D_METHOD("set_text", "text"), &LinkButton::set_text);
  271. ClassDB::bind_method(D_METHOD("get_text"), &LinkButton::get_text);
  272. ClassDB::bind_method(D_METHOD("set_text_overrun_behavior", "overrun_behavior"), &LinkButton::set_text_overrun_behavior);
  273. ClassDB::bind_method(D_METHOD("get_text_overrun_behavior"), &LinkButton::get_text_overrun_behavior);
  274. ClassDB::bind_method(D_METHOD("set_ellipsis_char", "char"), &LinkButton::set_ellipsis_char);
  275. ClassDB::bind_method(D_METHOD("get_ellipsis_char"), &LinkButton::get_ellipsis_char);
  276. ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &LinkButton::set_text_direction);
  277. ClassDB::bind_method(D_METHOD("get_text_direction"), &LinkButton::get_text_direction);
  278. ClassDB::bind_method(D_METHOD("set_language", "language"), &LinkButton::set_language);
  279. ClassDB::bind_method(D_METHOD("get_language"), &LinkButton::get_language);
  280. ClassDB::bind_method(D_METHOD("set_uri", "uri"), &LinkButton::set_uri);
  281. ClassDB::bind_method(D_METHOD("get_uri"), &LinkButton::get_uri);
  282. ClassDB::bind_method(D_METHOD("set_underline_mode", "underline_mode"), &LinkButton::set_underline_mode);
  283. ClassDB::bind_method(D_METHOD("get_underline_mode"), &LinkButton::get_underline_mode);
  284. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override", "parser"), &LinkButton::set_structured_text_bidi_override);
  285. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override"), &LinkButton::get_structured_text_bidi_override);
  286. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override_options", "args"), &LinkButton::set_structured_text_bidi_override_options);
  287. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override_options"), &LinkButton::get_structured_text_bidi_override_options);
  288. BIND_ENUM_CONSTANT(UNDERLINE_MODE_ALWAYS);
  289. BIND_ENUM_CONSTANT(UNDERLINE_MODE_ON_HOVER);
  290. BIND_ENUM_CONSTANT(UNDERLINE_MODE_NEVER);
  291. ADD_PROPERTY(PropertyInfo(Variant::STRING, "text"), "set_text", "get_text");
  292. ADD_PROPERTY(PropertyInfo(Variant::INT, "underline", PROPERTY_HINT_ENUM, "Always,On Hover,Never"), "set_underline_mode", "get_underline_mode");
  293. ADD_PROPERTY(PropertyInfo(Variant::STRING, "uri"), "set_uri", "get_uri");
  294. ADD_GROUP("Text Behavior", "");
  295. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_overrun_behavior", PROPERTY_HINT_ENUM, "Trim Nothing,Trim Characters,Trim Words,Ellipsis (6+ Characters),Word Ellipsis (6+ Characters),Ellipsis (Always),Word Ellipsis (Always)"), "set_text_overrun_behavior", "get_text_overrun_behavior");
  296. ADD_PROPERTY(PropertyInfo(Variant::STRING, "ellipsis_char"), "set_ellipsis_char", "get_ellipsis_char");
  297. ADD_GROUP("BiDi", "");
  298. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_text_direction", "get_text_direction");
  299. ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID, ""), "set_language", "get_language");
  300. 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");
  301. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "structured_text_bidi_override_options"), "set_structured_text_bidi_override_options", "get_structured_text_bidi_override_options");
  302. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, LinkButton, focus);
  303. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LinkButton, font_color);
  304. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LinkButton, font_focus_color);
  305. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LinkButton, font_pressed_color);
  306. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LinkButton, font_hover_color);
  307. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LinkButton, font_hover_pressed_color);
  308. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LinkButton, font_disabled_color);
  309. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, LinkButton, font);
  310. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, LinkButton, font_size);
  311. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, LinkButton, outline_size);
  312. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LinkButton, font_outline_color);
  313. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, LinkButton, underline_spacing);
  314. }
  315. LinkButton::LinkButton(const String &p_text) {
  316. text_buf.instantiate();
  317. set_focus_mode(FOCUS_ACCESSIBILITY);
  318. set_default_cursor_shape(CURSOR_POINTING_HAND);
  319. set_text(p_text);
  320. }