input_map.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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-2014 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::add_action(const StringName& p_action) {
  33. ERR_FAIL_COND( input_map.has(p_action) );
  34. input_map[p_action]=Action();
  35. static int last_id=1;
  36. input_map[p_action].id=last_id;
  37. input_id_map[last_id]=p_action;
  38. last_id++;
  39. }
  40. void InputMap::erase_action(const StringName& p_action) {
  41. ERR_FAIL_COND( !input_map.has(p_action) );
  42. input_id_map.erase(input_map[p_action].id);
  43. input_map.erase(p_action);
  44. }
  45. StringName InputMap::get_action_from_id(int p_id) const {
  46. ERR_FAIL_COND_V(!input_id_map.has(p_id),StringName());
  47. return input_id_map[p_id];
  48. }
  49. List<InputEvent>::Element *InputMap::_find_event(List<InputEvent> &p_list,const InputEvent& p_event) const {
  50. for (List<InputEvent>::Element *E=p_list.front();E;E=E->next()) {
  51. const InputEvent& e=E->get();
  52. if(e.type!=p_event.type)
  53. continue;
  54. if (e.type!=InputEvent::KEY && e.device!=p_event.device)
  55. continue;
  56. bool same=false;
  57. switch(p_event.type) {
  58. case InputEvent::KEY: {
  59. same=(e.key.scancode==p_event.key.scancode && e.key.mod == p_event.key.mod);
  60. } break;
  61. case InputEvent::JOYSTICK_BUTTON: {
  62. same=(e.joy_button.button_index==p_event.joy_button.button_index);
  63. } break;
  64. case InputEvent::MOUSE_BUTTON: {
  65. same=(e.mouse_button.button_index==p_event.mouse_button.button_index);
  66. } break;
  67. case InputEvent::JOYSTICK_MOTION: {
  68. same=(e.joy_motion.axis==p_event.joy_motion.axis);
  69. } break;
  70. }
  71. if (same)
  72. return E;
  73. }
  74. return NULL;
  75. }
  76. bool InputMap::has_action(const StringName& p_action) const {
  77. return input_map.has(p_action);
  78. }
  79. void InputMap::action_add_event(const StringName& p_action,const InputEvent& p_event) {
  80. ERR_FAIL_COND(p_event.type==InputEvent::ACTION);
  81. ERR_FAIL_COND( !input_map.has(p_action) );
  82. if (_find_event(input_map[p_action].inputs,p_event))
  83. return; //already gots
  84. input_map[p_action].inputs.push_back(p_event);
  85. }
  86. int InputMap::get_action_id(const StringName& p_action) const {
  87. ERR_FAIL_COND_V(!input_map.has(p_action),-1);
  88. return input_map[p_action].id;
  89. }
  90. bool InputMap::action_has_event(const StringName& p_action,const InputEvent& p_event) {
  91. ERR_FAIL_COND_V( !input_map.has(p_action), false );
  92. return (_find_event(input_map[p_action].inputs,p_event)!=NULL);
  93. }
  94. void InputMap::action_erase_event(const StringName& p_action,const InputEvent& p_event) {
  95. ERR_FAIL_COND( !input_map.has(p_action) );
  96. List<InputEvent>::Element *E=_find_event(input_map[p_action].inputs,p_event);
  97. if (E)
  98. return; //already gots
  99. input_map[p_action].inputs.erase(E);
  100. }
  101. const List<InputEvent> *InputMap::get_action_list(const StringName& p_action) {
  102. const Map<StringName, Action>::Element *E=input_map.find(p_action);
  103. if (!E)
  104. return NULL;
  105. return &E->get().inputs;
  106. }
  107. bool InputMap::event_is_action(const InputEvent& p_event, const StringName& p_action) const {
  108. Map<StringName,Action >::Element *E=input_map.find(p_action);
  109. if(!E) {
  110. ERR_EXPLAIN("Request for unexisting InputMap action: "+String(p_action));
  111. ERR_FAIL_COND_V(!E,false);
  112. }
  113. if (p_event.type==InputEvent::ACTION) {
  114. return p_event.action.action==E->get().id;
  115. }
  116. return _find_event(E->get().inputs,p_event)!=NULL;
  117. }
  118. void InputMap::load_from_globals() {
  119. input_map.clear();;
  120. List<PropertyInfo> pinfo;
  121. Globals::get_singleton()->get_property_list(&pinfo);
  122. for(List<PropertyInfo>::Element *E=pinfo.front();E;E=E->next()) {
  123. const PropertyInfo &pi=E->get();
  124. if (!pi.name.begins_with("input/"))
  125. continue;
  126. String name = pi.name.substr(pi.name.find("/")+1,pi.name.length());
  127. add_action(name);
  128. Array va = Globals::get_singleton()->get(pi.name);;
  129. for(int i=0;i<va.size();i++) {
  130. InputEvent ie=va[i];
  131. if (ie.type==InputEvent::NONE)
  132. continue;
  133. action_add_event(name,ie);
  134. }
  135. }
  136. }
  137. InputMap::InputMap() {
  138. ERR_FAIL_COND(singleton);
  139. singleton=this;
  140. }