button.cpp 21 KB

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