button.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*************************************************************************/
  2. /* button.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "core/translation.h"
  32. #include "servers/visual_server.h"
  33. Size2 Button::get_minimum_size() const {
  34. Size2 minsize = get_font("font")->get_string_size(xl_text);
  35. if (clip_text)
  36. minsize.width = 0;
  37. Ref<Texture> _icon;
  38. if (icon.is_null() && has_icon("icon"))
  39. _icon = Control::get_icon("icon");
  40. else
  41. _icon = icon;
  42. if (!_icon.is_null()) {
  43. minsize.height = MAX(minsize.height, _icon->get_height());
  44. minsize.width += _icon->get_width();
  45. if (xl_text != "")
  46. minsize.width += get_constant("hseparation");
  47. }
  48. return get_stylebox("normal")->get_minimum_size() + minsize;
  49. }
  50. void Button::_set_internal_margin(Margin p_margin, float p_value) {
  51. _internal_margin[p_margin] = p_value;
  52. }
  53. void Button::_notification(int p_what) {
  54. if (p_what == NOTIFICATION_TRANSLATION_CHANGED) {
  55. xl_text = tr(text);
  56. minimum_size_changed();
  57. update();
  58. }
  59. if (p_what == NOTIFICATION_DRAW) {
  60. RID ci = get_canvas_item();
  61. Size2 size = get_size();
  62. Color color;
  63. Color color_icon(1, 1, 1, 1);
  64. Ref<StyleBox> style = get_stylebox("normal");
  65. switch (get_draw_mode()) {
  66. case DRAW_NORMAL: {
  67. style = get_stylebox("normal");
  68. if (!flat)
  69. style->draw(ci, Rect2(Point2(0, 0), size));
  70. color = get_color("font_color");
  71. if (has_color("icon_color_normal"))
  72. color_icon = get_color("icon_color_normal");
  73. } break;
  74. case DRAW_HOVER_PRESSED: {
  75. if (has_stylebox("hover_pressed") && has_stylebox_override("hover_pressed")) {
  76. style = get_stylebox("hover_pressed");
  77. if (!flat)
  78. style->draw(ci, Rect2(Point2(0, 0), size));
  79. if (has_color("font_color_hover_pressed"))
  80. color = get_color("font_color_hover_pressed");
  81. else
  82. color = get_color("font_color");
  83. if (has_color("icon_color_hover_pressed"))
  84. color_icon = get_color("icon_color_hover_pressed");
  85. break;
  86. }
  87. }
  88. case DRAW_PRESSED: {
  89. style = get_stylebox("pressed");
  90. if (!flat)
  91. style->draw(ci, Rect2(Point2(0, 0), size));
  92. if (has_color("font_color_pressed"))
  93. color = get_color("font_color_pressed");
  94. else
  95. color = get_color("font_color");
  96. if (has_color("icon_color_pressed"))
  97. color_icon = get_color("icon_color_pressed");
  98. } break;
  99. case DRAW_HOVER: {
  100. style = get_stylebox("hover");
  101. if (!flat)
  102. style->draw(ci, Rect2(Point2(0, 0), size));
  103. color = get_color("font_color_hover");
  104. if (has_color("icon_color_hover"))
  105. color_icon = get_color("icon_color_hover");
  106. } break;
  107. case DRAW_DISABLED: {
  108. style = get_stylebox("disabled");
  109. if (!flat)
  110. style->draw(ci, Rect2(Point2(0, 0), size));
  111. color = get_color("font_color_disabled");
  112. if (has_color("icon_color_disabled"))
  113. color_icon = get_color("icon_color_disabled");
  114. } break;
  115. }
  116. if (has_focus()) {
  117. Ref<StyleBox> style = get_stylebox("focus");
  118. style->draw(ci, Rect2(Point2(), size));
  119. }
  120. Ref<Font> font = get_font("font");
  121. Ref<Texture> _icon;
  122. if (icon.is_null() && has_icon("icon"))
  123. _icon = Control::get_icon("icon");
  124. else
  125. _icon = icon;
  126. Point2 icon_ofs = (!_icon.is_null()) ? Point2(_icon->get_width() + get_constant("hseparation"), 0) : Point2();
  127. int text_clip = size.width - style->get_minimum_size().width - icon_ofs.width;
  128. Point2 text_ofs = (size - style->get_minimum_size() - icon_ofs - font->get_string_size(xl_text) - Point2(_internal_margin[MARGIN_RIGHT] - _internal_margin[MARGIN_LEFT], 0)) / 2.0;
  129. switch (align) {
  130. case ALIGN_LEFT: {
  131. text_ofs.x = style->get_margin(MARGIN_LEFT) + icon_ofs.x + _internal_margin[MARGIN_LEFT] + get_constant("hseparation");
  132. text_ofs.y += style->get_offset().y;
  133. } break;
  134. case ALIGN_CENTER: {
  135. if (text_ofs.x < 0)
  136. text_ofs.x = 0;
  137. text_ofs += icon_ofs;
  138. text_ofs += style->get_offset();
  139. } break;
  140. case ALIGN_RIGHT: {
  141. if (_internal_margin[MARGIN_RIGHT] > 0) {
  142. text_ofs.x = size.x - style->get_margin(MARGIN_RIGHT) - font->get_string_size(xl_text).x - _internal_margin[MARGIN_RIGHT] - get_constant("hseparation");
  143. } else {
  144. text_ofs.x = size.x - style->get_margin(MARGIN_RIGHT) - font->get_string_size(xl_text).x;
  145. }
  146. text_ofs.y += style->get_offset().y;
  147. } break;
  148. }
  149. text_ofs.y += font->get_ascent();
  150. font->draw(ci, text_ofs.floor(), xl_text, color, clip_text ? text_clip : -1);
  151. if (!_icon.is_null()) {
  152. int valign = size.height - style->get_minimum_size().y;
  153. if (is_disabled())
  154. color_icon.a = 0.4;
  155. if (_internal_margin[MARGIN_LEFT] > 0) {
  156. _icon->draw(ci, style->get_offset() + Point2(_internal_margin[MARGIN_LEFT] + get_constant("hseparation"), Math::floor((valign - _icon->get_height()) / 2.0)), color_icon);
  157. } else {
  158. _icon->draw(ci, style->get_offset() + Point2(0, Math::floor((valign - _icon->get_height()) / 2.0)), color_icon);
  159. }
  160. }
  161. }
  162. }
  163. void Button::set_text(const String &p_text) {
  164. if (text == p_text)
  165. return;
  166. text = p_text;
  167. xl_text = tr(p_text);
  168. update();
  169. _change_notify("text");
  170. minimum_size_changed();
  171. }
  172. String Button::get_text() const {
  173. return text;
  174. }
  175. void Button::set_icon(const Ref<Texture> &p_icon) {
  176. if (icon == p_icon)
  177. return;
  178. icon = p_icon;
  179. update();
  180. _change_notify("icon");
  181. minimum_size_changed();
  182. }
  183. Ref<Texture> Button::get_icon() const {
  184. return icon;
  185. }
  186. void Button::set_flat(bool p_flat) {
  187. flat = p_flat;
  188. update();
  189. _change_notify("flat");
  190. }
  191. bool Button::is_flat() const {
  192. return flat;
  193. }
  194. void Button::set_clip_text(bool p_clip_text) {
  195. clip_text = p_clip_text;
  196. update();
  197. minimum_size_changed();
  198. }
  199. bool Button::get_clip_text() const {
  200. return clip_text;
  201. }
  202. void Button::set_text_align(TextAlign p_align) {
  203. align = p_align;
  204. update();
  205. }
  206. Button::TextAlign Button::get_text_align() const {
  207. return align;
  208. }
  209. void Button::_bind_methods() {
  210. ClassDB::bind_method(D_METHOD("set_text", "text"), &Button::set_text);
  211. ClassDB::bind_method(D_METHOD("get_text"), &Button::get_text);
  212. ClassDB::bind_method(D_METHOD("set_button_icon", "texture"), &Button::set_icon);
  213. ClassDB::bind_method(D_METHOD("get_button_icon"), &Button::get_icon);
  214. ClassDB::bind_method(D_METHOD("set_flat", "enabled"), &Button::set_flat);
  215. ClassDB::bind_method(D_METHOD("set_clip_text", "enabled"), &Button::set_clip_text);
  216. ClassDB::bind_method(D_METHOD("get_clip_text"), &Button::get_clip_text);
  217. ClassDB::bind_method(D_METHOD("set_text_align", "align"), &Button::set_text_align);
  218. ClassDB::bind_method(D_METHOD("get_text_align"), &Button::get_text_align);
  219. ClassDB::bind_method(D_METHOD("is_flat"), &Button::is_flat);
  220. BIND_ENUM_CONSTANT(ALIGN_LEFT);
  221. BIND_ENUM_CONSTANT(ALIGN_CENTER);
  222. BIND_ENUM_CONSTANT(ALIGN_RIGHT);
  223. ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT_INTL), "set_text", "get_text");
  224. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_button_icon", "get_button_icon");
  225. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flat"), "set_flat", "is_flat");
  226. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clip_text"), "set_clip_text", "get_clip_text");
  227. ADD_PROPERTY(PropertyInfo(Variant::INT, "align", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_text_align", "get_text_align");
  228. }
  229. Button::Button(const String &p_text) {
  230. flat = false;
  231. clip_text = false;
  232. set_mouse_filter(MOUSE_FILTER_STOP);
  233. set_text(p_text);
  234. align = ALIGN_CENTER;
  235. for (int i = 0; i < 4; i++) {
  236. _internal_margin[i] = 0;
  237. }
  238. }
  239. Button::~Button() {
  240. }