OMKDistributedCameraListener.cpp

Go to the documentation of this file.
00001 #include "OMKDistributedCameraListener.h"
00002 #include "OMKSystemEventIdentifier.h"
00003 #include "OMKOgreVis.h"
00004 #include "OMKTransformType.h"
00005 
00006 using namespace OMK ;
00007 using namespace OMK::Inp ;
00008 using namespace OMK::Vis ;
00009 using namespace OMK::Type ;
00010 using namespace Ogre ;
00011 using namespace Wm4 ;
00012 using namespace OIS ;
00013 
00014 REGISTER_OBJECT_FACTORY( DistributedCameraListener, "DistributedCameraListener" ) ;
00015 
00016 //-------------------------------------------------------------------------
00017 // constructor
00018 //-------------------------------------------------------------------------
00019 DistributedCameraListener::DistributedCameraListener
00020 ( OMK::Controller& ctrl, const ObjectDescriptor& objectDescriptor )
00021 : 
00022 SimulatedObject( ctrl, objectDescriptor ),
00023 _ogreVis( NULL ),
00024 _updateTransform( false ),
00025 _translationSpeed( 1.0f ),
00026 _wheelTranslationSpeed( 1.0f ),
00027 _rotationSpeed( 0.001f ),
00028 _inputManager( NULL ),
00029 _mouse( NULL ),
00030 _keyboard( NULL )
00031 { 
00032 }
00033 
00034 //-------------------------------------------------------------------------
00035 // destructor
00036 //-------------------------------------------------------------------------
00037 DistributedCameraListener::~DistributedCameraListener( void ) 
00038 {
00039 }
00040 
00041 //-------------------------------------------------------------------------
00042 // init
00043 //-------------------------------------------------------------------------
00044 void 
00045 DistributedCameraListener::init()
00046 {
00047   readConfigurationParameters() ;
00048 
00049   ParamList pl ;
00050   size_t windowHnd( 0 ) ;
00051   std::ostringstream windowHndStr ;
00052 
00053 #if defined WIN32
00054   // Uncomment these two lines to allow users to switch keyboards via the language bar
00055   pl.insert(std::make_pair( std::string( "w32_keyboard" ), std::string( "DISCL_FOREGROUND" ) ) );
00056   pl.insert(std::make_pair( std::string( "w32_keyboard" ), std::string( "DISCL_NONEXCLUSIVE" ) ) );
00057 
00058 #elif defined OIS_LINUX_PLATFORM
00059   pl.insert( std::make_pair( std::string( "XAutoRepeatOn" ), std::string( "true") ) );    
00060   pl.insert( std::make_pair( std::string( "x11_mouse_hide" ), std::string( "false" ) ) );    
00061   pl.insert( std::make_pair( std::string( "x11_mouse_grab" ), std::string( "false" ) ) );    
00062   pl.insert( std::make_pair( std::string( "x11_keyboard_grab" ), std::string( "false" ) ) );    
00063 #endif
00064 
00065   _ogreVis->touchRoot().getAutoCreatedWindow()->getCustomAttribute( "WINDOW", &windowHnd ) ;
00066   windowHndStr << windowHnd ;
00067   pl.insert( std::make_pair( std::string( "WINDOW" ), windowHndStr.str() ) ) ;
00068 
00069   _inputManager = InputManager::createInputSystem( pl ) ;
00070 
00071   //create devices
00072   _keyboard = static_cast<Keyboard*>( _inputManager->createInputObject( OISKeyboard, false ) ) ;
00073   _mouse = static_cast<Mouse*>( _inputManager->createInputObject( OISMouse, false ) ) ;
00074 
00075   //Set initial mouse clipping size
00076   windowResized( _ogreVis->touchRoot().getAutoCreatedWindow() );
00077 
00078   //Register as a Window listener
00079   WindowEventUtilities::addWindowEventListener( _ogreVis->touchRoot().getAutoCreatedWindow(), this ) ;
00080 
00081   //register as a frame listener
00082   _ogreVis->touchRoot().addFrameListener( this ) ;
00083 }
00084 
00085 //-------------------------------------------------------------------------
00086 // finish
00087 //-------------------------------------------------------------------------
00088 void 
00089 DistributedCameraListener::finish()
00090 {
00091   //Remove ourself as a Window listener
00092   WindowEventUtilities::removeWindowEventListener( _ogreVis->touchRoot().getAutoCreatedWindow(), this ) ;
00093   windowClosed( _ogreVis->touchRoot().getAutoCreatedWindow() ) ;
00094 }
00095 
00096 //-------------------------------------------------------------------------
00097 // compute
00098 //-------------------------------------------------------------------------
00099 void 
00100 DistributedCameraListener::compute() 
00101 {
00102   if ( _updateTransform == true )
00103   {
00104    // std::cerr << "DistributedCameraListener::compute() Signal" << std::endl ;
00105     fireValuedSignal< TransformType >( "moveCamera", _transform ) ;
00106     _updateTransform = false ;
00107   }
00108 }
00109 
00110 //-------------------------------------------------------------------------
00111 // readConfigurationParameters
00112 //-------------------------------------------------------------------------
00113 void
00114 DistributedCameraListener::readConfigurationParameters()
00115 {
00116   const ConfigurationParameterDescriptor* node( getConfigurationParameters() ) ;
00117 
00118   // Retrieve the VisuName parameter
00119   std::string visuName ;
00120   ParametersAccessor::get( node, "VisuName", visuName ) ;
00121   OMASSERT( ( visuName.empty() == false ) && "The VisuName configuration parameter must be declared" ) ;
00122   SimulatedObject* vis( getController().getPointerToSimulatedObjectNamed( visuName ) ) ;
00123   OMASSERT( ( vis != NULL ) && "The VisuName configuration parameter is not a SimulatedObject" ) ;
00124   _ogreVis = dynamic_cast<OgreVis*>( vis ) ;
00125   OMASSERT( ( _ogreVis != NULL ) && "The VisuName configuration parameter is not an OgreVis" ) ;
00126   OMASSERT( ( _ogreVis->getRoot().isInitialised() == true ) && "The OgreVis 'VisuName' is incorrectly initialised" ) ;
00127 }
00128 
00129 //-------------------------------------------------------------------------
00130 // windowResized
00131 //-------------------------------------------------------------------------
00132 void 
00133 DistributedCameraListener::windowResized( RenderWindow* rw )
00134 {
00135   unsigned int width( 0 ) ;
00136   unsigned int height( 0 ) ;
00137   unsigned int depth( 0 ) ;
00138   int left( 0 ) ;
00139   int top( 0 ) ;
00140   rw->getMetrics( width,  height, depth, left, top ) ;
00141   _mouse->getMouseState().width = width ;
00142   _mouse->getMouseState().height = height ;
00143 }
00144 
00145 //-------------------------------------------------------------------------
00146 // windowClosed
00147 //-------------------------------------------------------------------------
00148 void 
00149 DistributedCameraListener::windowClosed( RenderWindow* rw )
00150 {
00151   //Only close for window that created OIS (the main window)
00152   if( rw == _ogreVis->touchRoot().getAutoCreatedWindow() )
00153   {
00154     if( _inputManager != NULL )
00155     {
00156       _inputManager->destroyInputObject( _mouse ) ;
00157       _inputManager->destroyInputObject( _keyboard ) ;
00158       OIS::InputManager::destroyInputSystem( _inputManager ) ;
00159       _inputManager = NULL ;
00160     }
00161     sendEvent( getController().getName(), SystemEventIdentifier::MaskStop ) ;
00162   }
00163 }
00164 
00165 //-------------------------------------------------------------------------
00166 // processUnbufferedKeyInput
00167 //-------------------------------------------------------------------------
00168 bool 
00169 DistributedCameraListener::processUnbufferedKeyInput( const FrameEvent& evt )
00170 {
00171   // quit
00172   if( _keyboard->isKeyDown( KC_ESCAPE ) )
00173   {
00174     sendEvent( getController().getName(), SystemEventIdentifier::MaskStop ) ;
00175   }
00176 
00177   // forward
00178   if ( _keyboard->isKeyDown( KC_UP ) )
00179   {
00180     _transform.setTranslate( _transform.getTranslate() - ( _transform.getDirection() * _translationSpeed ) ) ;
00181     _updateTransform = true ;
00182   }
00183 
00184   // backward
00185   if ( _keyboard->isKeyDown( KC_DOWN ) )
00186   {
00187     _transform.setTranslate( _transform.getTranslate() + ( _transform.getDirection() * _translationSpeed ) ) ;
00188     _updateTransform = true ;
00189   }
00190 
00191   // slide left
00192   if ( _keyboard->isKeyDown( KC_LEFT ) )
00193   {
00194     _transform.setTranslate( _transform.getTranslate() - ( _transform.getRight() * _translationSpeed ) ) ;
00195     _updateTransform = true ;
00196   }
00197 
00198   // slide right
00199   if ( _keyboard->isKeyDown( KC_RIGHT ) )
00200   {
00201     _transform.setTranslate( _transform.getTranslate() + ( _transform.getRight() * _translationSpeed ) ) ;
00202     _updateTransform = true ;
00203   }
00204 
00205   // increase translation speed
00206   if ( _keyboard->isKeyDown( KC_ADD ) )
00207   {
00208     _translationSpeed += 1;
00209   }
00210 
00211   // decrease translation speed
00212   if ( _keyboard->isKeyDown( KC_SUBTRACT ) )
00213   {
00214     _translationSpeed -= ( 0 < _translationSpeed - 1 ) ? 1 : 0 ;
00215   }
00216 
00217   return true ;
00218 }
00219 
00220 //-------------------------------------------------------------------------
00221 // processUnbufferedMouseInput
00222 //-------------------------------------------------------------------------
00223 bool 
00224 DistributedCameraListener::processUnbufferedMouseInput( const FrameEvent& evt )
00225 {
00226   if( _mouse->getMouseState().buttonDown( OIS::MB_Left ) ) 
00227   {
00228     float dx( _mouse->getMouseState().X.rel * _rotationSpeed ) ;
00229     float dy( _mouse->getMouseState().Y.rel * _rotationSpeed ) ;
00230 
00231     if ( ( dx != 0.f ) || ( dy != 0.f ) )
00232     {
00233       Matrix3f rotation ;
00234       // yaw (fixed to the world axis)
00235       rotation.FromAxisAngle( Vector3f::UNIT_Y, -dx ) ;
00236       _transform.setRotate( rotation * _transform.getRotate() ) ;
00237       // pitch
00238       rotation.FromAxisAngle( _transform.getRotate() * Vector3f::UNIT_X, -dy ) ;
00239       _transform.setRotate( rotation * _transform.getRotate() ) ;
00240 
00241       _updateTransform = true ;
00242     }
00243   }
00244 
00245   return true ;
00246 }
00247 
00248 //-------------------------------------------------------------------------
00249 // frameStarted
00250 //-------------------------------------------------------------------------
00251 bool 
00252 DistributedCameraListener::frameStarted( const FrameEvent& evt )
00253 {
00254   bool returnCode( _ogreVis->touchRoot().getAutoCreatedWindow()->isClosed() ) ;
00255   if ( returnCode == false )
00256   {
00257     //Need to capture/update each device
00258     _keyboard->capture() ;
00259     _mouse->capture() ;
00260 
00261     returnCode = processUnbufferedKeyInput( evt ) ;
00262     if ( returnCode == true )
00263     {
00264       returnCode = processUnbufferedMouseInput( evt ) ;
00265     }
00266   }
00267   return returnCode ;
00268 }

logo OpenMask

Documentation generated on Mon Jun 9 11:45:56 2008

Generated with doxygen by Dimitri van Heesch ,   1997-2007