link_button.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 "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. if (has_focus()) {
  70. color = get_color("font_color_focus");
  71. } else {
  72. color = get_color("font_color");
  73. }
  74. do_underline = underline_mode == UNDERLINE_MODE_ALWAYS;
  75. } break;
  76. case DRAW_HOVER_PRESSED:
  77. case DRAW_PRESSED: {
  78. if (has_color("font_color_pressed")) {
  79. color = get_color("font_color_pressed");
  80. } else {
  81. color = get_color("font_color");
  82. }
  83. do_underline = underline_mode != UNDERLINE_MODE_NEVER;
  84. } break;
  85. case DRAW_HOVER: {
  86. color = get_color("font_color_hover");
  87. do_underline = underline_mode != UNDERLINE_MODE_NEVER;
  88. } break;
  89. case DRAW_DISABLED: {
  90. color = get_color("font_color_disabled");
  91. do_underline = underline_mode == UNDERLINE_MODE_ALWAYS;
  92. } break;
  93. }
  94. if (has_focus()) {
  95. Ref<StyleBox> style = get_stylebox("focus");
  96. style->draw(ci, Rect2(Point2(), size));
  97. }
  98. Ref<Font> font = get_font("font");
  99. draw_string(font, Vector2(0, font->get_ascent()), xl_text, color);
  100. if (do_underline) {
  101. int underline_spacing = get_constant("underline_spacing");
  102. int width = font->get_string_size(xl_text).width;
  103. int y = font->get_ascent() + underline_spacing;
  104. draw_line(Vector2(0, y), Vector2(width, y), color);
  105. }
  106. } break;
  107. }
  108. }
  109. void LinkButton::_bind_methods() {
  110. ClassDB::bind_method(D_METHOD("set_text", "text"), &LinkButton::set_text);
  111. ClassDB::bind_method(D_METHOD("get_text"), &LinkButton::get_text);
  112. ClassDB::bind_method(D_METHOD("set_underline_mode", "underline_mode"), &LinkButton::set_underline_mode);
  113. ClassDB::bind_method(D_METHOD("get_underline_mode"), &LinkButton::get_underline_mode);
  114. BIND_ENUM_CONSTANT(UNDERLINE_MODE_ALWAYS);
  115. BIND_ENUM_CONSTANT(UNDERLINE_MODE_ON_HOVER);
  116. BIND_ENUM_CONSTANT(UNDERLINE_MODE_NEVER);
  117. ADD_PROPERTY(PropertyInfo(Variant::STRING, "text"), "set_text", "get_text");
  118. ADD_PROPERTY(PropertyInfo(Variant::INT, "underline", PROPERTY_HINT_ENUM, "Always,On Hover,Never"), "set_underline_mode", "get_underline_mode");
  119. }
  120. LinkButton::LinkButton() {
  121. underline_mode = UNDERLINE_MODE_ALWAYS;
  122. set_focus_mode(FOCUS_NONE);
  123. set_default_cursor_shape(CURSOR_POINTING_HAND);
  124. }