input_map.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /**************************************************************************/
  2. /* input_map.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 = nullptr;
  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", "exact_match"), &InputMap::event_is_action, DEFVAL(false));
  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. }
  92. for (const List<StringName>::Element *E = actions.front(); E; E = E->next()) {
  93. ret.push_back(E->get());
  94. }
  95. return ret;
  96. }
  97. List<StringName> InputMap::get_actions() const {
  98. List<StringName> actions = List<StringName>();
  99. if (input_map.empty()) {
  100. return actions;
  101. }
  102. for (Map<StringName, Action>::Element *E = input_map.front(); E; E = E->next()) {
  103. actions.push_back(E->key());
  104. }
  105. return actions;
  106. }
  107. List<Ref<InputEvent>>::Element *InputMap::_find_event(Action &p_action, const Ref<InputEvent> &p_event, bool p_exact_match, bool *p_pressed, float *p_strength, float *p_raw_strength) const {
  108. ERR_FAIL_COND_V(!p_event.is_valid(), nullptr);
  109. for (List<Ref<InputEvent>>::Element *E = p_action.inputs.front(); E; E = E->next()) {
  110. const Ref<InputEvent> e = E->get();
  111. //if (e.type != Ref<InputEvent>::KEY && e.device != p_event.device) -- unsure about the KEY comparison, why is this here?
  112. // continue;
  113. int device = e->get_device();
  114. if (device == ALL_DEVICES || device == p_event->get_device()) {
  115. if (e->action_match(p_event, p_exact_match, p_pressed, p_strength, p_raw_strength, p_action.deadzone)) {
  116. return E;
  117. }
  118. }
  119. }
  120. return nullptr;
  121. }
  122. bool InputMap::has_action(const StringName &p_action) const {
  123. return input_map.has(p_action);
  124. }
  125. float InputMap::action_get_deadzone(const StringName &p_action) {
  126. ERR_FAIL_COND_V_MSG(!input_map.has(p_action), 0.0f, suggest_actions(p_action));
  127. return input_map[p_action].deadzone;
  128. }
  129. void InputMap::action_set_deadzone(const StringName &p_action, float p_deadzone) {
  130. ERR_FAIL_COND_MSG(!input_map.has(p_action), suggest_actions(p_action));
  131. input_map[p_action].deadzone = p_deadzone;
  132. }
  133. void InputMap::action_add_event(const StringName &p_action, const Ref<InputEvent> &p_event) {
  134. ERR_FAIL_COND_MSG(p_event.is_null(), "It's not a reference to a valid InputEvent object.");
  135. ERR_FAIL_COND_MSG(!input_map.has(p_action), suggest_actions(p_action));
  136. if (_find_event(input_map[p_action], p_event, true)) {
  137. return; // Already added.
  138. }
  139. input_map[p_action].inputs.push_back(p_event);
  140. }
  141. bool InputMap::action_has_event(const StringName &p_action, const Ref<InputEvent> &p_event) {
  142. ERR_FAIL_COND_V_MSG(!input_map.has(p_action), false, suggest_actions(p_action));
  143. return (_find_event(input_map[p_action], p_event, true) != nullptr);
  144. }
  145. void InputMap::action_erase_event(const StringName &p_action, const Ref<InputEvent> &p_event) {
  146. ERR_FAIL_COND_MSG(!input_map.has(p_action), suggest_actions(p_action));
  147. List<Ref<InputEvent>>::Element *E = _find_event(input_map[p_action], p_event, true);
  148. if (E) {
  149. input_map[p_action].inputs.erase(E);
  150. if (Input::get_singleton()->is_action_pressed(p_action)) {
  151. Input::get_singleton()->action_release(p_action);
  152. }
  153. }
  154. }
  155. void InputMap::action_erase_events(const StringName &p_action) {
  156. ERR_FAIL_COND_MSG(!input_map.has(p_action), suggest_actions(p_action));
  157. input_map[p_action].inputs.clear();
  158. }
  159. Array InputMap::_get_action_list(const StringName &p_action) {
  160. Array ret;
  161. const List<Ref<InputEvent>> *al = get_action_list(p_action);
  162. if (al) {
  163. for (const List<Ref<InputEvent>>::Element *E = al->front(); E; E = E->next()) {
  164. ret.push_back(E->get());
  165. }
  166. }
  167. return ret;
  168. }
  169. const List<Ref<InputEvent>> *InputMap::get_action_list(const StringName &p_action) {
  170. const Map<StringName, Action>::Element *E = input_map.find(p_action);
  171. if (!E) {
  172. return nullptr;
  173. }
  174. return &E->get().inputs;
  175. }
  176. bool InputMap::event_is_action(const Ref<InputEvent> &p_event, const StringName &p_action, bool p_exact_match) const {
  177. return event_get_action_status(p_event, p_action, p_exact_match);
  178. }
  179. bool InputMap::event_get_action_status(const Ref<InputEvent> &p_event, const StringName &p_action, bool p_exact_match, bool *p_pressed, float *p_strength, float *p_raw_strength) const {
  180. Map<StringName, Action>::Element *E = input_map.find(p_action);
  181. ERR_FAIL_COND_V_MSG(!E, false, suggest_actions(p_action));
  182. Ref<InputEventAction> input_event_action = p_event;
  183. if (input_event_action.is_valid()) {
  184. bool pressed = input_event_action->is_pressed();
  185. if (p_pressed != nullptr) {
  186. *p_pressed = pressed;
  187. }
  188. if (p_strength != nullptr) {
  189. *p_strength = pressed ? input_event_action->get_strength() : 0.0f;
  190. }
  191. return input_event_action->get_action() == p_action;
  192. }
  193. bool pressed;
  194. float strength;
  195. float raw_strength;
  196. List<Ref<InputEvent>>::Element *event = _find_event(E->get(), p_event, p_exact_match, &pressed, &strength, &raw_strength);
  197. if (event != nullptr) {
  198. if (p_pressed != nullptr) {
  199. *p_pressed = pressed;
  200. }
  201. if (p_strength != nullptr) {
  202. *p_strength = strength;
  203. }
  204. if (p_raw_strength != nullptr) {
  205. *p_raw_strength = raw_strength;
  206. }
  207. return true;
  208. } else {
  209. return false;
  210. }
  211. }
  212. const Map<StringName, InputMap::Action> &InputMap::get_action_map() const {
  213. return input_map;
  214. }
  215. void InputMap::load_from_globals() {
  216. input_map.clear();
  217. List<PropertyInfo> pinfo;
  218. ProjectSettings::get_singleton()->get_property_list(&pinfo);
  219. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  220. const PropertyInfo &pi = E->get();
  221. if (!pi.name.begins_with("input/")) {
  222. continue;
  223. }
  224. String name = pi.name.substr(pi.name.find("/") + 1, pi.name.length());
  225. Dictionary action = ProjectSettings::get_singleton()->get(pi.name);
  226. float deadzone = action.has("deadzone") ? (float)action["deadzone"] : 0.5f;
  227. Array events = action["events"];
  228. add_action(name, deadzone);
  229. for (int i = 0; i < events.size(); i++) {
  230. Ref<InputEvent> event = events[i];
  231. if (event.is_null()) {
  232. continue;
  233. }
  234. action_add_event(name, event);
  235. }
  236. }
  237. }
  238. void InputMap::load_default() {
  239. Ref<InputEventKey> key;
  240. add_action("ui_accept");
  241. key.instance();
  242. key->set_scancode(KEY_ENTER);
  243. action_add_event("ui_accept", key);
  244. key.instance();
  245. key->set_scancode(KEY_KP_ENTER);
  246. action_add_event("ui_accept", key);
  247. key.instance();
  248. key->set_scancode(KEY_SPACE);
  249. action_add_event("ui_accept", key);
  250. add_action("ui_select");
  251. key.instance();
  252. key->set_scancode(KEY_SPACE);
  253. action_add_event("ui_select", key);
  254. add_action("ui_cancel");
  255. key.instance();
  256. key->set_scancode(KEY_ESCAPE);
  257. action_add_event("ui_cancel", key);
  258. add_action("ui_focus_next");
  259. key.instance();
  260. key->set_scancode(KEY_TAB);
  261. action_add_event("ui_focus_next", key);
  262. add_action("ui_focus_prev");
  263. key.instance();
  264. key->set_scancode(KEY_TAB);
  265. key->set_shift(true);
  266. action_add_event("ui_focus_prev", key);
  267. add_action("ui_left");
  268. key.instance();
  269. key->set_scancode(KEY_LEFT);
  270. action_add_event("ui_left", key);
  271. add_action("ui_right");
  272. key.instance();
  273. key->set_scancode(KEY_RIGHT);
  274. action_add_event("ui_right", key);
  275. add_action("ui_up");
  276. key.instance();
  277. key->set_scancode(KEY_UP);
  278. action_add_event("ui_up", key);
  279. add_action("ui_down");
  280. key.instance();
  281. key->set_scancode(KEY_DOWN);
  282. action_add_event("ui_down", key);
  283. add_action("ui_page_up");
  284. key.instance();
  285. key->set_scancode(KEY_PAGEUP);
  286. action_add_event("ui_page_up", key);
  287. add_action("ui_page_down");
  288. key.instance();
  289. key->set_scancode(KEY_PAGEDOWN);
  290. action_add_event("ui_page_down", key);
  291. add_action("ui_home");
  292. key.instance();
  293. key->set_scancode(KEY_HOME);
  294. action_add_event("ui_home", key);
  295. add_action("ui_end");
  296. key.instance();
  297. key->set_scancode(KEY_END);
  298. action_add_event("ui_end", key);
  299. }
  300. InputMap::InputMap() {
  301. ERR_FAIL_COND_MSG(singleton, "Singleton in InputMap already exist.");
  302. singleton = this;
  303. }