base_button.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /*************************************************************************/
  2. /* base_button.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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/os/keyboard.h"
  32. #include "scene/main/window.h"
  33. #include "scene/scene_string_names.h"
  34. void BaseButton::_unpress_group() {
  35. if (!button_group.is_valid()) {
  36. return;
  37. }
  38. if (toggle_mode) {
  39. status.pressed = true;
  40. }
  41. for (BaseButton *E : button_group->buttons) {
  42. if (E == this) {
  43. continue;
  44. }
  45. E->set_pressed(false);
  46. }
  47. }
  48. void BaseButton::gui_input(const Ref<InputEvent> &p_event) {
  49. ERR_FAIL_COND(p_event.is_null());
  50. if (status.disabled) { // no interaction with disabled button
  51. return;
  52. }
  53. Ref<InputEventMouseButton> mouse_button = p_event;
  54. bool ui_accept = p_event->is_action("ui_accept") && !p_event->is_echo();
  55. bool button_masked = mouse_button.is_valid() && (mouse_button_to_mask(mouse_button->get_button_index()) & button_mask) != MouseButton::NONE;
  56. if (button_masked || ui_accept) {
  57. was_mouse_pressed = button_masked;
  58. on_action_event(p_event);
  59. was_mouse_pressed = false;
  60. return;
  61. }
  62. Ref<InputEventMouseMotion> mouse_motion = p_event;
  63. if (mouse_motion.is_valid()) {
  64. if (status.press_attempt) {
  65. bool last_press_inside = status.pressing_inside;
  66. status.pressing_inside = has_point(mouse_motion->get_position());
  67. if (last_press_inside != status.pressing_inside) {
  68. queue_redraw();
  69. }
  70. }
  71. }
  72. }
  73. void BaseButton::_notification(int p_what) {
  74. switch (p_what) {
  75. case NOTIFICATION_MOUSE_ENTER: {
  76. status.hovering = true;
  77. queue_redraw();
  78. } break;
  79. case NOTIFICATION_MOUSE_EXIT: {
  80. status.hovering = false;
  81. queue_redraw();
  82. } break;
  83. case NOTIFICATION_DRAG_BEGIN:
  84. case NOTIFICATION_SCROLL_BEGIN: {
  85. if (status.press_attempt) {
  86. status.press_attempt = false;
  87. queue_redraw();
  88. }
  89. } break;
  90. case NOTIFICATION_FOCUS_ENTER: {
  91. queue_redraw();
  92. } break;
  93. case NOTIFICATION_FOCUS_EXIT: {
  94. if (status.press_attempt) {
  95. status.press_attempt = false;
  96. queue_redraw();
  97. } else if (status.hovering) {
  98. queue_redraw();
  99. }
  100. } break;
  101. case NOTIFICATION_VISIBILITY_CHANGED:
  102. case NOTIFICATION_EXIT_TREE: {
  103. if (p_what == NOTIFICATION_VISIBILITY_CHANGED && is_visible_in_tree()) {
  104. break;
  105. }
  106. if (!toggle_mode) {
  107. status.pressed = false;
  108. }
  109. status.hovering = false;
  110. status.press_attempt = false;
  111. status.pressing_inside = false;
  112. } break;
  113. }
  114. }
  115. void BaseButton::_pressed() {
  116. GDVIRTUAL_CALL(_pressed);
  117. pressed();
  118. emit_signal(SNAME("pressed"));
  119. }
  120. void BaseButton::_toggled(bool p_pressed) {
  121. GDVIRTUAL_CALL(_toggled, p_pressed);
  122. toggled(p_pressed);
  123. emit_signal(SNAME("toggled"), p_pressed);
  124. }
  125. void BaseButton::on_action_event(Ref<InputEvent> p_event) {
  126. if (p_event->is_pressed()) {
  127. status.press_attempt = true;
  128. status.pressing_inside = true;
  129. emit_signal(SNAME("button_down"));
  130. }
  131. if (status.press_attempt && status.pressing_inside) {
  132. if (toggle_mode) {
  133. bool is_pressed = p_event->is_pressed();
  134. if (Object::cast_to<InputEventShortcut>(*p_event)) {
  135. is_pressed = false;
  136. }
  137. if ((is_pressed && action_mode == ACTION_MODE_BUTTON_PRESS) || (!is_pressed && action_mode == ACTION_MODE_BUTTON_RELEASE)) {
  138. if (action_mode == ACTION_MODE_BUTTON_PRESS) {
  139. status.press_attempt = false;
  140. status.pressing_inside = false;
  141. }
  142. status.pressed = !status.pressed;
  143. _unpress_group();
  144. if (button_group.is_valid()) {
  145. button_group->emit_signal(SNAME("pressed"), this);
  146. }
  147. _toggled(status.pressed);
  148. _pressed();
  149. }
  150. } else {
  151. if ((p_event->is_pressed() && action_mode == ACTION_MODE_BUTTON_PRESS) || (!p_event->is_pressed() && action_mode == ACTION_MODE_BUTTON_RELEASE)) {
  152. _pressed();
  153. }
  154. }
  155. }
  156. if (!p_event->is_pressed()) {
  157. Ref<InputEventMouseButton> mouse_button = p_event;
  158. if (mouse_button.is_valid()) {
  159. if (!has_point(mouse_button->get_position())) {
  160. status.hovering = false;
  161. }
  162. }
  163. status.press_attempt = false;
  164. status.pressing_inside = false;
  165. emit_signal(SNAME("button_up"));
  166. }
  167. queue_redraw();
  168. }
  169. void BaseButton::pressed() {
  170. }
  171. void BaseButton::toggled(bool p_pressed) {
  172. }
  173. void BaseButton::set_disabled(bool p_disabled) {
  174. if (status.disabled == p_disabled) {
  175. return;
  176. }
  177. status.disabled = p_disabled;
  178. if (p_disabled) {
  179. if (!toggle_mode) {
  180. status.pressed = false;
  181. }
  182. status.press_attempt = false;
  183. status.pressing_inside = false;
  184. }
  185. queue_redraw();
  186. }
  187. bool BaseButton::is_disabled() const {
  188. return status.disabled;
  189. }
  190. void BaseButton::set_pressed(bool p_pressed) {
  191. if (!toggle_mode) {
  192. return;
  193. }
  194. if (status.pressed == p_pressed) {
  195. return;
  196. }
  197. status.pressed = p_pressed;
  198. if (p_pressed) {
  199. _unpress_group();
  200. if (button_group.is_valid()) {
  201. button_group->emit_signal(SNAME("pressed"), this);
  202. }
  203. }
  204. _toggled(status.pressed);
  205. queue_redraw();
  206. }
  207. void BaseButton::set_pressed_no_signal(bool p_pressed) {
  208. if (!toggle_mode) {
  209. return;
  210. }
  211. if (status.pressed == p_pressed) {
  212. return;
  213. }
  214. status.pressed = p_pressed;
  215. queue_redraw();
  216. }
  217. bool BaseButton::is_pressing() const {
  218. return status.press_attempt;
  219. }
  220. bool BaseButton::is_pressed() const {
  221. return toggle_mode ? status.pressed : status.press_attempt;
  222. }
  223. bool BaseButton::is_hovered() const {
  224. return status.hovering;
  225. }
  226. BaseButton::DrawMode BaseButton::get_draw_mode() const {
  227. if (status.disabled) {
  228. return DRAW_DISABLED;
  229. }
  230. if (!status.press_attempt && status.hovering) {
  231. if (status.pressed) {
  232. return DRAW_HOVER_PRESSED;
  233. }
  234. return DRAW_HOVER;
  235. } else {
  236. // Determine if pressed or not.
  237. bool pressing;
  238. if (status.press_attempt) {
  239. pressing = (status.pressing_inside || keep_pressed_outside);
  240. if (status.pressed) {
  241. pressing = !pressing;
  242. }
  243. } else {
  244. pressing = status.pressed;
  245. }
  246. if (pressing) {
  247. return DRAW_PRESSED;
  248. } else {
  249. return DRAW_NORMAL;
  250. }
  251. }
  252. }
  253. void BaseButton::set_toggle_mode(bool p_on) {
  254. // Make sure to set 'pressed' to false if we are not in toggle mode
  255. if (!p_on) {
  256. set_pressed(false);
  257. }
  258. toggle_mode = p_on;
  259. }
  260. bool BaseButton::is_toggle_mode() const {
  261. return toggle_mode;
  262. }
  263. void BaseButton::set_shortcut_in_tooltip(bool p_on) {
  264. shortcut_in_tooltip = p_on;
  265. }
  266. bool BaseButton::is_shortcut_in_tooltip_enabled() const {
  267. return shortcut_in_tooltip;
  268. }
  269. void BaseButton::set_action_mode(ActionMode p_mode) {
  270. action_mode = p_mode;
  271. }
  272. BaseButton::ActionMode BaseButton::get_action_mode() const {
  273. return action_mode;
  274. }
  275. void BaseButton::set_button_mask(MouseButton p_mask) {
  276. button_mask = p_mask;
  277. }
  278. MouseButton BaseButton::get_button_mask() const {
  279. return button_mask;
  280. }
  281. void BaseButton::set_keep_pressed_outside(bool p_on) {
  282. keep_pressed_outside = p_on;
  283. }
  284. bool BaseButton::is_keep_pressed_outside() const {
  285. return keep_pressed_outside;
  286. }
  287. void BaseButton::set_shortcut(const Ref<Shortcut> &p_shortcut) {
  288. shortcut = p_shortcut;
  289. set_process_shortcut_input(shortcut.is_valid());
  290. }
  291. Ref<Shortcut> BaseButton::get_shortcut() const {
  292. return shortcut;
  293. }
  294. void BaseButton::shortcut_input(const Ref<InputEvent> &p_event) {
  295. ERR_FAIL_COND(p_event.is_null());
  296. if (!_is_focus_owner_in_shortcut_context()) {
  297. return;
  298. }
  299. if (!is_disabled() && is_visible_in_tree() && !p_event->is_echo() && shortcut.is_valid() && shortcut->matches_event(p_event)) {
  300. on_action_event(p_event);
  301. accept_event();
  302. }
  303. }
  304. String BaseButton::get_tooltip(const Point2 &p_pos) const {
  305. String tooltip = Control::get_tooltip(p_pos);
  306. if (shortcut_in_tooltip && shortcut.is_valid() && shortcut->has_valid_event()) {
  307. String text = shortcut->get_name() + " (" + shortcut->get_as_text() + ")";
  308. if (!tooltip.is_empty() && shortcut->get_name().nocasecmp_to(tooltip) != 0) {
  309. text += "\n" + atr(tooltip);
  310. }
  311. tooltip = text;
  312. }
  313. return tooltip;
  314. }
  315. void BaseButton::set_button_group(const Ref<ButtonGroup> &p_group) {
  316. if (button_group.is_valid()) {
  317. button_group->buttons.erase(this);
  318. }
  319. button_group = p_group;
  320. if (button_group.is_valid()) {
  321. button_group->buttons.insert(this);
  322. }
  323. queue_redraw(); //checkbox changes to radio if set a buttongroup
  324. }
  325. Ref<ButtonGroup> BaseButton::get_button_group() const {
  326. return button_group;
  327. }
  328. void BaseButton::set_shortcut_context(Node *p_node) {
  329. if (p_node != nullptr) {
  330. shortcut_context = p_node->get_instance_id();
  331. } else {
  332. shortcut_context = ObjectID();
  333. }
  334. }
  335. Node *BaseButton::get_shortcut_context() const {
  336. Object *ctx_obj = ObjectDB::get_instance(shortcut_context);
  337. Node *ctx_node = Object::cast_to<Node>(ctx_obj);
  338. return ctx_node;
  339. }
  340. bool BaseButton::_is_focus_owner_in_shortcut_context() const {
  341. if (shortcut_context == ObjectID()) {
  342. // No context, therefore global - always "in" context.
  343. return true;
  344. }
  345. Node *ctx_node = get_shortcut_context();
  346. Control *vp_focus = get_viewport() ? get_viewport()->gui_get_focus_owner() : nullptr;
  347. // If the context is valid and the viewport focus is valid, check if the context is the focus or is a parent of it.
  348. return ctx_node && vp_focus && (ctx_node == vp_focus || ctx_node->is_ancestor_of(vp_focus));
  349. }
  350. bool BaseButton::_was_pressed_by_mouse() const {
  351. return was_mouse_pressed;
  352. }
  353. void BaseButton::_bind_methods() {
  354. ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &BaseButton::set_pressed);
  355. ClassDB::bind_method(D_METHOD("is_pressed"), &BaseButton::is_pressed);
  356. ClassDB::bind_method(D_METHOD("set_pressed_no_signal", "pressed"), &BaseButton::set_pressed_no_signal);
  357. ClassDB::bind_method(D_METHOD("is_hovered"), &BaseButton::is_hovered);
  358. ClassDB::bind_method(D_METHOD("set_toggle_mode", "enabled"), &BaseButton::set_toggle_mode);
  359. ClassDB::bind_method(D_METHOD("is_toggle_mode"), &BaseButton::is_toggle_mode);
  360. ClassDB::bind_method(D_METHOD("set_shortcut_in_tooltip", "enabled"), &BaseButton::set_shortcut_in_tooltip);
  361. ClassDB::bind_method(D_METHOD("is_shortcut_in_tooltip_enabled"), &BaseButton::is_shortcut_in_tooltip_enabled);
  362. ClassDB::bind_method(D_METHOD("set_disabled", "disabled"), &BaseButton::set_disabled);
  363. ClassDB::bind_method(D_METHOD("is_disabled"), &BaseButton::is_disabled);
  364. ClassDB::bind_method(D_METHOD("set_action_mode", "mode"), &BaseButton::set_action_mode);
  365. ClassDB::bind_method(D_METHOD("get_action_mode"), &BaseButton::get_action_mode);
  366. ClassDB::bind_method(D_METHOD("set_button_mask", "mask"), &BaseButton::set_button_mask);
  367. ClassDB::bind_method(D_METHOD("get_button_mask"), &BaseButton::get_button_mask);
  368. ClassDB::bind_method(D_METHOD("get_draw_mode"), &BaseButton::get_draw_mode);
  369. ClassDB::bind_method(D_METHOD("set_keep_pressed_outside", "enabled"), &BaseButton::set_keep_pressed_outside);
  370. ClassDB::bind_method(D_METHOD("is_keep_pressed_outside"), &BaseButton::is_keep_pressed_outside);
  371. ClassDB::bind_method(D_METHOD("set_shortcut", "shortcut"), &BaseButton::set_shortcut);
  372. ClassDB::bind_method(D_METHOD("get_shortcut"), &BaseButton::get_shortcut);
  373. ClassDB::bind_method(D_METHOD("set_button_group", "button_group"), &BaseButton::set_button_group);
  374. ClassDB::bind_method(D_METHOD("get_button_group"), &BaseButton::get_button_group);
  375. ClassDB::bind_method(D_METHOD("set_shortcut_context", "node"), &BaseButton::set_shortcut_context);
  376. ClassDB::bind_method(D_METHOD("get_shortcut_context"), &BaseButton::get_shortcut_context);
  377. GDVIRTUAL_BIND(_pressed);
  378. GDVIRTUAL_BIND(_toggled, "button_pressed");
  379. ADD_SIGNAL(MethodInfo("pressed"));
  380. ADD_SIGNAL(MethodInfo("button_up"));
  381. ADD_SIGNAL(MethodInfo("button_down"));
  382. ADD_SIGNAL(MethodInfo("toggled", PropertyInfo(Variant::BOOL, "button_pressed")));
  383. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
  384. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "toggle_mode"), "set_toggle_mode", "is_toggle_mode");
  385. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_in_tooltip"), "set_shortcut_in_tooltip", "is_shortcut_in_tooltip_enabled");
  386. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "button_pressed"), "set_pressed", "is_pressed");
  387. ADD_PROPERTY(PropertyInfo(Variant::INT, "action_mode", PROPERTY_HINT_ENUM, "Button Press,Button Release"), "set_action_mode", "get_action_mode");
  388. ADD_PROPERTY(PropertyInfo(Variant::INT, "button_mask", PROPERTY_HINT_FLAGS, "Mouse Left, Mouse Right, Mouse Middle"), "set_button_mask", "get_button_mask");
  389. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keep_pressed_outside"), "set_keep_pressed_outside", "is_keep_pressed_outside");
  390. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shortcut", PROPERTY_HINT_RESOURCE_TYPE, "Shortcut"), "set_shortcut", "get_shortcut");
  391. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "button_group", PROPERTY_HINT_RESOURCE_TYPE, "ButtonGroup"), "set_button_group", "get_button_group");
  392. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shortcut_context", PROPERTY_HINT_NODE_TYPE, "Node"), "set_shortcut_context", "get_shortcut_context");
  393. BIND_ENUM_CONSTANT(DRAW_NORMAL);
  394. BIND_ENUM_CONSTANT(DRAW_PRESSED);
  395. BIND_ENUM_CONSTANT(DRAW_HOVER);
  396. BIND_ENUM_CONSTANT(DRAW_DISABLED);
  397. BIND_ENUM_CONSTANT(DRAW_HOVER_PRESSED);
  398. BIND_ENUM_CONSTANT(ACTION_MODE_BUTTON_PRESS);
  399. BIND_ENUM_CONSTANT(ACTION_MODE_BUTTON_RELEASE);
  400. }
  401. BaseButton::BaseButton() {
  402. set_focus_mode(FOCUS_ALL);
  403. }
  404. BaseButton::~BaseButton() {
  405. if (button_group.is_valid()) {
  406. button_group->buttons.erase(this);
  407. }
  408. }
  409. void ButtonGroup::get_buttons(List<BaseButton *> *r_buttons) {
  410. for (BaseButton *E : buttons) {
  411. r_buttons->push_back(E);
  412. }
  413. }
  414. TypedArray<BaseButton> ButtonGroup::_get_buttons() {
  415. TypedArray<BaseButton> btns;
  416. for (const BaseButton *E : buttons) {
  417. btns.push_back(E);
  418. }
  419. return btns;
  420. }
  421. BaseButton *ButtonGroup::get_pressed_button() {
  422. for (BaseButton *E : buttons) {
  423. if (E->is_pressed()) {
  424. return E;
  425. }
  426. }
  427. return nullptr;
  428. }
  429. void ButtonGroup::_bind_methods() {
  430. ClassDB::bind_method(D_METHOD("get_pressed_button"), &ButtonGroup::get_pressed_button);
  431. ClassDB::bind_method(D_METHOD("get_buttons"), &ButtonGroup::_get_buttons);
  432. ADD_SIGNAL(MethodInfo("pressed", PropertyInfo(Variant::OBJECT, "button", PROPERTY_HINT_RESOURCE_TYPE, "BaseButton")));
  433. }
  434. ButtonGroup::ButtonGroup() {
  435. set_local_to_scene(true);
  436. }