button.cpp 34 KB

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