base_button.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /*************************************************************************/
  2. /* base_button.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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() && ((1 << (mouse_button->get_button_index() - 1)) & button_mask) != 0;
  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. if (p_what == NOTIFICATION_MOUSE_ENTER) {
  73. status.hovering = true;
  74. update();
  75. }
  76. if (p_what == NOTIFICATION_MOUSE_EXIT) {
  77. status.hovering = false;
  78. update();
  79. }
  80. if (p_what == NOTIFICATION_DRAG_BEGIN || p_what == NOTIFICATION_SCROLL_BEGIN) {
  81. if (status.press_attempt) {
  82. status.press_attempt = false;
  83. update();
  84. }
  85. }
  86. if (p_what == NOTIFICATION_FOCUS_ENTER) {
  87. update();
  88. }
  89. if (p_what == NOTIFICATION_FOCUS_EXIT) {
  90. if (status.press_attempt) {
  91. status.press_attempt = false;
  92. update();
  93. } else if (status.hovering) {
  94. update();
  95. }
  96. }
  97. if (p_what == NOTIFICATION_EXIT_TREE || (p_what == NOTIFICATION_VISIBILITY_CHANGED && !is_visible_in_tree())) {
  98. if (!toggle_mode) {
  99. status.pressed = false;
  100. }
  101. status.hovering = false;
  102. status.press_attempt = false;
  103. status.pressing_inside = false;
  104. }
  105. }
  106. void BaseButton::_pressed() {
  107. GDVIRTUAL_CALL(_pressed);
  108. pressed();
  109. emit_signal(SNAME("pressed"));
  110. }
  111. void BaseButton::_toggled(bool p_pressed) {
  112. GDVIRTUAL_CALL(_toggled, p_pressed);
  113. toggled(p_pressed);
  114. emit_signal(SNAME("toggled"), p_pressed);
  115. }
  116. void BaseButton::on_action_event(Ref<InputEvent> p_event) {
  117. if (p_event->is_pressed()) {
  118. status.press_attempt = true;
  119. status.pressing_inside = true;
  120. emit_signal(SNAME("button_down"));
  121. }
  122. if (status.press_attempt && status.pressing_inside) {
  123. if (toggle_mode) {
  124. bool is_pressed = p_event->is_pressed();
  125. if (Object::cast_to<InputEventShortcut>(*p_event)) {
  126. is_pressed = false;
  127. }
  128. if ((is_pressed && action_mode == ACTION_MODE_BUTTON_PRESS) || (!is_pressed && action_mode == ACTION_MODE_BUTTON_RELEASE)) {
  129. if (action_mode == ACTION_MODE_BUTTON_PRESS) {
  130. status.press_attempt = false;
  131. status.pressing_inside = false;
  132. }
  133. status.pressed = !status.pressed;
  134. _unpress_group();
  135. if (button_group.is_valid()) {
  136. button_group->emit_signal(SNAME("pressed"), this);
  137. }
  138. _toggled(status.pressed);
  139. _pressed();
  140. }
  141. } else {
  142. if ((p_event->is_pressed() && action_mode == ACTION_MODE_BUTTON_PRESS) || (!p_event->is_pressed() && action_mode == ACTION_MODE_BUTTON_RELEASE)) {
  143. _pressed();
  144. }
  145. }
  146. }
  147. if (!p_event->is_pressed()) {
  148. Ref<InputEventMouseButton> mouse_button = p_event;
  149. if (mouse_button.is_valid()) {
  150. if (!has_point(mouse_button->get_position())) {
  151. status.hovering = false;
  152. }
  153. }
  154. status.press_attempt = false;
  155. status.pressing_inside = false;
  156. emit_signal(SNAME("button_up"));
  157. }
  158. update();
  159. }
  160. void BaseButton::pressed() {
  161. }
  162. void BaseButton::toggled(bool p_pressed) {
  163. }
  164. void BaseButton::set_disabled(bool p_disabled) {
  165. if (status.disabled == p_disabled) {
  166. return;
  167. }
  168. status.disabled = p_disabled;
  169. if (p_disabled) {
  170. if (!toggle_mode) {
  171. status.pressed = false;
  172. }
  173. status.press_attempt = false;
  174. status.pressing_inside = false;
  175. }
  176. update();
  177. }
  178. bool BaseButton::is_disabled() const {
  179. return status.disabled;
  180. }
  181. void BaseButton::set_pressed(bool p_pressed) {
  182. if (!toggle_mode) {
  183. return;
  184. }
  185. if (status.pressed == p_pressed) {
  186. return;
  187. }
  188. status.pressed = p_pressed;
  189. if (p_pressed) {
  190. _unpress_group();
  191. if (button_group.is_valid()) {
  192. button_group->emit_signal(SNAME("pressed"), this);
  193. }
  194. }
  195. _toggled(status.pressed);
  196. update();
  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. update();
  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 (!status.press_attempt && status.hovering) {
  222. if (status.pressed) {
  223. return DRAW_HOVER_PRESSED;
  224. }
  225. return DRAW_HOVER;
  226. } else {
  227. /* determine if pressed or not */
  228. bool pressing;
  229. if (status.press_attempt) {
  230. pressing = (status.pressing_inside || keep_pressed_outside);
  231. if (status.pressed) {
  232. pressing = !pressing;
  233. }
  234. } else {
  235. pressing = status.pressed;
  236. }
  237. if (pressing) {
  238. return DRAW_PRESSED;
  239. } else {
  240. return DRAW_NORMAL;
  241. }
  242. }
  243. return DRAW_NORMAL;
  244. }
  245. void BaseButton::set_toggle_mode(bool p_on) {
  246. // Make sure to set 'pressed' to false if we are not in toggle mode
  247. if (!p_on) {
  248. set_pressed(false);
  249. }
  250. toggle_mode = p_on;
  251. }
  252. bool BaseButton::is_toggle_mode() const {
  253. return toggle_mode;
  254. }
  255. void BaseButton::set_shortcut_in_tooltip(bool p_on) {
  256. shortcut_in_tooltip = p_on;
  257. }
  258. bool BaseButton::is_shortcut_in_tooltip_enabled() const {
  259. return shortcut_in_tooltip;
  260. }
  261. void BaseButton::set_action_mode(ActionMode p_mode) {
  262. action_mode = p_mode;
  263. }
  264. BaseButton::ActionMode BaseButton::get_action_mode() const {
  265. return action_mode;
  266. }
  267. void BaseButton::set_button_mask(int p_mask) {
  268. button_mask = p_mask;
  269. }
  270. int BaseButton::get_button_mask() const {
  271. return button_mask;
  272. }
  273. void BaseButton::set_keep_pressed_outside(bool p_on) {
  274. keep_pressed_outside = p_on;
  275. }
  276. bool BaseButton::is_keep_pressed_outside() const {
  277. return keep_pressed_outside;
  278. }
  279. void BaseButton::set_shortcut(const Ref<Shortcut> &p_shortcut) {
  280. shortcut = p_shortcut;
  281. set_process_unhandled_key_input(shortcut.is_valid());
  282. }
  283. Ref<Shortcut> BaseButton::get_shortcut() const {
  284. return shortcut;
  285. }
  286. void BaseButton::unhandled_key_input(const Ref<InputEvent> &p_event) {
  287. ERR_FAIL_COND(p_event.is_null());
  288. if (!_is_focus_owner_in_shorcut_context()) {
  289. return;
  290. }
  291. if (!is_disabled() && is_visible_in_tree() && !p_event->is_echo() && shortcut.is_valid() && shortcut->matches_event(p_event)) {
  292. on_action_event(p_event);
  293. accept_event();
  294. }
  295. }
  296. String BaseButton::get_tooltip(const Point2 &p_pos) const {
  297. String tooltip = Control::get_tooltip(p_pos);
  298. if (shortcut_in_tooltip && shortcut.is_valid() && shortcut->has_valid_event()) {
  299. String text = shortcut->get_name() + " (" + shortcut->get_as_text() + ")";
  300. if (tooltip != String() && shortcut->get_name().nocasecmp_to(tooltip) != 0) {
  301. text += "\n" + tooltip;
  302. }
  303. tooltip = text;
  304. }
  305. return tooltip;
  306. }
  307. void BaseButton::set_button_group(const Ref<ButtonGroup> &p_group) {
  308. if (button_group.is_valid()) {
  309. button_group->buttons.erase(this);
  310. }
  311. button_group = p_group;
  312. if (button_group.is_valid()) {
  313. button_group->buttons.insert(this);
  314. }
  315. update(); //checkbox changes to radio if set a buttongroup
  316. }
  317. Ref<ButtonGroup> BaseButton::get_button_group() const {
  318. return button_group;
  319. }
  320. void BaseButton::set_shortcut_context(Node *p_node) {
  321. ERR_FAIL_NULL_MSG(p_node, "Shortcut context node can't be null.");
  322. shortcut_context = p_node->get_instance_id();
  323. }
  324. Node *BaseButton::get_shortcut_context() const {
  325. Object *ctx_obj = ObjectDB::get_instance(shortcut_context);
  326. Node *ctx_node = Object::cast_to<Node>(ctx_obj);
  327. return ctx_node;
  328. }
  329. bool BaseButton::_is_focus_owner_in_shorcut_context() const {
  330. if (shortcut_context == ObjectID()) {
  331. // No context, therefore global - always "in" context.
  332. return true;
  333. }
  334. Node *ctx_node = get_shortcut_context();
  335. Control *vp_focus = get_focus_owner();
  336. // If the context is valid and the viewport focus is valid, check if the context is the focus or is a parent of it.
  337. return ctx_node && vp_focus && (ctx_node == vp_focus || ctx_node->is_ancestor_of(vp_focus));
  338. }
  339. void BaseButton::_bind_methods() {
  340. ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &BaseButton::set_pressed);
  341. ClassDB::bind_method(D_METHOD("is_pressed"), &BaseButton::is_pressed);
  342. ClassDB::bind_method(D_METHOD("set_pressed_no_signal", "pressed"), &BaseButton::set_pressed_no_signal);
  343. ClassDB::bind_method(D_METHOD("is_hovered"), &BaseButton::is_hovered);
  344. ClassDB::bind_method(D_METHOD("set_toggle_mode", "enabled"), &BaseButton::set_toggle_mode);
  345. ClassDB::bind_method(D_METHOD("is_toggle_mode"), &BaseButton::is_toggle_mode);
  346. ClassDB::bind_method(D_METHOD("set_shortcut_in_tooltip", "enabled"), &BaseButton::set_shortcut_in_tooltip);
  347. ClassDB::bind_method(D_METHOD("is_shortcut_in_tooltip_enabled"), &BaseButton::is_shortcut_in_tooltip_enabled);
  348. ClassDB::bind_method(D_METHOD("set_disabled", "disabled"), &BaseButton::set_disabled);
  349. ClassDB::bind_method(D_METHOD("is_disabled"), &BaseButton::is_disabled);
  350. ClassDB::bind_method(D_METHOD("set_action_mode", "mode"), &BaseButton::set_action_mode);
  351. ClassDB::bind_method(D_METHOD("get_action_mode"), &BaseButton::get_action_mode);
  352. ClassDB::bind_method(D_METHOD("set_button_mask", "mask"), &BaseButton::set_button_mask);
  353. ClassDB::bind_method(D_METHOD("get_button_mask"), &BaseButton::get_button_mask);
  354. ClassDB::bind_method(D_METHOD("get_draw_mode"), &BaseButton::get_draw_mode);
  355. ClassDB::bind_method(D_METHOD("set_keep_pressed_outside", "enabled"), &BaseButton::set_keep_pressed_outside);
  356. ClassDB::bind_method(D_METHOD("is_keep_pressed_outside"), &BaseButton::is_keep_pressed_outside);
  357. ClassDB::bind_method(D_METHOD("set_shortcut", "shortcut"), &BaseButton::set_shortcut);
  358. ClassDB::bind_method(D_METHOD("get_shortcut"), &BaseButton::get_shortcut);
  359. ClassDB::bind_method(D_METHOD("set_button_group", "button_group"), &BaseButton::set_button_group);
  360. ClassDB::bind_method(D_METHOD("get_button_group"), &BaseButton::get_button_group);
  361. ClassDB::bind_method(D_METHOD("set_shortcut_context", "node"), &BaseButton::set_shortcut_context);
  362. ClassDB::bind_method(D_METHOD("get_shortcut_context"), &BaseButton::get_shortcut_context);
  363. GDVIRTUAL_BIND(_pressed);
  364. GDVIRTUAL_BIND(_toggled, "button_pressed");
  365. ADD_SIGNAL(MethodInfo("pressed"));
  366. ADD_SIGNAL(MethodInfo("button_up"));
  367. ADD_SIGNAL(MethodInfo("button_down"));
  368. ADD_SIGNAL(MethodInfo("toggled", PropertyInfo(Variant::BOOL, "button_pressed")));
  369. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
  370. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "toggle_mode"), "set_toggle_mode", "is_toggle_mode");
  371. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_in_tooltip"), "set_shortcut_in_tooltip", "is_shortcut_in_tooltip_enabled");
  372. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed");
  373. ADD_PROPERTY(PropertyInfo(Variant::INT, "action_mode", PROPERTY_HINT_ENUM, "Button Press,Button Release"), "set_action_mode", "get_action_mode");
  374. ADD_PROPERTY(PropertyInfo(Variant::INT, "button_mask", PROPERTY_HINT_FLAGS, "Mouse Left, Mouse Right, Mouse Middle"), "set_button_mask", "get_button_mask");
  375. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keep_pressed_outside"), "set_keep_pressed_outside", "is_keep_pressed_outside");
  376. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shortcut", PROPERTY_HINT_RESOURCE_TYPE, "Shortcut"), "set_shortcut", "get_shortcut");
  377. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "button_group", PROPERTY_HINT_RESOURCE_TYPE, "ButtonGroup"), "set_button_group", "get_button_group");
  378. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shortcut_context", PROPERTY_HINT_RESOURCE_TYPE, "Node"), "set_shortcut_context", "get_shortcut_context");
  379. BIND_ENUM_CONSTANT(DRAW_NORMAL);
  380. BIND_ENUM_CONSTANT(DRAW_PRESSED);
  381. BIND_ENUM_CONSTANT(DRAW_HOVER);
  382. BIND_ENUM_CONSTANT(DRAW_DISABLED);
  383. BIND_ENUM_CONSTANT(DRAW_HOVER_PRESSED);
  384. BIND_ENUM_CONSTANT(ACTION_MODE_BUTTON_PRESS);
  385. BIND_ENUM_CONSTANT(ACTION_MODE_BUTTON_RELEASE);
  386. }
  387. BaseButton::BaseButton() {
  388. set_focus_mode(FOCUS_ALL);
  389. }
  390. BaseButton::~BaseButton() {
  391. if (button_group.is_valid()) {
  392. button_group->buttons.erase(this);
  393. }
  394. }
  395. void ButtonGroup::get_buttons(List<BaseButton *> *r_buttons) {
  396. for (Set<BaseButton *>::Element *E = buttons.front(); E; E = E->next()) {
  397. r_buttons->push_back(E->get());
  398. }
  399. }
  400. Array ButtonGroup::_get_buttons() {
  401. Array btns;
  402. for (Set<BaseButton *>::Element *E = buttons.front(); E; E = E->next()) {
  403. btns.push_back(E->get());
  404. }
  405. return btns;
  406. }
  407. BaseButton *ButtonGroup::get_pressed_button() {
  408. for (Set<BaseButton *>::Element *E = buttons.front(); E; E = E->next()) {
  409. if (E->get()->is_pressed()) {
  410. return E->get();
  411. }
  412. }
  413. return nullptr;
  414. }
  415. void ButtonGroup::_bind_methods() {
  416. ClassDB::bind_method(D_METHOD("get_pressed_button"), &ButtonGroup::get_pressed_button);
  417. ClassDB::bind_method(D_METHOD("get_buttons"), &ButtonGroup::_get_buttons);
  418. ADD_SIGNAL(MethodInfo("pressed", PropertyInfo(Variant::OBJECT, "button")));
  419. }
  420. ButtonGroup::ButtonGroup() {
  421. set_local_to_scene(true);
  422. }