InputManager.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #include "core/InputManager.h"
  2. #include "core/InputManagerCommands.h"
  3. #include "core/ConfigCmdManager.h"
  4. #include "core/Console.h"
  5. #include "core/Logger.h"
  6. #include "core/Timer.h"
  7. template<>InputManager *Ogre::Singleton< InputManager >::msSingleton = NULL;
  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. m_EventQueue.push_back(QGears::Event((down == true) ? QGears::ET_KEY_PRESS : QGears::ET_KEY_RELEASE, button, text));
  34. m_RepeatFirstWait = true;
  35. m_RepeatTimer = 0;
  36. }
  37. if( down == true && Console::getSingleton().IsVisible() != true )
  38. {
  39. ActivateBinds( button );
  40. }
  41. }
  42. void
  43. InputManager::MousePressed( int button, bool down )
  44. {
  45. m_EventQueue.push_back(QGears::Event((down == true) ? QGears::ET_MOUSE_PRESS : QGears::ET_MOUSE_RELEASE, button, 0));
  46. }
  47. void
  48. InputManager::MouseMoved( int x, int y )
  49. {
  50. m_EventQueue.push_back(QGears::Event(QGears::ET_MOUSE_MOVE, x, y));
  51. }
  52. void
  53. InputManager::MouseScrolled( int value )
  54. {
  55. m_EventQueue.push_back(QGears::Event(QGears::ET_MOUSE_SCROLL, value, 0));
  56. }
  57. void
  58. InputManager::Update()
  59. {
  60. m_RepeatTimer += Timer::getSingleton().GetSystemTimeDelta();
  61. if( ( m_RepeatFirstWait == true && m_RepeatTimer >= 0.5 ) || ( m_RepeatFirstWait == false && m_RepeatTimer >= 0.05 ) )
  62. {
  63. for( int button = 0; button < 256; ++button )
  64. {
  65. if( m_ButtonState[ button ] == true )
  66. {
  67. m_EventQueue.push_back(QGears::Event(QGears::ET_KEY_REPEAT, button, m_ButtonText[button]));
  68. }
  69. }
  70. m_RepeatFirstWait = false;
  71. m_RepeatTimer = 0;
  72. }
  73. // send impulses
  74. for( int button = 0; button < 256; ++button )
  75. {
  76. if( m_ButtonState[ button ] == true )
  77. {
  78. m_EventQueue.push_back(QGears::Event(QGears::ET_KEY_IMPULSE, button, m_ButtonText[button]));
  79. }
  80. }
  81. }
  82. bool
  83. InputManager::IsButtonPressed( int button ) const
  84. {
  85. return m_ButtonState[ button ];
  86. }
  87. void
  88. InputManager::GetInputEvents( InputEventArray& input_events )
  89. {
  90. input_events = m_EventQueue;
  91. m_EventQueue.clear();
  92. }
  93. void
  94. InputManager::BindCommand( const Ogre::String& cmd, const ButtonList& buttons )
  95. {
  96. BindInfo info = BindInfo( cmd, buttons );
  97. m_Binds.push_back( info );
  98. }
  99. void
  100. InputManager::ActivateBinds( int button )
  101. {
  102. std::vector< int > binds_indexes;
  103. for( size_t i = 0; i < m_Binds.size(); ++i )
  104. {
  105. // if we found pressed button in this bind
  106. if( std::find( m_Binds[ i ].key_set.begin(), m_Binds[ i ].key_set.end(), button ) != m_Binds[ i ].key_set.end() )
  107. {
  108. size_t j = 0;
  109. for( ; j < m_Binds[ i ].key_set.size(); ++j )
  110. {
  111. if( IsButtonPressed( m_Binds[ i ].key_set[ j ] ) == false )
  112. {
  113. break;
  114. }
  115. }
  116. if( j >= m_Binds[ i ].key_set.size() )
  117. {
  118. binds_indexes.push_back( i );
  119. }
  120. }
  121. }
  122. std::vector< int > binds_to_activate;
  123. size_t max_size = 0;
  124. for( size_t i = 0; i < binds_indexes.size(); ++i )
  125. {
  126. if( max_size < m_Binds[ binds_indexes[ i ] ].key_set.size() )
  127. {
  128. max_size = m_Binds[ binds_indexes[ i ] ].key_set.size();
  129. binds_to_activate.clear();
  130. binds_to_activate.push_back( binds_indexes[ i ] );
  131. }
  132. else if( max_size == m_Binds[ binds_indexes[ i ] ].key_set.size() )
  133. {
  134. binds_to_activate.push_back( binds_indexes[ i ] );
  135. }
  136. }
  137. for( size_t i = 0; i < binds_to_activate.size(); ++i )
  138. {
  139. Ogre::StringVector params = StringTokenise( m_Binds[ binds_to_activate[ i ] ].cmd );
  140. // handle command
  141. ConfigCmd* cmd = ConfigCmdManager::getSingleton().Find( params[ 0 ] );
  142. if( cmd != NULL )
  143. {
  144. cmd->GetHandler()( params );
  145. }
  146. else
  147. {
  148. LOG_ERROR( "Can't find command \"" + params[ 0 ] + "\" in bind command \"" + m_Binds[ i ].cmd + "\"." );
  149. }
  150. }
  151. }