button.cpp 35 KB

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