button.cpp 20 KB

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