input_map.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*************************************************************************/
  2. /* input_map.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "input_map.h"
  31. #include "globals.h"
  32. #include "os/keyboard.h"
  33. InputMap *InputMap::singleton = NULL;
  34. void InputMap::_bind_methods() {
  35. ObjectTypeDB::bind_method(_MD("has_action", "action"), &InputMap::has_action);
  36. ObjectTypeDB::bind_method(_MD("get_action_id", "action"), &InputMap::get_action_id);
  37. ObjectTypeDB::bind_method(_MD("get_action_from_id", "id"), &InputMap::get_action_from_id);
  38. ObjectTypeDB::bind_method(_MD("get_actions"), &InputMap::_get_actions);
  39. ObjectTypeDB::bind_method(_MD("add_action", "action"), &InputMap::add_action);
  40. ObjectTypeDB::bind_method(_MD("erase_action", "action"), &InputMap::erase_action);
  41. ObjectTypeDB::bind_method(_MD("action_add_event", "action", "event"), &InputMap::action_add_event);
  42. ObjectTypeDB::bind_method(_MD("action_has_event", "action", "event"), &InputMap::action_has_event);
  43. ObjectTypeDB::bind_method(_MD("action_erase_event", "action", "event"), &InputMap::action_erase_event);
  44. ObjectTypeDB::bind_method(_MD("get_action_list", "action"), &InputMap::_get_action_list);
  45. ObjectTypeDB::bind_method(_MD("event_is_action", "event", "action"), &InputMap::event_is_action);
  46. ObjectTypeDB::bind_method(_MD("load_from_globals"), &InputMap::load_from_globals);
  47. }
  48. void InputMap::add_action(const StringName &p_action) {
  49. ERR_FAIL_COND(input_map.has(p_action));
  50. input_map[p_action] = Action();
  51. static int last_id = 1;
  52. input_map[p_action].id = last_id;
  53. input_id_map[last_id] = p_action;
  54. last_id++;
  55. }
  56. void InputMap::erase_action(const StringName &p_action) {
  57. ERR_FAIL_COND(!input_map.has(p_action));
  58. input_id_map.erase(input_map[p_action].id);
  59. input_map.erase(p_action);
  60. }
  61. StringName InputMap::get_action_from_id(int p_id) const {
  62. ERR_FAIL_COND_V(!input_id_map.has(p_id), StringName());
  63. return input_id_map[p_id];
  64. }
  65. Array InputMap::_get_actions() {
  66. Array ret;
  67. List<StringName> actions = get_actions();
  68. if (actions.empty())
  69. return ret;
  70. for (const List<StringName>::Element *E = actions.front(); E; E = E->next()) {
  71. ret.push_back(E->get());
  72. }
  73. return ret;
  74. }
  75. List<StringName> InputMap::get_actions() const {
  76. List<StringName> actions = List<StringName>();
  77. if (input_map.empty()) {
  78. return actions;
  79. }
  80. for (Map<StringName, Action>::Element *E = input_map.front(); E; E = E->next()) {
  81. actions.push_back(E->key());
  82. }
  83. return actions;
  84. }
  85. List<InputEvent>::Element *InputMap::_find_event(List<InputEvent> &p_list, const InputEvent &p_event) const {
  86. for (List<InputEvent>::Element *E = p_list.front(); E; E = E->next()) {
  87. const InputEvent &e = E->get();
  88. if (e.type != p_event.type)
  89. continue;
  90. if (e.type != InputEvent::KEY && e.device != p_event.device)
  91. continue;
  92. bool same = false;
  93. switch (p_event.type) {
  94. case InputEvent::KEY: {
  95. same = (e.key.scancode == p_event.key.scancode && e.key.mod == p_event.key.mod);
  96. } break;
  97. case InputEvent::JOYSTICK_BUTTON: {
  98. same = (e.joy_button.button_index == p_event.joy_button.button_index);
  99. } break;
  100. case InputEvent::MOUSE_BUTTON: {
  101. same = (e.mouse_button.button_index == p_event.mouse_button.button_index);
  102. } break;
  103. case InputEvent::JOYSTICK_MOTION: {
  104. same = (e.joy_motion.axis == p_event.joy_motion.axis && (e.joy_motion.axis_value < 0) == (p_event.joy_motion.axis_value < 0));
  105. } break;
  106. }
  107. if (same)
  108. return E;
  109. }
  110. return NULL;
  111. }
  112. bool InputMap::has_action(const StringName &p_action) const {
  113. return input_map.has(p_action);
  114. }
  115. void InputMap::action_add_event(const StringName &p_action, const InputEvent &p_event) {
  116. ERR_FAIL_COND(p_event.type == InputEvent::ACTION);
  117. ERR_FAIL_COND(!input_map.has(p_action));
  118. if (_find_event(input_map[p_action].inputs, p_event))
  119. return; //already gots
  120. input_map[p_action].inputs.push_back(p_event);
  121. }
  122. int InputMap::get_action_id(const StringName &p_action) const {
  123. ERR_FAIL_COND_V(!input_map.has(p_action), -1);
  124. return input_map[p_action].id;
  125. }
  126. bool InputMap::action_has_event(const StringName &p_action, const InputEvent &p_event) {
  127. ERR_FAIL_COND_V(!input_map.has(p_action), false);
  128. return (_find_event(input_map[p_action].inputs, p_event) != NULL);
  129. }
  130. void InputMap::action_erase_event(const StringName &p_action, const InputEvent &p_event) {
  131. ERR_FAIL_COND(!input_map.has(p_action));
  132. List<InputEvent>::Element *E = _find_event(input_map[p_action].inputs, p_event);
  133. if (E)
  134. input_map[p_action].inputs.erase(E);
  135. }
  136. Array InputMap::_get_action_list(const StringName &p_action) {
  137. Array ret;
  138. const List<InputEvent> *al = get_action_list(p_action);
  139. if (al) {
  140. for (const List<InputEvent>::Element *E = al->front(); E; E = E->next()) {
  141. ret.push_back(E->get());
  142. }
  143. }
  144. return ret;
  145. }
  146. const List<InputEvent> *InputMap::get_action_list(const StringName &p_action) {
  147. const Map<StringName, Action>::Element *E = input_map.find(p_action);
  148. if (!E)
  149. return NULL;
  150. return &E->get().inputs;
  151. }
  152. bool InputMap::event_is_action(const InputEvent &p_event, const StringName &p_action) const {
  153. Map<StringName, Action>::Element *E = input_map.find(p_action);
  154. if (!E) {
  155. ERR_EXPLAIN("Request for nonexistent InputMap action: " + String(p_action));
  156. ERR_FAIL_COND_V(!E, false);
  157. }
  158. if (p_event.type == InputEvent::ACTION) {
  159. return p_event.action.action == E->get().id;
  160. }
  161. return _find_event(E->get().inputs, p_event) != NULL;
  162. }
  163. void InputMap::load_from_globals() {
  164. input_map.clear();
  165. List<PropertyInfo> pinfo;
  166. Globals::get_singleton()->get_property_list(&pinfo);
  167. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  168. const PropertyInfo &pi = E->get();
  169. if (!pi.name.begins_with("input/"))
  170. continue;
  171. String name = pi.name.substr(pi.name.find("/") + 1, pi.name.length());
  172. add_action(name);
  173. Array va = Globals::get_singleton()->get(pi.name);
  174. for (int i = 0; i < va.size(); i++) {
  175. InputEvent ie = va[i];
  176. if (ie.type == InputEvent::NONE)
  177. continue;
  178. action_add_event(name, ie);
  179. }
  180. }
  181. }
  182. void InputMap::load_default() {
  183. InputEvent key;
  184. key.type = InputEvent::KEY;
  185. add_action("ui_accept");
  186. key.key.scancode = KEY_RETURN;
  187. action_add_event("ui_accept", key);
  188. key.key.scancode = KEY_ENTER;
  189. action_add_event("ui_accept", key);
  190. key.key.scancode = KEY_SPACE;
  191. action_add_event("ui_accept", key);
  192. add_action("ui_select");
  193. key.key.scancode = KEY_SPACE;
  194. action_add_event("ui_select", key);
  195. add_action("ui_cancel");
  196. key.key.scancode = KEY_ESCAPE;
  197. action_add_event("ui_cancel", key);
  198. add_action("ui_focus_next");
  199. key.key.scancode = KEY_TAB;
  200. action_add_event("ui_focus_next", key);
  201. add_action("ui_focus_prev");
  202. key.key.scancode = KEY_TAB;
  203. key.key.mod.shift = true;
  204. action_add_event("ui_focus_prev", key);
  205. key.key.mod.shift = false;
  206. add_action("ui_left");
  207. key.key.scancode = KEY_LEFT;
  208. action_add_event("ui_left", key);
  209. add_action("ui_right");
  210. key.key.scancode = KEY_RIGHT;
  211. action_add_event("ui_right", key);
  212. add_action("ui_up");
  213. key.key.scancode = KEY_UP;
  214. action_add_event("ui_up", key);
  215. add_action("ui_down");
  216. key.key.scancode = KEY_DOWN;
  217. action_add_event("ui_down", key);
  218. add_action("ui_page_up");
  219. key.key.scancode = KEY_PAGEUP;
  220. action_add_event("ui_page_up", key);
  221. add_action("ui_page_down");
  222. key.key.scancode = KEY_PAGEDOWN;
  223. action_add_event("ui_page_down", key);
  224. // set("display/orientation", "landscape");
  225. }
  226. InputMap::InputMap() {
  227. ERR_FAIL_COND(singleton);
  228. singleton = this;
  229. }