input_map.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*************************************************************************/
  2. /* input_map.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "input_map.h"
  30. #include "globals.h"
  31. #include "os/keyboard.h"
  32. InputMap *InputMap::singleton=NULL;
  33. void InputMap::_bind_methods() {
  34. ObjectTypeDB::bind_method(_MD("has_action","action"),&InputMap::has_action);
  35. ObjectTypeDB::bind_method(_MD("get_action_id","action"),&InputMap::get_action_id);
  36. ObjectTypeDB::bind_method(_MD("get_action_from_id","id"),&InputMap::get_action_from_id);
  37. ObjectTypeDB::bind_method(_MD("get_actions"),&InputMap::_get_actions);
  38. ObjectTypeDB::bind_method(_MD("add_action","action"),&InputMap::add_action);
  39. ObjectTypeDB::bind_method(_MD("erase_action","action"),&InputMap::erase_action);
  40. ObjectTypeDB::bind_method(_MD("action_add_event","action","event"),&InputMap::action_add_event);
  41. ObjectTypeDB::bind_method(_MD("action_has_event","action","event"),&InputMap::action_has_event);
  42. ObjectTypeDB::bind_method(_MD("action_erase_event","action","event"),&InputMap::action_erase_event);
  43. ObjectTypeDB::bind_method(_MD("get_action_list","action"),&InputMap::_get_action_list);
  44. ObjectTypeDB::bind_method(_MD("event_is_action","event","action"),&InputMap::event_is_action);
  45. ObjectTypeDB::bind_method(_MD("load_from_globals"),&InputMap::load_from_globals);
  46. }
  47. void InputMap::add_action(const StringName& p_action) {
  48. ERR_FAIL_COND( input_map.has(p_action) );
  49. input_map[p_action]=Action();
  50. static int last_id=1;
  51. input_map[p_action].id=last_id;
  52. input_id_map[last_id]=p_action;
  53. last_id++;
  54. }
  55. void InputMap::erase_action(const StringName& p_action) {
  56. ERR_FAIL_COND( !input_map.has(p_action) );
  57. input_id_map.erase(input_map[p_action].id);
  58. input_map.erase(p_action);
  59. }
  60. StringName InputMap::get_action_from_id(int p_id) const {
  61. ERR_FAIL_COND_V(!input_id_map.has(p_id),StringName());
  62. return input_id_map[p_id];
  63. }
  64. Array InputMap::_get_actions() {
  65. Array ret;
  66. List<StringName> actions = get_actions();
  67. if(actions.empty())
  68. return ret;
  69. for(const List<StringName>::Element *E=actions.front();E;E=E->next()) {
  70. ret.push_back(E->get());
  71. }
  72. return ret;
  73. }
  74. List<StringName> InputMap::get_actions() const {
  75. List<StringName> actions = List<StringName>();
  76. if(input_map.empty()){
  77. return actions;
  78. }
  79. for (Map<StringName, Action>::Element *E=input_map.front();E;E=E->next()) {
  80. actions.push_back(E->key());
  81. }
  82. return actions;
  83. }
  84. List<InputEvent>::Element *InputMap::_find_event(List<InputEvent> &p_list,const InputEvent& p_event) const {
  85. for (List<InputEvent>::Element *E=p_list.front();E;E=E->next()) {
  86. const InputEvent& e=E->get();
  87. if(e.type!=p_event.type)
  88. continue;
  89. if (e.type!=InputEvent::KEY && e.device!=p_event.device)
  90. continue;
  91. bool same=false;
  92. switch(p_event.type) {
  93. case InputEvent::KEY: {
  94. same=(e.key.scancode==p_event.key.scancode && e.key.mod == p_event.key.mod);
  95. } break;
  96. case InputEvent::JOYSTICK_BUTTON: {
  97. same=(e.joy_button.button_index==p_event.joy_button.button_index);
  98. } break;
  99. case InputEvent::MOUSE_BUTTON: {
  100. same=(e.mouse_button.button_index==p_event.mouse_button.button_index);
  101. } break;
  102. case InputEvent::JOYSTICK_MOTION: {
  103. same=(e.joy_motion.axis==p_event.joy_motion.axis && (e.joy_motion.axis_value < 0) == (p_event.joy_motion.axis_value < 0));
  104. } break;
  105. }
  106. if (same)
  107. return E;
  108. }
  109. return NULL;
  110. }
  111. bool InputMap::has_action(const StringName& p_action) const {
  112. return input_map.has(p_action);
  113. }
  114. void InputMap::action_add_event(const StringName& p_action,const InputEvent& p_event) {
  115. ERR_FAIL_COND(p_event.type==InputEvent::ACTION);
  116. ERR_FAIL_COND( !input_map.has(p_action) );
  117. if (_find_event(input_map[p_action].inputs,p_event))
  118. return; //already gots
  119. input_map[p_action].inputs.push_back(p_event);
  120. }
  121. int InputMap::get_action_id(const StringName& p_action) const {
  122. ERR_FAIL_COND_V(!input_map.has(p_action),-1);
  123. return input_map[p_action].id;
  124. }
  125. bool InputMap::action_has_event(const StringName& p_action,const InputEvent& p_event) {
  126. ERR_FAIL_COND_V( !input_map.has(p_action), false );
  127. return (_find_event(input_map[p_action].inputs,p_event)!=NULL);
  128. }
  129. void InputMap::action_erase_event(const StringName& p_action,const InputEvent& p_event) {
  130. ERR_FAIL_COND( !input_map.has(p_action) );
  131. List<InputEvent>::Element *E=_find_event(input_map[p_action].inputs,p_event);
  132. if (E)
  133. input_map[p_action].inputs.erase(E);
  134. }
  135. Array InputMap::_get_action_list(const StringName& p_action) {
  136. Array ret;
  137. const List<InputEvent> *al = get_action_list(p_action);
  138. if (al) {
  139. for(const List<InputEvent>::Element *E=al->front();E;E=E->next()) {
  140. ret.push_back(E->get());;
  141. }
  142. }
  143. return ret;
  144. }
  145. const List<InputEvent> *InputMap::get_action_list(const StringName& p_action) {
  146. const Map<StringName, Action>::Element *E=input_map.find(p_action);
  147. if (!E)
  148. return NULL;
  149. return &E->get().inputs;
  150. }
  151. bool InputMap::event_is_action(const InputEvent& p_event, const StringName& p_action) const {
  152. Map<StringName,Action >::Element *E=input_map.find(p_action);
  153. if(!E) {
  154. ERR_EXPLAIN("Request for nonexistent InputMap action: "+String(p_action));
  155. ERR_FAIL_COND_V(!E,false);
  156. }
  157. if (p_event.type==InputEvent::ACTION) {
  158. return p_event.action.action==E->get().id;
  159. }
  160. return _find_event(E->get().inputs,p_event)!=NULL;
  161. }
  162. bool InputMap::event_is_joy_motion_action_pressed(const InputEvent& p_event) const {
  163. ERR_FAIL_COND_V(p_event.type!=InputEvent::JOYSTICK_MOTION,false);
  164. bool pressed=false;
  165. //this could be optimized by having a separate list of joymotions?
  166. for (Map<StringName, Action>::Element *A=input_map.front();A;A=A->next()) {
  167. for (List<InputEvent>::Element *E=A->get().inputs.front();E;E=E->next()) {
  168. const InputEvent& e=E->get();
  169. if(e.type!=p_event.type)
  170. continue;
  171. if (e.type!=InputEvent::KEY && e.device!=p_event.device)
  172. continue;
  173. switch(p_event.type) {
  174. case InputEvent::KEY: {
  175. if (e.key.scancode==p_event.key.scancode && e.key.mod == p_event.key.mod)
  176. return e.key.pressed;
  177. } break;
  178. case InputEvent::JOYSTICK_BUTTON: {
  179. if (e.joy_button.button_index==p_event.joy_button.button_index) {
  180. return e.joy_button.pressed;
  181. }
  182. } break;
  183. case InputEvent::MOUSE_BUTTON: {
  184. if (e.mouse_button.button_index==p_event.mouse_button.button_index) {
  185. return e.mouse_button.pressed;
  186. }
  187. } break;
  188. case InputEvent::JOYSTICK_MOTION: {
  189. if (e.joy_motion.axis==p_event.joy_motion.axis) {
  190. if (
  191. (e.joy_motion.axis_value * p_event.joy_motion.axis_value >0) && //same axis
  192. ABS(e.joy_motion.axis_value)>0.5 && ABS(p_event.joy_motion.axis_value)>0.5 )
  193. pressed=true;
  194. }
  195. } break;
  196. }
  197. }
  198. }
  199. return pressed;
  200. }
  201. void InputMap::load_from_globals() {
  202. input_map.clear();;
  203. List<PropertyInfo> pinfo;
  204. Globals::get_singleton()->get_property_list(&pinfo);
  205. for(List<PropertyInfo>::Element *E=pinfo.front();E;E=E->next()) {
  206. const PropertyInfo &pi=E->get();
  207. if (!pi.name.begins_with("input/"))
  208. continue;
  209. String name = pi.name.substr(pi.name.find("/")+1,pi.name.length());
  210. add_action(name);
  211. Array va = Globals::get_singleton()->get(pi.name);;
  212. for(int i=0;i<va.size();i++) {
  213. InputEvent ie=va[i];
  214. if (ie.type==InputEvent::NONE)
  215. continue;
  216. action_add_event(name,ie);
  217. }
  218. }
  219. }
  220. void InputMap::load_default() {
  221. InputEvent key;
  222. key.type=InputEvent::KEY;
  223. add_action("input/ui_accept");
  224. key.key.scancode=KEY_RETURN;
  225. action_add_event("input/ui_accept",key);
  226. key.key.scancode=KEY_ENTER;
  227. action_add_event("input/ui_accept",key);
  228. key.key.scancode=KEY_SPACE;
  229. action_add_event("input/ui_accept",key);
  230. add_action("input/ui_select");
  231. key.key.scancode=KEY_SPACE;
  232. action_add_event("input/ui_select",key);
  233. add_action("input/ui_cancel");
  234. key.key.scancode=KEY_ESCAPE;
  235. action_add_event("input/ui_cancel",key);
  236. add_action("input/ui_focus_next");
  237. key.key.scancode=KEY_TAB;
  238. action_add_event("input/ui_focus_next",key);
  239. add_action("input/ui_focus_prev");
  240. key.key.scancode=KEY_TAB;
  241. key.key.mod.shift=true;
  242. action_add_event("input/ui_focus_prev",key);
  243. key.key.mod.shift=false;
  244. add_action("input/ui_left");
  245. key.key.scancode=KEY_LEFT;
  246. action_add_event("input/ui_left",key);
  247. add_action("input/ui_right");
  248. key.key.scancode=KEY_RIGHT;
  249. action_add_event("input/ui_right",key);
  250. add_action("input/ui_up");
  251. key.key.scancode=KEY_UP;
  252. action_add_event("input/ui_up",key);
  253. add_action("input/ui_down");
  254. key.key.scancode=KEY_DOWN;
  255. action_add_event("input/ui_down",key);
  256. add_action("input/ui_page_up");
  257. key.key.scancode=KEY_PAGEUP;
  258. action_add_event("input/ui_page_up",key);
  259. add_action("input/ui_page_down");
  260. key.key.scancode=KEY_PAGEDOWN;
  261. action_add_event("input/ui_page_down",key);
  262. // set("display/orientation", "landscape");
  263. }
  264. InputMap::InputMap() {
  265. ERR_FAIL_COND(singleton);
  266. singleton=this;
  267. }