input_map.cpp 11 KB

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