base_button.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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/viewport.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(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. if (get_script_instance()) {
  108. get_script_instance()->call(SceneStringNames::get_singleton()->_pressed);
  109. }
  110. pressed();
  111. emit_signal("pressed");
  112. }
  113. void BaseButton::_toggled(bool p_pressed) {
  114. if (get_script_instance()) {
  115. get_script_instance()->call(SceneStringNames::get_singleton()->_toggled, p_pressed);
  116. }
  117. toggled(p_pressed);
  118. emit_signal("toggled", p_pressed);
  119. }
  120. void BaseButton::on_action_event(Ref<InputEvent> p_event) {
  121. if (p_event->is_pressed()) {
  122. status.press_attempt = true;
  123. status.pressing_inside = true;
  124. emit_signal("button_down");
  125. }
  126. if (status.press_attempt && status.pressing_inside) {
  127. if (toggle_mode) {
  128. if ((p_event->is_pressed() && action_mode == ACTION_MODE_BUTTON_PRESS) || (!p_event->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("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("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. _change_notify("disabled");
  178. }
  179. bool BaseButton::is_disabled() const {
  180. return status.disabled;
  181. }
  182. void BaseButton::set_pressed(bool p_pressed) {
  183. if (!toggle_mode) {
  184. return;
  185. }
  186. if (status.pressed == p_pressed) {
  187. return;
  188. }
  189. _change_notify("pressed");
  190. status.pressed = p_pressed;
  191. if (p_pressed) {
  192. _unpress_group();
  193. if (button_group.is_valid()) {
  194. button_group->emit_signal("pressed", this);
  195. }
  196. }
  197. _toggled(status.pressed);
  198. update();
  199. }
  200. void BaseButton::set_pressed_no_signal(bool p_pressed) {
  201. if (!toggle_mode) {
  202. return;
  203. }
  204. if (status.pressed == p_pressed) {
  205. return;
  206. }
  207. status.pressed = p_pressed;
  208. update();
  209. }
  210. bool BaseButton::is_pressing() const {
  211. return status.press_attempt;
  212. }
  213. bool BaseButton::is_pressed() const {
  214. return toggle_mode ? status.pressed : status.press_attempt;
  215. }
  216. bool BaseButton::is_hovered() const {
  217. return status.hovering;
  218. }
  219. BaseButton::DrawMode BaseButton::get_draw_mode() const {
  220. if (status.disabled) {
  221. return DRAW_DISABLED;
  222. };
  223. if (!status.press_attempt && status.hovering) {
  224. if (status.pressed) {
  225. return DRAW_HOVER_PRESSED;
  226. }
  227. return DRAW_HOVER;
  228. } else {
  229. /* determine if pressed or not */
  230. bool pressing;
  231. if (status.press_attempt) {
  232. pressing = (status.pressing_inside || keep_pressed_outside);
  233. if (status.pressed) {
  234. pressing = !pressing;
  235. }
  236. } else {
  237. pressing = status.pressed;
  238. }
  239. if (pressing) {
  240. return DRAW_PRESSED;
  241. } else {
  242. return DRAW_NORMAL;
  243. }
  244. }
  245. return DRAW_NORMAL;
  246. }
  247. void BaseButton::set_toggle_mode(bool p_on) {
  248. toggle_mode = p_on;
  249. }
  250. bool BaseButton::is_toggle_mode() const {
  251. return toggle_mode;
  252. }
  253. void BaseButton::set_shortcut_in_tooltip(bool p_on) {
  254. shortcut_in_tooltip = p_on;
  255. }
  256. bool BaseButton::is_shortcut_in_tooltip_enabled() const {
  257. return shortcut_in_tooltip;
  258. }
  259. void BaseButton::set_action_mode(ActionMode p_mode) {
  260. action_mode = p_mode;
  261. }
  262. BaseButton::ActionMode BaseButton::get_action_mode() const {
  263. return action_mode;
  264. }
  265. void BaseButton::set_button_mask(int p_mask) {
  266. button_mask = p_mask;
  267. }
  268. int BaseButton::get_button_mask() const {
  269. return button_mask;
  270. }
  271. void BaseButton::set_enabled_focus_mode(FocusMode p_mode) {
  272. enabled_focus_mode = p_mode;
  273. if (!status.disabled) {
  274. set_focus_mode(p_mode);
  275. }
  276. }
  277. Control::FocusMode BaseButton::get_enabled_focus_mode() const {
  278. return enabled_focus_mode;
  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_input(shortcut.is_valid());
  289. }
  290. Ref<ShortCut> BaseButton::get_shortcut() const {
  291. return shortcut;
  292. }
  293. void BaseButton::_unhandled_input(Ref<InputEvent> p_event) {
  294. ERR_FAIL_COND(p_event.is_null());
  295. if (!is_disabled() && is_visible_in_tree() && !p_event->is_echo() && shortcut.is_valid() && shortcut->is_shortcut(p_event)) {
  296. if (get_viewport()->get_modal_stack_top() && !get_viewport()->get_modal_stack_top()->is_a_parent_of(this)) {
  297. return; //ignore because of modal window
  298. }
  299. on_action_event(p_event);
  300. }
  301. }
  302. String BaseButton::get_tooltip(const Point2 &p_pos) const {
  303. String tooltip = Control::get_tooltip(p_pos);
  304. if (shortcut_in_tooltip && shortcut.is_valid() && shortcut->is_valid()) {
  305. String text = shortcut->get_name() + " (" + shortcut->get_as_text() + ")";
  306. if (shortcut->get_name().nocasecmp_to(tooltip) != 0) {
  307. text += "\n" + tr(tooltip);
  308. }
  309. tooltip = text;
  310. }
  311. return tooltip;
  312. }
  313. void BaseButton::set_button_group(const Ref<ButtonGroup> &p_group) {
  314. if (button_group.is_valid()) {
  315. button_group->buttons.erase(this);
  316. }
  317. button_group = p_group;
  318. if (button_group.is_valid()) {
  319. button_group->buttons.insert(this);
  320. }
  321. update(); //checkbox changes to radio if set a buttongroup
  322. }
  323. Ref<ButtonGroup> BaseButton::get_button_group() const {
  324. return button_group;
  325. }
  326. void BaseButton::_bind_methods() {
  327. ClassDB::bind_method(D_METHOD("_gui_input"), &BaseButton::_gui_input);
  328. ClassDB::bind_method(D_METHOD("_unhandled_input"), &BaseButton::_unhandled_input);
  329. ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &BaseButton::set_pressed);
  330. ClassDB::bind_method(D_METHOD("is_pressed"), &BaseButton::is_pressed);
  331. ClassDB::bind_method(D_METHOD("set_pressed_no_signal", "pressed"), &BaseButton::set_pressed_no_signal);
  332. ClassDB::bind_method(D_METHOD("is_hovered"), &BaseButton::is_hovered);
  333. ClassDB::bind_method(D_METHOD("set_toggle_mode", "enabled"), &BaseButton::set_toggle_mode);
  334. ClassDB::bind_method(D_METHOD("is_toggle_mode"), &BaseButton::is_toggle_mode);
  335. ClassDB::bind_method(D_METHOD("set_shortcut_in_tooltip", "enabled"), &BaseButton::set_shortcut_in_tooltip);
  336. ClassDB::bind_method(D_METHOD("is_shortcut_in_tooltip_enabled"), &BaseButton::is_shortcut_in_tooltip_enabled);
  337. ClassDB::bind_method(D_METHOD("set_disabled", "disabled"), &BaseButton::set_disabled);
  338. ClassDB::bind_method(D_METHOD("is_disabled"), &BaseButton::is_disabled);
  339. ClassDB::bind_method(D_METHOD("set_action_mode", "mode"), &BaseButton::set_action_mode);
  340. ClassDB::bind_method(D_METHOD("get_action_mode"), &BaseButton::get_action_mode);
  341. ClassDB::bind_method(D_METHOD("set_button_mask", "mask"), &BaseButton::set_button_mask);
  342. ClassDB::bind_method(D_METHOD("get_button_mask"), &BaseButton::get_button_mask);
  343. ClassDB::bind_method(D_METHOD("get_draw_mode"), &BaseButton::get_draw_mode);
  344. ClassDB::bind_method(D_METHOD("set_enabled_focus_mode", "mode"), &BaseButton::set_enabled_focus_mode);
  345. ClassDB::bind_method(D_METHOD("get_enabled_focus_mode"), &BaseButton::get_enabled_focus_mode);
  346. ClassDB::bind_method(D_METHOD("set_keep_pressed_outside", "enabled"), &BaseButton::set_keep_pressed_outside);
  347. ClassDB::bind_method(D_METHOD("is_keep_pressed_outside"), &BaseButton::is_keep_pressed_outside);
  348. ClassDB::bind_method(D_METHOD("set_shortcut", "shortcut"), &BaseButton::set_shortcut);
  349. ClassDB::bind_method(D_METHOD("get_shortcut"), &BaseButton::get_shortcut);
  350. ClassDB::bind_method(D_METHOD("set_button_group", "button_group"), &BaseButton::set_button_group);
  351. ClassDB::bind_method(D_METHOD("get_button_group"), &BaseButton::get_button_group);
  352. BIND_VMETHOD(MethodInfo("_pressed"));
  353. BIND_VMETHOD(MethodInfo("_toggled", PropertyInfo(Variant::BOOL, "button_pressed")));
  354. ADD_SIGNAL(MethodInfo("pressed"));
  355. ADD_SIGNAL(MethodInfo("button_up"));
  356. ADD_SIGNAL(MethodInfo("button_down"));
  357. ADD_SIGNAL(MethodInfo("toggled", PropertyInfo(Variant::BOOL, "button_pressed")));
  358. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
  359. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "toggle_mode"), "set_toggle_mode", "is_toggle_mode");
  360. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_in_tooltip"), "set_shortcut_in_tooltip", "is_shortcut_in_tooltip_enabled");
  361. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed");
  362. ADD_PROPERTY(PropertyInfo(Variant::INT, "action_mode", PROPERTY_HINT_ENUM, "Button Press,Button Release"), "set_action_mode", "get_action_mode");
  363. ADD_PROPERTY(PropertyInfo(Variant::INT, "button_mask", PROPERTY_HINT_FLAGS, "Mouse Left, Mouse Right, Mouse Middle"), "set_button_mask", "get_button_mask");
  364. ADD_PROPERTY(PropertyInfo(Variant::INT, "enabled_focus_mode", PROPERTY_HINT_ENUM, "None,Click,All"), "set_enabled_focus_mode", "get_enabled_focus_mode");
  365. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keep_pressed_outside"), "set_keep_pressed_outside", "is_keep_pressed_outside");
  366. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shortcut", PROPERTY_HINT_RESOURCE_TYPE, "ShortCut"), "set_shortcut", "get_shortcut");
  367. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "group", PROPERTY_HINT_RESOURCE_TYPE, "ButtonGroup"), "set_button_group", "get_button_group");
  368. BIND_ENUM_CONSTANT(DRAW_NORMAL);
  369. BIND_ENUM_CONSTANT(DRAW_PRESSED);
  370. BIND_ENUM_CONSTANT(DRAW_HOVER);
  371. BIND_ENUM_CONSTANT(DRAW_DISABLED);
  372. BIND_ENUM_CONSTANT(DRAW_HOVER_PRESSED);
  373. BIND_ENUM_CONSTANT(ACTION_MODE_BUTTON_PRESS);
  374. BIND_ENUM_CONSTANT(ACTION_MODE_BUTTON_RELEASE);
  375. }
  376. BaseButton::BaseButton() {
  377. toggle_mode = false;
  378. shortcut_in_tooltip = true;
  379. keep_pressed_outside = false;
  380. status.pressed = false;
  381. status.press_attempt = false;
  382. status.hovering = false;
  383. status.pressing_inside = false;
  384. status.disabled = false;
  385. set_focus_mode(FOCUS_ALL);
  386. enabled_focus_mode = FOCUS_ALL;
  387. action_mode = ACTION_MODE_BUTTON_RELEASE;
  388. button_mask = BUTTON_MASK_LEFT;
  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. }