base_button.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. /*************************************************************************/
  2. /* base_button.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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> b = p_event;
  50. if (b.is_valid()) {
  51. if (((1 << (b->get_button_index() - 1)) & button_mask) == 0)
  52. return;
  53. if (status.pressing_button)
  54. return;
  55. if (action_mode == ACTION_MODE_BUTTON_PRESS) {
  56. if (b->is_pressed()) {
  57. emit_signal("button_down");
  58. if (!toggle_mode) { //mouse press attempt
  59. status.press_attempt = true;
  60. status.pressing_inside = true;
  61. pressed();
  62. if (get_script_instance()) {
  63. Variant::CallError ce;
  64. get_script_instance()->call(SceneStringNames::get_singleton()->_pressed, NULL, 0, ce);
  65. }
  66. _unpress_group();
  67. emit_signal("pressed");
  68. } else {
  69. status.pressed = !status.pressed;
  70. pressed();
  71. _unpress_group();
  72. emit_signal("pressed");
  73. toggled(status.pressed);
  74. if (get_script_instance()) {
  75. get_script_instance()->call(SceneStringNames::get_singleton()->_toggled, status.pressed);
  76. }
  77. emit_signal("toggled", status.pressed);
  78. }
  79. } else {
  80. emit_signal("button_up");
  81. /* this is pointless if (status.press_attempt && status.pressing_inside) {
  82. //released();
  83. emit_signal("released");
  84. }
  85. */
  86. status.press_attempt = false;
  87. }
  88. update();
  89. return;
  90. }
  91. if (b->is_pressed()) {
  92. status.press_attempt = true;
  93. status.pressing_inside = true;
  94. emit_signal("button_down");
  95. } else {
  96. emit_signal("button_up");
  97. if (status.press_attempt && status.pressing_inside) {
  98. if (!toggle_mode) { //mouse press attempt
  99. pressed();
  100. if (get_script_instance()) {
  101. Variant::CallError ce;
  102. get_script_instance()->call(SceneStringNames::get_singleton()->_pressed, NULL, 0, ce);
  103. }
  104. _unpress_group();
  105. emit_signal("pressed");
  106. } else {
  107. status.pressed = !status.pressed;
  108. pressed();
  109. _unpress_group();
  110. emit_signal("pressed");
  111. toggled(status.pressed);
  112. if (get_script_instance()) {
  113. get_script_instance()->call(SceneStringNames::get_singleton()->_toggled, status.pressed);
  114. }
  115. emit_signal("toggled", status.pressed);
  116. }
  117. }
  118. status.press_attempt = false;
  119. }
  120. update();
  121. }
  122. Ref<InputEventMouseMotion> mm = p_event;
  123. if (mm.is_valid()) {
  124. if (status.press_attempt && status.pressing_button == 0) {
  125. bool last_press_inside = status.pressing_inside;
  126. status.pressing_inside = has_point(mm->get_position());
  127. if (last_press_inside != status.pressing_inside)
  128. update();
  129. }
  130. }
  131. if (!mm.is_valid() && !b.is_valid()) {
  132. if (p_event->is_echo()) {
  133. return;
  134. }
  135. if (status.disabled) {
  136. return;
  137. }
  138. if (status.press_attempt && status.pressing_button == 0) {
  139. return;
  140. }
  141. if (p_event->is_action("ui_accept")) {
  142. if (p_event->is_pressed()) {
  143. status.pressing_button++;
  144. status.press_attempt = true;
  145. status.pressing_inside = true;
  146. emit_signal("button_down");
  147. } else if (status.press_attempt) {
  148. if (status.pressing_button)
  149. status.pressing_button--;
  150. if (status.pressing_button)
  151. return;
  152. status.press_attempt = false;
  153. status.pressing_inside = false;
  154. emit_signal("button_up");
  155. if (!toggle_mode) { //mouse press attempt
  156. pressed();
  157. if (get_script_instance()) {
  158. Variant::CallError ce;
  159. get_script_instance()->call(SceneStringNames::get_singleton()->_pressed, NULL, 0, ce);
  160. }
  161. _unpress_group();
  162. emit_signal("pressed");
  163. } else {
  164. status.pressed = !status.pressed;
  165. pressed();
  166. _unpress_group();
  167. emit_signal("pressed");
  168. toggled(status.pressed);
  169. if (get_script_instance()) {
  170. get_script_instance()->call(SceneStringNames::get_singleton()->_toggled, status.pressed);
  171. }
  172. emit_signal("toggled", status.pressed);
  173. }
  174. }
  175. accept_event();
  176. update();
  177. }
  178. }
  179. }
  180. void BaseButton::_notification(int p_what) {
  181. if (p_what == NOTIFICATION_MOUSE_ENTER) {
  182. status.hovering = true;
  183. update();
  184. }
  185. if (p_what == NOTIFICATION_MOUSE_EXIT) {
  186. status.hovering = false;
  187. update();
  188. }
  189. if (p_what == NOTIFICATION_DRAG_BEGIN || p_what == NOTIFICATION_SCROLL_BEGIN) {
  190. if (status.press_attempt) {
  191. status.press_attempt = false;
  192. status.pressing_button = 0;
  193. update();
  194. }
  195. }
  196. if (p_what == NOTIFICATION_FOCUS_ENTER) {
  197. status.hovering = true;
  198. update();
  199. }
  200. if (p_what == NOTIFICATION_FOCUS_EXIT) {
  201. if (status.pressing_button && status.press_attempt) {
  202. status.press_attempt = false;
  203. status.pressing_button = 0;
  204. status.hovering = false;
  205. update();
  206. } else if (status.hovering) {
  207. status.hovering = false;
  208. update();
  209. }
  210. }
  211. if (p_what == NOTIFICATION_ENTER_TREE) {
  212. }
  213. if (p_what == NOTIFICATION_EXIT_TREE) {
  214. }
  215. if (p_what == NOTIFICATION_VISIBILITY_CHANGED && !is_visible_in_tree()) {
  216. if (!toggle_mode) {
  217. status.pressed = false;
  218. }
  219. status.hovering = false;
  220. status.press_attempt = false;
  221. status.pressing_inside = false;
  222. status.pressing_button = 0;
  223. }
  224. }
  225. void BaseButton::pressed() {
  226. if (get_script_instance())
  227. get_script_instance()->call("pressed");
  228. }
  229. void BaseButton::toggled(bool p_pressed) {
  230. if (get_script_instance()) {
  231. get_script_instance()->call("toggled", p_pressed);
  232. }
  233. }
  234. void BaseButton::set_disabled(bool p_disabled) {
  235. if (status.disabled == p_disabled)
  236. return;
  237. status.disabled = p_disabled;
  238. if (p_disabled) {
  239. if (!toggle_mode) {
  240. status.pressed = false;
  241. }
  242. status.press_attempt = false;
  243. status.pressing_inside = false;
  244. status.pressing_button = 0;
  245. }
  246. update();
  247. _change_notify("disabled");
  248. }
  249. bool BaseButton::is_disabled() const {
  250. return status.disabled;
  251. }
  252. void BaseButton::set_pressed(bool p_pressed) {
  253. if (!toggle_mode)
  254. return;
  255. if (status.pressed == p_pressed)
  256. return;
  257. _change_notify("pressed");
  258. status.pressed = p_pressed;
  259. if (p_pressed) {
  260. _unpress_group();
  261. }
  262. update();
  263. }
  264. bool BaseButton::is_pressing() const {
  265. return status.press_attempt;
  266. }
  267. bool BaseButton::is_pressed() const {
  268. return toggle_mode ? status.pressed : status.press_attempt;
  269. }
  270. bool BaseButton::is_hovered() const {
  271. return status.hovering;
  272. }
  273. BaseButton::DrawMode BaseButton::get_draw_mode() const {
  274. if (status.disabled) {
  275. return DRAW_DISABLED;
  276. };
  277. if (!status.press_attempt && status.hovering) {
  278. if (status.pressed)
  279. return DRAW_HOVER_PRESSED;
  280. return DRAW_HOVER;
  281. } else {
  282. /* determine if pressed or not */
  283. bool pressing;
  284. if (status.press_attempt) {
  285. pressing = status.pressing_inside;
  286. if (status.pressed)
  287. pressing = !pressing;
  288. } else {
  289. pressing = status.pressed;
  290. }
  291. if (pressing)
  292. return DRAW_PRESSED;
  293. else
  294. return DRAW_NORMAL;
  295. }
  296. return DRAW_NORMAL;
  297. }
  298. void BaseButton::set_toggle_mode(bool p_on) {
  299. toggle_mode = p_on;
  300. }
  301. bool BaseButton::is_toggle_mode() const {
  302. return toggle_mode;
  303. }
  304. void BaseButton::set_shortcut_in_tooltip(bool p_on) {
  305. shortcut_in_tooltip = p_on;
  306. }
  307. bool BaseButton::is_shortcut_in_tooltip_enabled() const {
  308. return shortcut_in_tooltip;
  309. }
  310. void BaseButton::set_action_mode(ActionMode p_mode) {
  311. action_mode = p_mode;
  312. }
  313. BaseButton::ActionMode BaseButton::get_action_mode() const {
  314. return action_mode;
  315. }
  316. void BaseButton::set_button_mask(int p_mask) {
  317. button_mask = p_mask;
  318. }
  319. int BaseButton::get_button_mask() const {
  320. return button_mask;
  321. }
  322. void BaseButton::set_enabled_focus_mode(FocusMode p_mode) {
  323. enabled_focus_mode = p_mode;
  324. if (!status.disabled) {
  325. set_focus_mode(p_mode);
  326. }
  327. }
  328. Control::FocusMode BaseButton::get_enabled_focus_mode() const {
  329. return enabled_focus_mode;
  330. }
  331. void BaseButton::set_shortcut(const Ref<ShortCut> &p_shortcut) {
  332. if (shortcut.is_null() == p_shortcut.is_null())
  333. return;
  334. shortcut = p_shortcut;
  335. set_process_unhandled_input(shortcut.is_valid());
  336. }
  337. Ref<ShortCut> BaseButton::get_shortcut() const {
  338. return shortcut;
  339. }
  340. void BaseButton::_unhandled_input(Ref<InputEvent> p_event) {
  341. if (!is_disabled() && is_visible_in_tree() && p_event->is_pressed() && !p_event->is_echo() && shortcut.is_valid() && shortcut->is_shortcut(p_event)) {
  342. if (get_viewport()->get_modal_stack_top() && !get_viewport()->get_modal_stack_top()->is_a_parent_of(this))
  343. return; //ignore because of modal window
  344. if (is_toggle_mode()) {
  345. set_pressed(!is_pressed());
  346. emit_signal("toggled", is_pressed());
  347. }
  348. emit_signal("pressed");
  349. }
  350. }
  351. String BaseButton::get_tooltip(const Point2 &p_pos) const {
  352. String tooltip = Control::get_tooltip(p_pos);
  353. if (shortcut_in_tooltip && shortcut.is_valid() && shortcut->is_valid()) {
  354. String text = shortcut->get_name() + " (" + shortcut->get_as_text() + ")";
  355. if (shortcut->get_name().nocasecmp_to(tooltip) != 0) {
  356. text += "\n" + tooltip;
  357. }
  358. tooltip = text;
  359. }
  360. return tooltip;
  361. }
  362. void BaseButton::set_button_group(const Ref<ButtonGroup> &p_group) {
  363. if (button_group.is_valid()) {
  364. button_group->buttons.erase(this);
  365. }
  366. button_group = p_group;
  367. if (button_group.is_valid()) {
  368. button_group->buttons.insert(this);
  369. }
  370. update(); //checkbox changes to radio if set a buttongroup
  371. }
  372. Ref<ButtonGroup> BaseButton::get_button_group() const {
  373. return button_group;
  374. }
  375. void BaseButton::_bind_methods() {
  376. ClassDB::bind_method(D_METHOD("_gui_input"), &BaseButton::_gui_input);
  377. ClassDB::bind_method(D_METHOD("_unhandled_input"), &BaseButton::_unhandled_input);
  378. ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &BaseButton::set_pressed);
  379. ClassDB::bind_method(D_METHOD("is_pressed"), &BaseButton::is_pressed);
  380. ClassDB::bind_method(D_METHOD("is_hovered"), &BaseButton::is_hovered);
  381. ClassDB::bind_method(D_METHOD("set_toggle_mode", "enabled"), &BaseButton::set_toggle_mode);
  382. ClassDB::bind_method(D_METHOD("is_toggle_mode"), &BaseButton::is_toggle_mode);
  383. ClassDB::bind_method(D_METHOD("set_shortcut_in_tooltip", "enabled"), &BaseButton::set_shortcut_in_tooltip);
  384. ClassDB::bind_method(D_METHOD("is_shortcut_in_tooltip_enabled"), &BaseButton::is_shortcut_in_tooltip_enabled);
  385. ClassDB::bind_method(D_METHOD("set_disabled", "disabled"), &BaseButton::set_disabled);
  386. ClassDB::bind_method(D_METHOD("is_disabled"), &BaseButton::is_disabled);
  387. ClassDB::bind_method(D_METHOD("set_action_mode", "mode"), &BaseButton::set_action_mode);
  388. ClassDB::bind_method(D_METHOD("get_action_mode"), &BaseButton::get_action_mode);
  389. ClassDB::bind_method(D_METHOD("set_button_mask", "mask"), &BaseButton::set_button_mask);
  390. ClassDB::bind_method(D_METHOD("get_button_mask"), &BaseButton::get_button_mask);
  391. ClassDB::bind_method(D_METHOD("get_draw_mode"), &BaseButton::get_draw_mode);
  392. ClassDB::bind_method(D_METHOD("set_enabled_focus_mode", "mode"), &BaseButton::set_enabled_focus_mode);
  393. ClassDB::bind_method(D_METHOD("get_enabled_focus_mode"), &BaseButton::get_enabled_focus_mode);
  394. ClassDB::bind_method(D_METHOD("set_shortcut", "shortcut"), &BaseButton::set_shortcut);
  395. ClassDB::bind_method(D_METHOD("get_shortcut"), &BaseButton::get_shortcut);
  396. ClassDB::bind_method(D_METHOD("set_button_group", "button_group"), &BaseButton::set_button_group);
  397. ClassDB::bind_method(D_METHOD("get_button_group"), &BaseButton::get_button_group);
  398. BIND_VMETHOD(MethodInfo("_pressed"));
  399. BIND_VMETHOD(MethodInfo("_toggled", PropertyInfo(Variant::BOOL, "button_pressed")));
  400. ADD_SIGNAL(MethodInfo("pressed"));
  401. ADD_SIGNAL(MethodInfo("button_up"));
  402. ADD_SIGNAL(MethodInfo("button_down"));
  403. ADD_SIGNAL(MethodInfo("toggled", PropertyInfo(Variant::BOOL, "button_pressed")));
  404. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
  405. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "toggle_mode"), "set_toggle_mode", "is_toggle_mode");
  406. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_in_tooltip"), "set_shortcut_in_tooltip", "is_shortcut_in_tooltip_enabled");
  407. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed");
  408. ADD_PROPERTY(PropertyInfo(Variant::INT, "action_mode", PROPERTY_HINT_ENUM, "Button Press,Button Release"), "set_action_mode", "get_action_mode");
  409. ADD_PROPERTY(PropertyInfo(Variant::INT, "button_mask", PROPERTY_HINT_FLAGS, "Mouse Left, Mouse Right, Mouse Middle"), "set_button_mask", "get_button_mask");
  410. ADD_PROPERTY(PropertyInfo(Variant::INT, "enabled_focus_mode", PROPERTY_HINT_ENUM, "None,Click,All"), "set_enabled_focus_mode", "get_enabled_focus_mode");
  411. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shortcut", PROPERTY_HINT_RESOURCE_TYPE, "ShortCut"), "set_shortcut", "get_shortcut");
  412. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "group", PROPERTY_HINT_RESOURCE_TYPE, "ButtonGroup"), "set_button_group", "get_button_group");
  413. BIND_ENUM_CONSTANT(DRAW_NORMAL);
  414. BIND_ENUM_CONSTANT(DRAW_PRESSED);
  415. BIND_ENUM_CONSTANT(DRAW_HOVER);
  416. BIND_ENUM_CONSTANT(DRAW_DISABLED);
  417. BIND_ENUM_CONSTANT(DRAW_HOVER_PRESSED);
  418. BIND_ENUM_CONSTANT(ACTION_MODE_BUTTON_PRESS);
  419. BIND_ENUM_CONSTANT(ACTION_MODE_BUTTON_RELEASE);
  420. }
  421. BaseButton::BaseButton() {
  422. toggle_mode = false;
  423. shortcut_in_tooltip = true;
  424. status.pressed = false;
  425. status.press_attempt = false;
  426. status.hovering = false;
  427. status.pressing_inside = false;
  428. status.disabled = false;
  429. status.pressing_button = 0;
  430. set_focus_mode(FOCUS_ALL);
  431. enabled_focus_mode = FOCUS_ALL;
  432. action_mode = ACTION_MODE_BUTTON_RELEASE;
  433. button_mask = BUTTON_MASK_LEFT;
  434. }
  435. BaseButton::~BaseButton() {
  436. if (button_group.is_valid()) {
  437. button_group->buttons.erase(this);
  438. }
  439. }
  440. void ButtonGroup::get_buttons(List<BaseButton *> *r_buttons) {
  441. for (Set<BaseButton *>::Element *E = buttons.front(); E; E = E->next()) {
  442. r_buttons->push_back(E->get());
  443. }
  444. }
  445. Array ButtonGroup::_get_buttons() {
  446. Array btns;
  447. for (Set<BaseButton *>::Element *E = buttons.front(); E; E = E->next()) {
  448. btns.push_back(E->get());
  449. }
  450. return btns;
  451. }
  452. BaseButton *ButtonGroup::get_pressed_button() {
  453. for (Set<BaseButton *>::Element *E = buttons.front(); E; E = E->next()) {
  454. if (E->get()->is_pressed())
  455. return E->get();
  456. }
  457. return NULL;
  458. }
  459. void ButtonGroup::_bind_methods() {
  460. ClassDB::bind_method(D_METHOD("get_pressed_button"), &ButtonGroup::get_pressed_button);
  461. ClassDB::bind_method(D_METHOD("get_buttons"), &ButtonGroup::_get_buttons);
  462. }
  463. ButtonGroup::ButtonGroup() {
  464. set_local_to_scene(true);
  465. }