button.cpp 11 KB

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