link_button.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*************************************************************************/
  2. /* link_button.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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. void LinkButton::set_text(const String &p_text) {
  32. text = p_text;
  33. update();
  34. minimum_size_changed();
  35. }
  36. String LinkButton::get_text() const {
  37. return text;
  38. }
  39. void LinkButton::set_underline_mode(UnderlineMode p_underline_mode) {
  40. underline_mode = p_underline_mode;
  41. update();
  42. }
  43. LinkButton::UnderlineMode LinkButton::get_underline_mode() const {
  44. return underline_mode;
  45. }
  46. Size2 LinkButton::get_minimum_size() const {
  47. return get_font("font")->get_string_size(text);
  48. }
  49. void LinkButton::_notification(int p_what) {
  50. switch (p_what) {
  51. case NOTIFICATION_DRAW: {
  52. RID ci = get_canvas_item();
  53. Size2 size = get_size();
  54. Color color;
  55. bool do_underline = false;
  56. //print_line(get_text()+": "+itos(is_flat())+" hover "+itos(get_draw_mode()));
  57. switch (get_draw_mode()) {
  58. case DRAW_NORMAL: {
  59. color = get_color("font_color");
  60. do_underline = underline_mode == UNDERLINE_MODE_ALWAYS;
  61. } break;
  62. case DRAW_PRESSED: {
  63. if (has_color("font_color_pressed"))
  64. color = get_color("font_color_pressed");
  65. else
  66. color = get_color("font_color");
  67. do_underline = underline_mode != UNDERLINE_MODE_NEVER;
  68. } break;
  69. case DRAW_HOVER: {
  70. color = get_color("font_color_hover");
  71. do_underline = underline_mode != UNDERLINE_MODE_NEVER;
  72. } break;
  73. case DRAW_DISABLED: {
  74. color = get_color("font_color_disabled");
  75. do_underline = underline_mode == UNDERLINE_MODE_ALWAYS;
  76. } break;
  77. }
  78. if (has_focus()) {
  79. Ref<StyleBox> style = get_stylebox("focus");
  80. style->draw(ci, Rect2(Point2(), size));
  81. }
  82. Ref<Font> font = get_font("font");
  83. draw_string(font, Vector2(0, font->get_ascent()), text, color);
  84. if (do_underline) {
  85. int underline_spacing = get_constant("underline_spacing");
  86. int width = font->get_string_size(text).width;
  87. int y = font->get_ascent() + underline_spacing;
  88. draw_line(Vector2(0, y), Vector2(width, y), color);
  89. }
  90. } break;
  91. }
  92. }
  93. void LinkButton::_bind_methods() {
  94. ClassDB::bind_method(D_METHOD("set_text", "text"), &LinkButton::set_text);
  95. ClassDB::bind_method(D_METHOD("get_text"), &LinkButton::get_text);
  96. ClassDB::bind_method(D_METHOD("set_underline_mode", "underline_mode"), &LinkButton::set_underline_mode);
  97. ClassDB::bind_method(D_METHOD("get_underline_mode"), &LinkButton::get_underline_mode);
  98. BIND_ENUM_CONSTANT(UNDERLINE_MODE_ALWAYS);
  99. BIND_ENUM_CONSTANT(UNDERLINE_MODE_ON_HOVER);
  100. BIND_ENUM_CONSTANT(UNDERLINE_MODE_NEVER);
  101. ADD_PROPERTYNZ(PropertyInfo(Variant::STRING, "text"), "set_text", "get_text");
  102. ADD_PROPERTYNZ(PropertyInfo(Variant::INT, "underline", PROPERTY_HINT_ENUM, "Always,On Hover,Never"), "set_underline_mode", "get_underline_mode");
  103. }
  104. LinkButton::LinkButton() {
  105. underline_mode = UNDERLINE_MODE_ALWAYS;
  106. set_enabled_focus_mode(FOCUS_NONE);
  107. set_default_cursor_shape(CURSOR_POINTING_HAND);
  108. }