button.cpp 26 KB

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