button.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  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 "scene/theme/theme_db.h"
  32. Size2 Button::get_minimum_size() const {
  33. Ref<Texture2D> _icon = icon;
  34. if (_icon.is_null() && has_theme_icon(SNAME("icon"))) {
  35. _icon = theme_cache.icon;
  36. }
  37. return get_minimum_size_for_text_and_icon("", _icon);
  38. }
  39. void Button::_set_internal_margin(Side p_side, float p_value) {
  40. _internal_margin[p_side] = p_value;
  41. }
  42. void Button::_queue_update_size_cache() {
  43. }
  44. void Button::_update_theme_item_cache() {
  45. Control::_update_theme_item_cache();
  46. const bool rtl = is_layout_rtl();
  47. if (rtl && has_theme_stylebox(SNAME("normal_mirrored"))) {
  48. theme_cache.max_style_size = theme_cache.normal_mirrored->get_minimum_size();
  49. theme_cache.style_margin_left = theme_cache.normal_mirrored->get_margin(SIDE_LEFT);
  50. theme_cache.style_margin_right = theme_cache.normal_mirrored->get_margin(SIDE_RIGHT);
  51. theme_cache.style_margin_top = theme_cache.normal_mirrored->get_margin(SIDE_TOP);
  52. theme_cache.style_margin_bottom = theme_cache.normal_mirrored->get_margin(SIDE_BOTTOM);
  53. } else {
  54. theme_cache.max_style_size = theme_cache.normal->get_minimum_size();
  55. theme_cache.style_margin_left = theme_cache.normal->get_margin(SIDE_LEFT);
  56. theme_cache.style_margin_right = theme_cache.normal->get_margin(SIDE_RIGHT);
  57. theme_cache.style_margin_top = theme_cache.normal->get_margin(SIDE_TOP);
  58. theme_cache.style_margin_bottom = theme_cache.normal->get_margin(SIDE_BOTTOM);
  59. }
  60. if (has_theme_stylebox("hover_pressed")) {
  61. if (rtl && has_theme_stylebox(SNAME("hover_pressed_mirrored"))) {
  62. theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.hover_pressed_mirrored->get_minimum_size());
  63. theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, theme_cache.hover_pressed_mirrored->get_margin(SIDE_LEFT));
  64. theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, theme_cache.hover_pressed_mirrored->get_margin(SIDE_RIGHT));
  65. theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, theme_cache.hover_pressed_mirrored->get_margin(SIDE_TOP));
  66. theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, theme_cache.hover_pressed_mirrored->get_margin(SIDE_BOTTOM));
  67. } else {
  68. theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.hover_pressed->get_minimum_size());
  69. theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, theme_cache.hover_pressed->get_margin(SIDE_LEFT));
  70. theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, theme_cache.hover_pressed->get_margin(SIDE_RIGHT));
  71. theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, theme_cache.hover_pressed->get_margin(SIDE_TOP));
  72. theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, theme_cache.hover_pressed->get_margin(SIDE_BOTTOM));
  73. }
  74. }
  75. if (rtl && has_theme_stylebox(SNAME("pressed_mirrored"))) {
  76. theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.pressed_mirrored->get_minimum_size());
  77. theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, theme_cache.pressed_mirrored->get_margin(SIDE_LEFT));
  78. theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, theme_cache.pressed_mirrored->get_margin(SIDE_RIGHT));
  79. theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, theme_cache.pressed_mirrored->get_margin(SIDE_TOP));
  80. theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, theme_cache.pressed_mirrored->get_margin(SIDE_BOTTOM));
  81. } else {
  82. theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.pressed->get_minimum_size());
  83. theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, theme_cache.pressed->get_margin(SIDE_LEFT));
  84. theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, theme_cache.pressed->get_margin(SIDE_RIGHT));
  85. theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, theme_cache.pressed->get_margin(SIDE_TOP));
  86. theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, theme_cache.pressed->get_margin(SIDE_BOTTOM));
  87. }
  88. if (rtl && has_theme_stylebox(SNAME("hover_mirrored"))) {
  89. theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.hover_mirrored->get_minimum_size());
  90. theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, theme_cache.hover_mirrored->get_margin(SIDE_LEFT));
  91. theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, theme_cache.hover_mirrored->get_margin(SIDE_RIGHT));
  92. theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, theme_cache.hover_mirrored->get_margin(SIDE_TOP));
  93. theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, theme_cache.hover_mirrored->get_margin(SIDE_BOTTOM));
  94. } else {
  95. theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.hover->get_minimum_size());
  96. theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, theme_cache.hover->get_margin(SIDE_LEFT));
  97. theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, theme_cache.hover->get_margin(SIDE_RIGHT));
  98. theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, theme_cache.hover->get_margin(SIDE_TOP));
  99. theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, theme_cache.hover->get_margin(SIDE_BOTTOM));
  100. }
  101. if (rtl && has_theme_stylebox(SNAME("disabled_mirrored"))) {
  102. theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.disabled_mirrored->get_minimum_size());
  103. theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, theme_cache.disabled_mirrored->get_margin(SIDE_LEFT));
  104. theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, theme_cache.disabled_mirrored->get_margin(SIDE_RIGHT));
  105. theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, theme_cache.disabled_mirrored->get_margin(SIDE_TOP));
  106. theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, theme_cache.disabled_mirrored->get_margin(SIDE_BOTTOM));
  107. } else {
  108. theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.disabled->get_minimum_size());
  109. theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, theme_cache.disabled->get_margin(SIDE_LEFT));
  110. theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, theme_cache.disabled->get_margin(SIDE_RIGHT));
  111. theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, theme_cache.disabled->get_margin(SIDE_TOP));
  112. theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, theme_cache.disabled->get_margin(SIDE_BOTTOM));
  113. }
  114. theme_cache.max_style_size = theme_cache.max_style_size.max(Vector2(theme_cache.style_margin_left + theme_cache.style_margin_right, theme_cache.style_margin_top + theme_cache.style_margin_bottom));
  115. }
  116. Size2 Button::_get_largest_stylebox_size() const {
  117. return theme_cache.max_style_size;
  118. }
  119. Ref<StyleBox> Button::_get_current_stylebox() const {
  120. Ref<StyleBox> stylebox = theme_cache.normal;
  121. const bool rtl = is_layout_rtl();
  122. switch (get_draw_mode()) {
  123. case DRAW_NORMAL: {
  124. if (rtl && has_theme_stylebox(SNAME("normal_mirrored"))) {
  125. stylebox = theme_cache.normal_mirrored;
  126. } else {
  127. stylebox = theme_cache.normal;
  128. }
  129. } break;
  130. case DRAW_HOVER_PRESSED: {
  131. // Edge case for CheckButton and CheckBox.
  132. if (has_theme_stylebox("hover_pressed")) {
  133. if (rtl && has_theme_stylebox(SNAME("hover_pressed_mirrored"))) {
  134. stylebox = theme_cache.hover_pressed_mirrored;
  135. } else {
  136. stylebox = theme_cache.hover_pressed;
  137. }
  138. break;
  139. }
  140. }
  141. [[fallthrough]];
  142. case DRAW_PRESSED: {
  143. if (rtl && has_theme_stylebox(SNAME("pressed_mirrored"))) {
  144. stylebox = theme_cache.pressed_mirrored;
  145. } else {
  146. stylebox = theme_cache.pressed;
  147. }
  148. } break;
  149. case DRAW_HOVER: {
  150. if (rtl && has_theme_stylebox(SNAME("hover_mirrored"))) {
  151. stylebox = theme_cache.hover_mirrored;
  152. } else {
  153. stylebox = theme_cache.hover;
  154. }
  155. } break;
  156. case DRAW_DISABLED: {
  157. if (rtl && has_theme_stylebox(SNAME("disabled_mirrored"))) {
  158. stylebox = theme_cache.disabled_mirrored;
  159. } else {
  160. stylebox = theme_cache.disabled;
  161. }
  162. } break;
  163. }
  164. return stylebox;
  165. }
  166. void Button::_notification(int p_what) {
  167. switch (p_what) {
  168. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  169. queue_redraw();
  170. } break;
  171. case NOTIFICATION_TRANSLATION_CHANGED: {
  172. xl_text = atr(text);
  173. _shape();
  174. update_minimum_size();
  175. queue_redraw();
  176. } break;
  177. case NOTIFICATION_THEME_CHANGED: {
  178. _shape();
  179. update_minimum_size();
  180. queue_redraw();
  181. } break;
  182. case NOTIFICATION_RESIZED: {
  183. if (autowrap_mode != TextServer::AUTOWRAP_OFF) {
  184. _shape();
  185. update_minimum_size();
  186. queue_redraw();
  187. }
  188. } break;
  189. case NOTIFICATION_DRAW: {
  190. const RID ci = get_canvas_item();
  191. const Size2 size = get_size();
  192. Ref<StyleBox> style = _get_current_stylebox();
  193. // Draws the stylebox in the current state.
  194. if (!flat) {
  195. style->draw(ci, Rect2(Point2(), size));
  196. }
  197. if (has_focus()) {
  198. theme_cache.focus->draw(ci, Rect2(Point2(), size));
  199. }
  200. Ref<Texture2D> _icon = icon;
  201. if (_icon.is_null() && has_theme_icon(SNAME("icon"))) {
  202. _icon = theme_cache.icon;
  203. }
  204. if (xl_text.is_empty() && _icon.is_null()) {
  205. break;
  206. }
  207. const float style_margin_left = (theme_cache.align_to_largest_stylebox) ? theme_cache.style_margin_left : style->get_margin(SIDE_LEFT);
  208. const float style_margin_right = (theme_cache.align_to_largest_stylebox) ? theme_cache.style_margin_right : style->get_margin(SIDE_RIGHT);
  209. const float style_margin_top = (theme_cache.align_to_largest_stylebox) ? theme_cache.style_margin_top : style->get_margin(SIDE_TOP);
  210. const float style_margin_bottom = (theme_cache.align_to_largest_stylebox) ? theme_cache.style_margin_bottom : style->get_margin(SIDE_BOTTOM);
  211. Size2 drawable_size_remained = size;
  212. { // The size after the stelybox is stripped.
  213. drawable_size_remained.width -= style_margin_left + style_margin_right;
  214. drawable_size_remained.height -= style_margin_top + style_margin_bottom;
  215. }
  216. const int h_separation = MAX(0, theme_cache.h_separation);
  217. float left_internal_margin_with_h_separation = _internal_margin[SIDE_LEFT];
  218. float right_internal_margin_with_h_separation = _internal_margin[SIDE_RIGHT];
  219. { // The width reserved for internal element in derived classes (and h_separation if needed).
  220. if (_internal_margin[SIDE_LEFT] > 0.0f) {
  221. left_internal_margin_with_h_separation += h_separation;
  222. }
  223. if (_internal_margin[SIDE_RIGHT] > 0.0f) {
  224. right_internal_margin_with_h_separation += h_separation;
  225. }
  226. drawable_size_remained.width -= left_internal_margin_with_h_separation + right_internal_margin_with_h_separation; // The size after the internal element is stripped.
  227. }
  228. HorizontalAlignment icon_align_rtl_checked = horizontal_icon_alignment;
  229. HorizontalAlignment align_rtl_checked = alignment;
  230. // Swap icon and text alignment sides if right-to-left layout is set.
  231. if (is_layout_rtl()) {
  232. if (horizontal_icon_alignment == HORIZONTAL_ALIGNMENT_RIGHT) {
  233. icon_align_rtl_checked = HORIZONTAL_ALIGNMENT_LEFT;
  234. } else if (horizontal_icon_alignment == HORIZONTAL_ALIGNMENT_LEFT) {
  235. icon_align_rtl_checked = HORIZONTAL_ALIGNMENT_RIGHT;
  236. }
  237. if (alignment == HORIZONTAL_ALIGNMENT_RIGHT) {
  238. align_rtl_checked = HORIZONTAL_ALIGNMENT_LEFT;
  239. } else if (alignment == HORIZONTAL_ALIGNMENT_LEFT) {
  240. align_rtl_checked = HORIZONTAL_ALIGNMENT_RIGHT;
  241. }
  242. }
  243. Color font_color;
  244. Color icon_modulate_color(1, 1, 1, 1);
  245. // Get the font color and icon modulate color in the current state.
  246. switch (get_draw_mode()) {
  247. case DRAW_NORMAL: {
  248. // Focus colors only take precedence over normal state.
  249. if (has_focus()) {
  250. font_color = theme_cache.font_focus_color;
  251. if (has_theme_color(SNAME("icon_focus_color"))) {
  252. icon_modulate_color = theme_cache.icon_focus_color;
  253. }
  254. } else {
  255. font_color = theme_cache.font_color;
  256. if (has_theme_color(SNAME("icon_normal_color"))) {
  257. icon_modulate_color = theme_cache.icon_normal_color;
  258. }
  259. }
  260. } break;
  261. case DRAW_HOVER_PRESSED: {
  262. font_color = theme_cache.font_hover_pressed_color;
  263. if (has_theme_color(SNAME("icon_hover_pressed_color"))) {
  264. icon_modulate_color = theme_cache.icon_hover_pressed_color;
  265. }
  266. } break;
  267. case DRAW_PRESSED: {
  268. if (has_theme_color(SNAME("font_pressed_color"))) {
  269. font_color = theme_cache.font_pressed_color;
  270. } else {
  271. font_color = theme_cache.font_color;
  272. }
  273. if (has_theme_color(SNAME("icon_pressed_color"))) {
  274. icon_modulate_color = theme_cache.icon_pressed_color;
  275. }
  276. } break;
  277. case DRAW_HOVER: {
  278. font_color = theme_cache.font_hover_color;
  279. if (has_theme_color(SNAME("icon_hover_color"))) {
  280. icon_modulate_color = theme_cache.icon_hover_color;
  281. }
  282. } break;
  283. case DRAW_DISABLED: {
  284. font_color = theme_cache.font_disabled_color;
  285. if (has_theme_color(SNAME("icon_disabled_color"))) {
  286. icon_modulate_color = theme_cache.icon_disabled_color;
  287. } else {
  288. icon_modulate_color.a = 0.4;
  289. }
  290. } break;
  291. }
  292. const bool is_clipped = clip_text || overrun_behavior != TextServer::OVERRUN_NO_TRIMMING || autowrap_mode != TextServer::AUTOWRAP_OFF;
  293. const Size2 custom_element_size = drawable_size_remained;
  294. // Draw the icon.
  295. if (_icon.is_valid()) {
  296. Size2 icon_size;
  297. { // Calculate the drawing size of the icon.
  298. icon_size = _icon->get_size();
  299. if (expand_icon) {
  300. const Size2 text_buf_size = text_buf->get_size();
  301. Size2 _size = custom_element_size;
  302. if (!is_clipped && icon_align_rtl_checked != HORIZONTAL_ALIGNMENT_CENTER && text_buf_size.width > 0.0f) {
  303. // If there is not enough space for icon and h_separation, h_separation will occupy the space first,
  304. // so the icon's width may be negative. Keep it negative to make it easier to calculate the space
  305. // reserved for text later.
  306. _size.width -= text_buf_size.width + h_separation;
  307. }
  308. if (vertical_icon_alignment != VERTICAL_ALIGNMENT_CENTER) {
  309. _size.height -= text_buf_size.height;
  310. }
  311. float icon_width = icon_size.width * _size.height / icon_size.height;
  312. float icon_height = _size.height;
  313. if (icon_width > _size.width) {
  314. icon_width = _size.width;
  315. icon_height = icon_size.height * icon_width / icon_size.width;
  316. }
  317. icon_size = Size2(icon_width, icon_height);
  318. }
  319. icon_size = _fit_icon_size(icon_size);
  320. icon_size = icon_size.round();
  321. }
  322. if (icon_size.width > 0.0f) {
  323. // Calculate the drawing position of the icon.
  324. Point2 icon_ofs;
  325. switch (icon_align_rtl_checked) {
  326. case HORIZONTAL_ALIGNMENT_CENTER: {
  327. icon_ofs.x = (custom_element_size.width - icon_size.width) / 2.0f;
  328. }
  329. [[fallthrough]];
  330. case HORIZONTAL_ALIGNMENT_FILL:
  331. case HORIZONTAL_ALIGNMENT_LEFT: {
  332. icon_ofs.x += style_margin_left;
  333. icon_ofs.x += left_internal_margin_with_h_separation;
  334. } break;
  335. case HORIZONTAL_ALIGNMENT_RIGHT: {
  336. icon_ofs.x = size.x - style_margin_right;
  337. icon_ofs.x -= right_internal_margin_with_h_separation;
  338. icon_ofs.x -= icon_size.width;
  339. } break;
  340. }
  341. switch (vertical_icon_alignment) {
  342. case VERTICAL_ALIGNMENT_CENTER: {
  343. icon_ofs.y = (custom_element_size.height - icon_size.height) / 2.0f;
  344. }
  345. [[fallthrough]];
  346. case VERTICAL_ALIGNMENT_FILL:
  347. case VERTICAL_ALIGNMENT_TOP: {
  348. icon_ofs.y += style_margin_top;
  349. } break;
  350. case VERTICAL_ALIGNMENT_BOTTOM: {
  351. icon_ofs.y = size.y - style_margin_bottom - icon_size.height;
  352. } break;
  353. }
  354. icon_ofs = icon_ofs.floor();
  355. Rect2 icon_region = Rect2(icon_ofs, icon_size);
  356. draw_texture_rect(_icon, icon_region, false, icon_modulate_color);
  357. }
  358. if (!xl_text.is_empty()) {
  359. // Update the size after the icon is stripped. Stripping only when the icon alignments are not center.
  360. if (icon_align_rtl_checked != HORIZONTAL_ALIGNMENT_CENTER) {
  361. // Subtract the space's width occupied by icon and h_separation together.
  362. drawable_size_remained.width -= icon_size.width + h_separation;
  363. }
  364. if (vertical_icon_alignment != VERTICAL_ALIGNMENT_CENTER) {
  365. drawable_size_remained.height -= icon_size.height;
  366. }
  367. }
  368. }
  369. // Draw the text.
  370. if (!xl_text.is_empty()) {
  371. text_buf->set_alignment(align_rtl_checked);
  372. float text_buf_width = Math::ceil(MAX(1.0f, drawable_size_remained.width)); // The space's width filled by the text_buf.
  373. if (autowrap_mode != TextServer::AUTOWRAP_OFF && !Math::is_equal_approx(text_buf_width, text_buf->get_width())) {
  374. update_minimum_size();
  375. }
  376. text_buf->set_width(text_buf_width);
  377. Point2 text_ofs;
  378. switch (align_rtl_checked) {
  379. case HORIZONTAL_ALIGNMENT_CENTER: {
  380. text_ofs.x = (drawable_size_remained.width - text_buf_width) / 2.0f;
  381. }
  382. [[fallthrough]];
  383. case HORIZONTAL_ALIGNMENT_FILL:
  384. case HORIZONTAL_ALIGNMENT_LEFT:
  385. case HORIZONTAL_ALIGNMENT_RIGHT: {
  386. text_ofs.x += style_margin_left;
  387. text_ofs.x += left_internal_margin_with_h_separation;
  388. if (icon_align_rtl_checked == HORIZONTAL_ALIGNMENT_LEFT) {
  389. // Offset by the space's width that occupied by icon and h_separation together.
  390. text_ofs.x += custom_element_size.width - drawable_size_remained.width;
  391. }
  392. } break;
  393. }
  394. text_ofs.y = (drawable_size_remained.height - text_buf->get_size().height) / 2.0f + style_margin_top;
  395. if (vertical_icon_alignment == VERTICAL_ALIGNMENT_TOP) {
  396. text_ofs.y += custom_element_size.height - drawable_size_remained.height; // Offset by the icon's height.
  397. }
  398. Color font_outline_color = theme_cache.font_outline_color;
  399. int outline_size = theme_cache.outline_size;
  400. if (outline_size > 0 && font_outline_color.a > 0.0f) {
  401. text_buf->draw_outline(ci, text_ofs, outline_size, font_outline_color);
  402. }
  403. text_buf->draw(ci, text_ofs, font_color);
  404. }
  405. } break;
  406. }
  407. }
  408. Size2 Button::_fit_icon_size(const Size2 &p_size) const {
  409. int max_width = theme_cache.icon_max_width;
  410. Size2 icon_size = p_size;
  411. if (max_width > 0 && icon_size.width > max_width) {
  412. icon_size.height = icon_size.height * max_width / icon_size.width;
  413. icon_size.width = max_width;
  414. }
  415. return icon_size;
  416. }
  417. Size2 Button::get_minimum_size_for_text_and_icon(const String &p_text, Ref<Texture2D> p_icon) const {
  418. // Do not include `_internal_margin`, it's already added in the `get_minimum_size` overrides.
  419. Ref<TextParagraph> paragraph;
  420. if (p_text.is_empty()) {
  421. paragraph = text_buf;
  422. } else {
  423. paragraph.instantiate();
  424. const_cast<Button *>(this)->_shape(paragraph, p_text);
  425. }
  426. Size2 minsize = paragraph->get_size();
  427. if (clip_text || overrun_behavior != TextServer::OVERRUN_NO_TRIMMING || autowrap_mode != TextServer::AUTOWRAP_OFF) {
  428. minsize.width = 0;
  429. }
  430. if (!expand_icon && p_icon.is_valid()) {
  431. Size2 icon_size = _fit_icon_size(p_icon->get_size());
  432. if (vertical_icon_alignment == VERTICAL_ALIGNMENT_CENTER) {
  433. minsize.height = MAX(minsize.height, icon_size.height);
  434. } else {
  435. minsize.height += icon_size.height;
  436. }
  437. if (horizontal_icon_alignment != HORIZONTAL_ALIGNMENT_CENTER) {
  438. minsize.width += icon_size.width;
  439. if (!xl_text.is_empty() || !p_text.is_empty()) {
  440. minsize.width += MAX(0, theme_cache.h_separation);
  441. }
  442. } else {
  443. minsize.width = MAX(minsize.width, icon_size.width);
  444. }
  445. }
  446. if (!xl_text.is_empty() || !p_text.is_empty()) {
  447. Ref<Font> font = theme_cache.font;
  448. float font_height = font->get_height(theme_cache.font_size);
  449. if (vertical_icon_alignment == VERTICAL_ALIGNMENT_CENTER) {
  450. minsize.height = MAX(font_height, minsize.height);
  451. } else {
  452. minsize.height += font_height;
  453. }
  454. }
  455. return (theme_cache.align_to_largest_stylebox ? _get_largest_stylebox_size() : _get_current_stylebox()->get_minimum_size()) + minsize;
  456. }
  457. void Button::_shape(Ref<TextParagraph> p_paragraph, String p_text) {
  458. if (p_paragraph.is_null()) {
  459. p_paragraph = text_buf;
  460. }
  461. if (p_text.is_empty()) {
  462. p_text = xl_text;
  463. }
  464. p_paragraph->clear();
  465. Ref<Font> font = theme_cache.font;
  466. int font_size = theme_cache.font_size;
  467. if (font.is_null() || font_size == 0) {
  468. // Can't shape without a valid font and a non-zero size.
  469. return;
  470. }
  471. BitField<TextServer::LineBreakFlag> autowrap_flags = TextServer::BREAK_MANDATORY;
  472. switch (autowrap_mode) {
  473. case TextServer::AUTOWRAP_WORD_SMART:
  474. autowrap_flags = TextServer::BREAK_WORD_BOUND | TextServer::BREAK_ADAPTIVE | TextServer::BREAK_MANDATORY;
  475. break;
  476. case TextServer::AUTOWRAP_WORD:
  477. autowrap_flags = TextServer::BREAK_WORD_BOUND | TextServer::BREAK_MANDATORY;
  478. break;
  479. case TextServer::AUTOWRAP_ARBITRARY:
  480. autowrap_flags = TextServer::BREAK_GRAPHEME_BOUND | TextServer::BREAK_MANDATORY;
  481. break;
  482. case TextServer::AUTOWRAP_OFF:
  483. break;
  484. }
  485. autowrap_flags = autowrap_flags | TextServer::BREAK_TRIM_EDGE_SPACES;
  486. p_paragraph->set_break_flags(autowrap_flags);
  487. p_paragraph->set_line_spacing(theme_cache.line_spacing);
  488. if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
  489. p_paragraph->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  490. } else {
  491. p_paragraph->set_direction((TextServer::Direction)text_direction);
  492. }
  493. p_paragraph->add_string(p_text, font, font_size, language);
  494. p_paragraph->set_text_overrun_behavior(overrun_behavior);
  495. }
  496. void Button::set_text_overrun_behavior(TextServer::OverrunBehavior p_behavior) {
  497. if (overrun_behavior != p_behavior) {
  498. bool need_update_cache = overrun_behavior == TextServer::OVERRUN_NO_TRIMMING || p_behavior == TextServer::OVERRUN_NO_TRIMMING;
  499. overrun_behavior = p_behavior;
  500. _shape();
  501. if (need_update_cache) {
  502. _queue_update_size_cache();
  503. }
  504. queue_redraw();
  505. update_minimum_size();
  506. }
  507. }
  508. TextServer::OverrunBehavior Button::get_text_overrun_behavior() const {
  509. return overrun_behavior;
  510. }
  511. void Button::set_text(const String &p_text) {
  512. if (text != p_text) {
  513. text = p_text;
  514. xl_text = atr(text);
  515. _shape();
  516. queue_redraw();
  517. update_minimum_size();
  518. }
  519. }
  520. String Button::get_text() const {
  521. return text;
  522. }
  523. void Button::set_autowrap_mode(TextServer::AutowrapMode p_mode) {
  524. if (autowrap_mode != p_mode) {
  525. autowrap_mode = p_mode;
  526. _shape();
  527. queue_redraw();
  528. update_minimum_size();
  529. }
  530. }
  531. TextServer::AutowrapMode Button::get_autowrap_mode() const {
  532. return autowrap_mode;
  533. }
  534. void Button::set_text_direction(Control::TextDirection p_text_direction) {
  535. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  536. if (text_direction != p_text_direction) {
  537. text_direction = p_text_direction;
  538. _shape();
  539. queue_redraw();
  540. }
  541. }
  542. Control::TextDirection Button::get_text_direction() const {
  543. return text_direction;
  544. }
  545. void Button::set_language(const String &p_language) {
  546. if (language != p_language) {
  547. language = p_language;
  548. _shape();
  549. queue_redraw();
  550. }
  551. }
  552. String Button::get_language() const {
  553. return language;
  554. }
  555. void Button::set_button_icon(const Ref<Texture2D> &p_icon) {
  556. if (icon == p_icon) {
  557. return;
  558. }
  559. if (icon.is_valid()) {
  560. icon->disconnect_changed(callable_mp(this, &Button::_texture_changed));
  561. }
  562. icon = p_icon;
  563. if (icon.is_valid()) {
  564. icon->connect_changed(callable_mp(this, &Button::_texture_changed));
  565. }
  566. queue_redraw();
  567. update_minimum_size();
  568. }
  569. void Button::_texture_changed() {
  570. queue_redraw();
  571. update_minimum_size();
  572. }
  573. Ref<Texture2D> Button::get_button_icon() const {
  574. return icon;
  575. }
  576. void Button::set_expand_icon(bool p_enabled) {
  577. if (expand_icon != p_enabled) {
  578. expand_icon = p_enabled;
  579. _queue_update_size_cache();
  580. queue_redraw();
  581. update_minimum_size();
  582. }
  583. }
  584. bool Button::is_expand_icon() const {
  585. return expand_icon;
  586. }
  587. void Button::set_flat(bool p_enabled) {
  588. if (flat != p_enabled) {
  589. flat = p_enabled;
  590. queue_redraw();
  591. }
  592. }
  593. bool Button::is_flat() const {
  594. return flat;
  595. }
  596. void Button::set_clip_text(bool p_enabled) {
  597. if (clip_text != p_enabled) {
  598. clip_text = p_enabled;
  599. _queue_update_size_cache();
  600. queue_redraw();
  601. update_minimum_size();
  602. }
  603. }
  604. bool Button::get_clip_text() const {
  605. return clip_text;
  606. }
  607. void Button::set_text_alignment(HorizontalAlignment p_alignment) {
  608. if (alignment != p_alignment) {
  609. alignment = p_alignment;
  610. queue_redraw();
  611. }
  612. }
  613. HorizontalAlignment Button::get_text_alignment() const {
  614. return alignment;
  615. }
  616. void Button::set_icon_alignment(HorizontalAlignment p_alignment) {
  617. if (horizontal_icon_alignment == p_alignment) {
  618. return;
  619. }
  620. horizontal_icon_alignment = p_alignment;
  621. update_minimum_size();
  622. queue_redraw();
  623. }
  624. void Button::set_vertical_icon_alignment(VerticalAlignment p_alignment) {
  625. if (vertical_icon_alignment == p_alignment) {
  626. return;
  627. }
  628. bool need_update_cache = vertical_icon_alignment == VERTICAL_ALIGNMENT_CENTER || p_alignment == VERTICAL_ALIGNMENT_CENTER;
  629. vertical_icon_alignment = p_alignment;
  630. if (need_update_cache) {
  631. _queue_update_size_cache();
  632. }
  633. update_minimum_size();
  634. queue_redraw();
  635. }
  636. HorizontalAlignment Button::get_icon_alignment() const {
  637. return horizontal_icon_alignment;
  638. }
  639. VerticalAlignment Button::get_vertical_icon_alignment() const {
  640. return vertical_icon_alignment;
  641. }
  642. void Button::_bind_methods() {
  643. ClassDB::bind_method(D_METHOD("set_text", "text"), &Button::set_text);
  644. ClassDB::bind_method(D_METHOD("get_text"), &Button::get_text);
  645. ClassDB::bind_method(D_METHOD("set_text_overrun_behavior", "overrun_behavior"), &Button::set_text_overrun_behavior);
  646. ClassDB::bind_method(D_METHOD("get_text_overrun_behavior"), &Button::get_text_overrun_behavior);
  647. ClassDB::bind_method(D_METHOD("set_autowrap_mode", "autowrap_mode"), &Button::set_autowrap_mode);
  648. ClassDB::bind_method(D_METHOD("get_autowrap_mode"), &Button::get_autowrap_mode);
  649. ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &Button::set_text_direction);
  650. ClassDB::bind_method(D_METHOD("get_text_direction"), &Button::get_text_direction);
  651. ClassDB::bind_method(D_METHOD("set_language", "language"), &Button::set_language);
  652. ClassDB::bind_method(D_METHOD("get_language"), &Button::get_language);
  653. ClassDB::bind_method(D_METHOD("set_button_icon", "texture"), &Button::set_button_icon);
  654. ClassDB::bind_method(D_METHOD("get_button_icon"), &Button::get_button_icon);
  655. ClassDB::bind_method(D_METHOD("set_flat", "enabled"), &Button::set_flat);
  656. ClassDB::bind_method(D_METHOD("is_flat"), &Button::is_flat);
  657. ClassDB::bind_method(D_METHOD("set_clip_text", "enabled"), &Button::set_clip_text);
  658. ClassDB::bind_method(D_METHOD("get_clip_text"), &Button::get_clip_text);
  659. ClassDB::bind_method(D_METHOD("set_text_alignment", "alignment"), &Button::set_text_alignment);
  660. ClassDB::bind_method(D_METHOD("get_text_alignment"), &Button::get_text_alignment);
  661. ClassDB::bind_method(D_METHOD("set_icon_alignment", "icon_alignment"), &Button::set_icon_alignment);
  662. ClassDB::bind_method(D_METHOD("get_icon_alignment"), &Button::get_icon_alignment);
  663. ClassDB::bind_method(D_METHOD("set_vertical_icon_alignment", "vertical_icon_alignment"), &Button::set_vertical_icon_alignment);
  664. ClassDB::bind_method(D_METHOD("get_vertical_icon_alignment"), &Button::get_vertical_icon_alignment);
  665. ClassDB::bind_method(D_METHOD("set_expand_icon", "enabled"), &Button::set_expand_icon);
  666. ClassDB::bind_method(D_METHOD("is_expand_icon"), &Button::is_expand_icon);
  667. ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_MULTILINE_TEXT), "set_text", "get_text");
  668. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_button_icon", "get_button_icon");
  669. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flat"), "set_flat", "is_flat");
  670. ADD_GROUP("Text Behavior", "");
  671. ADD_PROPERTY(PropertyInfo(Variant::INT, "alignment", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_text_alignment", "get_text_alignment");
  672. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_overrun_behavior", PROPERTY_HINT_ENUM, "Trim Nothing,Trim Characters,Trim Words,Ellipsis,Word Ellipsis"), "set_text_overrun_behavior", "get_text_overrun_behavior");
  673. ADD_PROPERTY(PropertyInfo(Variant::INT, "autowrap_mode", PROPERTY_HINT_ENUM, "Off,Arbitrary,Word,Word (Smart)"), "set_autowrap_mode", "get_autowrap_mode");
  674. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clip_text"), "set_clip_text", "get_clip_text");
  675. ADD_GROUP("Icon Behavior", "");
  676. ADD_PROPERTY(PropertyInfo(Variant::INT, "icon_alignment", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_icon_alignment", "get_icon_alignment");
  677. ADD_PROPERTY(PropertyInfo(Variant::INT, "vertical_icon_alignment", PROPERTY_HINT_ENUM, "Top,Center,Bottom"), "set_vertical_icon_alignment", "get_vertical_icon_alignment");
  678. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "expand_icon"), "set_expand_icon", "is_expand_icon");
  679. ADD_GROUP("BiDi", "");
  680. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_text_direction", "get_text_direction");
  681. ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID, ""), "set_language", "get_language");
  682. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Button, normal);
  683. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Button, normal_mirrored);
  684. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Button, pressed);
  685. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Button, pressed_mirrored);
  686. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Button, hover);
  687. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Button, hover_mirrored);
  688. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Button, hover_pressed);
  689. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Button, hover_pressed_mirrored);
  690. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Button, disabled);
  691. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Button, disabled_mirrored);
  692. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Button, focus);
  693. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Button, font_color);
  694. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Button, font_focus_color);
  695. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Button, font_pressed_color);
  696. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Button, font_hover_color);
  697. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Button, font_hover_pressed_color);
  698. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Button, font_disabled_color);
  699. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, Button, font);
  700. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, Button, font_size);
  701. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Button, outline_size);
  702. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Button, font_outline_color);
  703. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Button, icon_normal_color);
  704. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Button, icon_focus_color);
  705. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Button, icon_pressed_color);
  706. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Button, icon_hover_color);
  707. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Button, icon_hover_pressed_color);
  708. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Button, icon_disabled_color);
  709. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Button, icon);
  710. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Button, h_separation);
  711. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Button, icon_max_width);
  712. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Button, align_to_largest_stylebox);
  713. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Button, line_spacing);
  714. }
  715. Button::Button(const String &p_text) {
  716. text_buf.instantiate();
  717. text_buf->set_break_flags(TextServer::BREAK_MANDATORY | TextServer::BREAK_TRIM_EDGE_SPACES);
  718. set_mouse_filter(MOUSE_FILTER_STOP);
  719. set_text(p_text);
  720. }
  721. Button::~Button() {
  722. }