button.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /**************************************************************************/
  2. /* button.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "scene/scene_string_names.h"
  33. #include "servers/visual_server.h"
  34. Size2 Button::get_minimum_size() const {
  35. Size2 minsize = get_font("font")->total_size_of_lines(xl_text.split("\n"));
  36. if (clip_text) {
  37. minsize.width = 0;
  38. }
  39. if (!expand_icon) {
  40. Ref<Texture> _icon;
  41. if (icon.is_null() && has_icon("icon")) {
  42. _icon = Control::get_icon("icon");
  43. } else {
  44. _icon = icon;
  45. }
  46. if (!_icon.is_null()) {
  47. minsize.height = MAX(minsize.height, _icon->get_height());
  48. if (icon_align != ALIGN_CENTER) {
  49. minsize.width += _icon->get_width();
  50. if (xl_text != "") {
  51. minsize.width += get_constant("hseparation");
  52. }
  53. } else {
  54. minsize.width = MAX(minsize.width, _icon->get_width());
  55. }
  56. }
  57. }
  58. return get_stylebox("normal")->get_minimum_size() + minsize;
  59. }
  60. void Button::_set_internal_margin(Margin p_margin, float p_value) {
  61. _internal_margin[p_margin] = p_value;
  62. }
  63. void Button::_notification(int p_what) {
  64. switch (p_what) {
  65. case NOTIFICATION_TRANSLATION_CHANGED: {
  66. xl_text = tr(text);
  67. minimum_size_changed();
  68. update();
  69. } break;
  70. case NOTIFICATION_DRAW: {
  71. RID ci = get_canvas_item();
  72. Size2 size = get_size();
  73. Color color;
  74. Color color_icon(1, 1, 1, 1);
  75. Ref<StyleBox> style = get_stylebox("normal");
  76. Vector<String> lines = xl_text.split("\n");
  77. switch (get_draw_mode()) {
  78. case DRAW_NORMAL: {
  79. style = get_stylebox("normal");
  80. if (!flat) {
  81. style->draw(ci, Rect2(Point2(0, 0), size));
  82. }
  83. // Focus colors only take precedence over normal state.
  84. if (has_focus()) {
  85. color = get_color("font_color_focus");
  86. if (has_color("icon_color_focus")) {
  87. color_icon = get_color("icon_color_focus");
  88. }
  89. } else {
  90. color = get_color("font_color");
  91. if (has_color("icon_color_normal")) {
  92. color_icon = get_color("icon_color_normal");
  93. }
  94. }
  95. } break;
  96. case DRAW_HOVER_PRESSED: {
  97. if (has_stylebox("hover_pressed")) {
  98. style = get_stylebox("hover_pressed");
  99. if (!flat) {
  100. style->draw(ci, Rect2(Point2(0, 0), size));
  101. }
  102. if (has_color("font_color_hover_pressed")) {
  103. color = get_color("font_color_hover_pressed");
  104. } else {
  105. color = get_color("font_color");
  106. }
  107. if (has_color("icon_color_hover_pressed")) {
  108. color_icon = get_color("icon_color_hover_pressed");
  109. }
  110. break;
  111. }
  112. FALLTHROUGH;
  113. }
  114. case DRAW_PRESSED: {
  115. style = get_stylebox("pressed");
  116. if (!flat) {
  117. style->draw(ci, Rect2(Point2(0, 0), size));
  118. }
  119. if (has_color("font_color_pressed")) {
  120. color = get_color("font_color_pressed");
  121. } else {
  122. color = get_color("font_color");
  123. }
  124. if (has_color("icon_color_pressed")) {
  125. color_icon = get_color("icon_color_pressed");
  126. }
  127. } break;
  128. case DRAW_HOVER: {
  129. style = get_stylebox("hover");
  130. if (!flat) {
  131. style->draw(ci, Rect2(Point2(0, 0), size));
  132. }
  133. color = get_color("font_color_hover");
  134. if (has_color("icon_color_hover")) {
  135. color_icon = get_color("icon_color_hover");
  136. }
  137. } break;
  138. case DRAW_DISABLED: {
  139. style = get_stylebox("disabled");
  140. if (!flat) {
  141. style->draw(ci, Rect2(Point2(0, 0), size));
  142. }
  143. color = get_color("font_color_disabled");
  144. if (has_color("icon_color_disabled")) {
  145. color_icon = get_color("icon_color_disabled");
  146. } else {
  147. color_icon.a = 0.4;
  148. }
  149. } break;
  150. }
  151. if (has_focus()) {
  152. Ref<StyleBox> style2 = get_stylebox("focus");
  153. style2->draw(ci, Rect2(Point2(), size));
  154. }
  155. Ref<Font> font = get_font("font");
  156. Ref<Texture> _icon;
  157. if (icon.is_null() && has_icon("icon")) {
  158. _icon = Control::get_icon("icon");
  159. } else {
  160. _icon = icon;
  161. }
  162. Rect2 icon_region = Rect2();
  163. if (!_icon.is_null()) {
  164. int valign = size.height - style->get_minimum_size().y;
  165. float icon_ofs_region = 0;
  166. Point2 style_offset;
  167. Size2 icon_size = _icon->get_size();
  168. if (icon_align == ALIGN_LEFT) {
  169. style_offset.x = style->get_margin(MARGIN_LEFT);
  170. if (_internal_margin[MARGIN_LEFT] > 0) {
  171. icon_ofs_region = _internal_margin[MARGIN_LEFT] + get_constant("hseparation");
  172. }
  173. } else if (icon_align == ALIGN_CENTER) {
  174. style_offset.x = 0;
  175. } else if (icon_align == ALIGN_RIGHT) {
  176. style_offset.x = -style->get_margin(MARGIN_RIGHT);
  177. if (_internal_margin[MARGIN_RIGHT] > 0) {
  178. icon_ofs_region = -_internal_margin[MARGIN_RIGHT] - get_constant("hseparation");
  179. }
  180. }
  181. style_offset.y = style->get_margin(MARGIN_TOP);
  182. if (expand_icon) {
  183. Size2 _size = get_size() - style->get_offset() * 2;
  184. int icon_text_separation = text.empty() ? 0 : get_constant("h_separation");
  185. _size.width -= icon_text_separation + icon_ofs_region;
  186. if (!clip_text && icon_align != ALIGN_CENTER) {
  187. _size.width -= get_font("font")->total_size_of_lines(lines).width;
  188. }
  189. float icon_width = _icon->get_width() * _size.height / _icon->get_height();
  190. float icon_height = _size.height;
  191. if (icon_width > _size.width) {
  192. icon_width = _size.width;
  193. icon_height = _icon->get_height() * icon_width / _icon->get_width();
  194. }
  195. icon_size = Size2(icon_width, icon_height);
  196. }
  197. if (icon_align == ALIGN_LEFT) {
  198. icon_region = Rect2(style_offset + Point2(icon_ofs_region, Math::floor((valign - icon_size.y) * 0.5)), icon_size);
  199. } else if (icon_align == ALIGN_CENTER) {
  200. icon_region = Rect2(style_offset + Point2(icon_ofs_region + Math::floor((size.x - icon_size.x) * 0.5), Math::floor((valign - icon_size.y) * 0.5)), icon_size);
  201. } else {
  202. icon_region = Rect2(style_offset + Point2(icon_ofs_region + size.x - icon_size.x, Math::floor((valign - icon_size.y) * 0.5)), icon_size);
  203. }
  204. if (icon_region.size.width > 0) {
  205. draw_texture_rect_region(_icon, icon_region, Rect2(Point2(), _icon->get_size()), color_icon);
  206. }
  207. }
  208. Point2 icon_ofs = !_icon.is_null() ? Point2(icon_region.size.width + get_constant("hseparation"), 0) : Point2();
  209. if (align == ALIGN_CENTER && icon_align == ALIGN_CENTER) {
  210. icon_ofs.x = 0;
  211. }
  212. int text_clip = size.width - style->get_minimum_size().width - icon_ofs.width;
  213. if (_internal_margin[MARGIN_LEFT] > 0) {
  214. text_clip -= _internal_margin[MARGIN_LEFT] + get_constant("hseparation");
  215. }
  216. if (_internal_margin[MARGIN_RIGHT] > 0) {
  217. text_clip -= _internal_margin[MARGIN_RIGHT] + get_constant("hseparation");
  218. }
  219. int num_lines = lines.size();
  220. float line_height = font->get_height();
  221. for (int i = 0; i < num_lines; i++) {
  222. String line_text = lines[i];
  223. Point2 text_ofs = (size - style->get_minimum_size() - icon_ofs - font->get_string_size(line_text) - Point2(_internal_margin[MARGIN_RIGHT] - _internal_margin[MARGIN_LEFT], 0)) / 2.0;
  224. switch (align) {
  225. case ALIGN_LEFT: {
  226. if (icon_align != ALIGN_LEFT) {
  227. icon_ofs.x = 0;
  228. }
  229. if (_internal_margin[MARGIN_LEFT] > 0) {
  230. text_ofs.x = style->get_margin(MARGIN_LEFT) + icon_ofs.x + _internal_margin[MARGIN_LEFT] + get_constant("hseparation");
  231. } else {
  232. text_ofs.x = style->get_margin(MARGIN_LEFT) + icon_ofs.x;
  233. }
  234. text_ofs.y += style->get_offset().y;
  235. } break;
  236. case ALIGN_CENTER: {
  237. if (text_ofs.x < 0) {
  238. text_ofs.x = 0;
  239. }
  240. if (icon_align == ALIGN_LEFT) {
  241. text_ofs += icon_ofs;
  242. }
  243. text_ofs += style->get_offset();
  244. } break;
  245. case ALIGN_RIGHT: {
  246. int text_width = font->get_string_size(line_text).x;
  247. if (_internal_margin[MARGIN_RIGHT] > 0) {
  248. text_ofs.x = size.x - style->get_margin(MARGIN_RIGHT) - text_width - _internal_margin[MARGIN_RIGHT] - get_constant("hseparation");
  249. } else {
  250. text_ofs.x = size.x - style->get_margin(MARGIN_RIGHT) - text_width;
  251. }
  252. text_ofs.y += style->get_offset().y;
  253. if (icon_align == ALIGN_RIGHT) {
  254. text_ofs.x -= icon_ofs.x;
  255. }
  256. } break;
  257. }
  258. text_ofs.y += font->get_ascent();
  259. text_ofs.y += line_height * (((float)i) - (((float)(num_lines - 1)) / 2.0));
  260. select_font(font);
  261. font->draw(ci, text_ofs.floor(), line_text, color, clip_text ? text_clip : -1);
  262. }
  263. } break;
  264. }
  265. }
  266. void Button::set_text(const String &p_text) {
  267. if (text == p_text) {
  268. return;
  269. }
  270. text = p_text;
  271. xl_text = tr(p_text);
  272. update();
  273. _change_notify("text");
  274. minimum_size_changed();
  275. }
  276. String Button::get_text() const {
  277. return text;
  278. }
  279. void Button::set_icon(const Ref<Texture> &p_icon) {
  280. if (icon == p_icon) {
  281. return;
  282. }
  283. if (icon.is_valid()) {
  284. icon->disconnect(SceneStringNames::get_singleton()->changed, this, "_texture_changed");
  285. }
  286. icon = p_icon;
  287. if (icon.is_valid()) {
  288. icon->connect(SceneStringNames::get_singleton()->changed, this, "_texture_changed");
  289. }
  290. update();
  291. _change_notify("icon");
  292. minimum_size_changed();
  293. }
  294. Ref<Texture> Button::get_icon() const {
  295. return icon;
  296. }
  297. void Button::_texture_changed() {
  298. update();
  299. minimum_size_changed();
  300. }
  301. void Button::set_expand_icon(bool p_expand_icon) {
  302. expand_icon = p_expand_icon;
  303. update();
  304. minimum_size_changed();
  305. }
  306. bool Button::is_expand_icon() const {
  307. return expand_icon;
  308. }
  309. void Button::set_flat(bool p_flat) {
  310. flat = p_flat;
  311. update();
  312. _change_notify("flat");
  313. }
  314. bool Button::is_flat() const {
  315. return flat;
  316. }
  317. void Button::set_clip_text(bool p_clip_text) {
  318. clip_text = p_clip_text;
  319. update();
  320. minimum_size_changed();
  321. }
  322. bool Button::get_clip_text() const {
  323. return clip_text;
  324. }
  325. void Button::set_text_align(TextAlign p_align) {
  326. align = p_align;
  327. update();
  328. }
  329. Button::TextAlign Button::get_text_align() const {
  330. return align;
  331. }
  332. void Button::set_icon_align(TextAlign p_align) {
  333. icon_align = p_align;
  334. minimum_size_changed();
  335. update();
  336. }
  337. Button::TextAlign Button::get_icon_align() const {
  338. return icon_align;
  339. }
  340. void Button::_bind_methods() {
  341. ClassDB::bind_method(D_METHOD("set_text", "text"), &Button::set_text);
  342. ClassDB::bind_method(D_METHOD("get_text"), &Button::get_text);
  343. ClassDB::bind_method(D_METHOD("set_button_icon", "texture"), &Button::set_icon);
  344. ClassDB::bind_method(D_METHOD("get_button_icon"), &Button::get_icon);
  345. ClassDB::bind_method(D_METHOD("set_flat", "enabled"), &Button::set_flat);
  346. ClassDB::bind_method(D_METHOD("is_flat"), &Button::is_flat);
  347. ClassDB::bind_method(D_METHOD("set_clip_text", "enabled"), &Button::set_clip_text);
  348. ClassDB::bind_method(D_METHOD("get_clip_text"), &Button::get_clip_text);
  349. ClassDB::bind_method(D_METHOD("set_text_align", "align"), &Button::set_text_align);
  350. ClassDB::bind_method(D_METHOD("get_text_align"), &Button::get_text_align);
  351. ClassDB::bind_method(D_METHOD("set_icon_align", "icon_align"), &Button::set_icon_align);
  352. ClassDB::bind_method(D_METHOD("get_icon_align"), &Button::get_icon_align);
  353. ClassDB::bind_method(D_METHOD("set_expand_icon", "enabled"), &Button::set_expand_icon);
  354. ClassDB::bind_method(D_METHOD("is_expand_icon"), &Button::is_expand_icon);
  355. ClassDB::bind_method(D_METHOD("_texture_changed"), &Button::_texture_changed);
  356. BIND_ENUM_CONSTANT(ALIGN_LEFT);
  357. BIND_ENUM_CONSTANT(ALIGN_CENTER);
  358. BIND_ENUM_CONSTANT(ALIGN_RIGHT);
  359. ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_MULTILINE_TEXT, "", PROPERTY_USAGE_DEFAULT_INTL), "set_text", "get_text");
  360. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_button_icon", "get_button_icon");
  361. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flat"), "set_flat", "is_flat");
  362. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clip_text"), "set_clip_text", "get_clip_text");
  363. ADD_PROPERTY(PropertyInfo(Variant::INT, "align", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_text_align", "get_text_align");
  364. ADD_PROPERTY(PropertyInfo(Variant::INT, "icon_align", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_icon_align", "get_icon_align");
  365. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "expand_icon"), "set_expand_icon", "is_expand_icon");
  366. }
  367. Button::Button(const String &p_text) {
  368. flat = false;
  369. clip_text = false;
  370. expand_icon = false;
  371. set_mouse_filter(MOUSE_FILTER_STOP);
  372. set_text(p_text);
  373. align = ALIGN_CENTER;
  374. icon_align = ALIGN_LEFT;
  375. for (int i = 0; i < 4; i++) {
  376. _internal_margin[i] = 0;
  377. }
  378. }
  379. Button::~Button() {
  380. }