input_map.cpp 12 KB

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