button.cpp 35 KB

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