base_button.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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/viewport.h"
  33. #include "scene/scene_string_names.h"
  34. void BaseButton::_unpress_group() {
  35. if (!button_group.is_valid())
  36. return;
  37. if (toggle_mode) {
  38. status.pressed = true;
  39. }
  40. for (Set<BaseButton *>::Element *E = button_group->buttons.front(); E; E = E->next()) {
  41. if (E->get() == this)
  42. continue;
  43. E->get()->set_pressed(false);
  44. }
  45. }
  46. void BaseButton::_gui_input(Ref<InputEvent> p_event) {
  47. if (status.disabled) // no interaction with disabled button
  48. return;
  49. Ref<InputEventMouseButton> mouse_button = p_event;
  50. bool ui_accept = p_event->is_action("ui_accept") && !p_event->is_echo();
  51. bool button_masked = mouse_button.is_valid() && ((1 << (mouse_button->get_button_index() - 1)) & button_mask) > 0;
  52. if (button_masked || ui_accept) {
  53. on_action_event(p_event);
  54. return;
  55. }
  56. Ref<InputEventMouseMotion> mouse_motion = p_event;
  57. if (mouse_motion.is_valid()) {
  58. if (status.press_attempt) {
  59. bool last_press_inside = status.pressing_inside;
  60. status.pressing_inside = has_point(mouse_motion->get_position());
  61. if (last_press_inside != status.pressing_inside) {
  62. update();
  63. }
  64. }
  65. }
  66. }
  67. void BaseButton::_notification(int p_what) {
  68. if (p_what == NOTIFICATION_MOUSE_ENTER) {
  69. status.hovering = true;
  70. update();
  71. }
  72. if (p_what == NOTIFICATION_MOUSE_EXIT) {
  73. status.hovering = false;
  74. update();
  75. }
  76. if (p_what == NOTIFICATION_DRAG_BEGIN || p_what == NOTIFICATION_SCROLL_BEGIN) {
  77. if (status.press_attempt) {
  78. status.press_attempt = false;
  79. update();
  80. }
  81. }
  82. if (p_what == NOTIFICATION_FOCUS_ENTER) {
  83. status.hovering = true;
  84. update();
  85. }
  86. if (p_what == NOTIFICATION_FOCUS_EXIT) {
  87. if (status.press_attempt) {
  88. status.press_attempt = false;
  89. status.hovering = false;
  90. update();
  91. } else if (status.hovering) {
  92. status.hovering = false;
  93. update();
  94. }
  95. }
  96. if (p_what == NOTIFICATION_EXIT_TREE || (p_what == NOTIFICATION_VISIBILITY_CHANGED && !is_visible_in_tree())) {
  97. if (!toggle_mode) {
  98. status.pressed = false;
  99. }
  100. status.hovering = false;
  101. status.press_attempt = false;
  102. status.pressing_inside = false;
  103. }
  104. }
  105. void BaseButton::_pressed() {
  106. if (get_script_instance()) {
  107. get_script_instance()->call(SceneStringNames::get_singleton()->_pressed);
  108. }
  109. pressed();
  110. emit_signal("pressed");
  111. }
  112. void BaseButton::_toggled(bool p_pressed) {
  113. if (get_script_instance()) {
  114. get_script_instance()->call(SceneStringNames::get_singleton()->_toggled, p_pressed);
  115. }
  116. toggled(p_pressed);
  117. emit_signal("toggled", p_pressed);
  118. }
  119. void BaseButton::on_action_event(Ref<InputEvent> p_event) {
  120. if (p_event->is_pressed()) {
  121. status.press_attempt = true;
  122. status.pressing_inside = true;
  123. emit_signal("button_down");
  124. }
  125. if (status.press_attempt && status.pressing_inside) {
  126. if (toggle_mode) {
  127. if ((p_event->is_pressed() && action_mode == ACTION_MODE_BUTTON_PRESS) || (!p_event->is_pressed() && action_mode == ACTION_MODE_BUTTON_RELEASE)) {
  128. if (action_mode == ACTION_MODE_BUTTON_PRESS) {
  129. status.press_attempt = false;
  130. status.pressing_inside = false;
  131. }
  132. status.pressed = !status.pressed;
  133. _unpress_group();
  134. _toggled(status.pressed);
  135. _pressed();
  136. }
  137. } else {
  138. if ((p_event->is_pressed() && action_mode == ACTION_MODE_BUTTON_PRESS) || (!p_event->is_pressed() && action_mode == ACTION_MODE_BUTTON_RELEASE)) {
  139. _pressed();
  140. }
  141. }
  142. }
  143. if (!p_event->is_pressed()) {
  144. Ref<InputEventMouseButton> mouse_button = p_event;
  145. if (mouse_button.is_valid()) {
  146. if (!has_point(mouse_button->get_position())) {
  147. status.hovering = false;
  148. }
  149. }
  150. // pressed state should be correct with button_up signal
  151. emit_signal("button_up");
  152. status.press_attempt = false;
  153. status.pressing_inside = false;
  154. }
  155. update();
  156. }
  157. void BaseButton::pressed() {
  158. }
  159. void BaseButton::toggled(bool p_pressed) {
  160. }
  161. void BaseButton::set_disabled(bool p_disabled) {
  162. if (status.disabled == p_disabled)
  163. return;
  164. status.disabled = p_disabled;
  165. if (p_disabled) {
  166. if (!toggle_mode) {
  167. status.pressed = false;
  168. }
  169. status.press_attempt = false;
  170. status.pressing_inside = false;
  171. }
  172. update();
  173. _change_notify("disabled");
  174. }
  175. bool BaseButton::is_disabled() const {
  176. return status.disabled;
  177. }
  178. void BaseButton::set_pressed(bool p_pressed) {
  179. if (!toggle_mode)
  180. return;
  181. if (status.pressed == p_pressed)
  182. return;
  183. _change_notify("pressed");
  184. status.pressed = p_pressed;
  185. if (p_pressed) {
  186. _unpress_group();
  187. }
  188. _toggled(status.pressed);
  189. update();
  190. }
  191. bool BaseButton::is_pressing() const {
  192. return status.press_attempt;
  193. }
  194. bool BaseButton::is_pressed() const {
  195. return toggle_mode ? status.pressed : status.press_attempt;
  196. }
  197. bool BaseButton::is_hovered() const {
  198. return status.hovering;
  199. }
  200. BaseButton::DrawMode BaseButton::get_draw_mode() const {
  201. if (status.disabled) {
  202. return DRAW_DISABLED;
  203. };
  204. if (!status.press_attempt && status.hovering) {
  205. if (status.pressed)
  206. return DRAW_HOVER_PRESSED;
  207. return DRAW_HOVER;
  208. } else {
  209. /* determine if pressed or not */
  210. bool pressing;
  211. if (status.press_attempt) {
  212. pressing = (status.pressing_inside || keep_pressed_outside);
  213. if (status.pressed)
  214. pressing = !pressing;
  215. } else {
  216. pressing = status.pressed;
  217. }
  218. if (pressing)
  219. return DRAW_PRESSED;
  220. else
  221. return DRAW_NORMAL;
  222. }
  223. return DRAW_NORMAL;
  224. }
  225. void BaseButton::set_toggle_mode(bool p_on) {
  226. toggle_mode = p_on;
  227. }
  228. bool BaseButton::is_toggle_mode() const {
  229. return toggle_mode;
  230. }
  231. void BaseButton::set_shortcut_in_tooltip(bool p_on) {
  232. shortcut_in_tooltip = p_on;
  233. }
  234. bool BaseButton::is_shortcut_in_tooltip_enabled() const {
  235. return shortcut_in_tooltip;
  236. }
  237. void BaseButton::set_action_mode(ActionMode p_mode) {
  238. action_mode = p_mode;
  239. }
  240. BaseButton::ActionMode BaseButton::get_action_mode() const {
  241. return action_mode;
  242. }
  243. void BaseButton::set_button_mask(int p_mask) {
  244. button_mask = p_mask;
  245. }
  246. int BaseButton::get_button_mask() const {
  247. return button_mask;
  248. }
  249. void BaseButton::set_enabled_focus_mode(FocusMode p_mode) {
  250. enabled_focus_mode = p_mode;
  251. if (!status.disabled) {
  252. set_focus_mode(p_mode);
  253. }
  254. }
  255. Control::FocusMode BaseButton::get_enabled_focus_mode() const {
  256. return enabled_focus_mode;
  257. }
  258. void BaseButton::set_keep_pressed_outside(bool p_on) {
  259. keep_pressed_outside = p_on;
  260. }
  261. bool BaseButton::is_keep_pressed_outside() const {
  262. return keep_pressed_outside;
  263. }
  264. void BaseButton::set_shortcut(const Ref<ShortCut> &p_shortcut) {
  265. shortcut = p_shortcut;
  266. set_process_unhandled_input(shortcut.is_valid());
  267. }
  268. Ref<ShortCut> BaseButton::get_shortcut() const {
  269. return shortcut;
  270. }
  271. void BaseButton::_unhandled_input(Ref<InputEvent> p_event) {
  272. if (!is_disabled() && is_visible_in_tree() && !p_event->is_echo() && shortcut.is_valid() && shortcut->is_shortcut(p_event)) {
  273. if (get_viewport()->get_modal_stack_top() && !get_viewport()->get_modal_stack_top()->is_a_parent_of(this))
  274. return; //ignore because of modal window
  275. on_action_event(p_event);
  276. }
  277. }
  278. String BaseButton::get_tooltip(const Point2 &p_pos) const {
  279. String tooltip = Control::get_tooltip(p_pos);
  280. if (shortcut_in_tooltip && shortcut.is_valid() && shortcut->is_valid()) {
  281. String text = shortcut->get_name() + " (" + shortcut->get_as_text() + ")";
  282. if (shortcut->get_name().nocasecmp_to(tooltip) != 0) {
  283. text += "\n" + tooltip;
  284. }
  285. tooltip = text;
  286. }
  287. return tooltip;
  288. }
  289. void BaseButton::set_button_group(const Ref<ButtonGroup> &p_group) {
  290. if (button_group.is_valid()) {
  291. button_group->buttons.erase(this);
  292. }
  293. button_group = p_group;
  294. if (button_group.is_valid()) {
  295. button_group->buttons.insert(this);
  296. }
  297. update(); //checkbox changes to radio if set a buttongroup
  298. }
  299. Ref<ButtonGroup> BaseButton::get_button_group() const {
  300. return button_group;
  301. }
  302. void BaseButton::_bind_methods() {
  303. ClassDB::bind_method(D_METHOD("_gui_input"), &BaseButton::_gui_input);
  304. ClassDB::bind_method(D_METHOD("_unhandled_input"), &BaseButton::_unhandled_input);
  305. ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &BaseButton::set_pressed);
  306. ClassDB::bind_method(D_METHOD("is_pressed"), &BaseButton::is_pressed);
  307. ClassDB::bind_method(D_METHOD("is_hovered"), &BaseButton::is_hovered);
  308. ClassDB::bind_method(D_METHOD("set_toggle_mode", "enabled"), &BaseButton::set_toggle_mode);
  309. ClassDB::bind_method(D_METHOD("is_toggle_mode"), &BaseButton::is_toggle_mode);
  310. ClassDB::bind_method(D_METHOD("set_shortcut_in_tooltip", "enabled"), &BaseButton::set_shortcut_in_tooltip);
  311. ClassDB::bind_method(D_METHOD("is_shortcut_in_tooltip_enabled"), &BaseButton::is_shortcut_in_tooltip_enabled);
  312. ClassDB::bind_method(D_METHOD("set_disabled", "disabled"), &BaseButton::set_disabled);
  313. ClassDB::bind_method(D_METHOD("is_disabled"), &BaseButton::is_disabled);
  314. ClassDB::bind_method(D_METHOD("set_action_mode", "mode"), &BaseButton::set_action_mode);
  315. ClassDB::bind_method(D_METHOD("get_action_mode"), &BaseButton::get_action_mode);
  316. ClassDB::bind_method(D_METHOD("set_button_mask", "mask"), &BaseButton::set_button_mask);
  317. ClassDB::bind_method(D_METHOD("get_button_mask"), &BaseButton::get_button_mask);
  318. ClassDB::bind_method(D_METHOD("get_draw_mode"), &BaseButton::get_draw_mode);
  319. ClassDB::bind_method(D_METHOD("set_enabled_focus_mode", "mode"), &BaseButton::set_enabled_focus_mode);
  320. ClassDB::bind_method(D_METHOD("get_enabled_focus_mode"), &BaseButton::get_enabled_focus_mode);
  321. ClassDB::bind_method(D_METHOD("set_keep_pressed_outside", "enabled"), &BaseButton::set_keep_pressed_outside);
  322. ClassDB::bind_method(D_METHOD("is_keep_pressed_outside"), &BaseButton::is_keep_pressed_outside);
  323. ClassDB::bind_method(D_METHOD("set_shortcut", "shortcut"), &BaseButton::set_shortcut);
  324. ClassDB::bind_method(D_METHOD("get_shortcut"), &BaseButton::get_shortcut);
  325. ClassDB::bind_method(D_METHOD("set_button_group", "button_group"), &BaseButton::set_button_group);
  326. ClassDB::bind_method(D_METHOD("get_button_group"), &BaseButton::get_button_group);
  327. BIND_VMETHOD(MethodInfo("_pressed"));
  328. BIND_VMETHOD(MethodInfo("_toggled", PropertyInfo(Variant::BOOL, "button_pressed")));
  329. ADD_SIGNAL(MethodInfo("pressed"));
  330. ADD_SIGNAL(MethodInfo("button_up"));
  331. ADD_SIGNAL(MethodInfo("button_down"));
  332. ADD_SIGNAL(MethodInfo("toggled", PropertyInfo(Variant::BOOL, "button_pressed")));
  333. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
  334. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "toggle_mode"), "set_toggle_mode", "is_toggle_mode");
  335. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_in_tooltip"), "set_shortcut_in_tooltip", "is_shortcut_in_tooltip_enabled");
  336. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed");
  337. ADD_PROPERTY(PropertyInfo(Variant::INT, "action_mode", PROPERTY_HINT_ENUM, "Button Press,Button Release"), "set_action_mode", "get_action_mode");
  338. ADD_PROPERTY(PropertyInfo(Variant::INT, "button_mask", PROPERTY_HINT_FLAGS, "Mouse Left, Mouse Right, Mouse Middle"), "set_button_mask", "get_button_mask");
  339. ADD_PROPERTY(PropertyInfo(Variant::INT, "enabled_focus_mode", PROPERTY_HINT_ENUM, "None,Click,All"), "set_enabled_focus_mode", "get_enabled_focus_mode");
  340. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keep_pressed_outside"), "set_keep_pressed_outside", "is_keep_pressed_outside");
  341. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shortcut", PROPERTY_HINT_RESOURCE_TYPE, "ShortCut"), "set_shortcut", "get_shortcut");
  342. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "group", PROPERTY_HINT_RESOURCE_TYPE, "ButtonGroup"), "set_button_group", "get_button_group");
  343. BIND_ENUM_CONSTANT(DRAW_NORMAL);
  344. BIND_ENUM_CONSTANT(DRAW_PRESSED);
  345. BIND_ENUM_CONSTANT(DRAW_HOVER);
  346. BIND_ENUM_CONSTANT(DRAW_DISABLED);
  347. BIND_ENUM_CONSTANT(DRAW_HOVER_PRESSED);
  348. BIND_ENUM_CONSTANT(ACTION_MODE_BUTTON_PRESS);
  349. BIND_ENUM_CONSTANT(ACTION_MODE_BUTTON_RELEASE);
  350. }
  351. BaseButton::BaseButton() {
  352. toggle_mode = false;
  353. shortcut_in_tooltip = true;
  354. keep_pressed_outside = false;
  355. status.pressed = false;
  356. status.press_attempt = false;
  357. status.hovering = false;
  358. status.pressing_inside = false;
  359. status.disabled = false;
  360. set_focus_mode(FOCUS_ALL);
  361. enabled_focus_mode = FOCUS_ALL;
  362. action_mode = ACTION_MODE_BUTTON_RELEASE;
  363. button_mask = BUTTON_MASK_LEFT;
  364. }
  365. BaseButton::~BaseButton() {
  366. if (button_group.is_valid()) {
  367. button_group->buttons.erase(this);
  368. }
  369. }
  370. void ButtonGroup::get_buttons(List<BaseButton *> *r_buttons) {
  371. for (Set<BaseButton *>::Element *E = buttons.front(); E; E = E->next()) {
  372. r_buttons->push_back(E->get());
  373. }
  374. }
  375. Array ButtonGroup::_get_buttons() {
  376. Array btns;
  377. for (Set<BaseButton *>::Element *E = buttons.front(); E; E = E->next()) {
  378. btns.push_back(E->get());
  379. }
  380. return btns;
  381. }
  382. BaseButton *ButtonGroup::get_pressed_button() {
  383. for (Set<BaseButton *>::Element *E = buttons.front(); E; E = E->next()) {
  384. if (E->get()->is_pressed())
  385. return E->get();
  386. }
  387. return NULL;
  388. }
  389. void ButtonGroup::_bind_methods() {
  390. ClassDB::bind_method(D_METHOD("get_pressed_button"), &ButtonGroup::get_pressed_button);
  391. ClassDB::bind_method(D_METHOD("get_buttons"), &ButtonGroup::_get_buttons);
  392. }
  393. ButtonGroup::ButtonGroup() {
  394. set_local_to_scene(true);
  395. }