button.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*************************************************************************/
  2. /* 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 "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(xl_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 (xl_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_TRANSLATION_CHANGED) {
  53. xl_text = tr(text);
  54. minimum_size_changed();
  55. update();
  56. }
  57. if (p_what == NOTIFICATION_DRAW) {
  58. RID ci = get_canvas_item();
  59. Size2 size = get_size();
  60. Color color;
  61. Color color_icon(1, 1, 1, 1);
  62. //print_line(get_text()+": "+itos(is_flat())+" hover "+itos(get_draw_mode()));
  63. Ref<StyleBox> style = get_stylebox("normal");
  64. switch (get_draw_mode()) {
  65. case DRAW_NORMAL: {
  66. style = get_stylebox("normal");
  67. if (!flat)
  68. style->draw(ci, Rect2(Point2(0, 0), size));
  69. color = get_color("font_color");
  70. if (has_color("icon_color_normal"))
  71. color_icon = get_color("icon_color_normal");
  72. } break;
  73. case DRAW_PRESSED: {
  74. style = get_stylebox("pressed");
  75. if (!flat)
  76. style->draw(ci, Rect2(Point2(0, 0), size));
  77. if (has_color("font_color_pressed"))
  78. color = get_color("font_color_pressed");
  79. else
  80. color = get_color("font_color");
  81. if (has_color("icon_color_pressed"))
  82. color_icon = get_color("icon_color_pressed");
  83. } break;
  84. case DRAW_HOVER: {
  85. style = get_stylebox("hover");
  86. if (!flat)
  87. style->draw(ci, Rect2(Point2(0, 0), size));
  88. color = get_color("font_color_hover");
  89. if (has_color("icon_color_hover"))
  90. color_icon = get_color("icon_color_hover");
  91. } break;
  92. case DRAW_DISABLED: {
  93. style = get_stylebox("disabled");
  94. if (!flat)
  95. style->draw(ci, Rect2(Point2(0, 0), size));
  96. color = get_color("font_color_disabled");
  97. if (has_color("icon_color_disabled"))
  98. color_icon = get_color("icon_color_disabled");
  99. } break;
  100. }
  101. if (has_focus()) {
  102. Ref<StyleBox> style = get_stylebox("focus");
  103. style->draw(ci, Rect2(Point2(), size));
  104. }
  105. Ref<Font> font = get_font("font");
  106. Ref<Texture> _icon;
  107. if (icon.is_null() && has_icon("icon"))
  108. _icon = Control::get_icon("icon");
  109. else
  110. _icon = icon;
  111. Point2 icon_ofs = (!_icon.is_null()) ? Point2(_icon->get_width() + get_constant("hseparation"), 0) : Point2();
  112. int text_clip = size.width - style->get_minimum_size().width - icon_ofs.width;
  113. Point2 text_ofs = (size - style->get_minimum_size() - icon_ofs - font->get_string_size(xl_text)) / 2.0;
  114. switch (align) {
  115. case ALIGN_LEFT: {
  116. text_ofs.x = style->get_margin(MARGIN_LEFT) + icon_ofs.x;
  117. text_ofs.y += style->get_offset().y;
  118. } break;
  119. case ALIGN_CENTER: {
  120. if (text_ofs.x < 0)
  121. text_ofs.x = 0;
  122. text_ofs += icon_ofs;
  123. text_ofs += style->get_offset();
  124. } break;
  125. case ALIGN_RIGHT: {
  126. text_ofs.x = size.x - style->get_margin(MARGIN_RIGHT) - font->get_string_size(xl_text).x;
  127. text_ofs.y += style->get_offset().y;
  128. } break;
  129. }
  130. text_ofs.y += font->get_ascent();
  131. font->draw(ci, text_ofs.floor(), xl_text, color, clip_text ? text_clip : -1);
  132. if (!_icon.is_null()) {
  133. int valign = size.height - style->get_minimum_size().y;
  134. if (is_disabled())
  135. color_icon.a = 0.4;
  136. _icon->draw(ci, style->get_offset() + Point2(0, Math::floor((valign - _icon->get_height()) / 2.0)), color_icon);
  137. }
  138. }
  139. }
  140. void Button::set_text(const String &p_text) {
  141. if (text == p_text)
  142. return;
  143. text = p_text;
  144. xl_text = tr(p_text);
  145. update();
  146. _change_notify("text");
  147. minimum_size_changed();
  148. }
  149. String Button::get_text() const {
  150. return text;
  151. }
  152. void Button::set_icon(const Ref<Texture> &p_icon) {
  153. if (icon == p_icon)
  154. return;
  155. icon = p_icon;
  156. update();
  157. _change_notify("icon");
  158. minimum_size_changed();
  159. }
  160. Ref<Texture> Button::get_icon() const {
  161. return icon;
  162. }
  163. void Button::set_flat(bool p_flat) {
  164. flat = p_flat;
  165. update();
  166. _change_notify("flat");
  167. }
  168. bool Button::is_flat() const {
  169. return flat;
  170. }
  171. void Button::set_clip_text(bool p_clip_text) {
  172. clip_text = p_clip_text;
  173. update();
  174. minimum_size_changed();
  175. }
  176. bool Button::get_clip_text() const {
  177. return clip_text;
  178. }
  179. void Button::set_text_align(TextAlign p_align) {
  180. align = p_align;
  181. update();
  182. }
  183. Button::TextAlign Button::get_text_align() const {
  184. return align;
  185. }
  186. void Button::_bind_methods() {
  187. ClassDB::bind_method(D_METHOD("set_text", "text"), &Button::set_text);
  188. ClassDB::bind_method(D_METHOD("get_text"), &Button::get_text);
  189. ClassDB::bind_method(D_METHOD("set_button_icon", "texture"), &Button::set_icon);
  190. ClassDB::bind_method(D_METHOD("get_button_icon"), &Button::get_icon);
  191. ClassDB::bind_method(D_METHOD("set_flat", "enabled"), &Button::set_flat);
  192. ClassDB::bind_method(D_METHOD("set_clip_text", "enabled"), &Button::set_clip_text);
  193. ClassDB::bind_method(D_METHOD("get_clip_text"), &Button::get_clip_text);
  194. ClassDB::bind_method(D_METHOD("set_text_align", "align"), &Button::set_text_align);
  195. ClassDB::bind_method(D_METHOD("get_text_align"), &Button::get_text_align);
  196. ClassDB::bind_method(D_METHOD("is_flat"), &Button::is_flat);
  197. BIND_ENUM_CONSTANT(ALIGN_LEFT);
  198. BIND_ENUM_CONSTANT(ALIGN_CENTER);
  199. BIND_ENUM_CONSTANT(ALIGN_RIGHT);
  200. ADD_PROPERTYNZ(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT_INTL), "set_text", "get_text");
  201. ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_button_icon", "get_button_icon");
  202. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flat"), "set_flat", "is_flat");
  203. ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "clip_text"), "set_clip_text", "get_clip_text");
  204. ADD_PROPERTYNO(PropertyInfo(Variant::INT, "align", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_text_align", "get_text_align");
  205. }
  206. Button::Button(const String &p_text) {
  207. flat = false;
  208. clip_text = false;
  209. set_mouse_filter(MOUSE_FILTER_STOP);
  210. set_text(p_text);
  211. align = ALIGN_CENTER;
  212. }
  213. Button::~Button() {
  214. }