InputManager.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #include "core/ConfigCmdManager.h"
  2. #include "core/Console.h"
  3. #include "core/InputManager.h"
  4. #include "core/InputManagerCommands.h"
  5. #include "core/Logger.h"
  6. #include "core/Timer.h"
  7. template<>InputManager *Ogre::Singleton<InputManager>::msSingleton = nullptr;
  8. InputManager::InputManager():
  9. m_RepeatFirstWait(true),
  10. m_RepeatTimer(0)
  11. {
  12. InitCmd();
  13. Reset();
  14. }
  15. InputManager::~InputManager()
  16. {
  17. }
  18. void
  19. InputManager::Reset()
  20. {
  21. for(int button = 0; button < 256; ++button)
  22. {
  23. m_ButtonState[button] = false;
  24. }
  25. }
  26. void
  27. InputManager::ButtonPressed(int button, char text, bool down)
  28. {
  29. if( m_ButtonState[ button ] != down )
  30. {
  31. m_ButtonState[ button ] = down;
  32. m_ButtonText[ button ] = text;
  33. QGears::Event event;
  34. event.type = (down == true) ? QGears::ET_KEY_PRESS : QGears::ET_KEY_RELEASE;
  35. event.param1 = button;
  36. event.param2 = text;
  37. m_EventQueue.push_back( event );
  38. m_RepeatFirstWait = true;
  39. m_RepeatTimer = 0;
  40. }
  41. if( Console::getSingleton().IsVisible() != true )
  42. {
  43. if( down == true )
  44. {
  45. ActivateBinds( button );
  46. AddGameEvents(button, QGears::ET_KEY_PRESS);
  47. }
  48. else
  49. {
  50. AddGameEvents(button, QGears::ET_KEY_RELEASE);
  51. }
  52. }
  53. }
  54. void
  55. InputManager::MousePressed( int button, bool down )
  56. {
  57. QGears::Event event;
  58. event.type = (down == true) ? QGears::ET_KEY_PRESS : QGears::ET_KEY_RELEASE;
  59. event.param1 = button;
  60. m_EventQueue.push_back( event );
  61. }
  62. void
  63. InputManager::MouseMoved( int x, int y )
  64. {
  65. QGears::Event event;
  66. event.type = QGears::ET_MOUSE_MOVE;
  67. event.param1 = x;
  68. event.param2 = y;
  69. m_EventQueue.push_back( event );
  70. }
  71. void
  72. InputManager::MouseScrolled( int value )
  73. {
  74. QGears::Event event;
  75. event.type = QGears::ET_MOUSE_SCROLL;
  76. event.param1 = value;
  77. m_EventQueue.push_back( event );
  78. }
  79. void
  80. InputManager::Update()
  81. {
  82. m_RepeatTimer += Timer::getSingleton().GetSystemTimeDelta();
  83. if( ( m_RepeatFirstWait == true && m_RepeatTimer >= 0.5 ) || ( m_RepeatFirstWait == false && m_RepeatTimer >= 0.05 ) )
  84. {
  85. for( int button = 0; button < 256; ++button )
  86. {
  87. if( m_ButtonState[ button ] == true )
  88. {
  89. QGears::Event event;
  90. event.type = QGears::ET_KEY_REPEAT_WAIT;
  91. event.param1 = button;
  92. event.param2 = m_ButtonText[ button ];
  93. m_EventQueue.push_back( event );
  94. if( Console::getSingleton().IsVisible() != true )
  95. {
  96. AddGameEvents(button, QGears::ET_KEY_REPEAT_WAIT);
  97. }
  98. }
  99. }
  100. m_RepeatFirstWait = false;
  101. m_RepeatTimer = 0;
  102. }
  103. for( int button = 0; button < 256; ++button )
  104. {
  105. if( m_ButtonState[ button ] == true )
  106. {
  107. QGears::Event event;
  108. event.type = QGears::ET_KEY_REPEAT;
  109. event.param1 = button;
  110. event.param2 = m_ButtonText[ button ];
  111. m_EventQueue.push_back( event );
  112. if( Console::getSingleton().IsVisible() != true )
  113. {
  114. AddGameEvents(button, QGears::ET_KEY_REPEAT);
  115. }
  116. }
  117. }
  118. }
  119. bool
  120. InputManager::IsButtonPressed( int button ) const
  121. {
  122. return m_ButtonState[ button ];
  123. }
  124. void
  125. InputManager::GetInputEvents( InputEventArray& input_events )
  126. {
  127. input_events = m_EventQueue;
  128. m_EventQueue.clear();
  129. }
  130. void
  131. InputManager::BindCommand( ConfigCmd* cmd, const Ogre::StringVector& params, const ButtonList& buttons )
  132. {
  133. BindInfo info;
  134. info.cmd = cmd;
  135. info.params = params;
  136. info.buttons = buttons;
  137. m_Binds.push_back( info );
  138. }
  139. void
  140. InputManager::BindGameEvent( const Ogre::String& event, const ButtonList& buttons )
  141. {
  142. BindGameEventInfo info;
  143. info.event = event;
  144. info.buttons = buttons;
  145. m_BindGameEvents.push_back( info );
  146. }
  147. void
  148. InputManager::ActivateBinds( const int button )
  149. {
  150. std::vector< int > binds_indexes;
  151. for( unsigned int i = 0; i < m_Binds.size(); ++i )
  152. {
  153. // if we found pressed button in this bind
  154. if( std::find( m_Binds[ i ].buttons.begin(), m_Binds[ i ].buttons.end(), button ) != m_Binds[ i ].buttons.end() )
  155. {
  156. unsigned int j = 0;
  157. for( ; j < m_Binds[ i ].buttons.size(); ++j )
  158. {
  159. if( IsButtonPressed( m_Binds[ i ].buttons[ j ] ) == false )
  160. {
  161. break;
  162. }
  163. }
  164. if( j >= m_Binds[ i ].buttons.size() )
  165. {
  166. binds_indexes.push_back( i );
  167. }
  168. }
  169. }
  170. std::vector< int > binds_to_activate;
  171. unsigned int max_size = 0;
  172. for( unsigned int i = 0; i < binds_indexes.size(); ++i )
  173. {
  174. if( max_size < m_Binds[ binds_indexes[ i ] ].buttons.size() )
  175. {
  176. max_size = m_Binds[ binds_indexes[ i ] ].buttons.size();
  177. binds_to_activate.clear();
  178. binds_to_activate.push_back( binds_indexes[ i ] );
  179. }
  180. else if( max_size == m_Binds[ binds_indexes[ i ] ].buttons.size() )
  181. {
  182. binds_to_activate.push_back( binds_indexes[ i ] );
  183. }
  184. }
  185. for( unsigned int i = 0; i < binds_to_activate.size(); ++i )
  186. {
  187. m_Binds[ binds_to_activate[ i ] ].cmd->GetHandler()( m_Binds[ binds_to_activate[ i ] ].params );
  188. }
  189. }
  190. void
  191. InputManager::AddGameEvents(const int button, const QGears::EventType type)
  192. {
  193. std::vector< int > binds_indexes;
  194. for( unsigned int i = 0; i < m_BindGameEvents.size(); ++i )
  195. {
  196. // if we found button in this bind
  197. if( std::find( m_BindGameEvents[ i ].buttons.begin(), m_BindGameEvents[ i ].buttons.end(), button ) != m_BindGameEvents[ i ].buttons.end() )
  198. {
  199. unsigned int j = 0;
  200. for( ; j < m_BindGameEvents[ i ].buttons.size(); ++j )
  201. {
  202. if( m_BindGameEvents[ i ].buttons[ j ] != button )
  203. {
  204. if( IsButtonPressed( m_BindGameEvents[ i ].buttons[ j ] ) == false )
  205. {
  206. break;
  207. }
  208. }
  209. }
  210. if( j >= m_BindGameEvents[ i ].buttons.size() )
  211. {
  212. binds_indexes.push_back( i );
  213. }
  214. }
  215. }
  216. std::vector< int > binds_to_activate;
  217. unsigned int max_size = 0;
  218. for( unsigned int i = 0; i < binds_indexes.size(); ++i )
  219. {
  220. if( max_size < m_BindGameEvents[ binds_indexes[ i ] ].buttons.size() )
  221. {
  222. max_size = m_BindGameEvents[ binds_indexes[ i ] ].buttons.size();
  223. binds_to_activate.clear();
  224. binds_to_activate.push_back( binds_indexes[ i ] );
  225. }
  226. else if( max_size == m_BindGameEvents[ binds_indexes[ i ] ].buttons.size() )
  227. {
  228. binds_to_activate.push_back( binds_indexes[ i ] );
  229. }
  230. }
  231. for( unsigned int i = 0; i < binds_to_activate.size(); ++i )
  232. {
  233. QGears::Event event;
  234. event.type = type;
  235. event.event = m_BindGameEvents[ binds_to_activate[ i ] ].event;
  236. m_EventQueue.push_back( event );
  237. }
  238. }