input_map.cpp 8.9 KB

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