input_map.cpp 8.7 KB

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