2
0

link_button.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*************************************************************************/
  2. /* link_button.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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 "core/string/translation.h"
  32. void LinkButton::_shape() {
  33. Ref<Font> font = get_theme_font(SNAME("font"));
  34. int font_size = get_theme_font_size(SNAME("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, opentype_features, (language != "") ? language : TranslationServer::get_singleton()->get_tool_locale());
  43. }
  44. void LinkButton::set_text(const String &p_text) {
  45. if (text == p_text) {
  46. return;
  47. }
  48. text = p_text;
  49. xl_text = atr(text);
  50. _shape();
  51. minimum_size_changed();
  52. update();
  53. }
  54. String LinkButton::get_text() const {
  55. return text;
  56. }
  57. void LinkButton::set_structured_text_bidi_override(Control::StructuredTextParser p_parser) {
  58. if (st_parser != p_parser) {
  59. st_parser = p_parser;
  60. _shape();
  61. update();
  62. }
  63. }
  64. Control::StructuredTextParser LinkButton::get_structured_text_bidi_override() const {
  65. return st_parser;
  66. }
  67. void LinkButton::set_structured_text_bidi_override_options(Array p_args) {
  68. st_args = p_args;
  69. _shape();
  70. update();
  71. }
  72. Array LinkButton::get_structured_text_bidi_override_options() const {
  73. return st_args;
  74. }
  75. void LinkButton::set_text_direction(Control::TextDirection p_text_direction) {
  76. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  77. if (text_direction != p_text_direction) {
  78. text_direction = p_text_direction;
  79. _shape();
  80. update();
  81. }
  82. }
  83. Control::TextDirection LinkButton::get_text_direction() const {
  84. return text_direction;
  85. }
  86. void LinkButton::clear_opentype_features() {
  87. opentype_features.clear();
  88. _shape();
  89. update();
  90. }
  91. void LinkButton::set_opentype_feature(const String &p_name, int p_value) {
  92. int32_t tag = TS->name_to_tag(p_name);
  93. if (!opentype_features.has(tag) || (int)opentype_features[tag] != p_value) {
  94. opentype_features[tag] = p_value;
  95. _shape();
  96. update();
  97. }
  98. }
  99. int LinkButton::get_opentype_feature(const String &p_name) const {
  100. int32_t tag = TS->name_to_tag(p_name);
  101. if (!opentype_features.has(tag)) {
  102. return -1;
  103. }
  104. return opentype_features[tag];
  105. }
  106. void LinkButton::set_language(const String &p_language) {
  107. if (language != p_language) {
  108. language = p_language;
  109. _shape();
  110. update();
  111. }
  112. }
  113. String LinkButton::get_language() const {
  114. return language;
  115. }
  116. void LinkButton::set_underline_mode(UnderlineMode p_underline_mode) {
  117. underline_mode = p_underline_mode;
  118. update();
  119. }
  120. LinkButton::UnderlineMode LinkButton::get_underline_mode() const {
  121. return underline_mode;
  122. }
  123. Size2 LinkButton::get_minimum_size() const {
  124. return text_buf->get_size();
  125. }
  126. void LinkButton::_notification(int p_what) {
  127. switch (p_what) {
  128. case NOTIFICATION_TRANSLATION_CHANGED: {
  129. xl_text = atr(text);
  130. _shape();
  131. minimum_size_changed();
  132. update();
  133. } break;
  134. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  135. update();
  136. } break;
  137. case NOTIFICATION_THEME_CHANGED: {
  138. _shape();
  139. minimum_size_changed();
  140. update();
  141. } break;
  142. case NOTIFICATION_DRAW: {
  143. RID ci = get_canvas_item();
  144. Size2 size = get_size();
  145. Color color;
  146. bool do_underline = false;
  147. switch (get_draw_mode()) {
  148. case DRAW_NORMAL: {
  149. if (has_focus()) {
  150. color = get_theme_color(SNAME("font_focus_color"));
  151. } else {
  152. color = get_theme_color(SNAME("font_color"));
  153. }
  154. do_underline = underline_mode == UNDERLINE_MODE_ALWAYS;
  155. } break;
  156. case DRAW_HOVER_PRESSED:
  157. case DRAW_PRESSED: {
  158. if (has_theme_color(SNAME("font_pressed_color"))) {
  159. color = get_theme_color(SNAME("font_pressed_color"));
  160. } else {
  161. color = get_theme_color(SNAME("font_color"));
  162. }
  163. do_underline = underline_mode != UNDERLINE_MODE_NEVER;
  164. } break;
  165. case DRAW_HOVER: {
  166. color = get_theme_color(SNAME("font_hover_color"));
  167. do_underline = underline_mode != UNDERLINE_MODE_NEVER;
  168. } break;
  169. case DRAW_DISABLED: {
  170. color = get_theme_color(SNAME("font_disabled_color"));
  171. do_underline = underline_mode == UNDERLINE_MODE_ALWAYS;
  172. } break;
  173. }
  174. if (has_focus()) {
  175. Ref<StyleBox> style = get_theme_stylebox(SNAME("focus"));
  176. style->draw(ci, Rect2(Point2(), size));
  177. }
  178. int width = text_buf->get_line_width();
  179. Color font_outline_color = get_theme_color(SNAME("font_outline_color"));
  180. int outline_size = get_theme_constant(SNAME("outline_size"));
  181. if (is_layout_rtl()) {
  182. if (outline_size > 0 && font_outline_color.a > 0) {
  183. text_buf->draw_outline(get_canvas_item(), Vector2(size.width - width, 0), outline_size, font_outline_color);
  184. }
  185. text_buf->draw(get_canvas_item(), Vector2(size.width - width, 0), color);
  186. } else {
  187. if (outline_size > 0 && font_outline_color.a > 0) {
  188. text_buf->draw_outline(get_canvas_item(), Vector2(0, 0), outline_size, font_outline_color);
  189. }
  190. text_buf->draw(get_canvas_item(), Vector2(0, 0), color);
  191. }
  192. if (do_underline) {
  193. int underline_spacing = get_theme_constant(SNAME("underline_spacing")) + text_buf->get_line_underline_position();
  194. int y = text_buf->get_line_ascent() + underline_spacing;
  195. if (is_layout_rtl()) {
  196. draw_line(Vector2(size.width - width, y), Vector2(size.width, y), color, text_buf->get_line_underline_thickness());
  197. } else {
  198. draw_line(Vector2(0, y), Vector2(width, y), color, text_buf->get_line_underline_thickness());
  199. }
  200. }
  201. } break;
  202. }
  203. }
  204. bool LinkButton::_set(const StringName &p_name, const Variant &p_value) {
  205. String str = p_name;
  206. if (str.begins_with("opentype_features/")) {
  207. String name = str.get_slicec('/', 1);
  208. int32_t tag = TS->name_to_tag(name);
  209. double value = p_value;
  210. if (value == -1) {
  211. if (opentype_features.has(tag)) {
  212. opentype_features.erase(tag);
  213. _shape();
  214. update();
  215. }
  216. } else {
  217. if ((double)opentype_features[tag] != value) {
  218. opentype_features[tag] = value;
  219. _shape();
  220. update();
  221. }
  222. }
  223. notify_property_list_changed();
  224. return true;
  225. }
  226. return false;
  227. }
  228. bool LinkButton::_get(const StringName &p_name, Variant &r_ret) const {
  229. String str = p_name;
  230. if (str.begins_with("opentype_features/")) {
  231. String name = str.get_slicec('/', 1);
  232. int32_t tag = TS->name_to_tag(name);
  233. if (opentype_features.has(tag)) {
  234. r_ret = opentype_features[tag];
  235. return true;
  236. } else {
  237. r_ret = -1;
  238. return true;
  239. }
  240. }
  241. return false;
  242. }
  243. void LinkButton::_get_property_list(List<PropertyInfo> *p_list) const {
  244. for (const Variant *ftr = opentype_features.next(nullptr); ftr != nullptr; ftr = opentype_features.next(ftr)) {
  245. String name = TS->tag_to_name(*ftr);
  246. p_list->push_back(PropertyInfo(Variant::FLOAT, "opentype_features/" + name));
  247. }
  248. p_list->push_back(PropertyInfo(Variant::NIL, "opentype_features/_new", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR));
  249. }
  250. void LinkButton::_bind_methods() {
  251. ClassDB::bind_method(D_METHOD("set_text", "text"), &LinkButton::set_text);
  252. ClassDB::bind_method(D_METHOD("get_text"), &LinkButton::get_text);
  253. ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &LinkButton::set_text_direction);
  254. ClassDB::bind_method(D_METHOD("get_text_direction"), &LinkButton::get_text_direction);
  255. ClassDB::bind_method(D_METHOD("set_opentype_feature", "tag", "value"), &LinkButton::set_opentype_feature);
  256. ClassDB::bind_method(D_METHOD("get_opentype_feature", "tag"), &LinkButton::get_opentype_feature);
  257. ClassDB::bind_method(D_METHOD("clear_opentype_features"), &LinkButton::clear_opentype_features);
  258. ClassDB::bind_method(D_METHOD("set_language", "language"), &LinkButton::set_language);
  259. ClassDB::bind_method(D_METHOD("get_language"), &LinkButton::get_language);
  260. ClassDB::bind_method(D_METHOD("set_underline_mode", "underline_mode"), &LinkButton::set_underline_mode);
  261. ClassDB::bind_method(D_METHOD("get_underline_mode"), &LinkButton::get_underline_mode);
  262. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override", "parser"), &LinkButton::set_structured_text_bidi_override);
  263. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override"), &LinkButton::get_structured_text_bidi_override);
  264. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override_options", "args"), &LinkButton::set_structured_text_bidi_override_options);
  265. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override_options"), &LinkButton::get_structured_text_bidi_override_options);
  266. BIND_ENUM_CONSTANT(UNDERLINE_MODE_ALWAYS);
  267. BIND_ENUM_CONSTANT(UNDERLINE_MODE_ON_HOVER);
  268. BIND_ENUM_CONSTANT(UNDERLINE_MODE_NEVER);
  269. ADD_PROPERTY(PropertyInfo(Variant::STRING, "text"), "set_text", "get_text");
  270. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_text_direction", "get_text_direction");
  271. ADD_PROPERTY(PropertyInfo(Variant::STRING, "language"), "set_language", "get_language");
  272. ADD_PROPERTY(PropertyInfo(Variant::INT, "underline", PROPERTY_HINT_ENUM, "Always,On Hover,Never"), "set_underline_mode", "get_underline_mode");
  273. ADD_GROUP("Structured Text", "structured_text_");
  274. 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");
  275. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "structured_text_bidi_override_options"), "set_structured_text_bidi_override_options", "get_structured_text_bidi_override_options");
  276. }
  277. LinkButton::LinkButton() {
  278. text_buf.instantiate();
  279. set_focus_mode(FOCUS_NONE);
  280. set_default_cursor_shape(CURSOR_POINTING_HAND);
  281. }