button.cpp 21 KB

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