base_button.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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 (Set<BaseButton *>::Element *E = button_group->buttons.front(); E; E = E->next()) {
  42. if (E->get() == this) {
  43. continue;
  44. }
  45. E->get()->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. on_action_event(p_event);
  58. return;
  59. }
  60. Ref<InputEventMouseMotion> mouse_motion = p_event;
  61. if (mouse_motion.is_valid()) {
  62. if (status.press_attempt) {
  63. bool last_press_inside = status.pressing_inside;
  64. status.pressing_inside = has_point(mouse_motion->get_position());
  65. if (last_press_inside != status.pressing_inside) {
  66. update();
  67. }
  68. }
  69. }
  70. }
  71. void BaseButton::_notification(int p_what) {
  72. switch (p_what) {
  73. case NOTIFICATION_MOUSE_ENTER: {
  74. status.hovering = true;
  75. update();
  76. } break;
  77. case NOTIFICATION_MOUSE_EXIT: {
  78. status.hovering = false;
  79. update();
  80. } break;
  81. case NOTIFICATION_DRAG_BEGIN:
  82. case NOTIFICATION_SCROLL_BEGIN: {
  83. if (status.press_attempt) {
  84. status.press_attempt = false;
  85. update();
  86. }
  87. } break;
  88. case NOTIFICATION_FOCUS_ENTER: {
  89. update();
  90. } break;
  91. case NOTIFICATION_FOCUS_EXIT: {
  92. if (status.press_attempt) {
  93. status.press_attempt = false;
  94. update();
  95. } else if (status.hovering) {
  96. update();
  97. }
  98. } break;
  99. case NOTIFICATION_VISIBILITY_CHANGED:
  100. case NOTIFICATION_EXIT_TREE: {
  101. if (p_what == NOTIFICATION_VISIBILITY_CHANGED && is_visible_in_tree()) {
  102. break;
  103. }
  104. if (!toggle_mode) {
  105. status.pressed = false;
  106. }
  107. status.hovering = false;
  108. status.press_attempt = false;
  109. status.pressing_inside = false;
  110. } break;
  111. }
  112. }
  113. void BaseButton::_pressed() {
  114. GDVIRTUAL_CALL(_pressed);
  115. pressed();
  116. emit_signal(SNAME("pressed"));
  117. }
  118. void BaseButton::_toggled(bool p_pressed) {
  119. GDVIRTUAL_CALL(_toggled, p_pressed);
  120. toggled(p_pressed);
  121. emit_signal(SNAME("toggled"), p_pressed);
  122. }
  123. void BaseButton::on_action_event(Ref<InputEvent> p_event) {
  124. if (p_event->is_pressed()) {
  125. status.press_attempt = true;
  126. status.pressing_inside = true;
  127. emit_signal(SNAME("button_down"));
  128. }
  129. if (status.press_attempt && status.pressing_inside) {
  130. if (toggle_mode) {
  131. bool is_pressed = p_event->is_pressed();
  132. if (Object::cast_to<InputEventShortcut>(*p_event)) {
  133. is_pressed = false;
  134. }
  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. update();
  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. update();
  184. }
  185. bool BaseButton::is_disabled() const {
  186. return status.disabled;
  187. }
  188. void BaseButton::set_pressed(bool p_pressed) {
  189. if (!toggle_mode) {
  190. return;
  191. }
  192. if (status.pressed == p_pressed) {
  193. return;
  194. }
  195. status.pressed = p_pressed;
  196. if (p_pressed) {
  197. _unpress_group();
  198. if (button_group.is_valid()) {
  199. button_group->emit_signal(SNAME("pressed"), this);
  200. }
  201. }
  202. _toggled(status.pressed);
  203. update();
  204. }
  205. void BaseButton::set_pressed_no_signal(bool p_pressed) {
  206. if (!toggle_mode) {
  207. return;
  208. }
  209. if (status.pressed == p_pressed) {
  210. return;
  211. }
  212. status.pressed = p_pressed;
  213. update();
  214. }
  215. bool BaseButton::is_pressing() const {
  216. return status.press_attempt;
  217. }
  218. bool BaseButton::is_pressed() const {
  219. return toggle_mode ? status.pressed : status.press_attempt;
  220. }
  221. bool BaseButton::is_hovered() const {
  222. return status.hovering;
  223. }
  224. BaseButton::DrawMode BaseButton::get_draw_mode() const {
  225. if (status.disabled) {
  226. return DRAW_DISABLED;
  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. return DRAW_NORMAL;
  251. }
  252. void BaseButton::set_toggle_mode(bool p_on) {
  253. // Make sure to set 'pressed' to false if we are not in toggle mode
  254. if (!p_on) {
  255. set_pressed(false);
  256. }
  257. toggle_mode = p_on;
  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(MouseButton p_mask) {
  275. button_mask = p_mask;
  276. }
  277. MouseButton 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(const Ref<Shortcut> &p_shortcut) {
  287. shortcut = p_shortcut;
  288. set_process_unhandled_key_input(shortcut.is_valid());
  289. }
  290. Ref<Shortcut> BaseButton::get_shortcut() const {
  291. return shortcut;
  292. }
  293. void BaseButton::unhandled_key_input(const Ref<InputEvent> &p_event) {
  294. ERR_FAIL_COND(p_event.is_null());
  295. if (!_is_focus_owner_in_shorcut_context()) {
  296. return;
  297. }
  298. if (!is_disabled() && is_visible_in_tree() && !p_event->is_echo() && shortcut.is_valid() && shortcut->matches_event(p_event)) {
  299. on_action_event(p_event);
  300. accept_event();
  301. }
  302. }
  303. String BaseButton::get_tooltip(const Point2 &p_pos) const {
  304. String tooltip = Control::get_tooltip(p_pos);
  305. if (shortcut_in_tooltip && shortcut.is_valid() && shortcut->has_valid_event()) {
  306. String text = shortcut->get_name() + " (" + shortcut->get_as_text() + ")";
  307. if (!tooltip.is_empty() && shortcut->get_name().nocasecmp_to(tooltip) != 0) {
  308. text += "\n" + atr(tooltip);
  309. }
  310. tooltip = text;
  311. }
  312. return tooltip;
  313. }
  314. void BaseButton::set_button_group(const Ref<ButtonGroup> &p_group) {
  315. if (button_group.is_valid()) {
  316. button_group->buttons.erase(this);
  317. }
  318. button_group = p_group;
  319. if (button_group.is_valid()) {
  320. button_group->buttons.insert(this);
  321. }
  322. update(); //checkbox changes to radio if set a buttongroup
  323. }
  324. Ref<ButtonGroup> BaseButton::get_button_group() const {
  325. return button_group;
  326. }
  327. void BaseButton::set_shortcut_context(Node *p_node) {
  328. if (p_node != nullptr) {
  329. shortcut_context = p_node->get_instance_id();
  330. } else {
  331. shortcut_context = ObjectID();
  332. }
  333. }
  334. Node *BaseButton::get_shortcut_context() const {
  335. Object *ctx_obj = ObjectDB::get_instance(shortcut_context);
  336. Node *ctx_node = Object::cast_to<Node>(ctx_obj);
  337. return ctx_node;
  338. }
  339. bool BaseButton::_is_focus_owner_in_shorcut_context() const {
  340. if (shortcut_context == ObjectID()) {
  341. // No context, therefore global - always "in" context.
  342. return true;
  343. }
  344. Node *ctx_node = get_shortcut_context();
  345. Control *vp_focus = get_viewport() ? get_viewport()->gui_get_focus_owner() : nullptr;
  346. // If the context is valid and the viewport focus is valid, check if the context is the focus or is a parent of it.
  347. return ctx_node && vp_focus && (ctx_node == vp_focus || ctx_node->is_ancestor_of(vp_focus));
  348. }
  349. void BaseButton::_bind_methods() {
  350. ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &BaseButton::set_pressed);
  351. ClassDB::bind_method(D_METHOD("is_pressed"), &BaseButton::is_pressed);
  352. ClassDB::bind_method(D_METHOD("set_pressed_no_signal", "pressed"), &BaseButton::set_pressed_no_signal);
  353. ClassDB::bind_method(D_METHOD("is_hovered"), &BaseButton::is_hovered);
  354. ClassDB::bind_method(D_METHOD("set_toggle_mode", "enabled"), &BaseButton::set_toggle_mode);
  355. ClassDB::bind_method(D_METHOD("is_toggle_mode"), &BaseButton::is_toggle_mode);
  356. ClassDB::bind_method(D_METHOD("set_shortcut_in_tooltip", "enabled"), &BaseButton::set_shortcut_in_tooltip);
  357. ClassDB::bind_method(D_METHOD("is_shortcut_in_tooltip_enabled"), &BaseButton::is_shortcut_in_tooltip_enabled);
  358. ClassDB::bind_method(D_METHOD("set_disabled", "disabled"), &BaseButton::set_disabled);
  359. ClassDB::bind_method(D_METHOD("is_disabled"), &BaseButton::is_disabled);
  360. ClassDB::bind_method(D_METHOD("set_action_mode", "mode"), &BaseButton::set_action_mode);
  361. ClassDB::bind_method(D_METHOD("get_action_mode"), &BaseButton::get_action_mode);
  362. ClassDB::bind_method(D_METHOD("set_button_mask", "mask"), &BaseButton::set_button_mask);
  363. ClassDB::bind_method(D_METHOD("get_button_mask"), &BaseButton::get_button_mask);
  364. ClassDB::bind_method(D_METHOD("get_draw_mode"), &BaseButton::get_draw_mode);
  365. ClassDB::bind_method(D_METHOD("set_keep_pressed_outside", "enabled"), &BaseButton::set_keep_pressed_outside);
  366. ClassDB::bind_method(D_METHOD("is_keep_pressed_outside"), &BaseButton::is_keep_pressed_outside);
  367. ClassDB::bind_method(D_METHOD("set_shortcut", "shortcut"), &BaseButton::set_shortcut);
  368. ClassDB::bind_method(D_METHOD("get_shortcut"), &BaseButton::get_shortcut);
  369. ClassDB::bind_method(D_METHOD("set_button_group", "button_group"), &BaseButton::set_button_group);
  370. ClassDB::bind_method(D_METHOD("get_button_group"), &BaseButton::get_button_group);
  371. ClassDB::bind_method(D_METHOD("set_shortcut_context", "node"), &BaseButton::set_shortcut_context);
  372. ClassDB::bind_method(D_METHOD("get_shortcut_context"), &BaseButton::get_shortcut_context);
  373. GDVIRTUAL_BIND(_pressed);
  374. GDVIRTUAL_BIND(_toggled, "button_pressed");
  375. ADD_SIGNAL(MethodInfo("pressed"));
  376. ADD_SIGNAL(MethodInfo("button_up"));
  377. ADD_SIGNAL(MethodInfo("button_down"));
  378. ADD_SIGNAL(MethodInfo("toggled", PropertyInfo(Variant::BOOL, "button_pressed")));
  379. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
  380. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "toggle_mode"), "set_toggle_mode", "is_toggle_mode");
  381. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_in_tooltip"), "set_shortcut_in_tooltip", "is_shortcut_in_tooltip_enabled");
  382. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "button_pressed"), "set_pressed", "is_pressed");
  383. ADD_PROPERTY(PropertyInfo(Variant::INT, "action_mode", PROPERTY_HINT_ENUM, "Button Press,Button Release"), "set_action_mode", "get_action_mode");
  384. ADD_PROPERTY(PropertyInfo(Variant::INT, "button_mask", PROPERTY_HINT_FLAGS, "Mouse Left, Mouse Right, Mouse Middle"), "set_button_mask", "get_button_mask");
  385. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keep_pressed_outside"), "set_keep_pressed_outside", "is_keep_pressed_outside");
  386. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shortcut", PROPERTY_HINT_RESOURCE_TYPE, "Shortcut"), "set_shortcut", "get_shortcut");
  387. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "button_group", PROPERTY_HINT_RESOURCE_TYPE, "ButtonGroup"), "set_button_group", "get_button_group");
  388. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shortcut_context", PROPERTY_HINT_RESOURCE_TYPE, "Node"), "set_shortcut_context", "get_shortcut_context");
  389. BIND_ENUM_CONSTANT(DRAW_NORMAL);
  390. BIND_ENUM_CONSTANT(DRAW_PRESSED);
  391. BIND_ENUM_CONSTANT(DRAW_HOVER);
  392. BIND_ENUM_CONSTANT(DRAW_DISABLED);
  393. BIND_ENUM_CONSTANT(DRAW_HOVER_PRESSED);
  394. BIND_ENUM_CONSTANT(ACTION_MODE_BUTTON_PRESS);
  395. BIND_ENUM_CONSTANT(ACTION_MODE_BUTTON_RELEASE);
  396. }
  397. BaseButton::BaseButton() {
  398. set_focus_mode(FOCUS_ALL);
  399. }
  400. BaseButton::~BaseButton() {
  401. if (button_group.is_valid()) {
  402. button_group->buttons.erase(this);
  403. }
  404. }
  405. void ButtonGroup::get_buttons(List<BaseButton *> *r_buttons) {
  406. for (Set<BaseButton *>::Element *E = buttons.front(); E; E = E->next()) {
  407. r_buttons->push_back(E->get());
  408. }
  409. }
  410. Array ButtonGroup::_get_buttons() {
  411. Array btns;
  412. for (Set<BaseButton *>::Element *E = buttons.front(); E; E = E->next()) {
  413. btns.push_back(E->get());
  414. }
  415. return btns;
  416. }
  417. BaseButton *ButtonGroup::get_pressed_button() {
  418. for (Set<BaseButton *>::Element *E = buttons.front(); E; E = E->next()) {
  419. if (E->get()->is_pressed()) {
  420. return E->get();
  421. }
  422. }
  423. return nullptr;
  424. }
  425. void ButtonGroup::_bind_methods() {
  426. ClassDB::bind_method(D_METHOD("get_pressed_button"), &ButtonGroup::get_pressed_button);
  427. ClassDB::bind_method(D_METHOD("get_buttons"), &ButtonGroup::_get_buttons);
  428. ADD_SIGNAL(MethodInfo("pressed", PropertyInfo(Variant::OBJECT, "button", PROPERTY_HINT_RESOURCE_TYPE, "BaseButton")));
  429. }
  430. ButtonGroup::ButtonGroup() {
  431. set_local_to_scene(true);
  432. }