base_button.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. /**************************************************************************/
  2. /* base_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 "base_button.h"
  31. #include "core/config/project_settings.h"
  32. #include "scene/gui/label.h"
  33. #include "scene/main/window.h"
  34. void BaseButton::_unpress_group() {
  35. if (button_group.is_null()) {
  36. return;
  37. }
  38. if (toggle_mode && !button_group->is_allow_unpress()) {
  39. status.pressed = true;
  40. queue_accessibility_update();
  41. }
  42. for (BaseButton *E : button_group->buttons) {
  43. if (E == this) {
  44. continue;
  45. }
  46. E->set_pressed(false);
  47. }
  48. }
  49. void BaseButton::gui_input(const Ref<InputEvent> &p_event) {
  50. ERR_FAIL_COND(p_event.is_null());
  51. if (status.disabled) { // no interaction with disabled button
  52. return;
  53. }
  54. Ref<InputEventMouseButton> mouse_button = p_event;
  55. bool ui_accept = p_event->is_action("ui_accept", true) && !p_event->is_echo();
  56. bool button_masked = mouse_button.is_valid() && button_mask.has_flag(mouse_button_to_mask(mouse_button->get_button_index()));
  57. if (button_masked || ui_accept) {
  58. was_mouse_pressed = button_masked;
  59. on_action_event(p_event);
  60. was_mouse_pressed = false;
  61. return;
  62. }
  63. Ref<InputEventMouseMotion> mouse_motion = p_event;
  64. if (mouse_motion.is_valid()) {
  65. if (status.press_attempt) {
  66. bool last_press_inside = status.pressing_inside;
  67. status.pressing_inside = has_point(mouse_motion->get_position());
  68. if (last_press_inside != status.pressing_inside) {
  69. queue_redraw();
  70. }
  71. }
  72. }
  73. }
  74. void BaseButton::_accessibility_action_click(const Variant &p_data) {
  75. if (toggle_mode) {
  76. status.pressed = !status.pressed;
  77. if (status.pressed) {
  78. _unpress_group();
  79. if (button_group.is_valid()) {
  80. button_group->emit_signal(SceneStringName(pressed), this);
  81. }
  82. }
  83. _toggled(status.pressed);
  84. _pressed();
  85. } else {
  86. _pressed();
  87. }
  88. queue_accessibility_update();
  89. queue_redraw();
  90. }
  91. void BaseButton::_notification(int p_what) {
  92. switch (p_what) {
  93. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  94. RID ae = get_accessibility_element();
  95. ERR_FAIL_COND(ae.is_null());
  96. DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_BUTTON);
  97. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_CLICK, callable_mp(this, &BaseButton::_accessibility_action_click));
  98. DisplayServer::get_singleton()->accessibility_update_set_flag(ae, DisplayServer::AccessibilityFlags::FLAG_DISABLED, status.disabled);
  99. if (toggle_mode) {
  100. DisplayServer::get_singleton()->accessibility_update_set_checked(ae, status.pressed);
  101. }
  102. if (button_group.is_valid()) {
  103. for (const BaseButton *btn : button_group->buttons) {
  104. if (btn->is_part_of_edited_scene()) {
  105. continue;
  106. }
  107. DisplayServer::get_singleton()->accessibility_update_add_related_radio_group(ae, btn->get_accessibility_element());
  108. }
  109. }
  110. if (shortcut_in_tooltip && shortcut.is_valid() && shortcut->has_valid_event()) {
  111. String text = atr(shortcut->get_name()) + " (" + shortcut->get_as_text() + ")";
  112. String tooltip = get_tooltip_text();
  113. if (!tooltip.is_empty() && shortcut->get_name().nocasecmp_to(tooltip) != 0) {
  114. text += "\n" + atr(tooltip);
  115. }
  116. DisplayServer::get_singleton()->accessibility_update_set_tooltip(ae, text);
  117. }
  118. } break;
  119. case NOTIFICATION_MOUSE_ENTER: {
  120. status.hovering = true;
  121. queue_accessibility_update();
  122. queue_redraw();
  123. } break;
  124. case NOTIFICATION_MOUSE_EXIT: {
  125. status.hovering = false;
  126. queue_accessibility_update();
  127. queue_redraw();
  128. } break;
  129. case NOTIFICATION_DRAG_BEGIN:
  130. case NOTIFICATION_SCROLL_BEGIN: {
  131. if (status.press_attempt) {
  132. status.press_attempt = false;
  133. queue_redraw();
  134. }
  135. } break;
  136. case NOTIFICATION_FOCUS_ENTER: {
  137. queue_redraw();
  138. } break;
  139. case NOTIFICATION_FOCUS_EXIT: {
  140. if (status.press_attempt) {
  141. status.press_attempt = false;
  142. queue_redraw();
  143. } else if (status.hovering) {
  144. queue_redraw();
  145. }
  146. if (status.pressed_down_with_focus) {
  147. status.pressed_down_with_focus = false;
  148. emit_signal(SNAME("button_up"));
  149. }
  150. } break;
  151. case NOTIFICATION_VISIBILITY_CHANGED:
  152. case NOTIFICATION_EXIT_TREE: {
  153. if (p_what == NOTIFICATION_VISIBILITY_CHANGED && is_visible_in_tree()) {
  154. break;
  155. }
  156. if (!toggle_mode) {
  157. status.pressed = false;
  158. }
  159. status.hovering = false;
  160. status.press_attempt = false;
  161. status.pressing_inside = false;
  162. } break;
  163. }
  164. }
  165. void BaseButton::_pressed() {
  166. GDVIRTUAL_CALL(_pressed);
  167. pressed();
  168. emit_signal(SceneStringName(pressed));
  169. }
  170. void BaseButton::_toggled(bool p_pressed) {
  171. GDVIRTUAL_CALL(_toggled, p_pressed);
  172. toggled(p_pressed);
  173. emit_signal(SceneStringName(toggled), p_pressed);
  174. }
  175. void BaseButton::on_action_event(Ref<InputEvent> p_event) {
  176. Ref<InputEventMouseButton> mouse_button = p_event;
  177. if (p_event->is_pressed() && (mouse_button.is_null() || status.hovering)) {
  178. status.press_attempt = true;
  179. status.pressing_inside = true;
  180. if (!status.pressed_down_with_focus) {
  181. status.pressed_down_with_focus = true;
  182. emit_signal(SNAME("button_down"));
  183. }
  184. }
  185. if (status.press_attempt && status.pressing_inside) {
  186. if (toggle_mode) {
  187. bool is_pressed = p_event->is_pressed();
  188. if ((is_pressed && action_mode == ACTION_MODE_BUTTON_PRESS) || (!is_pressed && action_mode == ACTION_MODE_BUTTON_RELEASE)) {
  189. if (action_mode == ACTION_MODE_BUTTON_PRESS) {
  190. status.press_attempt = false;
  191. status.pressing_inside = false;
  192. }
  193. status.pressed = !status.pressed;
  194. _unpress_group();
  195. if (button_group.is_valid()) {
  196. button_group->emit_signal(SceneStringName(pressed), this);
  197. }
  198. _toggled(status.pressed);
  199. _pressed();
  200. queue_accessibility_update();
  201. }
  202. } else {
  203. if ((p_event->is_pressed() && action_mode == ACTION_MODE_BUTTON_PRESS) || (!p_event->is_pressed() && action_mode == ACTION_MODE_BUTTON_RELEASE)) {
  204. _pressed();
  205. }
  206. }
  207. }
  208. if (!p_event->is_pressed()) {
  209. status.press_attempt = false;
  210. status.pressing_inside = false;
  211. if (status.pressed_down_with_focus) {
  212. status.pressed_down_with_focus = false;
  213. emit_signal(SNAME("button_up"));
  214. }
  215. }
  216. queue_redraw();
  217. }
  218. void BaseButton::pressed() {
  219. }
  220. void BaseButton::toggled(bool p_pressed) {
  221. }
  222. void BaseButton::set_disabled(bool p_disabled) {
  223. if (status.disabled == p_disabled) {
  224. return;
  225. }
  226. status.disabled = p_disabled;
  227. if (p_disabled) {
  228. if (!toggle_mode) {
  229. status.pressed = false;
  230. }
  231. status.press_attempt = false;
  232. status.pressing_inside = false;
  233. }
  234. queue_accessibility_update();
  235. queue_redraw();
  236. update_minimum_size();
  237. }
  238. bool BaseButton::is_disabled() const {
  239. return status.disabled;
  240. }
  241. void BaseButton::set_pressed(bool p_pressed) {
  242. bool prev_pressed = status.pressed;
  243. set_pressed_no_signal(p_pressed);
  244. if (status.pressed == prev_pressed) {
  245. return;
  246. }
  247. if (p_pressed) {
  248. _unpress_group();
  249. if (button_group.is_valid()) {
  250. button_group->emit_signal(SceneStringName(pressed), this);
  251. }
  252. }
  253. _toggled(status.pressed);
  254. }
  255. void BaseButton::set_pressed_no_signal(bool p_pressed) {
  256. if (!toggle_mode) {
  257. return;
  258. }
  259. if (status.pressed == p_pressed) {
  260. return;
  261. }
  262. status.pressed = p_pressed;
  263. queue_accessibility_update();
  264. queue_redraw();
  265. }
  266. bool BaseButton::is_pressing() const {
  267. return status.press_attempt;
  268. }
  269. bool BaseButton::is_pressed() const {
  270. return toggle_mode ? status.pressed : status.press_attempt;
  271. }
  272. bool BaseButton::is_hovered() const {
  273. return status.hovering;
  274. }
  275. BaseButton::DrawMode BaseButton::get_draw_mode() const {
  276. if (status.disabled) {
  277. return DRAW_DISABLED;
  278. }
  279. if (in_shortcut_feedback) {
  280. return DRAW_HOVER_PRESSED;
  281. }
  282. if (!status.press_attempt && status.hovering) {
  283. if (status.pressed) {
  284. return DRAW_HOVER_PRESSED;
  285. }
  286. return DRAW_HOVER;
  287. } else {
  288. // Determine if pressed or not.
  289. bool pressing;
  290. if (status.press_attempt) {
  291. pressing = (status.pressing_inside || keep_pressed_outside);
  292. if (status.pressed) {
  293. pressing = !pressing;
  294. }
  295. } else {
  296. pressing = status.pressed;
  297. }
  298. if (pressing) {
  299. return DRAW_PRESSED;
  300. } else {
  301. return DRAW_NORMAL;
  302. }
  303. }
  304. }
  305. void BaseButton::set_toggle_mode(bool p_on) {
  306. // Make sure to set 'pressed' to false if we are not in toggle mode
  307. if (!p_on) {
  308. set_pressed(false);
  309. }
  310. queue_accessibility_update();
  311. toggle_mode = p_on;
  312. update_configuration_warnings();
  313. }
  314. bool BaseButton::is_toggle_mode() const {
  315. return toggle_mode;
  316. }
  317. void BaseButton::set_shortcut_in_tooltip(bool p_on) {
  318. if (shortcut_in_tooltip != p_on) {
  319. shortcut_in_tooltip = p_on;
  320. queue_accessibility_update();
  321. }
  322. }
  323. bool BaseButton::is_shortcut_in_tooltip_enabled() const {
  324. return shortcut_in_tooltip;
  325. }
  326. void BaseButton::set_action_mode(ActionMode p_mode) {
  327. action_mode = p_mode;
  328. }
  329. BaseButton::ActionMode BaseButton::get_action_mode() const {
  330. return action_mode;
  331. }
  332. void BaseButton::set_button_mask(BitField<MouseButtonMask> p_mask) {
  333. button_mask = p_mask;
  334. }
  335. BitField<MouseButtonMask> BaseButton::get_button_mask() const {
  336. return button_mask;
  337. }
  338. void BaseButton::set_keep_pressed_outside(bool p_on) {
  339. keep_pressed_outside = p_on;
  340. }
  341. bool BaseButton::is_keep_pressed_outside() const {
  342. return keep_pressed_outside;
  343. }
  344. void BaseButton::set_shortcut_feedback(bool p_enable) {
  345. shortcut_feedback = p_enable;
  346. }
  347. bool BaseButton::is_shortcut_feedback() const {
  348. return shortcut_feedback;
  349. }
  350. void BaseButton::set_shortcut(const Ref<Shortcut> &p_shortcut) {
  351. if (shortcut != p_shortcut) {
  352. shortcut = p_shortcut;
  353. set_process_shortcut_input(shortcut.is_valid());
  354. queue_accessibility_update();
  355. }
  356. }
  357. Ref<Shortcut> BaseButton::get_shortcut() const {
  358. return shortcut;
  359. }
  360. void BaseButton::_shortcut_feedback_timeout() {
  361. in_shortcut_feedback = false;
  362. queue_redraw();
  363. }
  364. void BaseButton::shortcut_input(const Ref<InputEvent> &p_event) {
  365. ERR_FAIL_COND(p_event.is_null());
  366. if (!is_disabled() && p_event->is_pressed() && is_visible_in_tree() && !p_event->is_echo() && shortcut.is_valid() && shortcut->matches_event(p_event)) {
  367. if (toggle_mode) {
  368. status.pressed = !status.pressed;
  369. _unpress_group();
  370. if (button_group.is_valid()) {
  371. button_group->emit_signal(SceneStringName(pressed), this);
  372. }
  373. _toggled(status.pressed);
  374. _pressed();
  375. queue_accessibility_update();
  376. } else {
  377. _pressed();
  378. }
  379. queue_redraw();
  380. accept_event();
  381. if (shortcut_feedback && is_inside_tree()) {
  382. if (shortcut_feedback_timer == nullptr) {
  383. shortcut_feedback_timer = memnew(Timer);
  384. shortcut_feedback_timer->set_one_shot(true);
  385. add_child(shortcut_feedback_timer, false, INTERNAL_MODE_BACK);
  386. shortcut_feedback_timer->set_wait_time(GLOBAL_GET_CACHED(double, "gui/timers/button_shortcut_feedback_highlight_time"));
  387. shortcut_feedback_timer->connect("timeout", callable_mp(this, &BaseButton::_shortcut_feedback_timeout));
  388. }
  389. in_shortcut_feedback = true;
  390. shortcut_feedback_timer->start();
  391. }
  392. }
  393. }
  394. Control *BaseButton::make_custom_tooltip(const String &p_text) const {
  395. Control *control = Control::make_custom_tooltip(p_text);
  396. if (control) {
  397. return control;
  398. }
  399. if (!shortcut_in_tooltip || shortcut.is_null() || !shortcut->has_valid_event()) {
  400. return nullptr; // Use the default tooltip label.
  401. }
  402. String text = atr(shortcut->get_name()) + " (" + shortcut->get_as_text() + ")";
  403. if (!p_text.is_empty() && shortcut->get_name().nocasecmp_to(p_text) != 0) {
  404. text += "\n" + atr(p_text);
  405. }
  406. // Make a label similar to the default tooltip label.
  407. // Auto translation is disabled because we already did that manually above.
  408. //
  409. // We can't customize the tooltip text by overriding `get_tooltip()`
  410. // because otherwise user-defined `_make_custom_tooltip()` would receive
  411. // the translated and annotated text.
  412. Label *label = memnew(Label(text));
  413. label->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  414. label->set_theme_type_variation(SNAME("TooltipLabel"));
  415. return label;
  416. }
  417. void BaseButton::set_button_group(const Ref<ButtonGroup> &p_group) {
  418. if (button_group.is_valid()) {
  419. button_group->buttons.erase(this);
  420. }
  421. button_group = p_group;
  422. if (button_group.is_valid()) {
  423. button_group->buttons.insert(this);
  424. }
  425. queue_accessibility_update();
  426. queue_redraw(); //checkbox changes to radio if set a buttongroup
  427. update_configuration_warnings();
  428. }
  429. Ref<ButtonGroup> BaseButton::get_button_group() const {
  430. return button_group;
  431. }
  432. bool BaseButton::_was_pressed_by_mouse() const {
  433. return was_mouse_pressed;
  434. }
  435. PackedStringArray BaseButton::get_configuration_warnings() const {
  436. PackedStringArray warnings = Control::get_configuration_warnings();
  437. if (get_button_group().is_valid() && !is_toggle_mode()) {
  438. warnings.push_back(RTR("ButtonGroup is intended to be used only with buttons that have toggle_mode set to true."));
  439. }
  440. return warnings;
  441. }
  442. void BaseButton::_bind_methods() {
  443. ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &BaseButton::set_pressed);
  444. ClassDB::bind_method(D_METHOD("is_pressed"), &BaseButton::is_pressed);
  445. ClassDB::bind_method(D_METHOD("set_pressed_no_signal", "pressed"), &BaseButton::set_pressed_no_signal);
  446. ClassDB::bind_method(D_METHOD("is_hovered"), &BaseButton::is_hovered);
  447. ClassDB::bind_method(D_METHOD("set_toggle_mode", "enabled"), &BaseButton::set_toggle_mode);
  448. ClassDB::bind_method(D_METHOD("is_toggle_mode"), &BaseButton::is_toggle_mode);
  449. ClassDB::bind_method(D_METHOD("set_shortcut_in_tooltip", "enabled"), &BaseButton::set_shortcut_in_tooltip);
  450. ClassDB::bind_method(D_METHOD("is_shortcut_in_tooltip_enabled"), &BaseButton::is_shortcut_in_tooltip_enabled);
  451. ClassDB::bind_method(D_METHOD("set_disabled", "disabled"), &BaseButton::set_disabled);
  452. ClassDB::bind_method(D_METHOD("is_disabled"), &BaseButton::is_disabled);
  453. ClassDB::bind_method(D_METHOD("set_action_mode", "mode"), &BaseButton::set_action_mode);
  454. ClassDB::bind_method(D_METHOD("get_action_mode"), &BaseButton::get_action_mode);
  455. ClassDB::bind_method(D_METHOD("set_button_mask", "mask"), &BaseButton::set_button_mask);
  456. ClassDB::bind_method(D_METHOD("get_button_mask"), &BaseButton::get_button_mask);
  457. ClassDB::bind_method(D_METHOD("get_draw_mode"), &BaseButton::get_draw_mode);
  458. ClassDB::bind_method(D_METHOD("set_keep_pressed_outside", "enabled"), &BaseButton::set_keep_pressed_outside);
  459. ClassDB::bind_method(D_METHOD("is_keep_pressed_outside"), &BaseButton::is_keep_pressed_outside);
  460. ClassDB::bind_method(D_METHOD("set_shortcut_feedback", "enabled"), &BaseButton::set_shortcut_feedback);
  461. ClassDB::bind_method(D_METHOD("is_shortcut_feedback"), &BaseButton::is_shortcut_feedback);
  462. ClassDB::bind_method(D_METHOD("set_shortcut", "shortcut"), &BaseButton::set_shortcut);
  463. ClassDB::bind_method(D_METHOD("get_shortcut"), &BaseButton::get_shortcut);
  464. ClassDB::bind_method(D_METHOD("set_button_group", "button_group"), &BaseButton::set_button_group);
  465. ClassDB::bind_method(D_METHOD("get_button_group"), &BaseButton::get_button_group);
  466. GDVIRTUAL_BIND(_pressed);
  467. GDVIRTUAL_BIND(_toggled, "toggled_on");
  468. ADD_SIGNAL(MethodInfo("pressed"));
  469. ADD_SIGNAL(MethodInfo("button_up"));
  470. ADD_SIGNAL(MethodInfo("button_down"));
  471. ADD_SIGNAL(MethodInfo("toggled", PropertyInfo(Variant::BOOL, "toggled_on")));
  472. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
  473. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "toggle_mode"), "set_toggle_mode", "is_toggle_mode");
  474. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "button_pressed"), "set_pressed", "is_pressed");
  475. ADD_PROPERTY(PropertyInfo(Variant::INT, "action_mode", PROPERTY_HINT_ENUM, "Button Press,Button Release"), "set_action_mode", "get_action_mode");
  476. ADD_PROPERTY(PropertyInfo(Variant::INT, "button_mask", PROPERTY_HINT_FLAGS, "Mouse Left, Mouse Right, Mouse Middle"), "set_button_mask", "get_button_mask");
  477. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keep_pressed_outside"), "set_keep_pressed_outside", "is_keep_pressed_outside");
  478. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "button_group", PROPERTY_HINT_RESOURCE_TYPE, "ButtonGroup"), "set_button_group", "get_button_group");
  479. ADD_GROUP("Shortcut", "");
  480. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shortcut", PROPERTY_HINT_RESOURCE_TYPE, "Shortcut"), "set_shortcut", "get_shortcut");
  481. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_feedback"), "set_shortcut_feedback", "is_shortcut_feedback");
  482. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_in_tooltip"), "set_shortcut_in_tooltip", "is_shortcut_in_tooltip_enabled");
  483. BIND_ENUM_CONSTANT(DRAW_NORMAL);
  484. BIND_ENUM_CONSTANT(DRAW_PRESSED);
  485. BIND_ENUM_CONSTANT(DRAW_HOVER);
  486. BIND_ENUM_CONSTANT(DRAW_DISABLED);
  487. BIND_ENUM_CONSTANT(DRAW_HOVER_PRESSED);
  488. BIND_ENUM_CONSTANT(ACTION_MODE_BUTTON_PRESS);
  489. BIND_ENUM_CONSTANT(ACTION_MODE_BUTTON_RELEASE);
  490. GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "gui/timers/button_shortcut_feedback_highlight_time", PROPERTY_HINT_RANGE, "0.01,10,0.01,suffix:s"), 0.2);
  491. }
  492. BaseButton::BaseButton() {
  493. set_focus_mode(FOCUS_ALL);
  494. }
  495. BaseButton::~BaseButton() {
  496. if (button_group.is_valid()) {
  497. button_group->buttons.erase(this);
  498. }
  499. }
  500. void ButtonGroup::get_buttons(List<BaseButton *> *r_buttons) {
  501. for (BaseButton *E : buttons) {
  502. r_buttons->push_back(E);
  503. }
  504. }
  505. TypedArray<BaseButton> ButtonGroup::_get_buttons() {
  506. TypedArray<BaseButton> btns;
  507. for (const BaseButton *E : buttons) {
  508. btns.push_back(E);
  509. }
  510. return btns;
  511. }
  512. BaseButton *ButtonGroup::get_pressed_button() {
  513. for (BaseButton *E : buttons) {
  514. if (E->is_pressed()) {
  515. return E;
  516. }
  517. }
  518. return nullptr;
  519. }
  520. void ButtonGroup::set_allow_unpress(bool p_enabled) {
  521. allow_unpress = p_enabled;
  522. }
  523. bool ButtonGroup::is_allow_unpress() {
  524. return allow_unpress;
  525. }
  526. void ButtonGroup::_bind_methods() {
  527. ClassDB::bind_method(D_METHOD("get_pressed_button"), &ButtonGroup::get_pressed_button);
  528. ClassDB::bind_method(D_METHOD("get_buttons"), &ButtonGroup::_get_buttons);
  529. ClassDB::bind_method(D_METHOD("set_allow_unpress", "enabled"), &ButtonGroup::set_allow_unpress);
  530. ClassDB::bind_method(D_METHOD("is_allow_unpress"), &ButtonGroup::is_allow_unpress);
  531. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_unpress"), "set_allow_unpress", "is_allow_unpress");
  532. ADD_SIGNAL(MethodInfo("pressed", PropertyInfo(Variant::OBJECT, "button", PROPERTY_HINT_RESOURCE_TYPE, "BaseButton")));
  533. }
  534. ButtonGroup::ButtonGroup() {
  535. set_local_to_scene(true);
  536. }