input_map.cpp 12 KB

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