input_map.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*************************************************************************/
  2. /* input_map.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "core/os/input.h"
  32. #include "core/os/keyboard.h"
  33. #include "core/project_settings.h"
  34. InputMap *InputMap::singleton = NULL;
  35. int InputMap::ALL_DEVICES = -1;
  36. void InputMap::_bind_methods() {
  37. ClassDB::bind_method(D_METHOD("has_action", "action"), &InputMap::has_action);
  38. ClassDB::bind_method(D_METHOD("get_actions"), &InputMap::_get_actions);
  39. ClassDB::bind_method(D_METHOD("add_action", "action", "deadzone"), &InputMap::add_action, DEFVAL(0.5f));
  40. ClassDB::bind_method(D_METHOD("erase_action", "action"), &InputMap::erase_action);
  41. ClassDB::bind_method(D_METHOD("action_set_deadzone", "action", "deadzone"), &InputMap::action_set_deadzone);
  42. ClassDB::bind_method(D_METHOD("action_get_deadzone", "action"), &InputMap::action_get_deadzone);
  43. ClassDB::bind_method(D_METHOD("action_add_event", "action", "event"), &InputMap::action_add_event);
  44. ClassDB::bind_method(D_METHOD("action_has_event", "action", "event"), &InputMap::action_has_event);
  45. ClassDB::bind_method(D_METHOD("action_erase_event", "action", "event"), &InputMap::action_erase_event);
  46. ClassDB::bind_method(D_METHOD("action_erase_events", "action"), &InputMap::action_erase_events);
  47. ClassDB::bind_method(D_METHOD("get_action_list", "action"), &InputMap::_get_action_list);
  48. ClassDB::bind_method(D_METHOD("event_is_action", "event", "action"), &InputMap::event_is_action);
  49. ClassDB::bind_method(D_METHOD("load_from_globals"), &InputMap::load_from_globals);
  50. }
  51. /**
  52. * Returns an nonexistent action error message with a suggestion of the closest
  53. * matching action name (if possible).
  54. */
  55. String InputMap::_suggest_actions(const StringName &p_action) const {
  56. List<StringName> actions = get_actions();
  57. StringName closest_action;
  58. float closest_similarity = 0.0;
  59. // Find the most action with the most similar name.
  60. for (List<StringName>::Element *E = actions.front(); E; E = E->next()) {
  61. const float similarity = String(E->get()).similarity(p_action);
  62. if (similarity > closest_similarity) {
  63. closest_action = E->get();
  64. closest_similarity = similarity;
  65. }
  66. }
  67. String error_message = vformat("The InputMap action \"%s\" doesn't exist.", p_action);
  68. if (closest_similarity >= 0.4) {
  69. // Only include a suggestion in the error message if it's similar enough.
  70. error_message += vformat(" Did you mean \"%s\"?", closest_action);
  71. }
  72. return error_message;
  73. }
  74. void InputMap::add_action(const StringName &p_action, float p_deadzone) {
  75. ERR_FAIL_COND_MSG(input_map.has(p_action), "InputMap already has action \"" + String(p_action) + "\".");
  76. input_map[p_action] = Action();
  77. static int last_id = 1;
  78. input_map[p_action].id = last_id;
  79. input_map[p_action].deadzone = p_deadzone;
  80. last_id++;
  81. }
  82. void InputMap::erase_action(const StringName &p_action) {
  83. ERR_FAIL_COND_MSG(!input_map.has(p_action), _suggest_actions(p_action));
  84. input_map.erase(p_action);
  85. }
  86. Array InputMap::_get_actions() {
  87. Array ret;
  88. List<StringName> actions = get_actions();
  89. if (actions.empty())
  90. return ret;
  91. for (const List<StringName>::Element *E = actions.front(); E; E = E->next()) {
  92. ret.push_back(E->get());
  93. }
  94. return ret;
  95. }
  96. List<StringName> InputMap::get_actions() const {
  97. List<StringName> actions = List<StringName>();
  98. if (input_map.empty()) {
  99. return actions;
  100. }
  101. for (Map<StringName, Action>::Element *E = input_map.front(); E; E = E->next()) {
  102. actions.push_back(E->key());
  103. }
  104. return actions;
  105. }
  106. List<Ref<InputEvent> >::Element *InputMap::_find_event(Action &p_action, const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength) const {
  107. ERR_FAIL_COND_V(!p_event.is_valid(), NULL);
  108. for (List<Ref<InputEvent> >::Element *E = p_action.inputs.front(); E; E = E->next()) {
  109. const Ref<InputEvent> e = E->get();
  110. //if (e.type != Ref<InputEvent>::KEY && e.device != p_event.device) -- unsure about the KEY comparison, why is this here?
  111. // continue;
  112. int device = e->get_device();
  113. if (device == ALL_DEVICES || device == p_event->get_device()) {
  114. if (e->action_match(p_event, p_pressed, p_strength, p_action.deadzone)) {
  115. return E;
  116. }
  117. }
  118. }
  119. return NULL;
  120. }
  121. bool InputMap::has_action(const StringName &p_action) const {
  122. return input_map.has(p_action);
  123. }
  124. float InputMap::action_get_deadzone(const StringName &p_action) {
  125. ERR_FAIL_COND_V_MSG(!input_map.has(p_action), 0.0f, _suggest_actions(p_action));
  126. return input_map[p_action].deadzone;
  127. }
  128. void InputMap::action_set_deadzone(const StringName &p_action, float p_deadzone) {
  129. ERR_FAIL_COND_MSG(!input_map.has(p_action), _suggest_actions(p_action));
  130. input_map[p_action].deadzone = p_deadzone;
  131. }
  132. void InputMap::action_add_event(const StringName &p_action, const Ref<InputEvent> &p_event) {
  133. ERR_FAIL_COND_MSG(p_event.is_null(), "It's not a reference to a valid InputEvent object.");
  134. ERR_FAIL_COND_MSG(!input_map.has(p_action), _suggest_actions(p_action));
  135. if (_find_event(input_map[p_action], p_event))
  136. return; //already gots
  137. input_map[p_action].inputs.push_back(p_event);
  138. }
  139. bool InputMap::action_has_event(const StringName &p_action, const Ref<InputEvent> &p_event) {
  140. ERR_FAIL_COND_V_MSG(!input_map.has(p_action), false, _suggest_actions(p_action));
  141. return (_find_event(input_map[p_action], p_event) != NULL);
  142. }
  143. void InputMap::action_erase_event(const StringName &p_action, const Ref<InputEvent> &p_event) {
  144. ERR_FAIL_COND_MSG(!input_map.has(p_action), _suggest_actions(p_action));
  145. List<Ref<InputEvent> >::Element *E = _find_event(input_map[p_action], p_event);
  146. if (E) {
  147. input_map[p_action].inputs.erase(E);
  148. if (Input::get_singleton()->is_action_pressed(p_action)) {
  149. Input::get_singleton()->action_release(p_action);
  150. }
  151. }
  152. }
  153. void InputMap::action_erase_events(const StringName &p_action) {
  154. ERR_FAIL_COND_MSG(!input_map.has(p_action), _suggest_actions(p_action));
  155. input_map[p_action].inputs.clear();
  156. }
  157. Array InputMap::_get_action_list(const StringName &p_action) {
  158. Array ret;
  159. const List<Ref<InputEvent> > *al = get_action_list(p_action);
  160. if (al) {
  161. for (const List<Ref<InputEvent> >::Element *E = al->front(); E; E = E->next()) {
  162. ret.push_back(E->get());
  163. }
  164. }
  165. return ret;
  166. }
  167. const List<Ref<InputEvent> > *InputMap::get_action_list(const StringName &p_action) {
  168. const Map<StringName, Action>::Element *E = input_map.find(p_action);
  169. if (!E)
  170. return NULL;
  171. return &E->get().inputs;
  172. }
  173. bool InputMap::event_is_action(const Ref<InputEvent> &p_event, const StringName &p_action) const {
  174. return event_get_action_status(p_event, p_action);
  175. }
  176. bool InputMap::event_get_action_status(const Ref<InputEvent> &p_event, const StringName &p_action, bool *p_pressed, float *p_strength) const {
  177. Map<StringName, Action>::Element *E = input_map.find(p_action);
  178. ERR_FAIL_COND_V_MSG(!E, false, _suggest_actions(p_action));
  179. Ref<InputEventAction> input_event_action = p_event;
  180. if (input_event_action.is_valid()) {
  181. if (p_pressed != NULL)
  182. *p_pressed = input_event_action->is_pressed();
  183. if (p_strength != NULL)
  184. *p_strength = (p_pressed != NULL && *p_pressed) ? input_event_action->get_strength() : 0.0f;
  185. return input_event_action->get_action() == p_action;
  186. }
  187. bool pressed;
  188. float strength;
  189. List<Ref<InputEvent> >::Element *event = _find_event(E->get(), p_event, &pressed, &strength);
  190. if (event != NULL) {
  191. if (p_pressed != NULL)
  192. *p_pressed = pressed;
  193. if (p_strength != NULL)
  194. *p_strength = strength;
  195. return true;
  196. } else {
  197. return false;
  198. }
  199. }
  200. const Map<StringName, InputMap::Action> &InputMap::get_action_map() const {
  201. return input_map;
  202. }
  203. void InputMap::load_from_globals() {
  204. input_map.clear();
  205. List<PropertyInfo> pinfo;
  206. ProjectSettings::get_singleton()->get_property_list(&pinfo);
  207. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  208. const PropertyInfo &pi = E->get();
  209. if (!pi.name.begins_with("input/"))
  210. continue;
  211. String name = pi.name.substr(pi.name.find("/") + 1, pi.name.length());
  212. Dictionary action = ProjectSettings::get_singleton()->get(pi.name);
  213. float deadzone = action.has("deadzone") ? (float)action["deadzone"] : 0.5f;
  214. Array events = action["events"];
  215. add_action(name, deadzone);
  216. for (int i = 0; i < events.size(); i++) {
  217. Ref<InputEvent> event = events[i];
  218. if (event.is_null())
  219. continue;
  220. action_add_event(name, event);
  221. }
  222. }
  223. }
  224. void InputMap::load_default() {
  225. Ref<InputEventKey> key;
  226. add_action("ui_accept");
  227. key.instance();
  228. key->set_scancode(KEY_ENTER);
  229. action_add_event("ui_accept", key);
  230. key.instance();
  231. key->set_scancode(KEY_KP_ENTER);
  232. action_add_event("ui_accept", key);
  233. key.instance();
  234. key->set_scancode(KEY_SPACE);
  235. action_add_event("ui_accept", key);
  236. add_action("ui_select");
  237. key.instance();
  238. key->set_scancode(KEY_SPACE);
  239. action_add_event("ui_select", key);
  240. add_action("ui_cancel");
  241. key.instance();
  242. key->set_scancode(KEY_ESCAPE);
  243. action_add_event("ui_cancel", key);
  244. add_action("ui_focus_next");
  245. key.instance();
  246. key->set_scancode(KEY_TAB);
  247. action_add_event("ui_focus_next", key);
  248. add_action("ui_focus_prev");
  249. key.instance();
  250. key->set_scancode(KEY_TAB);
  251. key->set_shift(true);
  252. action_add_event("ui_focus_prev", key);
  253. add_action("ui_left");
  254. key.instance();
  255. key->set_scancode(KEY_LEFT);
  256. action_add_event("ui_left", key);
  257. add_action("ui_right");
  258. key.instance();
  259. key->set_scancode(KEY_RIGHT);
  260. action_add_event("ui_right", key);
  261. add_action("ui_up");
  262. key.instance();
  263. key->set_scancode(KEY_UP);
  264. action_add_event("ui_up", key);
  265. add_action("ui_down");
  266. key.instance();
  267. key->set_scancode(KEY_DOWN);
  268. action_add_event("ui_down", key);
  269. add_action("ui_page_up");
  270. key.instance();
  271. key->set_scancode(KEY_PAGEUP);
  272. action_add_event("ui_page_up", key);
  273. add_action("ui_page_down");
  274. key.instance();
  275. key->set_scancode(KEY_PAGEDOWN);
  276. action_add_event("ui_page_down", key);
  277. add_action("ui_home");
  278. key.instance();
  279. key->set_scancode(KEY_HOME);
  280. action_add_event("ui_home", key);
  281. add_action("ui_end");
  282. key.instance();
  283. key->set_scancode(KEY_END);
  284. action_add_event("ui_end", key);
  285. }
  286. InputMap::InputMap() {
  287. ERR_FAIL_COND_MSG(singleton, "Singleton in InputMap already exist.");
  288. singleton = this;
  289. }