base_button.cpp 17 KB

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