button.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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. // Reshape and update size min. if text is invalidated by an external source (e.g., oversampling).
  191. if (text_buf.is_valid() && !TS->shaped_text_is_ready(text_buf->get_rid())) {
  192. _shape();
  193. update_minimum_size();
  194. }
  195. const RID ci = get_canvas_item();
  196. const Size2 size = get_size();
  197. Ref<StyleBox> style = _get_current_stylebox();
  198. // Draws the stylebox in the current state.
  199. if (!flat) {
  200. style->draw(ci, Rect2(Point2(), size));
  201. }
  202. if (has_focus()) {
  203. theme_cache.focus->draw(ci, Rect2(Point2(), size));
  204. }
  205. Ref<Texture2D> _icon = icon;
  206. if (_icon.is_null() && has_theme_icon(SNAME("icon"))) {
  207. _icon = theme_cache.icon;
  208. }
  209. if (xl_text.is_empty() && _icon.is_null()) {
  210. break;
  211. }
  212. const float style_margin_left = (theme_cache.align_to_largest_stylebox) ? theme_cache.style_margin_left : style->get_margin(SIDE_LEFT);
  213. const float style_margin_right = (theme_cache.align_to_largest_stylebox) ? theme_cache.style_margin_right : style->get_margin(SIDE_RIGHT);
  214. const float style_margin_top = (theme_cache.align_to_largest_stylebox) ? theme_cache.style_margin_top : style->get_margin(SIDE_TOP);
  215. const float style_margin_bottom = (theme_cache.align_to_largest_stylebox) ? theme_cache.style_margin_bottom : style->get_margin(SIDE_BOTTOM);
  216. Size2 drawable_size_remained = size;
  217. { // The size after the stelybox is stripped.
  218. drawable_size_remained.width -= style_margin_left + style_margin_right;
  219. drawable_size_remained.height -= style_margin_top + style_margin_bottom;
  220. }
  221. const int h_separation = MAX(0, theme_cache.h_separation);
  222. float left_internal_margin_with_h_separation = _internal_margin[SIDE_LEFT];
  223. float right_internal_margin_with_h_separation = _internal_margin[SIDE_RIGHT];
  224. { // The width reserved for internal element in derived classes (and h_separation if needed).
  225. if (_internal_margin[SIDE_LEFT] > 0.0f) {
  226. left_internal_margin_with_h_separation += h_separation;
  227. }
  228. if (_internal_margin[SIDE_RIGHT] > 0.0f) {
  229. right_internal_margin_with_h_separation += h_separation;
  230. }
  231. drawable_size_remained.width -= left_internal_margin_with_h_separation + right_internal_margin_with_h_separation; // The size after the internal element is stripped.
  232. }
  233. HorizontalAlignment icon_align_rtl_checked = horizontal_icon_alignment;
  234. HorizontalAlignment align_rtl_checked = alignment;
  235. // Swap icon and text alignment sides if right-to-left layout is set.
  236. if (is_layout_rtl()) {
  237. if (horizontal_icon_alignment == HORIZONTAL_ALIGNMENT_RIGHT) {
  238. icon_align_rtl_checked = HORIZONTAL_ALIGNMENT_LEFT;
  239. } else if (horizontal_icon_alignment == HORIZONTAL_ALIGNMENT_LEFT) {
  240. icon_align_rtl_checked = HORIZONTAL_ALIGNMENT_RIGHT;
  241. }
  242. if (alignment == HORIZONTAL_ALIGNMENT_RIGHT) {
  243. align_rtl_checked = HORIZONTAL_ALIGNMENT_LEFT;
  244. } else if (alignment == HORIZONTAL_ALIGNMENT_LEFT) {
  245. align_rtl_checked = HORIZONTAL_ALIGNMENT_RIGHT;
  246. }
  247. }
  248. Color font_color;
  249. Color icon_modulate_color(1, 1, 1, 1);
  250. // Get the font color and icon modulate color in the current state.
  251. switch (get_draw_mode()) {
  252. case DRAW_NORMAL: {
  253. // Focus colors only take precedence over normal state.
  254. if (has_focus()) {
  255. font_color = theme_cache.font_focus_color;
  256. if (has_theme_color(SNAME("icon_focus_color"))) {
  257. icon_modulate_color = theme_cache.icon_focus_color;
  258. }
  259. } else {
  260. font_color = theme_cache.font_color;
  261. if (has_theme_color(SNAME("icon_normal_color"))) {
  262. icon_modulate_color = theme_cache.icon_normal_color;
  263. }
  264. }
  265. } break;
  266. case DRAW_HOVER_PRESSED: {
  267. font_color = theme_cache.font_hover_pressed_color;
  268. if (has_theme_color(SNAME("icon_hover_pressed_color"))) {
  269. icon_modulate_color = theme_cache.icon_hover_pressed_color;
  270. }
  271. } break;
  272. case DRAW_PRESSED: {
  273. if (has_theme_color(SNAME("font_pressed_color"))) {
  274. font_color = theme_cache.font_pressed_color;
  275. } else {
  276. font_color = theme_cache.font_color;
  277. }
  278. if (has_theme_color(SNAME("icon_pressed_color"))) {
  279. icon_modulate_color = theme_cache.icon_pressed_color;
  280. }
  281. } break;
  282. case DRAW_HOVER: {
  283. font_color = theme_cache.font_hover_color;
  284. if (has_theme_color(SNAME("icon_hover_color"))) {
  285. icon_modulate_color = theme_cache.icon_hover_color;
  286. }
  287. } break;
  288. case DRAW_DISABLED: {
  289. font_color = theme_cache.font_disabled_color;
  290. if (has_theme_color(SNAME("icon_disabled_color"))) {
  291. icon_modulate_color = theme_cache.icon_disabled_color;
  292. } else {
  293. icon_modulate_color.a = 0.4;
  294. }
  295. } break;
  296. }
  297. const bool is_clipped = clip_text || overrun_behavior != TextServer::OVERRUN_NO_TRIMMING || autowrap_mode != TextServer::AUTOWRAP_OFF;
  298. const Size2 custom_element_size = drawable_size_remained;
  299. // Draw the icon.
  300. if (_icon.is_valid()) {
  301. Size2 icon_size;
  302. { // Calculate the drawing size of the icon.
  303. icon_size = _icon->get_size();
  304. if (expand_icon) {
  305. const Size2 text_buf_size = text_buf->get_size();
  306. Size2 _size = custom_element_size;
  307. if (!is_clipped && icon_align_rtl_checked != HORIZONTAL_ALIGNMENT_CENTER && text_buf_size.width > 0.0f) {
  308. // If there is not enough space for icon and h_separation, h_separation will occupy the space first,
  309. // so the icon's width may be negative. Keep it negative to make it easier to calculate the space
  310. // reserved for text later.
  311. _size.width -= text_buf_size.width + h_separation;
  312. }
  313. if (vertical_icon_alignment != VERTICAL_ALIGNMENT_CENTER) {
  314. _size.height -= text_buf_size.height;
  315. }
  316. float icon_width = icon_size.width * _size.height / icon_size.height;
  317. float icon_height = _size.height;
  318. if (icon_width > _size.width) {
  319. icon_width = _size.width;
  320. icon_height = icon_size.height * icon_width / icon_size.width;
  321. }
  322. icon_size = Size2(icon_width, icon_height);
  323. }
  324. icon_size = _fit_icon_size(icon_size);
  325. icon_size = icon_size.round();
  326. }
  327. if (icon_size.width > 0.0f) {
  328. // Calculate the drawing position of the icon.
  329. Point2 icon_ofs;
  330. switch (icon_align_rtl_checked) {
  331. case HORIZONTAL_ALIGNMENT_CENTER: {
  332. icon_ofs.x = (custom_element_size.width - icon_size.width) / 2.0f;
  333. }
  334. [[fallthrough]];
  335. case HORIZONTAL_ALIGNMENT_FILL:
  336. case HORIZONTAL_ALIGNMENT_LEFT: {
  337. icon_ofs.x += style_margin_left;
  338. icon_ofs.x += left_internal_margin_with_h_separation;
  339. } break;
  340. case HORIZONTAL_ALIGNMENT_RIGHT: {
  341. icon_ofs.x = size.x - style_margin_right;
  342. icon_ofs.x -= right_internal_margin_with_h_separation;
  343. icon_ofs.x -= icon_size.width;
  344. } break;
  345. }
  346. switch (vertical_icon_alignment) {
  347. case VERTICAL_ALIGNMENT_CENTER: {
  348. icon_ofs.y = (custom_element_size.height - icon_size.height) / 2.0f;
  349. }
  350. [[fallthrough]];
  351. case VERTICAL_ALIGNMENT_FILL:
  352. case VERTICAL_ALIGNMENT_TOP: {
  353. icon_ofs.y += style_margin_top;
  354. } break;
  355. case VERTICAL_ALIGNMENT_BOTTOM: {
  356. icon_ofs.y = size.y - style_margin_bottom - icon_size.height;
  357. } break;
  358. }
  359. icon_ofs = icon_ofs.floor();
  360. Rect2 icon_region = Rect2(icon_ofs, icon_size);
  361. draw_texture_rect(_icon, icon_region, false, icon_modulate_color);
  362. }
  363. if (!xl_text.is_empty()) {
  364. // Update the size after the icon is stripped. Stripping only when the icon alignments are not center.
  365. if (icon_align_rtl_checked != HORIZONTAL_ALIGNMENT_CENTER) {
  366. // Subtract the space's width occupied by icon and h_separation together.
  367. drawable_size_remained.width -= icon_size.width + h_separation;
  368. }
  369. if (vertical_icon_alignment != VERTICAL_ALIGNMENT_CENTER) {
  370. drawable_size_remained.height -= icon_size.height;
  371. }
  372. }
  373. }
  374. // Draw the text.
  375. if (!xl_text.is_empty()) {
  376. text_buf->set_alignment(align_rtl_checked);
  377. float text_buf_width = Math::ceil(MAX(1.0f, drawable_size_remained.width)); // The space's width filled by the text_buf.
  378. if (autowrap_mode != TextServer::AUTOWRAP_OFF && !Math::is_equal_approx(text_buf_width, text_buf->get_width())) {
  379. update_minimum_size();
  380. }
  381. text_buf->set_width(text_buf_width);
  382. Point2 text_ofs;
  383. switch (align_rtl_checked) {
  384. case HORIZONTAL_ALIGNMENT_CENTER: {
  385. text_ofs.x = (drawable_size_remained.width - text_buf_width) / 2.0f;
  386. }
  387. [[fallthrough]];
  388. case HORIZONTAL_ALIGNMENT_FILL:
  389. case HORIZONTAL_ALIGNMENT_LEFT:
  390. case HORIZONTAL_ALIGNMENT_RIGHT: {
  391. text_ofs.x += style_margin_left;
  392. text_ofs.x += left_internal_margin_with_h_separation;
  393. if (icon_align_rtl_checked == HORIZONTAL_ALIGNMENT_LEFT) {
  394. // Offset by the space's width that occupied by icon and h_separation together.
  395. text_ofs.x += custom_element_size.width - drawable_size_remained.width;
  396. }
  397. } break;
  398. }
  399. text_ofs.y = (drawable_size_remained.height - text_buf->get_size().height) / 2.0f + style_margin_top;
  400. if (vertical_icon_alignment == VERTICAL_ALIGNMENT_TOP) {
  401. text_ofs.y += custom_element_size.height - drawable_size_remained.height; // Offset by the icon's height.
  402. }
  403. Color font_outline_color = theme_cache.font_outline_color;
  404. int outline_size = theme_cache.outline_size;
  405. if (outline_size > 0 && font_outline_color.a > 0.0f) {
  406. text_buf->draw_outline(ci, text_ofs, outline_size, font_outline_color);
  407. }
  408. text_buf->draw(ci, text_ofs, font_color);
  409. }
  410. } break;
  411. }
  412. }
  413. Size2 Button::_fit_icon_size(const Size2 &p_size) const {
  414. int max_width = theme_cache.icon_max_width;
  415. Size2 icon_size = p_size;
  416. if (max_width > 0 && icon_size.width > max_width) {
  417. icon_size.height = icon_size.height * max_width / icon_size.width;
  418. icon_size.width = max_width;
  419. }
  420. return icon_size;
  421. }
  422. Size2 Button::get_minimum_size_for_text_and_icon(const String &p_text, Ref<Texture2D> p_icon) const {
  423. // Do not include `_internal_margin`, it's already added in the `get_minimum_size` overrides.
  424. Ref<TextParagraph> paragraph;
  425. if (p_text.is_empty()) {
  426. paragraph = text_buf;
  427. } else {
  428. paragraph.instantiate();
  429. _shape(paragraph, p_text);
  430. }
  431. Size2 minsize = paragraph->get_size();
  432. if (clip_text || overrun_behavior != TextServer::OVERRUN_NO_TRIMMING || autowrap_mode != TextServer::AUTOWRAP_OFF) {
  433. minsize.width = 0;
  434. }
  435. if (!expand_icon && p_icon.is_valid()) {
  436. Size2 icon_size = _fit_icon_size(p_icon->get_size());
  437. if (vertical_icon_alignment == VERTICAL_ALIGNMENT_CENTER) {
  438. minsize.height = MAX(minsize.height, icon_size.height);
  439. } else {
  440. minsize.height += icon_size.height;
  441. }
  442. if (horizontal_icon_alignment != HORIZONTAL_ALIGNMENT_CENTER) {
  443. minsize.width += icon_size.width;
  444. if (!xl_text.is_empty() || !p_text.is_empty()) {
  445. minsize.width += MAX(0, theme_cache.h_separation);
  446. }
  447. } else {
  448. minsize.width = MAX(minsize.width, icon_size.width);
  449. }
  450. }
  451. if (!xl_text.is_empty() || !p_text.is_empty()) {
  452. Ref<Font> font = theme_cache.font;
  453. float font_height = font->get_height(theme_cache.font_size);
  454. if (vertical_icon_alignment == VERTICAL_ALIGNMENT_CENTER) {
  455. minsize.height = MAX(font_height, minsize.height);
  456. } else {
  457. minsize.height += font_height;
  458. }
  459. }
  460. return (theme_cache.align_to_largest_stylebox ? _get_largest_stylebox_size() : _get_current_stylebox()->get_minimum_size()) + minsize;
  461. }
  462. void Button::_shape(Ref<TextParagraph> p_paragraph, String p_text) const {
  463. if (p_paragraph.is_null()) {
  464. p_paragraph = text_buf;
  465. }
  466. if (p_text.is_empty()) {
  467. p_text = xl_text;
  468. }
  469. p_paragraph->clear();
  470. Ref<Font> font = theme_cache.font;
  471. int font_size = theme_cache.font_size;
  472. if (font.is_null() || font_size == 0) {
  473. // Can't shape without a valid font and a non-zero size.
  474. return;
  475. }
  476. BitField<TextServer::LineBreakFlag> autowrap_flags = TextServer::BREAK_MANDATORY;
  477. switch (autowrap_mode) {
  478. case TextServer::AUTOWRAP_WORD_SMART:
  479. autowrap_flags = TextServer::BREAK_WORD_BOUND | TextServer::BREAK_ADAPTIVE | TextServer::BREAK_MANDATORY;
  480. break;
  481. case TextServer::AUTOWRAP_WORD:
  482. autowrap_flags = TextServer::BREAK_WORD_BOUND | TextServer::BREAK_MANDATORY;
  483. break;
  484. case TextServer::AUTOWRAP_ARBITRARY:
  485. autowrap_flags = TextServer::BREAK_GRAPHEME_BOUND | TextServer::BREAK_MANDATORY;
  486. break;
  487. case TextServer::AUTOWRAP_OFF:
  488. break;
  489. }
  490. autowrap_flags = autowrap_flags | autowrap_flags_trim;
  491. p_paragraph->set_break_flags(autowrap_flags);
  492. p_paragraph->set_line_spacing(theme_cache.line_spacing);
  493. if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
  494. p_paragraph->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  495. } else {
  496. p_paragraph->set_direction((TextServer::Direction)text_direction);
  497. }
  498. p_paragraph->add_string(p_text, font, font_size, language);
  499. p_paragraph->set_text_overrun_behavior(overrun_behavior);
  500. }
  501. void Button::set_text_overrun_behavior(TextServer::OverrunBehavior p_behavior) {
  502. if (overrun_behavior != p_behavior) {
  503. bool need_update_cache = overrun_behavior == TextServer::OVERRUN_NO_TRIMMING || p_behavior == TextServer::OVERRUN_NO_TRIMMING;
  504. overrun_behavior = p_behavior;
  505. _shape();
  506. if (need_update_cache) {
  507. _queue_update_size_cache();
  508. }
  509. queue_redraw();
  510. update_minimum_size();
  511. }
  512. }
  513. TextServer::OverrunBehavior Button::get_text_overrun_behavior() const {
  514. return overrun_behavior;
  515. }
  516. void Button::set_text(const String &p_text) {
  517. if (text != p_text) {
  518. text = p_text;
  519. xl_text = atr(text);
  520. _shape();
  521. queue_redraw();
  522. update_minimum_size();
  523. }
  524. }
  525. String Button::get_text() const {
  526. return text;
  527. }
  528. void Button::set_autowrap_mode(TextServer::AutowrapMode p_mode) {
  529. if (autowrap_mode != p_mode) {
  530. autowrap_mode = p_mode;
  531. _shape();
  532. queue_redraw();
  533. update_minimum_size();
  534. }
  535. }
  536. TextServer::AutowrapMode Button::get_autowrap_mode() const {
  537. return autowrap_mode;
  538. }
  539. void Button::set_autowrap_trim_flags(BitField<TextServer::LineBreakFlag> p_flags) {
  540. if (autowrap_flags_trim != (p_flags & TextServer::BREAK_TRIM_MASK)) {
  541. autowrap_flags_trim = p_flags & TextServer::BREAK_TRIM_MASK;
  542. _shape();
  543. queue_redraw();
  544. update_minimum_size();
  545. }
  546. }
  547. BitField<TextServer::LineBreakFlag> Button::get_autowrap_trim_flags() const {
  548. return autowrap_flags_trim;
  549. }
  550. void Button::set_text_direction(Control::TextDirection p_text_direction) {
  551. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  552. if (text_direction != p_text_direction) {
  553. text_direction = p_text_direction;
  554. _shape();
  555. queue_redraw();
  556. }
  557. }
  558. Control::TextDirection Button::get_text_direction() const {
  559. return text_direction;
  560. }
  561. void Button::set_language(const String &p_language) {
  562. if (language != p_language) {
  563. language = p_language;
  564. _shape();
  565. queue_redraw();
  566. }
  567. }
  568. String Button::get_language() const {
  569. return language;
  570. }
  571. void Button::set_button_icon(const Ref<Texture2D> &p_icon) {
  572. if (icon == p_icon) {
  573. return;
  574. }
  575. if (icon.is_valid()) {
  576. icon->disconnect_changed(callable_mp(this, &Button::_texture_changed));
  577. }
  578. icon = p_icon;
  579. if (icon.is_valid()) {
  580. icon->connect_changed(callable_mp(this, &Button::_texture_changed));
  581. }
  582. queue_redraw();
  583. update_minimum_size();
  584. }
  585. void Button::_texture_changed() {
  586. queue_redraw();
  587. update_minimum_size();
  588. }
  589. Ref<Texture2D> Button::get_button_icon() const {
  590. return icon;
  591. }
  592. void Button::set_expand_icon(bool p_enabled) {
  593. if (expand_icon != p_enabled) {
  594. expand_icon = p_enabled;
  595. _queue_update_size_cache();
  596. queue_redraw();
  597. update_minimum_size();
  598. }
  599. }
  600. bool Button::is_expand_icon() const {
  601. return expand_icon;
  602. }
  603. void Button::set_flat(bool p_enabled) {
  604. if (flat != p_enabled) {
  605. flat = p_enabled;
  606. queue_redraw();
  607. }
  608. }
  609. bool Button::is_flat() const {
  610. return flat;
  611. }
  612. void Button::set_clip_text(bool p_enabled) {
  613. if (clip_text != p_enabled) {
  614. clip_text = p_enabled;
  615. _queue_update_size_cache();
  616. queue_redraw();
  617. update_minimum_size();
  618. }
  619. }
  620. bool Button::get_clip_text() const {
  621. return clip_text;
  622. }
  623. void Button::set_text_alignment(HorizontalAlignment p_alignment) {
  624. if (alignment != p_alignment) {
  625. alignment = p_alignment;
  626. queue_redraw();
  627. }
  628. }
  629. HorizontalAlignment Button::get_text_alignment() const {
  630. return alignment;
  631. }
  632. void Button::set_icon_alignment(HorizontalAlignment p_alignment) {
  633. if (horizontal_icon_alignment == p_alignment) {
  634. return;
  635. }
  636. horizontal_icon_alignment = p_alignment;
  637. update_minimum_size();
  638. queue_redraw();
  639. }
  640. void Button::set_vertical_icon_alignment(VerticalAlignment p_alignment) {
  641. if (vertical_icon_alignment == p_alignment) {
  642. return;
  643. }
  644. bool need_update_cache = vertical_icon_alignment == VERTICAL_ALIGNMENT_CENTER || p_alignment == VERTICAL_ALIGNMENT_CENTER;
  645. vertical_icon_alignment = p_alignment;
  646. if (need_update_cache) {
  647. _queue_update_size_cache();
  648. }
  649. update_minimum_size();
  650. queue_redraw();
  651. }
  652. HorizontalAlignment Button::get_icon_alignment() const {
  653. return horizontal_icon_alignment;
  654. }
  655. VerticalAlignment Button::get_vertical_icon_alignment() const {
  656. return vertical_icon_alignment;
  657. }
  658. void Button::_bind_methods() {
  659. ClassDB::bind_method(D_METHOD("set_text", "text"), &Button::set_text);
  660. ClassDB::bind_method(D_METHOD("get_text"), &Button::get_text);
  661. ClassDB::bind_method(D_METHOD("set_text_overrun_behavior", "overrun_behavior"), &Button::set_text_overrun_behavior);
  662. ClassDB::bind_method(D_METHOD("get_text_overrun_behavior"), &Button::get_text_overrun_behavior);
  663. ClassDB::bind_method(D_METHOD("set_autowrap_mode", "autowrap_mode"), &Button::set_autowrap_mode);
  664. ClassDB::bind_method(D_METHOD("get_autowrap_mode"), &Button::get_autowrap_mode);
  665. ClassDB::bind_method(D_METHOD("set_autowrap_trim_flags", "autowrap_trim_flags"), &Button::set_autowrap_trim_flags);
  666. ClassDB::bind_method(D_METHOD("get_autowrap_trim_flags"), &Button::get_autowrap_trim_flags);
  667. ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &Button::set_text_direction);
  668. ClassDB::bind_method(D_METHOD("get_text_direction"), &Button::get_text_direction);
  669. ClassDB::bind_method(D_METHOD("set_language", "language"), &Button::set_language);
  670. ClassDB::bind_method(D_METHOD("get_language"), &Button::get_language);
  671. ClassDB::bind_method(D_METHOD("set_button_icon", "texture"), &Button::set_button_icon);
  672. ClassDB::bind_method(D_METHOD("get_button_icon"), &Button::get_button_icon);
  673. ClassDB::bind_method(D_METHOD("set_flat", "enabled"), &Button::set_flat);
  674. ClassDB::bind_method(D_METHOD("is_flat"), &Button::is_flat);
  675. ClassDB::bind_method(D_METHOD("set_clip_text", "enabled"), &Button::set_clip_text);
  676. ClassDB::bind_method(D_METHOD("get_clip_text"), &Button::get_clip_text);
  677. ClassDB::bind_method(D_METHOD("set_text_alignment", "alignment"), &Button::set_text_alignment);
  678. ClassDB::bind_method(D_METHOD("get_text_alignment"), &Button::get_text_alignment);
  679. ClassDB::bind_method(D_METHOD("set_icon_alignment", "icon_alignment"), &Button::set_icon_alignment);
  680. ClassDB::bind_method(D_METHOD("get_icon_alignment"), &Button::get_icon_alignment);
  681. ClassDB::bind_method(D_METHOD("set_vertical_icon_alignment", "vertical_icon_alignment"), &Button::set_vertical_icon_alignment);
  682. ClassDB::bind_method(D_METHOD("get_vertical_icon_alignment"), &Button::get_vertical_icon_alignment);
  683. ClassDB::bind_method(D_METHOD("set_expand_icon", "enabled"), &Button::set_expand_icon);
  684. ClassDB::bind_method(D_METHOD("is_expand_icon"), &Button::is_expand_icon);
  685. ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_MULTILINE_TEXT), "set_text", "get_text");
  686. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_button_icon", "get_button_icon");
  687. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flat"), "set_flat", "is_flat");
  688. ADD_GROUP("Text Behavior", "");
  689. ADD_PROPERTY(PropertyInfo(Variant::INT, "alignment", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_text_alignment", "get_text_alignment");
  690. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_overrun_behavior", PROPERTY_HINT_ENUM, "Trim Nothing,Trim Characters,Trim Words,Ellipsis (6+ Characters),Word Ellipsis (6+ Characters),Ellipsis (Always),Word Ellipsis (Always)"), "set_text_overrun_behavior", "get_text_overrun_behavior");
  691. ADD_PROPERTY(PropertyInfo(Variant::INT, "autowrap_mode", PROPERTY_HINT_ENUM, "Off,Arbitrary,Word,Word (Smart)"), "set_autowrap_mode", "get_autowrap_mode");
  692. ADD_PROPERTY(PropertyInfo(Variant::INT, "autowrap_trim_flags", PROPERTY_HINT_FLAGS, vformat("Trim Spaces After Break:%d,Trim Spaces Before Break:%d", TextServer::BREAK_TRIM_START_EDGE_SPACES, TextServer::BREAK_TRIM_END_EDGE_SPACES)), "set_autowrap_trim_flags", "get_autowrap_trim_flags");
  693. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clip_text"), "set_clip_text", "get_clip_text");
  694. ADD_GROUP("Icon Behavior", "");
  695. ADD_PROPERTY(PropertyInfo(Variant::INT, "icon_alignment", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_icon_alignment", "get_icon_alignment");
  696. ADD_PROPERTY(PropertyInfo(Variant::INT, "vertical_icon_alignment", PROPERTY_HINT_ENUM, "Top,Center,Bottom"), "set_vertical_icon_alignment", "get_vertical_icon_alignment");
  697. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "expand_icon"), "set_expand_icon", "is_expand_icon");
  698. ADD_GROUP("BiDi", "");
  699. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_text_direction", "get_text_direction");
  700. ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID, ""), "set_language", "get_language");
  701. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Button, normal);
  702. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Button, normal_mirrored);
  703. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Button, pressed);
  704. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Button, pressed_mirrored);
  705. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Button, hover);
  706. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Button, hover_mirrored);
  707. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Button, hover_pressed);
  708. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Button, hover_pressed_mirrored);
  709. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Button, disabled);
  710. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Button, disabled_mirrored);
  711. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Button, focus);
  712. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Button, font_color);
  713. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Button, font_focus_color);
  714. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Button, font_pressed_color);
  715. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Button, font_hover_color);
  716. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Button, font_hover_pressed_color);
  717. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Button, font_disabled_color);
  718. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, Button, font);
  719. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, Button, font_size);
  720. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Button, outline_size);
  721. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Button, font_outline_color);
  722. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Button, icon_normal_color);
  723. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Button, icon_focus_color);
  724. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Button, icon_pressed_color);
  725. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Button, icon_hover_color);
  726. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Button, icon_hover_pressed_color);
  727. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Button, icon_disabled_color);
  728. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Button, icon);
  729. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Button, h_separation);
  730. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Button, icon_max_width);
  731. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Button, align_to_largest_stylebox);
  732. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Button, line_spacing);
  733. }
  734. Button::Button(const String &p_text) {
  735. text_buf.instantiate();
  736. text_buf->set_break_flags(TextServer::BREAK_MANDATORY | autowrap_flags_trim);
  737. set_mouse_filter(MOUSE_FILTER_STOP);
  738. set_text(p_text);
  739. }
  740. Button::~Button() {
  741. }