link_button.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*************************************************************************/
  2. /* link_button.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "link_button.h"
  30. void LinkButton::set_text(const String& p_text) {
  31. text=p_text;
  32. update();
  33. minimum_size_changed();
  34. }
  35. String LinkButton::get_text() const {
  36. return text;
  37. }
  38. void LinkButton::set_underline_mode(UnderlineMode p_underline_mode) {
  39. underline_mode=p_underline_mode;
  40. update();
  41. }
  42. LinkButton::UnderlineMode LinkButton::get_underline_mode() const {
  43. return underline_mode;
  44. }
  45. Size2 LinkButton::get_minimum_size() const {
  46. return get_font("font")->get_string_size( text );
  47. }
  48. void LinkButton::_notification(int p_what) {
  49. switch( p_what ) {
  50. case NOTIFICATION_DRAW: {
  51. RID ci = get_canvas_item();
  52. Size2 size=get_size();
  53. Color color;
  54. bool do_underline=false;
  55. //print_line(get_text()+": "+itos(is_flat())+" hover "+itos(get_draw_mode()));
  56. switch( get_draw_mode() ) {
  57. case DRAW_NORMAL: {
  58. color=get_color("font_color");
  59. do_underline=underline_mode==UNDERLINE_MODE_ALWAYS;
  60. } break;
  61. case DRAW_PRESSED: {
  62. if (has_color("font_color_pressed"))
  63. color=get_color("font_color_pressed");
  64. else
  65. color=get_color("font_color");
  66. do_underline=underline_mode!=UNDERLINE_MODE_NEVER;
  67. } break;
  68. case DRAW_HOVER: {
  69. color=get_color("font_color_hover");
  70. do_underline=underline_mode!=UNDERLINE_MODE_NEVER;
  71. } break;
  72. case DRAW_DISABLED: {
  73. color=get_color("font_color_disabled");
  74. do_underline=underline_mode==UNDERLINE_MODE_ALWAYS;
  75. } break;
  76. }
  77. if (has_focus()) {
  78. Ref<StyleBox> style = get_stylebox("focus");
  79. style->draw(ci,Rect2(Point2(),size));
  80. }
  81. Ref<Font> font=get_font("font");
  82. draw_string(font,Vector2(0,font->get_ascent()),text,color);
  83. if (do_underline) {
  84. int underline_spacing = get_constant("underline_spacing");
  85. int width = font->get_string_size(text).width;
  86. int y = font->get_ascent()+underline_spacing;
  87. draw_line(Vector2(0,y),Vector2(width,y),color);
  88. }
  89. } break;
  90. }
  91. }
  92. void LinkButton::_bind_methods() {
  93. ClassDB::bind_method(_MD("set_text","text"),&LinkButton::set_text);
  94. ClassDB::bind_method(_MD("get_text"),&LinkButton::get_text);
  95. ClassDB::bind_method(_MD("set_underline_mode","underline_mode"),&LinkButton::set_underline_mode);
  96. ClassDB::bind_method(_MD("get_underline_mode"),&LinkButton::get_underline_mode);
  97. BIND_CONSTANT( UNDERLINE_MODE_ALWAYS );
  98. BIND_CONSTANT( UNDERLINE_MODE_ON_HOVER );
  99. BIND_CONSTANT( UNDERLINE_MODE_NEVER );
  100. ADD_PROPERTYNZ(PropertyInfo(Variant::STRING,"text"), "set_text", "get_text");
  101. ADD_PROPERTYNZ(PropertyInfo(Variant::INT,"underline",PROPERTY_HINT_ENUM,"Always,On Hover,Never"), "set_underline_mode", "get_underline_mode");
  102. }
  103. LinkButton::LinkButton() {
  104. underline_mode=UNDERLINE_MODE_ALWAYS;
  105. set_enabled_focus_mode(FOCUS_NONE);
  106. set_default_cursor_shape(CURSOR_POINTING_HAND);
  107. }