2
0

base_button.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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. if (status.pressed_down_with_focus) {
  234. status.pressed_down_with_focus = false;
  235. emit_signal(SNAME("button_up"));
  236. }
  237. }
  238. queue_accessibility_update();
  239. queue_redraw();
  240. update_minimum_size();
  241. }
  242. bool BaseButton::is_disabled() const {
  243. return status.disabled;
  244. }
  245. void BaseButton::set_pressed(bool p_pressed) {
  246. bool prev_pressed = status.pressed;
  247. set_pressed_no_signal(p_pressed);
  248. if (status.pressed == prev_pressed) {
  249. return;
  250. }
  251. if (p_pressed) {
  252. _unpress_group();
  253. if (button_group.is_valid()) {
  254. button_group->emit_signal(SceneStringName(pressed), this);
  255. }
  256. }
  257. _toggled(status.pressed);
  258. }
  259. void BaseButton::set_pressed_no_signal(bool p_pressed) {
  260. if (!toggle_mode) {
  261. return;
  262. }
  263. if (status.pressed == p_pressed) {
  264. return;
  265. }
  266. status.pressed = p_pressed;
  267. queue_accessibility_update();
  268. queue_redraw();
  269. }
  270. bool BaseButton::is_pressing() const {
  271. return status.press_attempt;
  272. }
  273. bool BaseButton::is_pressed() const {
  274. return toggle_mode ? status.pressed : status.press_attempt;
  275. }
  276. bool BaseButton::is_hovered() const {
  277. return status.hovering;
  278. }
  279. BaseButton::DrawMode BaseButton::get_draw_mode() const {
  280. if (status.disabled) {
  281. return DRAW_DISABLED;
  282. }
  283. if (in_shortcut_feedback) {
  284. return DRAW_HOVER_PRESSED;
  285. }
  286. if (!status.press_attempt && status.hovering) {
  287. if (status.pressed) {
  288. return DRAW_HOVER_PRESSED;
  289. }
  290. return DRAW_HOVER;
  291. } else {
  292. // Determine if pressed or not.
  293. bool pressing;
  294. if (status.press_attempt) {
  295. pressing = (status.pressing_inside || keep_pressed_outside);
  296. if (status.pressed) {
  297. pressing = !pressing;
  298. }
  299. } else {
  300. pressing = status.pressed;
  301. }
  302. if (pressing) {
  303. return DRAW_PRESSED;
  304. } else {
  305. return DRAW_NORMAL;
  306. }
  307. }
  308. }
  309. void BaseButton::set_toggle_mode(bool p_on) {
  310. // Make sure to set 'pressed' to false if we are not in toggle mode
  311. if (!p_on) {
  312. set_pressed(false);
  313. }
  314. queue_accessibility_update();
  315. toggle_mode = p_on;
  316. update_configuration_warnings();
  317. }
  318. bool BaseButton::is_toggle_mode() const {
  319. return toggle_mode;
  320. }
  321. void BaseButton::set_shortcut_in_tooltip(bool p_on) {
  322. if (shortcut_in_tooltip != p_on) {
  323. shortcut_in_tooltip = p_on;
  324. queue_accessibility_update();
  325. }
  326. }
  327. bool BaseButton::is_shortcut_in_tooltip_enabled() const {
  328. return shortcut_in_tooltip;
  329. }
  330. void BaseButton::set_action_mode(ActionMode p_mode) {
  331. action_mode = p_mode;
  332. }
  333. BaseButton::ActionMode BaseButton::get_action_mode() const {
  334. return action_mode;
  335. }
  336. void BaseButton::set_button_mask(BitField<MouseButtonMask> p_mask) {
  337. button_mask = p_mask;
  338. }
  339. BitField<MouseButtonMask> BaseButton::get_button_mask() const {
  340. return button_mask;
  341. }
  342. void BaseButton::set_keep_pressed_outside(bool p_on) {
  343. keep_pressed_outside = p_on;
  344. }
  345. bool BaseButton::is_keep_pressed_outside() const {
  346. return keep_pressed_outside;
  347. }
  348. void BaseButton::set_shortcut_feedback(bool p_enable) {
  349. shortcut_feedback = p_enable;
  350. }
  351. bool BaseButton::is_shortcut_feedback() const {
  352. return shortcut_feedback;
  353. }
  354. void BaseButton::set_shortcut(const Ref<Shortcut> &p_shortcut) {
  355. if (shortcut != p_shortcut) {
  356. shortcut = p_shortcut;
  357. set_process_shortcut_input(shortcut.is_valid());
  358. queue_accessibility_update();
  359. }
  360. }
  361. Ref<Shortcut> BaseButton::get_shortcut() const {
  362. return shortcut;
  363. }
  364. void BaseButton::_shortcut_feedback_timeout() {
  365. in_shortcut_feedback = false;
  366. queue_redraw();
  367. }
  368. void BaseButton::shortcut_input(const Ref<InputEvent> &p_event) {
  369. ERR_FAIL_COND(p_event.is_null());
  370. if (!is_disabled() && p_event->is_pressed() && is_visible_in_tree() && !p_event->is_echo() && shortcut.is_valid() && shortcut->matches_event(p_event)) {
  371. if (toggle_mode) {
  372. status.pressed = !status.pressed;
  373. _unpress_group();
  374. if (button_group.is_valid()) {
  375. button_group->emit_signal(SceneStringName(pressed), this);
  376. }
  377. _toggled(status.pressed);
  378. _pressed();
  379. queue_accessibility_update();
  380. } else {
  381. _pressed();
  382. }
  383. queue_redraw();
  384. accept_event();
  385. if (shortcut_feedback && is_inside_tree()) {
  386. if (shortcut_feedback_timer == nullptr) {
  387. shortcut_feedback_timer = memnew(Timer);
  388. shortcut_feedback_timer->set_one_shot(true);
  389. add_child(shortcut_feedback_timer, false, INTERNAL_MODE_BACK);
  390. shortcut_feedback_timer->set_wait_time(GLOBAL_GET_CACHED(double, "gui/timers/button_shortcut_feedback_highlight_time"));
  391. shortcut_feedback_timer->connect("timeout", callable_mp(this, &BaseButton::_shortcut_feedback_timeout));
  392. }
  393. in_shortcut_feedback = true;
  394. shortcut_feedback_timer->start();
  395. }
  396. }
  397. }
  398. Control *BaseButton::make_custom_tooltip(const String &p_text) const {
  399. Control *control = Control::make_custom_tooltip(p_text);
  400. if (control) {
  401. return control;
  402. }
  403. if (!shortcut_in_tooltip || shortcut.is_null() || !shortcut->has_valid_event()) {
  404. return nullptr; // Use the default tooltip label.
  405. }
  406. String text = atr(shortcut->get_name()) + " (" + shortcut->get_as_text() + ")";
  407. if (!p_text.is_empty() && shortcut->get_name().nocasecmp_to(p_text) != 0) {
  408. text += "\n" + atr(p_text);
  409. }
  410. // Make a label similar to the default tooltip label.
  411. // Auto translation is disabled because we already did that manually above.
  412. //
  413. // We can't customize the tooltip text by overriding `get_tooltip()`
  414. // because otherwise user-defined `_make_custom_tooltip()` would receive
  415. // the translated and annotated text.
  416. Label *label = memnew(Label(text));
  417. label->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  418. label->set_theme_type_variation(SNAME("TooltipLabel"));
  419. return label;
  420. }
  421. void BaseButton::set_button_group(const Ref<ButtonGroup> &p_group) {
  422. if (button_group.is_valid()) {
  423. button_group->buttons.erase(this);
  424. }
  425. button_group = p_group;
  426. if (button_group.is_valid()) {
  427. button_group->buttons.insert(this);
  428. }
  429. queue_accessibility_update();
  430. queue_redraw(); //checkbox changes to radio if set a buttongroup
  431. update_configuration_warnings();
  432. }
  433. Ref<ButtonGroup> BaseButton::get_button_group() const {
  434. return button_group;
  435. }
  436. bool BaseButton::_was_pressed_by_mouse() const {
  437. return was_mouse_pressed;
  438. }
  439. PackedStringArray BaseButton::get_configuration_warnings() const {
  440. PackedStringArray warnings = Control::get_configuration_warnings();
  441. if (get_button_group().is_valid() && !is_toggle_mode()) {
  442. warnings.push_back(RTR("ButtonGroup is intended to be used only with buttons that have toggle_mode set to true."));
  443. }
  444. return warnings;
  445. }
  446. void BaseButton::_bind_methods() {
  447. ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &BaseButton::set_pressed);
  448. ClassDB::bind_method(D_METHOD("is_pressed"), &BaseButton::is_pressed);
  449. ClassDB::bind_method(D_METHOD("set_pressed_no_signal", "pressed"), &BaseButton::set_pressed_no_signal);
  450. ClassDB::bind_method(D_METHOD("is_hovered"), &BaseButton::is_hovered);
  451. ClassDB::bind_method(D_METHOD("set_toggle_mode", "enabled"), &BaseButton::set_toggle_mode);
  452. ClassDB::bind_method(D_METHOD("is_toggle_mode"), &BaseButton::is_toggle_mode);
  453. ClassDB::bind_method(D_METHOD("set_shortcut_in_tooltip", "enabled"), &BaseButton::set_shortcut_in_tooltip);
  454. ClassDB::bind_method(D_METHOD("is_shortcut_in_tooltip_enabled"), &BaseButton::is_shortcut_in_tooltip_enabled);
  455. ClassDB::bind_method(D_METHOD("set_disabled", "disabled"), &BaseButton::set_disabled);
  456. ClassDB::bind_method(D_METHOD("is_disabled"), &BaseButton::is_disabled);
  457. ClassDB::bind_method(D_METHOD("set_action_mode", "mode"), &BaseButton::set_action_mode);
  458. ClassDB::bind_method(D_METHOD("get_action_mode"), &BaseButton::get_action_mode);
  459. ClassDB::bind_method(D_METHOD("set_button_mask", "mask"), &BaseButton::set_button_mask);
  460. ClassDB::bind_method(D_METHOD("get_button_mask"), &BaseButton::get_button_mask);
  461. ClassDB::bind_method(D_METHOD("get_draw_mode"), &BaseButton::get_draw_mode);
  462. ClassDB::bind_method(D_METHOD("set_keep_pressed_outside", "enabled"), &BaseButton::set_keep_pressed_outside);
  463. ClassDB::bind_method(D_METHOD("is_keep_pressed_outside"), &BaseButton::is_keep_pressed_outside);
  464. ClassDB::bind_method(D_METHOD("set_shortcut_feedback", "enabled"), &BaseButton::set_shortcut_feedback);
  465. ClassDB::bind_method(D_METHOD("is_shortcut_feedback"), &BaseButton::is_shortcut_feedback);
  466. ClassDB::bind_method(D_METHOD("set_shortcut", "shortcut"), &BaseButton::set_shortcut);
  467. ClassDB::bind_method(D_METHOD("get_shortcut"), &BaseButton::get_shortcut);
  468. ClassDB::bind_method(D_METHOD("set_button_group", "button_group"), &BaseButton::set_button_group);
  469. ClassDB::bind_method(D_METHOD("get_button_group"), &BaseButton::get_button_group);
  470. GDVIRTUAL_BIND(_pressed);
  471. GDVIRTUAL_BIND(_toggled, "toggled_on");
  472. ADD_SIGNAL(MethodInfo("pressed"));
  473. ADD_SIGNAL(MethodInfo("button_up"));
  474. ADD_SIGNAL(MethodInfo("button_down"));
  475. ADD_SIGNAL(MethodInfo("toggled", PropertyInfo(Variant::BOOL, "toggled_on")));
  476. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
  477. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "toggle_mode"), "set_toggle_mode", "is_toggle_mode");
  478. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "button_pressed"), "set_pressed", "is_pressed");
  479. ADD_PROPERTY(PropertyInfo(Variant::INT, "action_mode", PROPERTY_HINT_ENUM, "Button Press,Button Release"), "set_action_mode", "get_action_mode");
  480. ADD_PROPERTY(PropertyInfo(Variant::INT, "button_mask", PROPERTY_HINT_FLAGS, "Mouse Left, Mouse Right, Mouse Middle"), "set_button_mask", "get_button_mask");
  481. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keep_pressed_outside"), "set_keep_pressed_outside", "is_keep_pressed_outside");
  482. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "button_group", PROPERTY_HINT_RESOURCE_TYPE, "ButtonGroup"), "set_button_group", "get_button_group");
  483. ADD_GROUP("Shortcut", "");
  484. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shortcut", PROPERTY_HINT_RESOURCE_TYPE, "Shortcut"), "set_shortcut", "get_shortcut");
  485. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_feedback"), "set_shortcut_feedback", "is_shortcut_feedback");
  486. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_in_tooltip"), "set_shortcut_in_tooltip", "is_shortcut_in_tooltip_enabled");
  487. BIND_ENUM_CONSTANT(DRAW_NORMAL);
  488. BIND_ENUM_CONSTANT(DRAW_PRESSED);
  489. BIND_ENUM_CONSTANT(DRAW_HOVER);
  490. BIND_ENUM_CONSTANT(DRAW_DISABLED);
  491. BIND_ENUM_CONSTANT(DRAW_HOVER_PRESSED);
  492. BIND_ENUM_CONSTANT(ACTION_MODE_BUTTON_PRESS);
  493. BIND_ENUM_CONSTANT(ACTION_MODE_BUTTON_RELEASE);
  494. GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "gui/timers/button_shortcut_feedback_highlight_time", PROPERTY_HINT_RANGE, "0.01,10,0.01,suffix:s"), 0.2);
  495. }
  496. BaseButton::BaseButton() {
  497. set_focus_mode(FOCUS_ALL);
  498. }
  499. BaseButton::~BaseButton() {
  500. if (button_group.is_valid()) {
  501. button_group->buttons.erase(this);
  502. }
  503. }
  504. void ButtonGroup::get_buttons(List<BaseButton *> *r_buttons) {
  505. for (BaseButton *E : buttons) {
  506. r_buttons->push_back(E);
  507. }
  508. }
  509. TypedArray<BaseButton> ButtonGroup::_get_buttons() {
  510. TypedArray<BaseButton> btns;
  511. for (const BaseButton *E : buttons) {
  512. btns.push_back(E);
  513. }
  514. return btns;
  515. }
  516. BaseButton *ButtonGroup::get_pressed_button() {
  517. for (BaseButton *E : buttons) {
  518. if (E->is_pressed()) {
  519. return E;
  520. }
  521. }
  522. return nullptr;
  523. }
  524. void ButtonGroup::set_allow_unpress(bool p_enabled) {
  525. allow_unpress = p_enabled;
  526. }
  527. bool ButtonGroup::is_allow_unpress() {
  528. return allow_unpress;
  529. }
  530. void ButtonGroup::_bind_methods() {
  531. ClassDB::bind_method(D_METHOD("get_pressed_button"), &ButtonGroup::get_pressed_button);
  532. ClassDB::bind_method(D_METHOD("get_buttons"), &ButtonGroup::_get_buttons);
  533. ClassDB::bind_method(D_METHOD("set_allow_unpress", "enabled"), &ButtonGroup::set_allow_unpress);
  534. ClassDB::bind_method(D_METHOD("is_allow_unpress"), &ButtonGroup::is_allow_unpress);
  535. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_unpress"), "set_allow_unpress", "is_allow_unpress");
  536. ADD_SIGNAL(MethodInfo("pressed", PropertyInfo(Variant::OBJECT, "button", PROPERTY_HINT_RESOURCE_TYPE, "BaseButton")));
  537. }
  538. ButtonGroup::ButtonGroup() {
  539. set_local_to_scene(true);
  540. }