| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- #include "core/InputManager.h"
- #include "core/InputManagerCommands.h"
- #include "core/ConfigCmdManager.h"
- #include "core/Console.h"
- #include "core/Logger.h"
- #include "core/Timer.h"
- template<>InputManager *Ogre::Singleton< InputManager >::msSingleton = NULL;
- InputManager::InputManager():
- m_RepeatFirstWait( true ),
- m_RepeatTimer( 0 )
- {
- InitCmd();
- Reset();
- }
- InputManager::~InputManager()
- {
- }
- void
- InputManager::Reset()
- {
- for( int button = 0; button < 256; ++button )
- {
- m_ButtonState[ button ] = false;
- }
- }
- void
- InputManager::ButtonPressed( int button, char text, bool down )
- {
- if( m_ButtonState[ button ] != down )
- {
- m_ButtonState[ button ] = down;
- m_ButtonText[ button ] = text;
- m_EventQueue.push_back(QGears::Event((down == true) ? QGears::ET_KEY_PRESS : QGears::ET_KEY_RELEASE, button, text));
- m_RepeatFirstWait = true;
- m_RepeatTimer = 0;
- }
- if( down == true && Console::getSingleton().IsVisible() != true )
- {
- ActivateBinds( button );
- }
- }
- void
- InputManager::MousePressed( int button, bool down )
- {
- m_EventQueue.push_back(QGears::Event((down == true) ? QGears::ET_MOUSE_PRESS : QGears::ET_MOUSE_RELEASE, button, 0));
- }
- void
- InputManager::MouseMoved( int x, int y )
- {
- m_EventQueue.push_back(QGears::Event(QGears::ET_MOUSE_MOVE, x, y));
- }
- void
- InputManager::MouseScrolled( int value )
- {
- m_EventQueue.push_back(QGears::Event(QGears::ET_MOUSE_SCROLL, value, 0));
- }
- void
- InputManager::Update()
- {
- m_RepeatTimer += Timer::getSingleton().GetSystemTimeDelta();
- if( ( m_RepeatFirstWait == true && m_RepeatTimer >= 0.5 ) || ( m_RepeatFirstWait == false && m_RepeatTimer >= 0.05 ) )
- {
- for( int button = 0; button < 256; ++button )
- {
- if( m_ButtonState[ button ] == true )
- {
- m_EventQueue.push_back(QGears::Event(QGears::ET_KEY_REPEAT, button, m_ButtonText[button]));
- }
- }
- m_RepeatFirstWait = false;
- m_RepeatTimer = 0;
- }
- // send impulses
- for( int button = 0; button < 256; ++button )
- {
- if( m_ButtonState[ button ] == true )
- {
- m_EventQueue.push_back(QGears::Event(QGears::ET_KEY_IMPULSE, button, m_ButtonText[button]));
- }
- }
- }
- bool
- InputManager::IsButtonPressed( int button ) const
- {
- return m_ButtonState[ button ];
- }
- void
- InputManager::GetInputEvents( InputEventArray& input_events )
- {
- input_events = m_EventQueue;
- m_EventQueue.clear();
- }
- void
- InputManager::BindCommand( const Ogre::String& cmd, const ButtonList& buttons )
- {
- BindInfo info = BindInfo( cmd, buttons );
- m_Binds.push_back( info );
- }
- void
- InputManager::ActivateBinds( int button )
- {
- std::vector< int > binds_indexes;
- for( size_t i = 0; i < m_Binds.size(); ++i )
- {
- // if we found pressed button in this bind
- if( std::find( m_Binds[ i ].key_set.begin(), m_Binds[ i ].key_set.end(), button ) != m_Binds[ i ].key_set.end() )
- {
- size_t j = 0;
- for( ; j < m_Binds[ i ].key_set.size(); ++j )
- {
- if( IsButtonPressed( m_Binds[ i ].key_set[ j ] ) == false )
- {
- break;
- }
- }
- if( j >= m_Binds[ i ].key_set.size() )
- {
- binds_indexes.push_back( i );
- }
- }
- }
- std::vector< int > binds_to_activate;
- size_t max_size = 0;
- for( size_t i = 0; i < binds_indexes.size(); ++i )
- {
- if( max_size < m_Binds[ binds_indexes[ i ] ].key_set.size() )
- {
- max_size = m_Binds[ binds_indexes[ i ] ].key_set.size();
- binds_to_activate.clear();
- binds_to_activate.push_back( binds_indexes[ i ] );
- }
- else if( max_size == m_Binds[ binds_indexes[ i ] ].key_set.size() )
- {
- binds_to_activate.push_back( binds_indexes[ i ] );
- }
- }
- for( size_t i = 0; i < binds_to_activate.size(); ++i )
- {
- Ogre::StringVector params = StringTokenise( m_Binds[ binds_to_activate[ i ] ].cmd );
- // handle command
- ConfigCmd* cmd = ConfigCmdManager::getSingleton().Find( params[ 0 ] );
- if( cmd != NULL )
- {
- cmd->GetHandler()( params );
- }
- else
- {
- LOG_ERROR( "Can't find command \"" + params[ 0 ] + "\" in bind command \"" + m_Binds[ i ].cmd + "\"." );
- }
- }
- }
|