base_button.cpp 16 KB

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