button.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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 "servers/rendering_server.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::_update_theme_item_cache() {
  44. BaseButton::_update_theme_item_cache();
  45. theme_cache.normal = get_theme_stylebox(SNAME("normal"));
  46. theme_cache.normal_mirrored = get_theme_stylebox(SNAME("normal_mirrored"));
  47. theme_cache.pressed = get_theme_stylebox(SNAME("pressed"));
  48. theme_cache.pressed_mirrored = get_theme_stylebox(SNAME("pressed_mirrored"));
  49. theme_cache.hover = get_theme_stylebox(SNAME("hover"));
  50. theme_cache.hover_mirrored = get_theme_stylebox(SNAME("hover_mirrored"));
  51. theme_cache.hover_pressed = get_theme_stylebox(SNAME("hover_pressed"));
  52. theme_cache.hover_pressed_mirrored = get_theme_stylebox(SNAME("hover_pressed_mirrored"));
  53. theme_cache.disabled = get_theme_stylebox(SNAME("disabled"));
  54. theme_cache.disabled_mirrored = get_theme_stylebox(SNAME("disabled_mirrored"));
  55. theme_cache.focus = get_theme_stylebox(SNAME("focus"));
  56. theme_cache.font_color = get_theme_color(SNAME("font_color"));
  57. theme_cache.font_focus_color = get_theme_color(SNAME("font_focus_color"));
  58. theme_cache.font_pressed_color = get_theme_color(SNAME("font_pressed_color"));
  59. theme_cache.font_hover_color = get_theme_color(SNAME("font_hover_color"));
  60. theme_cache.font_hover_pressed_color = get_theme_color(SNAME("font_hover_pressed_color"));
  61. theme_cache.font_disabled_color = get_theme_color(SNAME("font_disabled_color"));
  62. theme_cache.font = get_theme_font(SNAME("font"));
  63. theme_cache.font_size = get_theme_font_size(SNAME("font_size"));
  64. theme_cache.outline_size = get_theme_constant(SNAME("outline_size"));
  65. theme_cache.font_outline_color = get_theme_color(SNAME("font_outline_color"));
  66. theme_cache.icon_normal_color = get_theme_color(SNAME("icon_normal_color"));
  67. theme_cache.icon_focus_color = get_theme_color(SNAME("icon_focus_color"));
  68. theme_cache.icon_pressed_color = get_theme_color(SNAME("icon_pressed_color"));
  69. theme_cache.icon_hover_color = get_theme_color(SNAME("icon_hover_color"));
  70. theme_cache.icon_hover_pressed_color = get_theme_color(SNAME("icon_hover_pressed_color"));
  71. theme_cache.icon_disabled_color = get_theme_color(SNAME("icon_disabled_color"));
  72. theme_cache.icon = get_theme_icon(SNAME("icon"));
  73. theme_cache.h_separation = get_theme_constant(SNAME("h_separation"));
  74. theme_cache.icon_max_width = get_theme_constant(SNAME("icon_max_width"));
  75. }
  76. void Button::_notification(int p_what) {
  77. switch (p_what) {
  78. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  79. queue_redraw();
  80. } break;
  81. case NOTIFICATION_TRANSLATION_CHANGED: {
  82. xl_text = atr(text);
  83. _shape();
  84. update_minimum_size();
  85. queue_redraw();
  86. } break;
  87. case NOTIFICATION_THEME_CHANGED: {
  88. _shape();
  89. update_minimum_size();
  90. queue_redraw();
  91. } break;
  92. case NOTIFICATION_DRAW: {
  93. RID ci = get_canvas_item();
  94. Size2 size = get_size();
  95. Color color;
  96. Color color_icon(1, 1, 1, 1);
  97. Ref<StyleBox> style = theme_cache.normal;
  98. bool rtl = is_layout_rtl();
  99. switch (get_draw_mode()) {
  100. case DRAW_NORMAL: {
  101. if (rtl && has_theme_stylebox(SNAME("normal_mirrored"))) {
  102. style = theme_cache.normal_mirrored;
  103. } else {
  104. style = theme_cache.normal;
  105. }
  106. if (!flat) {
  107. style->draw(ci, Rect2(Point2(0, 0), size));
  108. }
  109. // Focus colors only take precedence over normal state.
  110. if (has_focus()) {
  111. color = theme_cache.font_focus_color;
  112. if (has_theme_color(SNAME("icon_focus_color"))) {
  113. color_icon = theme_cache.icon_focus_color;
  114. }
  115. } else {
  116. color = theme_cache.font_color;
  117. if (has_theme_color(SNAME("icon_normal_color"))) {
  118. color_icon = theme_cache.icon_normal_color;
  119. }
  120. }
  121. } break;
  122. case DRAW_HOVER_PRESSED: {
  123. // Edge case for CheckButton and CheckBox.
  124. if (has_theme_stylebox("hover_pressed")) {
  125. if (rtl && has_theme_stylebox(SNAME("hover_pressed_mirrored"))) {
  126. style = theme_cache.hover_pressed_mirrored;
  127. } else {
  128. style = theme_cache.hover_pressed;
  129. }
  130. if (!flat) {
  131. style->draw(ci, Rect2(Point2(0, 0), size));
  132. }
  133. if (has_theme_color(SNAME("font_hover_pressed_color"))) {
  134. color = theme_cache.font_hover_pressed_color;
  135. }
  136. if (has_theme_color(SNAME("icon_hover_pressed_color"))) {
  137. color_icon = theme_cache.icon_hover_pressed_color;
  138. }
  139. break;
  140. }
  141. [[fallthrough]];
  142. }
  143. case DRAW_PRESSED: {
  144. if (rtl && has_theme_stylebox(SNAME("pressed_mirrored"))) {
  145. style = theme_cache.pressed_mirrored;
  146. } else {
  147. style = theme_cache.pressed;
  148. }
  149. if (!flat) {
  150. style->draw(ci, Rect2(Point2(0, 0), size));
  151. }
  152. if (has_theme_color(SNAME("font_pressed_color"))) {
  153. color = theme_cache.font_pressed_color;
  154. } else {
  155. color = theme_cache.font_color;
  156. }
  157. if (has_theme_color(SNAME("icon_pressed_color"))) {
  158. color_icon = theme_cache.icon_pressed_color;
  159. }
  160. } break;
  161. case DRAW_HOVER: {
  162. if (rtl && has_theme_stylebox(SNAME("hover_mirrored"))) {
  163. style = theme_cache.hover_mirrored;
  164. } else {
  165. style = theme_cache.hover;
  166. }
  167. if (!flat) {
  168. style->draw(ci, Rect2(Point2(0, 0), size));
  169. }
  170. color = theme_cache.font_hover_color;
  171. if (has_theme_color(SNAME("icon_hover_color"))) {
  172. color_icon = theme_cache.icon_hover_color;
  173. }
  174. } break;
  175. case DRAW_DISABLED: {
  176. if (rtl && has_theme_stylebox(SNAME("disabled_mirrored"))) {
  177. style = theme_cache.disabled_mirrored;
  178. } else {
  179. style = theme_cache.disabled;
  180. }
  181. if (!flat) {
  182. style->draw(ci, Rect2(Point2(0, 0), size));
  183. }
  184. color = theme_cache.font_disabled_color;
  185. if (has_theme_color(SNAME("icon_disabled_color"))) {
  186. color_icon = theme_cache.icon_disabled_color;
  187. } else {
  188. color_icon.a = 0.4;
  189. }
  190. } break;
  191. }
  192. if (has_focus()) {
  193. Ref<StyleBox> style2 = theme_cache.focus;
  194. style2->draw(ci, Rect2(Point2(), size));
  195. }
  196. Ref<Texture2D> _icon;
  197. if (icon.is_null() && has_theme_icon(SNAME("icon"))) {
  198. _icon = theme_cache.icon;
  199. } else {
  200. _icon = icon;
  201. }
  202. Rect2 icon_region;
  203. HorizontalAlignment icon_align_rtl_checked = icon_alignment;
  204. HorizontalAlignment align_rtl_checked = alignment;
  205. // Swap icon and text alignment sides if right-to-left layout is set.
  206. if (rtl) {
  207. if (icon_alignment == HORIZONTAL_ALIGNMENT_RIGHT) {
  208. icon_align_rtl_checked = HORIZONTAL_ALIGNMENT_LEFT;
  209. } else if (icon_alignment == HORIZONTAL_ALIGNMENT_LEFT) {
  210. icon_align_rtl_checked = HORIZONTAL_ALIGNMENT_RIGHT;
  211. }
  212. if (alignment == HORIZONTAL_ALIGNMENT_RIGHT) {
  213. align_rtl_checked = HORIZONTAL_ALIGNMENT_LEFT;
  214. } else if (alignment == HORIZONTAL_ALIGNMENT_LEFT) {
  215. align_rtl_checked = HORIZONTAL_ALIGNMENT_RIGHT;
  216. }
  217. }
  218. if (!_icon.is_null()) {
  219. int valign = size.height - style->get_minimum_size().y;
  220. float icon_ofs_region = 0.0;
  221. Point2 style_offset;
  222. if (icon_align_rtl_checked == HORIZONTAL_ALIGNMENT_LEFT) {
  223. style_offset.x = style->get_margin(SIDE_LEFT);
  224. if (_internal_margin[SIDE_LEFT] > 0) {
  225. icon_ofs_region = _internal_margin[SIDE_LEFT] + theme_cache.h_separation;
  226. }
  227. } else if (icon_align_rtl_checked == HORIZONTAL_ALIGNMENT_CENTER) {
  228. style_offset.x = 0.0;
  229. } else if (icon_align_rtl_checked == HORIZONTAL_ALIGNMENT_RIGHT) {
  230. style_offset.x = -style->get_margin(SIDE_RIGHT);
  231. if (_internal_margin[SIDE_RIGHT] > 0) {
  232. icon_ofs_region = -_internal_margin[SIDE_RIGHT] - theme_cache.h_separation;
  233. }
  234. }
  235. style_offset.y = style->get_margin(SIDE_TOP);
  236. Size2 icon_size = _icon->get_size();
  237. if (expand_icon) {
  238. Size2 _size = get_size() - style->get_offset() * 2;
  239. int icon_text_separation = text.is_empty() ? 0 : theme_cache.h_separation;
  240. _size.width -= icon_text_separation + icon_ofs_region;
  241. if (!clip_text && icon_align_rtl_checked != HORIZONTAL_ALIGNMENT_CENTER) {
  242. _size.width -= text_buf->get_size().width;
  243. }
  244. float icon_width = _icon->get_width() * _size.height / _icon->get_height();
  245. float icon_height = _size.height;
  246. if (icon_width > _size.width) {
  247. icon_width = _size.width;
  248. icon_height = _icon->get_height() * icon_width / _icon->get_width();
  249. }
  250. icon_size = Size2(icon_width, icon_height);
  251. }
  252. icon_size = _fit_icon_size(icon_size);
  253. if (icon_align_rtl_checked == HORIZONTAL_ALIGNMENT_LEFT) {
  254. icon_region = Rect2(style_offset + Point2(icon_ofs_region, Math::floor((valign - icon_size.y) * 0.5)), icon_size);
  255. } else if (icon_align_rtl_checked == HORIZONTAL_ALIGNMENT_CENTER) {
  256. icon_region = Rect2(style_offset + Point2(icon_ofs_region + Math::floor((size.x - icon_size.x) * 0.5), Math::floor((valign - icon_size.y) * 0.5)), icon_size);
  257. } else {
  258. icon_region = Rect2(style_offset + Point2(icon_ofs_region + size.x - icon_size.x, Math::floor((valign - icon_size.y) * 0.5)), icon_size);
  259. }
  260. if (icon_region.size.width > 0) {
  261. Rect2 icon_region_rounded = Rect2(icon_region.position.round(), icon_region.size.round());
  262. draw_texture_rect(_icon, icon_region_rounded, false, color_icon);
  263. }
  264. }
  265. Point2 icon_ofs = !_icon.is_null() ? Point2(icon_region.size.width + theme_cache.h_separation, 0) : Point2();
  266. if (align_rtl_checked == HORIZONTAL_ALIGNMENT_CENTER && icon_align_rtl_checked == HORIZONTAL_ALIGNMENT_CENTER) {
  267. icon_ofs.x = 0.0;
  268. }
  269. int text_clip = size.width - style->get_minimum_size().width - icon_ofs.width;
  270. text_buf->set_width((clip_text || overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) ? text_clip : -1);
  271. int text_width = MAX(1, (clip_text || overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) ? MIN(text_clip, text_buf->get_size().x) : text_buf->get_size().x);
  272. if (_internal_margin[SIDE_LEFT] > 0) {
  273. text_clip -= _internal_margin[SIDE_LEFT] + theme_cache.h_separation;
  274. }
  275. if (_internal_margin[SIDE_RIGHT] > 0) {
  276. text_clip -= _internal_margin[SIDE_RIGHT] + theme_cache.h_separation;
  277. }
  278. Point2 text_ofs = (size - style->get_minimum_size() - icon_ofs - text_buf->get_size() - Point2(_internal_margin[SIDE_RIGHT] - _internal_margin[SIDE_LEFT], 0)) / 2.0;
  279. text_buf->set_alignment(align_rtl_checked);
  280. text_buf->set_width(text_width);
  281. switch (align_rtl_checked) {
  282. case HORIZONTAL_ALIGNMENT_FILL:
  283. case HORIZONTAL_ALIGNMENT_LEFT: {
  284. if (icon_align_rtl_checked != HORIZONTAL_ALIGNMENT_LEFT) {
  285. icon_ofs.x = 0.0;
  286. }
  287. if (_internal_margin[SIDE_LEFT] > 0) {
  288. text_ofs.x = style->get_margin(SIDE_LEFT) + icon_ofs.x + _internal_margin[SIDE_LEFT] + theme_cache.h_separation;
  289. } else {
  290. text_ofs.x = style->get_margin(SIDE_LEFT) + icon_ofs.x;
  291. }
  292. text_ofs.y += style->get_offset().y;
  293. } break;
  294. case HORIZONTAL_ALIGNMENT_CENTER: {
  295. if (text_ofs.x < 0) {
  296. text_ofs.x = 0;
  297. }
  298. if (icon_align_rtl_checked == HORIZONTAL_ALIGNMENT_LEFT) {
  299. text_ofs += icon_ofs;
  300. }
  301. text_ofs += style->get_offset();
  302. } break;
  303. case HORIZONTAL_ALIGNMENT_RIGHT: {
  304. if (_internal_margin[SIDE_RIGHT] > 0) {
  305. text_ofs.x = size.x - style->get_margin(SIDE_RIGHT) - text_width - _internal_margin[SIDE_RIGHT] - theme_cache.h_separation;
  306. } else {
  307. text_ofs.x = size.x - style->get_margin(SIDE_RIGHT) - text_width;
  308. }
  309. text_ofs.y += style->get_offset().y;
  310. if (icon_align_rtl_checked == HORIZONTAL_ALIGNMENT_RIGHT) {
  311. text_ofs.x -= icon_ofs.x;
  312. }
  313. } break;
  314. }
  315. Color font_outline_color = theme_cache.font_outline_color;
  316. int outline_size = theme_cache.outline_size;
  317. if (outline_size > 0 && font_outline_color.a > 0) {
  318. text_buf->draw_outline(ci, text_ofs, outline_size, font_outline_color);
  319. }
  320. text_buf->draw(ci, text_ofs, color);
  321. } break;
  322. }
  323. }
  324. Size2 Button::_fit_icon_size(const Size2 &p_size) const {
  325. int max_width = theme_cache.icon_max_width;
  326. Size2 icon_size = p_size;
  327. if (max_width > 0 && icon_size.width > max_width) {
  328. icon_size.height = icon_size.height * max_width / icon_size.width;
  329. icon_size.width = max_width;
  330. }
  331. return icon_size;
  332. }
  333. Size2 Button::get_minimum_size_for_text_and_icon(const String &p_text, Ref<Texture2D> p_icon) const {
  334. Ref<TextParagraph> paragraph;
  335. if (p_text.is_empty()) {
  336. paragraph = text_buf;
  337. } else {
  338. paragraph.instantiate();
  339. const_cast<Button *>(this)->_shape(paragraph, p_text);
  340. }
  341. Size2 minsize = paragraph->get_size();
  342. if (clip_text || overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) {
  343. minsize.width = 0;
  344. }
  345. if (!expand_icon && p_icon.is_valid()) {
  346. Size2 icon_size = _fit_icon_size(p_icon->get_size());
  347. minsize.height = MAX(minsize.height, icon_size.height);
  348. if (icon_alignment != HORIZONTAL_ALIGNMENT_CENTER) {
  349. minsize.width += icon_size.width;
  350. if (!xl_text.is_empty() || !p_text.is_empty()) {
  351. minsize.width += MAX(0, theme_cache.h_separation);
  352. }
  353. } else {
  354. minsize.width = MAX(minsize.width, icon_size.width);
  355. }
  356. }
  357. if (!xl_text.is_empty() || !p_text.is_empty()) {
  358. Ref<Font> font = theme_cache.font;
  359. float font_height = font->get_height(theme_cache.font_size);
  360. minsize.height = MAX(font_height, minsize.height);
  361. }
  362. return theme_cache.normal->get_minimum_size() + minsize;
  363. }
  364. void Button::_shape(Ref<TextParagraph> p_paragraph, String p_text) {
  365. if (p_paragraph.is_null()) {
  366. p_paragraph = text_buf;
  367. }
  368. if (p_text.is_empty()) {
  369. p_text = xl_text;
  370. }
  371. p_paragraph->clear();
  372. Ref<Font> font = theme_cache.font;
  373. int font_size = theme_cache.font_size;
  374. if (font.is_null() || font_size == 0) {
  375. // Can't shape without a valid font and a non-zero size.
  376. return;
  377. }
  378. if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
  379. p_paragraph->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  380. } else {
  381. p_paragraph->set_direction((TextServer::Direction)text_direction);
  382. }
  383. p_paragraph->add_string(p_text, font, font_size, language);
  384. p_paragraph->set_text_overrun_behavior(overrun_behavior);
  385. }
  386. void Button::set_text_overrun_behavior(TextServer::OverrunBehavior p_behavior) {
  387. if (overrun_behavior != p_behavior) {
  388. overrun_behavior = p_behavior;
  389. _shape();
  390. queue_redraw();
  391. update_minimum_size();
  392. }
  393. }
  394. TextServer::OverrunBehavior Button::get_text_overrun_behavior() const {
  395. return overrun_behavior;
  396. }
  397. void Button::set_text(const String &p_text) {
  398. if (text != p_text) {
  399. text = p_text;
  400. xl_text = atr(text);
  401. _shape();
  402. queue_redraw();
  403. update_minimum_size();
  404. }
  405. }
  406. String Button::get_text() const {
  407. return text;
  408. }
  409. void Button::set_text_direction(Control::TextDirection p_text_direction) {
  410. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  411. if (text_direction != p_text_direction) {
  412. text_direction = p_text_direction;
  413. _shape();
  414. queue_redraw();
  415. }
  416. }
  417. Control::TextDirection Button::get_text_direction() const {
  418. return text_direction;
  419. }
  420. void Button::set_language(const String &p_language) {
  421. if (language != p_language) {
  422. language = p_language;
  423. _shape();
  424. queue_redraw();
  425. }
  426. }
  427. String Button::get_language() const {
  428. return language;
  429. }
  430. void Button::set_icon(const Ref<Texture2D> &p_icon) {
  431. if (icon != p_icon) {
  432. icon = p_icon;
  433. queue_redraw();
  434. update_minimum_size();
  435. }
  436. }
  437. Ref<Texture2D> Button::get_icon() const {
  438. return icon;
  439. }
  440. void Button::set_expand_icon(bool p_enabled) {
  441. if (expand_icon != p_enabled) {
  442. expand_icon = p_enabled;
  443. queue_redraw();
  444. update_minimum_size();
  445. }
  446. }
  447. bool Button::is_expand_icon() const {
  448. return expand_icon;
  449. }
  450. void Button::set_flat(bool p_enabled) {
  451. if (flat != p_enabled) {
  452. flat = p_enabled;
  453. queue_redraw();
  454. }
  455. }
  456. bool Button::is_flat() const {
  457. return flat;
  458. }
  459. void Button::set_clip_text(bool p_enabled) {
  460. if (clip_text != p_enabled) {
  461. clip_text = p_enabled;
  462. queue_redraw();
  463. update_minimum_size();
  464. }
  465. }
  466. bool Button::get_clip_text() const {
  467. return clip_text;
  468. }
  469. void Button::set_text_alignment(HorizontalAlignment p_alignment) {
  470. if (alignment != p_alignment) {
  471. alignment = p_alignment;
  472. queue_redraw();
  473. }
  474. }
  475. HorizontalAlignment Button::get_text_alignment() const {
  476. return alignment;
  477. }
  478. void Button::set_icon_alignment(HorizontalAlignment p_alignment) {
  479. icon_alignment = p_alignment;
  480. update_minimum_size();
  481. queue_redraw();
  482. }
  483. HorizontalAlignment Button::get_icon_alignment() const {
  484. return icon_alignment;
  485. }
  486. void Button::_bind_methods() {
  487. ClassDB::bind_method(D_METHOD("set_text", "text"), &Button::set_text);
  488. ClassDB::bind_method(D_METHOD("get_text"), &Button::get_text);
  489. ClassDB::bind_method(D_METHOD("set_text_overrun_behavior", "overrun_behavior"), &Button::set_text_overrun_behavior);
  490. ClassDB::bind_method(D_METHOD("get_text_overrun_behavior"), &Button::get_text_overrun_behavior);
  491. ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &Button::set_text_direction);
  492. ClassDB::bind_method(D_METHOD("get_text_direction"), &Button::get_text_direction);
  493. ClassDB::bind_method(D_METHOD("set_language", "language"), &Button::set_language);
  494. ClassDB::bind_method(D_METHOD("get_language"), &Button::get_language);
  495. ClassDB::bind_method(D_METHOD("set_button_icon", "texture"), &Button::set_icon);
  496. ClassDB::bind_method(D_METHOD("get_button_icon"), &Button::get_icon);
  497. ClassDB::bind_method(D_METHOD("set_flat", "enabled"), &Button::set_flat);
  498. ClassDB::bind_method(D_METHOD("is_flat"), &Button::is_flat);
  499. ClassDB::bind_method(D_METHOD("set_clip_text", "enabled"), &Button::set_clip_text);
  500. ClassDB::bind_method(D_METHOD("get_clip_text"), &Button::get_clip_text);
  501. ClassDB::bind_method(D_METHOD("set_text_alignment", "alignment"), &Button::set_text_alignment);
  502. ClassDB::bind_method(D_METHOD("get_text_alignment"), &Button::get_text_alignment);
  503. ClassDB::bind_method(D_METHOD("set_icon_alignment", "icon_alignment"), &Button::set_icon_alignment);
  504. ClassDB::bind_method(D_METHOD("get_icon_alignment"), &Button::get_icon_alignment);
  505. ClassDB::bind_method(D_METHOD("set_expand_icon", "enabled"), &Button::set_expand_icon);
  506. ClassDB::bind_method(D_METHOD("is_expand_icon"), &Button::is_expand_icon);
  507. ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_MULTILINE_TEXT), "set_text", "get_text");
  508. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_button_icon", "get_button_icon");
  509. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flat"), "set_flat", "is_flat");
  510. ADD_GROUP("Text Behavior", "");
  511. ADD_PROPERTY(PropertyInfo(Variant::INT, "alignment", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_text_alignment", "get_text_alignment");
  512. 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");
  513. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clip_text"), "set_clip_text", "get_clip_text");
  514. ADD_GROUP("Icon Behavior", "");
  515. ADD_PROPERTY(PropertyInfo(Variant::INT, "icon_alignment", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_icon_alignment", "get_icon_alignment");
  516. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "expand_icon"), "set_expand_icon", "is_expand_icon");
  517. ADD_GROUP("BiDi", "");
  518. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_text_direction", "get_text_direction");
  519. ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID, ""), "set_language", "get_language");
  520. }
  521. Button::Button(const String &p_text) {
  522. text_buf.instantiate();
  523. text_buf->set_break_flags(TextServer::BREAK_MANDATORY | TextServer::BREAK_TRIM_EDGE_SPACES);
  524. set_mouse_filter(MOUSE_FILTER_STOP);
  525. set_text(p_text);
  526. }
  527. Button::~Button() {
  528. }