base_button.cpp 20 KB

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