input_map.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. InputMap *InputMap::singleton=NULL;
  32. void InputMap::_bind_methods() {
  33. ObjectTypeDB::bind_method(_MD("has_action","action"),&InputMap::has_action);
  34. ObjectTypeDB::bind_method(_MD("get_action_id","action"),&InputMap::get_action_id);
  35. ObjectTypeDB::bind_method(_MD("get_action_from_id","id"),&InputMap::get_action_from_id);
  36. ObjectTypeDB::bind_method(_MD("get_actions"),&InputMap::_get_actions);
  37. ObjectTypeDB::bind_method(_MD("add_action","action"),&InputMap::add_action);
  38. ObjectTypeDB::bind_method(_MD("erase_action","action"),&InputMap::erase_action);
  39. ObjectTypeDB::bind_method(_MD("action_add_event","action","event"),&InputMap::action_add_event);
  40. ObjectTypeDB::bind_method(_MD("action_has_event","action","event"),&InputMap::action_has_event);
  41. ObjectTypeDB::bind_method(_MD("action_erase_event","action","event"),&InputMap::action_erase_event);
  42. ObjectTypeDB::bind_method(_MD("get_action_list","action"),&InputMap::_get_action_list);
  43. ObjectTypeDB::bind_method(_MD("event_is_action","event","action"),&InputMap::event_is_action);
  44. ObjectTypeDB::bind_method(_MD("load_from_globals"),&InputMap::load_from_globals);
  45. }
  46. void InputMap::add_action(const StringName& p_action) {
  47. ERR_FAIL_COND( input_map.has(p_action) );
  48. input_map[p_action]=Action();
  49. static int last_id=1;
  50. input_map[p_action].id=last_id;
  51. input_id_map[last_id]=p_action;
  52. last_id++;
  53. }
  54. void InputMap::erase_action(const StringName& p_action) {
  55. ERR_FAIL_COND( !input_map.has(p_action) );
  56. input_id_map.erase(input_map[p_action].id);
  57. input_map.erase(p_action);
  58. }
  59. StringName InputMap::get_action_from_id(int p_id) const {
  60. ERR_FAIL_COND_V(!input_id_map.has(p_id),StringName());
  61. return input_id_map[p_id];
  62. }
  63. Array InputMap::_get_actions() {
  64. Array ret;
  65. List<StringName> actions = get_actions();
  66. if(actions.empty())
  67. return ret;
  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.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<InputEvent>::Element *InputMap::_find_event(List<InputEvent> &p_list,const InputEvent& p_event) const {
  84. for (List<InputEvent>::Element *E=p_list.front();E;E=E->next()) {
  85. const InputEvent& e=E->get();
  86. if(e.type!=p_event.type)
  87. continue;
  88. if (e.type!=InputEvent::KEY && e.device!=p_event.device)
  89. continue;
  90. bool same=false;
  91. switch(p_event.type) {
  92. case InputEvent::KEY: {
  93. same=(e.key.scancode==p_event.key.scancode && e.key.mod == p_event.key.mod);
  94. } break;
  95. case InputEvent::JOYSTICK_BUTTON: {
  96. same=(e.joy_button.button_index==p_event.joy_button.button_index);
  97. } break;
  98. case InputEvent::MOUSE_BUTTON: {
  99. same=(e.mouse_button.button_index==p_event.mouse_button.button_index);
  100. } break;
  101. case InputEvent::JOYSTICK_MOTION: {
  102. same=(e.joy_motion.axis==p_event.joy_motion.axis && (e.joy_motion.axis_value < 0) == (p_event.joy_motion.axis_value < 0));
  103. } break;
  104. }
  105. if (same)
  106. return E;
  107. }
  108. return NULL;
  109. }
  110. bool InputMap::has_action(const StringName& p_action) const {
  111. return input_map.has(p_action);
  112. }
  113. void InputMap::action_add_event(const StringName& p_action,const InputEvent& p_event) {
  114. ERR_FAIL_COND(p_event.type==InputEvent::ACTION);
  115. ERR_FAIL_COND( !input_map.has(p_action) );
  116. if (_find_event(input_map[p_action].inputs,p_event))
  117. return; //already gots
  118. input_map[p_action].inputs.push_back(p_event);
  119. }
  120. int InputMap::get_action_id(const StringName& p_action) const {
  121. ERR_FAIL_COND_V(!input_map.has(p_action),-1);
  122. return input_map[p_action].id;
  123. }
  124. bool InputMap::action_has_event(const StringName& p_action,const InputEvent& p_event) {
  125. ERR_FAIL_COND_V( !input_map.has(p_action), false );
  126. return (_find_event(input_map[p_action].inputs,p_event)!=NULL);
  127. }
  128. void InputMap::action_erase_event(const StringName& p_action,const InputEvent& p_event) {
  129. ERR_FAIL_COND( !input_map.has(p_action) );
  130. List<InputEvent>::Element *E=_find_event(input_map[p_action].inputs,p_event);
  131. if (E)
  132. input_map[p_action].inputs.erase(E);
  133. }
  134. Array InputMap::_get_action_list(const StringName& p_action) {
  135. Array ret;
  136. const List<InputEvent> *al = get_action_list(p_action);
  137. if (al) {
  138. for(const List<InputEvent>::Element *E=al->front();E;E=E->next()) {
  139. ret.push_back(E->get());;
  140. }
  141. }
  142. return ret;
  143. }
  144. const List<InputEvent> *InputMap::get_action_list(const StringName& p_action) {
  145. const Map<StringName, Action>::Element *E=input_map.find(p_action);
  146. if (!E)
  147. return NULL;
  148. return &E->get().inputs;
  149. }
  150. bool InputMap::event_is_action(const InputEvent& p_event, const StringName& p_action) const {
  151. Map<StringName,Action >::Element *E=input_map.find(p_action);
  152. if(!E) {
  153. ERR_EXPLAIN("Request for nonexistent InputMap action: "+String(p_action));
  154. ERR_FAIL_COND_V(!E,false);
  155. }
  156. if (p_event.type==InputEvent::ACTION) {
  157. return p_event.action.action==E->get().id;
  158. }
  159. return _find_event(E->get().inputs,p_event)!=NULL;
  160. }
  161. bool InputMap::event_is_joy_motion_action_pressed(const InputEvent& p_event) const {
  162. ERR_FAIL_COND_V(p_event.type!=InputEvent::JOYSTICK_MOTION,false);
  163. bool pressed=false;
  164. //this could be optimized by having a separate list of joymotions?
  165. for (Map<StringName, Action>::Element *A=input_map.front();A;A=A->next()) {
  166. for (List<InputEvent>::Element *E=A->get().inputs.front();E;E=E->next()) {
  167. const InputEvent& e=E->get();
  168. if(e.type!=p_event.type)
  169. continue;
  170. if (e.type!=InputEvent::KEY && e.device!=p_event.device)
  171. continue;
  172. switch(p_event.type) {
  173. case InputEvent::KEY: {
  174. if (e.key.scancode==p_event.key.scancode && e.key.mod == p_event.key.mod)
  175. return e.key.pressed;
  176. } break;
  177. case InputEvent::JOYSTICK_BUTTON: {
  178. if (e.joy_button.button_index==p_event.joy_button.button_index) {
  179. return e.joy_button.pressed;
  180. }
  181. } break;
  182. case InputEvent::MOUSE_BUTTON: {
  183. if (e.mouse_button.button_index==p_event.mouse_button.button_index) {
  184. return e.mouse_button.pressed;
  185. }
  186. } break;
  187. case InputEvent::JOYSTICK_MOTION: {
  188. if (e.joy_motion.axis==p_event.joy_motion.axis) {
  189. if (
  190. (e.joy_motion.axis_value * p_event.joy_motion.axis_value >0) && //same axis
  191. ABS(e.joy_motion.axis_value)>0.5 && ABS(p_event.joy_motion.axis_value)>0.5 )
  192. pressed=true;
  193. }
  194. } break;
  195. }
  196. }
  197. }
  198. return pressed;
  199. }
  200. void InputMap::load_from_globals() {
  201. input_map.clear();;
  202. List<PropertyInfo> pinfo;
  203. Globals::get_singleton()->get_property_list(&pinfo);
  204. for(List<PropertyInfo>::Element *E=pinfo.front();E;E=E->next()) {
  205. const PropertyInfo &pi=E->get();
  206. if (!pi.name.begins_with("input/"))
  207. continue;
  208. String name = pi.name.substr(pi.name.find("/")+1,pi.name.length());
  209. add_action(name);
  210. Array va = Globals::get_singleton()->get(pi.name);;
  211. for(int i=0;i<va.size();i++) {
  212. InputEvent ie=va[i];
  213. if (ie.type==InputEvent::NONE)
  214. continue;
  215. action_add_event(name,ie);
  216. }
  217. }
  218. }
  219. InputMap::InputMap() {
  220. ERR_FAIL_COND(singleton);
  221. singleton=this;
  222. }