button.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*************************************************************************/
  2. /* button.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "button.h"
  31. #include "print_string.h"
  32. #include "servers/visual_server.h"
  33. #include "translation.h"
  34. Size2 Button::get_minimum_size() const {
  35. Size2 minsize = get_font("font")->get_string_size(text);
  36. if (clip_text)
  37. minsize.width = 0;
  38. Ref<Texture> _icon;
  39. if (icon.is_null() && has_icon("icon"))
  40. _icon = Control::get_icon("icon");
  41. else
  42. _icon = icon;
  43. if (!_icon.is_null()) {
  44. minsize.height = MAX(minsize.height, _icon->get_height());
  45. minsize.width += _icon->get_width();
  46. if (text != "")
  47. minsize.width += get_constant("hseparation");
  48. }
  49. return get_stylebox("normal")->get_minimum_size() + minsize;
  50. }
  51. void Button::_notification(int p_what) {
  52. if (p_what == NOTIFICATION_DRAW) {
  53. RID ci = get_canvas_item();
  54. Size2 size = get_size();
  55. Color color;
  56. //print_line(get_text()+": "+itos(is_flat())+" hover "+itos(get_draw_mode()));
  57. switch (get_draw_mode()) {
  58. case DRAW_NORMAL: {
  59. if (!flat)
  60. get_stylebox("normal")->draw(ci, Rect2(Point2(0, 0), size));
  61. color = get_color("font_color");
  62. } break;
  63. case DRAW_PRESSED: {
  64. get_stylebox("pressed")->draw(ci, Rect2(Point2(0, 0), size));
  65. if (has_color("font_color_pressed"))
  66. color = get_color("font_color_pressed");
  67. else
  68. color = get_color("font_color");
  69. } break;
  70. case DRAW_HOVER: {
  71. get_stylebox("hover")->draw(ci, Rect2(Point2(0, 0), size));
  72. color = get_color("font_color_hover");
  73. } break;
  74. case DRAW_DISABLED: {
  75. get_stylebox("disabled")->draw(ci, Rect2(Point2(0, 0), size));
  76. color = get_color("font_color_disabled");
  77. } break;
  78. }
  79. if (has_focus()) {
  80. Ref<StyleBox> style = get_stylebox("focus");
  81. style->draw(ci, Rect2(Point2(), size));
  82. }
  83. Ref<StyleBox> style = get_stylebox("normal");
  84. Ref<Font> font = get_font("font");
  85. Ref<Texture> _icon;
  86. if (icon.is_null() && has_icon("icon"))
  87. _icon = Control::get_icon("icon");
  88. else
  89. _icon = icon;
  90. Point2 icon_ofs = (!_icon.is_null()) ? Point2(_icon->get_width() + get_constant("hseparation"), 0) : Point2();
  91. int text_clip = size.width - style->get_minimum_size().width - icon_ofs.width;
  92. Point2 text_ofs = (size - style->get_minimum_size() - icon_ofs - font->get_string_size(text)) / 2.0;
  93. switch (align) {
  94. case ALIGN_LEFT: {
  95. text_ofs.x = style->get_margin(MARGIN_LEFT) + icon_ofs.x;
  96. text_ofs.y += style->get_offset().y;
  97. } break;
  98. case ALIGN_CENTER: {
  99. if (text_ofs.x < 0)
  100. text_ofs.x = 0;
  101. text_ofs += icon_ofs;
  102. text_ofs += style->get_offset();
  103. } break;
  104. case ALIGN_RIGHT: {
  105. text_ofs.x = size.x - style->get_margin(MARGIN_RIGHT) - font->get_string_size(text).x;
  106. text_ofs.y += style->get_offset().y;
  107. } break;
  108. }
  109. text_ofs.y += font->get_ascent();
  110. font->draw(ci, text_ofs.floor(), text, color, clip_text ? text_clip : -1);
  111. if (!_icon.is_null()) {
  112. int valign = size.height - style->get_minimum_size().y;
  113. _icon->draw(ci, style->get_offset() + Point2(0, Math::floor((valign - _icon->get_height()) / 2.0)), is_disabled() ? Color(1, 1, 1, 0.4) : Color(1, 1, 1));
  114. }
  115. }
  116. }
  117. void Button::set_text(const String &p_text) {
  118. if (text == p_text)
  119. return;
  120. text = XL_MESSAGE(p_text);
  121. update();
  122. _change_notify("text");
  123. minimum_size_changed();
  124. }
  125. String Button::get_text() const {
  126. return text;
  127. }
  128. void Button::set_icon(const Ref<Texture> &p_icon) {
  129. if (icon == p_icon)
  130. return;
  131. icon = p_icon;
  132. update();
  133. _change_notify("icon");
  134. minimum_size_changed();
  135. }
  136. Ref<Texture> Button::get_icon() const {
  137. return icon;
  138. }
  139. void Button::set_flat(bool p_flat) {
  140. flat = p_flat;
  141. update();
  142. _change_notify("flat");
  143. }
  144. bool Button::is_flat() const {
  145. return flat;
  146. }
  147. void Button::set_clip_text(bool p_clip_text) {
  148. clip_text = p_clip_text;
  149. update();
  150. minimum_size_changed();
  151. }
  152. bool Button::get_clip_text() const {
  153. return clip_text;
  154. }
  155. void Button::set_text_align(TextAlign p_align) {
  156. align = p_align;
  157. update();
  158. }
  159. Button::TextAlign Button::get_text_align() const {
  160. return align;
  161. }
  162. void Button::_bind_methods() {
  163. ObjectTypeDB::bind_method(_MD("set_text", "text"), &Button::set_text);
  164. ObjectTypeDB::bind_method(_MD("get_text"), &Button::get_text);
  165. ObjectTypeDB::bind_method(_MD("set_button_icon", "texture:Texture"), &Button::set_icon);
  166. ObjectTypeDB::bind_method(_MD("get_button_icon:Texture"), &Button::get_icon);
  167. ObjectTypeDB::bind_method(_MD("set_flat", "enabled"), &Button::set_flat);
  168. ObjectTypeDB::bind_method(_MD("set_clip_text", "enabled"), &Button::set_clip_text);
  169. ObjectTypeDB::bind_method(_MD("get_clip_text"), &Button::get_clip_text);
  170. ObjectTypeDB::bind_method(_MD("set_text_align", "align"), &Button::set_text_align);
  171. ObjectTypeDB::bind_method(_MD("get_text_align"), &Button::get_text_align);
  172. ObjectTypeDB::bind_method(_MD("is_flat"), &Button::is_flat);
  173. BIND_CONSTANT(ALIGN_LEFT);
  174. BIND_CONSTANT(ALIGN_CENTER);
  175. BIND_CONSTANT(ALIGN_RIGHT);
  176. ADD_PROPERTYNZ(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT_INTL), _SCS("set_text"), _SCS("get_text"));
  177. ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), _SCS("set_button_icon"), _SCS("get_button_icon"));
  178. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flat"), _SCS("set_flat"), _SCS("is_flat"));
  179. ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "clip_text"), _SCS("set_clip_text"), _SCS("get_clip_text"));
  180. ADD_PROPERTYNO(PropertyInfo(Variant::INT, "align", PROPERTY_HINT_ENUM, "Left,Center,Right"), _SCS("set_text_align"), _SCS("get_text_align"));
  181. }
  182. Button::Button(const String &p_text) {
  183. flat = false;
  184. clip_text = false;
  185. set_stop_mouse(true);
  186. set_text(p_text);
  187. align = ALIGN_CENTER;
  188. }
  189. Button::~Button() {
  190. }