button.cpp 12 KB

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