button.cpp 31 KB

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