input_map.cpp 8.5 KB

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