button.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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) {
  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. _size.width -= get_theme_constant(SNAME("h_separation")) + icon_ofs_region;
  231. if (!clip_text && icon_align_rtl_checked != HORIZONTAL_ALIGNMENT_CENTER) {
  232. _size.width -= text_buf->get_size().width;
  233. }
  234. float icon_width = _icon->get_width() * _size.height / _icon->get_height();
  235. float icon_height = _size.height;
  236. if (icon_width > _size.width) {
  237. icon_width = _size.width;
  238. icon_height = _icon->get_height() * icon_width / _icon->get_width();
  239. }
  240. icon_size = Size2(icon_width, icon_height);
  241. }
  242. if (icon_align_rtl_checked == HORIZONTAL_ALIGNMENT_LEFT) {
  243. icon_region = Rect2(style_offset + Point2(icon_ofs_region, Math::floor((valign - icon_size.y) * 0.5)), icon_size);
  244. } else if (icon_align_rtl_checked == HORIZONTAL_ALIGNMENT_CENTER) {
  245. 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);
  246. } else {
  247. icon_region = Rect2(style_offset + Point2(icon_ofs_region + size.x - icon_size.x, Math::floor((valign - icon_size.y) * 0.5)), icon_size);
  248. }
  249. if (icon_region.size.width > 0) {
  250. draw_texture_rect_region(_icon, icon_region, Rect2(Point2(), _icon->get_size()), color_icon);
  251. }
  252. }
  253. Point2 icon_ofs = !_icon.is_null() ? Point2(icon_region.size.width + get_theme_constant(SNAME("h_separation")), 0) : Point2();
  254. if (align_rtl_checked == HORIZONTAL_ALIGNMENT_CENTER && icon_align_rtl_checked == HORIZONTAL_ALIGNMENT_CENTER) {
  255. icon_ofs.x = 0.0;
  256. }
  257. int text_clip = size.width - style->get_minimum_size().width - icon_ofs.width;
  258. text_buf->set_width(clip_text ? text_clip : -1);
  259. int text_width = MAX(1, clip_text ? MIN(text_clip, text_buf->get_size().x) : text_buf->get_size().x);
  260. if (_internal_margin[SIDE_LEFT] > 0) {
  261. text_clip -= _internal_margin[SIDE_LEFT] + get_theme_constant(SNAME("h_separation"));
  262. }
  263. if (_internal_margin[SIDE_RIGHT] > 0) {
  264. text_clip -= _internal_margin[SIDE_RIGHT] + get_theme_constant(SNAME("h_separation"));
  265. }
  266. 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;
  267. text_buf->set_alignment(align_rtl_checked);
  268. text_buf->set_width(text_width);
  269. switch (align_rtl_checked) {
  270. case HORIZONTAL_ALIGNMENT_FILL:
  271. case HORIZONTAL_ALIGNMENT_LEFT: {
  272. if (icon_align_rtl_checked != HORIZONTAL_ALIGNMENT_LEFT) {
  273. icon_ofs.x = 0.0;
  274. }
  275. if (_internal_margin[SIDE_LEFT] > 0) {
  276. text_ofs.x = style->get_margin(SIDE_LEFT) + icon_ofs.x + _internal_margin[SIDE_LEFT] + get_theme_constant(SNAME("h_separation"));
  277. } else {
  278. text_ofs.x = style->get_margin(SIDE_LEFT) + icon_ofs.x;
  279. }
  280. text_ofs.y += style->get_offset().y;
  281. } break;
  282. case HORIZONTAL_ALIGNMENT_CENTER: {
  283. if (text_ofs.x < 0) {
  284. text_ofs.x = 0;
  285. }
  286. if (icon_align_rtl_checked == HORIZONTAL_ALIGNMENT_LEFT) {
  287. text_ofs += icon_ofs;
  288. }
  289. text_ofs += style->get_offset();
  290. } break;
  291. case HORIZONTAL_ALIGNMENT_RIGHT: {
  292. if (_internal_margin[SIDE_RIGHT] > 0) {
  293. text_ofs.x = size.x - style->get_margin(SIDE_RIGHT) - text_width - _internal_margin[SIDE_RIGHT] - get_theme_constant(SNAME("h_separation"));
  294. } else {
  295. text_ofs.x = size.x - style->get_margin(SIDE_RIGHT) - text_width;
  296. }
  297. text_ofs.y += style->get_offset().y;
  298. if (icon_align_rtl_checked == HORIZONTAL_ALIGNMENT_RIGHT) {
  299. text_ofs.x -= icon_ofs.x;
  300. }
  301. } break;
  302. }
  303. Color font_outline_color = get_theme_color(SNAME("font_outline_color"));
  304. int outline_size = get_theme_constant(SNAME("outline_size"));
  305. if (outline_size > 0 && font_outline_color.a > 0) {
  306. text_buf->draw_outline(ci, text_ofs, outline_size, font_outline_color);
  307. }
  308. text_buf->draw(ci, text_ofs, color);
  309. } break;
  310. }
  311. }
  312. void Button::_shape() {
  313. Ref<Font> font = get_theme_font(SNAME("font"));
  314. int font_size = get_theme_font_size(SNAME("font_size"));
  315. text_buf->clear();
  316. if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
  317. text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  318. } else {
  319. text_buf->set_direction((TextServer::Direction)text_direction);
  320. }
  321. text_buf->add_string(xl_text, font, font_size, opentype_features, (!language.is_empty()) ? language : TranslationServer::get_singleton()->get_tool_locale());
  322. }
  323. void Button::set_text(const String &p_text) {
  324. if (text != p_text) {
  325. text = p_text;
  326. xl_text = atr(text);
  327. _shape();
  328. update();
  329. update_minimum_size();
  330. }
  331. }
  332. String Button::get_text() const {
  333. return text;
  334. }
  335. void Button::set_text_direction(Control::TextDirection p_text_direction) {
  336. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  337. if (text_direction != p_text_direction) {
  338. text_direction = p_text_direction;
  339. _shape();
  340. update();
  341. }
  342. }
  343. Control::TextDirection Button::get_text_direction() const {
  344. return text_direction;
  345. }
  346. void Button::clear_opentype_features() {
  347. opentype_features.clear();
  348. _shape();
  349. update();
  350. }
  351. void Button::set_opentype_feature(const String &p_name, int p_value) {
  352. int32_t tag = TS->name_to_tag(p_name);
  353. if (!opentype_features.has(tag) || (int)opentype_features[tag] != p_value) {
  354. opentype_features[tag] = p_value;
  355. _shape();
  356. update();
  357. }
  358. }
  359. int Button::get_opentype_feature(const String &p_name) const {
  360. int32_t tag = TS->name_to_tag(p_name);
  361. if (!opentype_features.has(tag)) {
  362. return -1;
  363. }
  364. return opentype_features[tag];
  365. }
  366. void Button::set_language(const String &p_language) {
  367. if (language != p_language) {
  368. language = p_language;
  369. _shape();
  370. update();
  371. }
  372. }
  373. String Button::get_language() const {
  374. return language;
  375. }
  376. void Button::set_icon(const Ref<Texture2D> &p_icon) {
  377. if (icon != p_icon) {
  378. icon = p_icon;
  379. update();
  380. update_minimum_size();
  381. }
  382. }
  383. Ref<Texture2D> Button::get_icon() const {
  384. return icon;
  385. }
  386. void Button::set_expand_icon(bool p_enabled) {
  387. if (expand_icon != p_enabled) {
  388. expand_icon = p_enabled;
  389. update();
  390. update_minimum_size();
  391. }
  392. }
  393. bool Button::is_expand_icon() const {
  394. return expand_icon;
  395. }
  396. void Button::set_flat(bool p_enabled) {
  397. if (flat != p_enabled) {
  398. flat = p_enabled;
  399. update();
  400. }
  401. }
  402. bool Button::is_flat() const {
  403. return flat;
  404. }
  405. void Button::set_clip_text(bool p_enabled) {
  406. if (clip_text != p_enabled) {
  407. clip_text = p_enabled;
  408. update();
  409. update_minimum_size();
  410. }
  411. }
  412. bool Button::get_clip_text() const {
  413. return clip_text;
  414. }
  415. void Button::set_text_alignment(HorizontalAlignment p_alignment) {
  416. if (alignment != p_alignment) {
  417. alignment = p_alignment;
  418. update();
  419. }
  420. }
  421. HorizontalAlignment Button::get_text_alignment() const {
  422. return alignment;
  423. }
  424. void Button::set_icon_alignment(HorizontalAlignment p_alignment) {
  425. icon_alignment = p_alignment;
  426. update_minimum_size();
  427. update();
  428. }
  429. HorizontalAlignment Button::get_icon_alignment() const {
  430. return icon_alignment;
  431. }
  432. bool Button::_set(const StringName &p_name, const Variant &p_value) {
  433. String str = p_name;
  434. if (str.begins_with("opentype_features/")) {
  435. String name = str.get_slicec('/', 1);
  436. int32_t tag = TS->name_to_tag(name);
  437. int value = p_value;
  438. if (value == -1) {
  439. if (opentype_features.has(tag)) {
  440. opentype_features.erase(tag);
  441. _shape();
  442. update();
  443. }
  444. } else {
  445. if (!opentype_features.has(tag) || (int)opentype_features[tag] != value) {
  446. opentype_features[tag] = value;
  447. _shape();
  448. update();
  449. }
  450. }
  451. notify_property_list_changed();
  452. return true;
  453. }
  454. return false;
  455. }
  456. bool Button::_get(const StringName &p_name, Variant &r_ret) const {
  457. String str = p_name;
  458. if (str.begins_with("opentype_features/")) {
  459. String name = str.get_slicec('/', 1);
  460. int32_t tag = TS->name_to_tag(name);
  461. if (opentype_features.has(tag)) {
  462. r_ret = opentype_features[tag];
  463. return true;
  464. } else {
  465. r_ret = -1;
  466. return true;
  467. }
  468. }
  469. return false;
  470. }
  471. void Button::_get_property_list(List<PropertyInfo> *p_list) const {
  472. for (const Variant *ftr = opentype_features.next(nullptr); ftr != nullptr; ftr = opentype_features.next(ftr)) {
  473. String name = TS->tag_to_name(*ftr);
  474. p_list->push_back(PropertyInfo(Variant::INT, "opentype_features/" + name));
  475. }
  476. p_list->push_back(PropertyInfo(Variant::NIL, "opentype_features/_new", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR));
  477. }
  478. void Button::_bind_methods() {
  479. ClassDB::bind_method(D_METHOD("set_text", "text"), &Button::set_text);
  480. ClassDB::bind_method(D_METHOD("get_text"), &Button::get_text);
  481. ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &Button::set_text_direction);
  482. ClassDB::bind_method(D_METHOD("get_text_direction"), &Button::get_text_direction);
  483. ClassDB::bind_method(D_METHOD("set_opentype_feature", "tag", "value"), &Button::set_opentype_feature);
  484. ClassDB::bind_method(D_METHOD("get_opentype_feature", "tag"), &Button::get_opentype_feature);
  485. ClassDB::bind_method(D_METHOD("clear_opentype_features"), &Button::clear_opentype_features);
  486. ClassDB::bind_method(D_METHOD("set_language", "language"), &Button::set_language);
  487. ClassDB::bind_method(D_METHOD("get_language"), &Button::get_language);
  488. ClassDB::bind_method(D_METHOD("set_button_icon", "texture"), &Button::set_icon);
  489. ClassDB::bind_method(D_METHOD("get_button_icon"), &Button::get_icon);
  490. ClassDB::bind_method(D_METHOD("set_flat", "enabled"), &Button::set_flat);
  491. ClassDB::bind_method(D_METHOD("is_flat"), &Button::is_flat);
  492. ClassDB::bind_method(D_METHOD("set_clip_text", "enabled"), &Button::set_clip_text);
  493. ClassDB::bind_method(D_METHOD("get_clip_text"), &Button::get_clip_text);
  494. ClassDB::bind_method(D_METHOD("set_text_alignment", "alignment"), &Button::set_text_alignment);
  495. ClassDB::bind_method(D_METHOD("get_text_alignment"), &Button::get_text_alignment);
  496. ClassDB::bind_method(D_METHOD("set_icon_alignment", "icon_alignment"), &Button::set_icon_alignment);
  497. ClassDB::bind_method(D_METHOD("get_icon_alignment"), &Button::get_icon_alignment);
  498. ClassDB::bind_method(D_METHOD("set_expand_icon", "enabled"), &Button::set_expand_icon);
  499. ClassDB::bind_method(D_METHOD("is_expand_icon"), &Button::is_expand_icon);
  500. ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_MULTILINE_TEXT, "", PROPERTY_USAGE_DEFAULT_INTL), "set_text", "get_text");
  501. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_text_direction", "get_text_direction");
  502. ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID, ""), "set_language", "get_language");
  503. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_button_icon", "get_button_icon");
  504. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flat"), "set_flat", "is_flat");
  505. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clip_text"), "set_clip_text", "get_clip_text");
  506. ADD_PROPERTY(PropertyInfo(Variant::INT, "alignment", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_text_alignment", "get_text_alignment");
  507. ADD_PROPERTY(PropertyInfo(Variant::INT, "icon_alignment", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_icon_alignment", "get_icon_alignment");
  508. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "expand_icon"), "set_expand_icon", "is_expand_icon");
  509. }
  510. Button::Button(const String &p_text) {
  511. text_buf.instantiate();
  512. text_buf->set_flags(TextServer::BREAK_MANDATORY);
  513. set_mouse_filter(MOUSE_FILTER_STOP);
  514. set_text(p_text);
  515. }
  516. Button::~Button() {
  517. }