input_map.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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-2015 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. return; //already gots
  112. input_map[p_action].inputs.erase(E);
  113. }
  114. Array InputMap::_get_action_list(const StringName& p_action) {
  115. Array ret;
  116. const List<InputEvent> *al = get_action_list(p_action);
  117. if (al) {
  118. for(const List<InputEvent>::Element *E=al->front();E;E=E->next()) {
  119. ret.push_back(E->get());;
  120. }
  121. }
  122. return ret;
  123. }
  124. const List<InputEvent> *InputMap::get_action_list(const StringName& p_action) {
  125. const Map<StringName, Action>::Element *E=input_map.find(p_action);
  126. if (!E)
  127. return NULL;
  128. return &E->get().inputs;
  129. }
  130. bool InputMap::event_is_action(const InputEvent& p_event, const StringName& p_action) const {
  131. Map<StringName,Action >::Element *E=input_map.find(p_action);
  132. if(!E) {
  133. ERR_EXPLAIN("Request for nonexistent InputMap action: "+String(p_action));
  134. ERR_FAIL_COND_V(!E,false);
  135. }
  136. if (p_event.type==InputEvent::ACTION) {
  137. return p_event.action.action==E->get().id;
  138. }
  139. return _find_event(E->get().inputs,p_event)!=NULL;
  140. }
  141. void InputMap::load_from_globals() {
  142. input_map.clear();;
  143. List<PropertyInfo> pinfo;
  144. Globals::get_singleton()->get_property_list(&pinfo);
  145. for(List<PropertyInfo>::Element *E=pinfo.front();E;E=E->next()) {
  146. const PropertyInfo &pi=E->get();
  147. if (!pi.name.begins_with("input/"))
  148. continue;
  149. String name = pi.name.substr(pi.name.find("/")+1,pi.name.length());
  150. add_action(name);
  151. Array va = Globals::get_singleton()->get(pi.name);;
  152. for(int i=0;i<va.size();i++) {
  153. InputEvent ie=va[i];
  154. if (ie.type==InputEvent::NONE)
  155. continue;
  156. action_add_event(name,ie);
  157. }
  158. }
  159. }
  160. InputMap::InputMap() {
  161. ERR_FAIL_COND(singleton);
  162. singleton=this;
  163. }