link_button.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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/translation.h"
  32. void LinkButton::set_text(const String &p_text) {
  33. if (text == p_text) {
  34. return;
  35. }
  36. text = p_text;
  37. xl_text = tr(p_text);
  38. update();
  39. _change_notify("text");
  40. minimum_size_changed();
  41. }
  42. String LinkButton::get_text() const {
  43. return text;
  44. }
  45. void LinkButton::set_underline_mode(UnderlineMode p_underline_mode) {
  46. underline_mode = p_underline_mode;
  47. update();
  48. }
  49. LinkButton::UnderlineMode LinkButton::get_underline_mode() const {
  50. return underline_mode;
  51. }
  52. Size2 LinkButton::get_minimum_size() const {
  53. return get_font("font")->get_string_size(xl_text);
  54. }
  55. void LinkButton::_notification(int p_what) {
  56. switch (p_what) {
  57. case NOTIFICATION_TRANSLATION_CHANGED: {
  58. xl_text = tr(text);
  59. minimum_size_changed();
  60. update();
  61. } break;
  62. case NOTIFICATION_DRAW: {
  63. RID ci = get_canvas_item();
  64. Size2 size = get_size();
  65. Color color;
  66. bool do_underline = false;
  67. switch (get_draw_mode()) {
  68. case DRAW_NORMAL: {
  69. color = get_color("font_color");
  70. do_underline = underline_mode == UNDERLINE_MODE_ALWAYS;
  71. } break;
  72. case DRAW_HOVER_PRESSED:
  73. case DRAW_PRESSED: {
  74. if (has_color("font_color_pressed"))
  75. color = get_color("font_color_pressed");
  76. else
  77. color = get_color("font_color");
  78. do_underline = underline_mode != UNDERLINE_MODE_NEVER;
  79. } break;
  80. case DRAW_HOVER: {
  81. color = get_color("font_color_hover");
  82. do_underline = underline_mode != UNDERLINE_MODE_NEVER;
  83. } break;
  84. case DRAW_DISABLED: {
  85. color = get_color("font_color_disabled");
  86. do_underline = underline_mode == UNDERLINE_MODE_ALWAYS;
  87. } break;
  88. }
  89. if (has_focus()) {
  90. Ref<StyleBox> style = get_stylebox("focus");
  91. style->draw(ci, Rect2(Point2(), size));
  92. }
  93. Ref<Font> font = get_font("font");
  94. draw_string(font, Vector2(0, font->get_ascent()), xl_text, color);
  95. if (do_underline) {
  96. int underline_spacing = get_constant("underline_spacing");
  97. int width = font->get_string_size(xl_text).width;
  98. int y = font->get_ascent() + underline_spacing;
  99. draw_line(Vector2(0, y), Vector2(width, y), color);
  100. }
  101. } break;
  102. }
  103. }
  104. void LinkButton::_bind_methods() {
  105. ClassDB::bind_method(D_METHOD("set_text", "text"), &LinkButton::set_text);
  106. ClassDB::bind_method(D_METHOD("get_text"), &LinkButton::get_text);
  107. ClassDB::bind_method(D_METHOD("set_underline_mode", "underline_mode"), &LinkButton::set_underline_mode);
  108. ClassDB::bind_method(D_METHOD("get_underline_mode"), &LinkButton::get_underline_mode);
  109. BIND_ENUM_CONSTANT(UNDERLINE_MODE_ALWAYS);
  110. BIND_ENUM_CONSTANT(UNDERLINE_MODE_ON_HOVER);
  111. BIND_ENUM_CONSTANT(UNDERLINE_MODE_NEVER);
  112. ADD_PROPERTY(PropertyInfo(Variant::STRING, "text"), "set_text", "get_text");
  113. ADD_PROPERTY(PropertyInfo(Variant::INT, "underline", PROPERTY_HINT_ENUM, "Always,On Hover,Never"), "set_underline_mode", "get_underline_mode");
  114. }
  115. LinkButton::LinkButton() {
  116. underline_mode = UNDERLINE_MODE_ALWAYS;
  117. set_focus_mode(FOCUS_NONE);
  118. set_default_cursor_shape(CURSOR_POINTING_HAND);
  119. }