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