base_button.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /*************************************************************************/
  2. /* base_button.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "os/keyboard.h"
  32. #include "print_string.h"
  33. #include "scene/main/viewport.h"
  34. #include "scene/scene_string_names.h"
  35. void BaseButton::_unpress_group() {
  36. if (!button_group.is_valid())
  37. return;
  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. E->get()->set_pressed(false);
  45. }
  46. }
  47. void BaseButton::_gui_input(Ref<InputEvent> p_event) {
  48. if (status.disabled) // no interaction with disabled button
  49. return;
  50. Ref<InputEventMouseButton> b = p_event;
  51. if (b.is_valid()) {
  52. if (status.disabled || b->get_button_index() != 1)
  53. return;
  54. if (status.pressing_button)
  55. return;
  56. if (action_mode == ACTION_MODE_BUTTON_PRESS) {
  57. if (b->is_pressed()) {
  58. emit_signal("button_down");
  59. if (!toggle_mode) { //mouse press attempt
  60. status.press_attempt = true;
  61. status.pressing_inside = true;
  62. pressed();
  63. if (get_script_instance()) {
  64. Variant::CallError ce;
  65. get_script_instance()->call(SceneStringNames::get_singleton()->_pressed, NULL, 0, ce);
  66. }
  67. emit_signal("pressed");
  68. _unpress_group();
  69. } else {
  70. status.pressed = !status.pressed;
  71. pressed();
  72. emit_signal("pressed");
  73. _unpress_group();
  74. toggled(status.pressed);
  75. if (get_script_instance()) {
  76. get_script_instance()->call(SceneStringNames::get_singleton()->_toggled, status.pressed);
  77. }
  78. emit_signal("toggled", status.pressed);
  79. }
  80. } else {
  81. emit_signal("button_up");
  82. /* this is pointless if (status.press_attempt && status.pressing_inside) {
  83. //released();
  84. emit_signal("released");
  85. }
  86. */
  87. status.press_attempt = false;
  88. }
  89. update();
  90. return;
  91. }
  92. if (b->is_pressed()) {
  93. status.press_attempt = true;
  94. status.pressing_inside = true;
  95. emit_signal("button_down");
  96. } else {
  97. emit_signal("button_up");
  98. if (status.press_attempt && status.pressing_inside) {
  99. if (!toggle_mode) { //mouse press attempt
  100. pressed();
  101. if (get_script_instance()) {
  102. Variant::CallError ce;
  103. get_script_instance()->call(SceneStringNames::get_singleton()->_pressed, NULL, 0, ce);
  104. }
  105. emit_signal("pressed");
  106. } else {
  107. status.pressed = !status.pressed;
  108. pressed();
  109. emit_signal("pressed");
  110. toggled(status.pressed);
  111. if (get_script_instance()) {
  112. get_script_instance()->call(SceneStringNames::get_singleton()->_toggled, status.pressed);
  113. }
  114. emit_signal("toggled", status.pressed);
  115. }
  116. _unpress_group();
  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. emit_signal("pressed");
  158. } else {
  159. status.pressed = !status.pressed;
  160. pressed();
  161. emit_signal("pressed");
  162. toggled(status.pressed);
  163. if (get_script_instance()) {
  164. get_script_instance()->call(SceneStringNames::get_singleton()->_toggled, status.pressed);
  165. }
  166. emit_signal("toggled", status.pressed);
  167. }
  168. _unpress_group();
  169. }
  170. accept_event();
  171. update();
  172. }
  173. }
  174. }
  175. void BaseButton::_notification(int p_what) {
  176. if (p_what == NOTIFICATION_MOUSE_ENTER) {
  177. status.hovering = true;
  178. update();
  179. }
  180. if (p_what == NOTIFICATION_MOUSE_EXIT) {
  181. status.hovering = false;
  182. update();
  183. }
  184. if (p_what == NOTIFICATION_DRAG_BEGIN) {
  185. if (status.press_attempt) {
  186. status.press_attempt = false;
  187. status.pressing_button = 0;
  188. update();
  189. }
  190. }
  191. if (p_what == NOTIFICATION_FOCUS_ENTER) {
  192. status.hovering = true;
  193. update();
  194. }
  195. if (p_what == NOTIFICATION_FOCUS_EXIT) {
  196. if (status.pressing_button && status.press_attempt) {
  197. status.press_attempt = false;
  198. status.pressing_button = 0;
  199. status.hovering = false;
  200. update();
  201. } else if (status.hovering) {
  202. status.hovering = false;
  203. update();
  204. }
  205. }
  206. if (p_what == NOTIFICATION_ENTER_TREE) {
  207. }
  208. if (p_what == NOTIFICATION_EXIT_TREE) {
  209. }
  210. if (p_what == NOTIFICATION_VISIBILITY_CHANGED && !is_visible_in_tree()) {
  211. if (!toggle_mode) {
  212. status.pressed = false;
  213. }
  214. status.hovering = false;
  215. status.press_attempt = false;
  216. status.pressing_inside = false;
  217. status.pressing_button = 0;
  218. }
  219. }
  220. void BaseButton::pressed() {
  221. if (get_script_instance())
  222. get_script_instance()->call("pressed");
  223. }
  224. void BaseButton::toggled(bool p_pressed) {
  225. if (get_script_instance()) {
  226. get_script_instance()->call("toggled", p_pressed);
  227. }
  228. }
  229. void BaseButton::set_disabled(bool p_disabled) {
  230. status.disabled = p_disabled;
  231. update();
  232. _change_notify("disabled");
  233. if (p_disabled)
  234. set_focus_mode(FOCUS_NONE);
  235. else
  236. set_focus_mode(enabled_focus_mode);
  237. }
  238. bool BaseButton::is_disabled() const {
  239. return status.disabled;
  240. }
  241. void BaseButton::set_pressed(bool p_pressed) {
  242. if (!toggle_mode)
  243. return;
  244. if (status.pressed == p_pressed)
  245. return;
  246. _change_notify("pressed");
  247. status.pressed = p_pressed;
  248. if (p_pressed) {
  249. _unpress_group();
  250. }
  251. update();
  252. }
  253. bool BaseButton::is_pressing() const {
  254. return status.press_attempt;
  255. }
  256. bool BaseButton::is_pressed() const {
  257. return toggle_mode ? status.pressed : status.press_attempt;
  258. }
  259. bool BaseButton::is_hovered() const {
  260. return status.hovering;
  261. }
  262. BaseButton::DrawMode BaseButton::get_draw_mode() const {
  263. if (status.disabled) {
  264. return DRAW_DISABLED;
  265. };
  266. //print_line("press attempt: "+itos(status.press_attempt)+" hover: "+itos(status.hovering)+" pressed: "+itos(status.pressed));
  267. if (status.press_attempt == false && status.hovering && !status.pressed) {
  268. return DRAW_HOVER;
  269. } else {
  270. /* determine if pressed or not */
  271. bool pressing;
  272. if (status.press_attempt) {
  273. pressing = status.pressing_inside;
  274. if (status.pressed)
  275. pressing = !pressing;
  276. } else {
  277. pressing = status.pressed;
  278. }
  279. if (pressing)
  280. return DRAW_PRESSED;
  281. else
  282. return DRAW_NORMAL;
  283. }
  284. return DRAW_NORMAL;
  285. }
  286. void BaseButton::set_toggle_mode(bool p_on) {
  287. toggle_mode = p_on;
  288. }
  289. bool BaseButton::is_toggle_mode() const {
  290. return toggle_mode;
  291. }
  292. void BaseButton::set_action_mode(ActionMode p_mode) {
  293. action_mode = p_mode;
  294. }
  295. BaseButton::ActionMode BaseButton::get_action_mode() const {
  296. return action_mode;
  297. }
  298. void BaseButton::set_enabled_focus_mode(FocusMode p_mode) {
  299. enabled_focus_mode = p_mode;
  300. if (!status.disabled) {
  301. set_focus_mode(p_mode);
  302. }
  303. }
  304. Control::FocusMode BaseButton::get_enabled_focus_mode() const {
  305. return enabled_focus_mode;
  306. }
  307. void BaseButton::set_shortcut(const Ref<ShortCut> &p_shortcut) {
  308. if (shortcut.is_null() == p_shortcut.is_null())
  309. return;
  310. shortcut = p_shortcut;
  311. set_process_unhandled_input(shortcut.is_valid());
  312. }
  313. Ref<ShortCut> BaseButton::get_shortcut() const {
  314. return shortcut;
  315. }
  316. void BaseButton::_unhandled_input(Ref<InputEvent> p_event) {
  317. if (!is_disabled() && is_visible_in_tree() && p_event->is_pressed() && !p_event->is_echo() && shortcut.is_valid() && shortcut->is_shortcut(p_event)) {
  318. if (get_viewport()->get_modal_stack_top() && !get_viewport()->get_modal_stack_top()->is_a_parent_of(this))
  319. return; //ignore because of modal window
  320. if (is_toggle_mode()) {
  321. set_pressed(!is_pressed());
  322. emit_signal("toggled", is_pressed());
  323. }
  324. emit_signal("pressed");
  325. }
  326. }
  327. String BaseButton::get_tooltip(const Point2 &p_pos) const {
  328. String tooltip = Control::get_tooltip(p_pos);
  329. if (shortcut.is_valid() && shortcut->is_valid()) {
  330. if (tooltip.find("$sc") != -1) {
  331. tooltip = tooltip.replace_first("$sc", "(" + shortcut->get_as_text() + ")");
  332. } else {
  333. tooltip += " (" + shortcut->get_as_text() + ")";
  334. }
  335. }
  336. return tooltip;
  337. }
  338. void BaseButton::set_button_group(const Ref<ButtonGroup> &p_group) {
  339. if (button_group.is_valid()) {
  340. button_group->buttons.erase(this);
  341. }
  342. button_group = p_group;
  343. if (button_group.is_valid()) {
  344. button_group->buttons.insert(this);
  345. }
  346. update(); //checkbox changes to radio if set a buttongroup
  347. }
  348. Ref<ButtonGroup> BaseButton::get_button_group() const {
  349. return button_group;
  350. }
  351. void BaseButton::_bind_methods() {
  352. ClassDB::bind_method(D_METHOD("_gui_input"), &BaseButton::_gui_input);
  353. ClassDB::bind_method(D_METHOD("_unhandled_input"), &BaseButton::_unhandled_input);
  354. ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &BaseButton::set_pressed);
  355. ClassDB::bind_method(D_METHOD("is_pressed"), &BaseButton::is_pressed);
  356. ClassDB::bind_method(D_METHOD("is_hovered"), &BaseButton::is_hovered);
  357. ClassDB::bind_method(D_METHOD("set_toggle_mode", "enabled"), &BaseButton::set_toggle_mode);
  358. ClassDB::bind_method(D_METHOD("is_toggle_mode"), &BaseButton::is_toggle_mode);
  359. ClassDB::bind_method(D_METHOD("set_disabled", "disabled"), &BaseButton::set_disabled);
  360. ClassDB::bind_method(D_METHOD("is_disabled"), &BaseButton::is_disabled);
  361. ClassDB::bind_method(D_METHOD("set_action_mode", "mode"), &BaseButton::set_action_mode);
  362. ClassDB::bind_method(D_METHOD("get_action_mode"), &BaseButton::get_action_mode);
  363. ClassDB::bind_method(D_METHOD("get_draw_mode"), &BaseButton::get_draw_mode);
  364. ClassDB::bind_method(D_METHOD("set_enabled_focus_mode", "mode"), &BaseButton::set_enabled_focus_mode);
  365. ClassDB::bind_method(D_METHOD("get_enabled_focus_mode"), &BaseButton::get_enabled_focus_mode);
  366. ClassDB::bind_method(D_METHOD("set_shortcut", "shortcut"), &BaseButton::set_shortcut);
  367. ClassDB::bind_method(D_METHOD("get_shortcut"), &BaseButton::get_shortcut);
  368. ClassDB::bind_method(D_METHOD("set_button_group", "button_group"), &BaseButton::set_button_group);
  369. ClassDB::bind_method(D_METHOD("get_button_group"), &BaseButton::get_button_group);
  370. BIND_VMETHOD(MethodInfo("_pressed"));
  371. BIND_VMETHOD(MethodInfo("_toggled", PropertyInfo(Variant::BOOL, "pressed")));
  372. ADD_SIGNAL(MethodInfo("pressed"));
  373. ADD_SIGNAL(MethodInfo("button_up"));
  374. ADD_SIGNAL(MethodInfo("button_down"));
  375. ADD_SIGNAL(MethodInfo("toggled", PropertyInfo(Variant::BOOL, "pressed")));
  376. ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
  377. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "toggle_mode"), "set_toggle_mode", "is_toggle_mode");
  378. ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed");
  379. ADD_PROPERTYNO(PropertyInfo(Variant::INT, "action_mode", PROPERTY_HINT_ENUM, "Button Press,Button Release"), "set_action_mode", "get_action_mode");
  380. ADD_PROPERTY(PropertyInfo(Variant::INT, "enabled_focus_mode", PROPERTY_HINT_ENUM, "None,Click,All"), "set_enabled_focus_mode", "get_enabled_focus_mode");
  381. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shortcut", PROPERTY_HINT_RESOURCE_TYPE, "ShortCut"), "set_shortcut", "get_shortcut");
  382. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "group", PROPERTY_HINT_RESOURCE_TYPE, "ButtonGroup"), "set_button_group", "get_button_group");
  383. BIND_ENUM_CONSTANT(DRAW_NORMAL);
  384. BIND_ENUM_CONSTANT(DRAW_PRESSED);
  385. BIND_ENUM_CONSTANT(DRAW_HOVER);
  386. BIND_ENUM_CONSTANT(DRAW_DISABLED);
  387. BIND_ENUM_CONSTANT(ACTION_MODE_BUTTON_PRESS);
  388. BIND_ENUM_CONSTANT(ACTION_MODE_BUTTON_RELEASE);
  389. }
  390. BaseButton::BaseButton() {
  391. toggle_mode = false;
  392. status.pressed = false;
  393. status.press_attempt = false;
  394. status.hovering = false;
  395. status.pressing_inside = false;
  396. status.disabled = false;
  397. status.pressing_button = 0;
  398. set_focus_mode(FOCUS_ALL);
  399. enabled_focus_mode = FOCUS_ALL;
  400. action_mode = ACTION_MODE_BUTTON_RELEASE;
  401. if (button_group.is_valid()) {
  402. button_group->buttons.erase(this);
  403. }
  404. }
  405. BaseButton::~BaseButton() {
  406. }
  407. void ButtonGroup::get_buttons(List<BaseButton *> *r_buttons) {
  408. for (Set<BaseButton *>::Element *E = buttons.front(); E; E = E->next()) {
  409. r_buttons->push_back(E->get());
  410. }
  411. }
  412. BaseButton *ButtonGroup::get_pressed_button() {
  413. for (Set<BaseButton *>::Element *E = buttons.front(); E; E = E->next()) {
  414. if (E->get()->is_pressed())
  415. return E->get();
  416. }
  417. return NULL;
  418. }
  419. void ButtonGroup::_bind_methods() {
  420. ClassDB::bind_method(D_METHOD("get_pressed_button"), &ButtonGroup::get_pressed_button);
  421. }
  422. ButtonGroup::ButtonGroup() {
  423. set_local_to_scene(true);
  424. }