OMKVisBase.cpp

Go to the documentation of this file.
00001 /************************************************************************/
00002 /* This file is part of openMask(c) INRIA, CNRS, Universite de Rennes 1 */
00003 /* 1993-2002, thereinafter the Software                                 */
00004 /*                                                                      */
00005 /* The Software has been developped within the Siames Project.          */
00006 /* INRIA, the University of Rennes 1 and CNRS jointly hold intellectual */
00007 /* property rights                                                      */
00008 /*                                                                      */
00009 /* The Software has been registered with the Agence pour la Protection  */
00010 /* des Programmes (APP) under registration number                       */
00011 /* IDDN.FR.001.510008.00.S.P.2001.000.41200                             */
00012 /*                                                                      */
00013 /* This file may be distributed under the terms of the Q Public License */
00014 /* version 1.0 as defined by Trolltech AS of Norway and appearing in    */
00015 /* the file LICENSE.QPL included in the packaging of this file.         */
00016 /*                                                                      */
00017 /* Licensees holding valid specific licenses issued by INRIA, CNRS or   */
00018 /* Universite Rennes 1 for the software may use this file in            */
00019 /* acordance with that specific license                                 */
00020 /************************************************************************/
00021 #include "OMKVisBase.h"
00022 #include "OMKAnimatorEventPlug.h"
00023 #include "OMKVisualObject.h"
00024 #include "OMKAnimator.h"
00025 #include "OMKAddAnimatorPrm.h"
00026 #include "OMKAddVisualObjectPrm.h"
00027 #include "OMKValuedEvent.h"
00028 #include "OMKMultipleConfigurationParameter.h"
00029 #include "OMKVisObjectExtension.h"
00030 #include "OMKVisServiceExtension.h" 
00031 
00032 using namespace OMK ;
00033 using namespace OMK::Vis ;
00034 using namespace OMK::Type ;
00035 
00036 EventIdentifier VisBase::ADD_VISUAL_OBJECT_EVENT_ID   ( "AddVisualObjectEvent"    ) ;
00037 EventIdentifier VisBase::DELETE_VISUAL_OBJECT_EVENT_ID( "DeleteVisualObjectEvent" ) ;
00038 EventIdentifier VisBase::ADD_ANIMATOR_EVENT_ID        ( "AddAnimatorEvent"        ) ;
00039 EventIdentifier VisBase::DELETE_ANIMATOR_EVENT_ID     ( "DeleteAnimatorEvent"     ) ;
00040 
00041 //-------------------------------------------------------------------------
00042 // constructor
00043 //-------------------------------------------------------------------------
00044 VisBase::VisBase( Controller& ctrl, const ObjectDescriptor& objectDescriptor )
00045 : ExtensibleSimulatedObject( ctrl, objectDescriptor )
00046 {
00047   new VisObjectExtension( this, "visualOjectLoader" ) ;
00048   new VisServiceExtension(this, "VisServiceExtension" ) ;
00049 }
00050 
00051 //-------------------------------------------------------------------------
00052 // destructor
00053 //-------------------------------------------------------------------------
00054 VisBase::~VisBase()
00055 {
00056 }
00057 
00058 //-------------------------------------------------------------------------
00059 // finish
00060 //-------------------------------------------------------------------------
00061 void 
00062 VisBase::finish()
00063 {
00064   cancelRegistrationForSignal ( ADD_VISUAL_OBJECT_EVENT_ID );
00065   cancelRegistrationForSignal ( ADD_ANIMATOR_EVENT_ID );
00066 
00067   OMTRACEID( OMK_DEBUG_VIS_EXEC, " Destruction " <<  getName().getCString() );  
00068   for ( VisualObjectMap::iterator i = _visualObjects.begin() ;
00069       i != _visualObjects.end() ;
00070       i++ )
00071   {
00072     delete i->second ;
00073   }
00074   ExtensibleSimulatedObject::finish();
00075 }
00076 //-------------------------------------------------------------------------
00077 // init
00078 //-------------------------------------------------------------------------
00079 void VisBase::init( )
00080 {
00081   ExtensibleSimulatedObject::init ();
00082   new AddVisualObjectEventListener( *this, &VisBase::processAddVisualObjectEvent, ADD_VISUAL_OBJECT_EVENT_ID ) ;
00083   new DeleteVisualObjectEventListener( *this, &VisBase::processDeleteVisualObjectEvent, DELETE_VISUAL_OBJECT_EVENT_ID ) ;
00084   new AddAnimatorEventListener( *this, &VisBase::processAddAnimatorEvent, ADD_ANIMATOR_EVENT_ID ) ;
00085   new DeleteAnimatorEventListener( *this, &VisBase::processDeleteAnimatorEvent, DELETE_ANIMATOR_EVENT_ID ) ;
00086   registerForSignal ( ADD_VISUAL_OBJECT_EVENT_ID );
00087   registerForSignal ( DELETE_VISUAL_OBJECT_EVENT_ID );
00088   registerForSignal ( ADD_ANIMATOR_EVENT_ID );
00089   registerForSignal ( DELETE_ANIMATOR_EVENT_ID );
00090 }
00091 //-------------------------------------------------------------------------
00092 // processEvent
00093 //-------------------------------------------------------------------------
00094 bool VisBase::processEvent( Event *e ) 
00095 {
00096   OMTRACEID( OMK_DEBUG_VIS_EXEC, ">>> eventId to process \"" << e->eventId.getString()<< "\"" << std::endl ) ;
00097   // Return value
00098   bool ok = false ;
00099 
00100   // Scan all the registered events to find the good ones
00101   EventPlugMap::iterator eventPlugs =_eventIdAndPlug.find( e->eventId ) ;
00102   if( eventPlugs != _eventIdAndPlug.end() )
00103   {
00104     for( EventPlugList::iterator i = eventPlugs->second.begin() ;
00105       i != eventPlugs->second.end() ;
00106       ++i )
00107     {
00108       ok = true ;
00109       (*i)->setEvent( e ) ;
00110     }
00111   }
00112   OMTRACEID( OMK_DEBUG_VIS_EXEC, ">>> :-| End " << ( ok ? "ok" : "not ok => default processing" ) << std::endl ) ;
00113   return ok || SimulatedObject::processEvent( e ) ;
00114 }
00115 
00116 //-------------------------------------------------------------------------
00117 // processAddVisualObjectEvent
00118 //-------------------------------------------------------------------------
00119 bool VisBase::processAddVisualObjectEvent( AddVisualObjectEvent *event ) 
00120 {
00121   OMASSERTM( event->eventId == ADD_VISUAL_OBJECT_EVENT_ID, "Not the good event" ) ;
00122   std::string errorMsg ;
00123   // Get the visual object
00124   VisualObject* visualObject = 0 ;
00125   // return value
00126   bool ok = false ;
00127   OMTRACEID( OMK_DEBUG_VIS_EXEC, " Event value: " <<  event->value );  
00128   visualObject =  getVisualObject( event->value.getObjectName() );
00129   if ( visualObject )
00130   {
00131   OMMESSAGE (" VisualObject already exists : " 
00132       << visualObject->getId() << " send by " << event->sender 
00133       << " Event " << *event);
00134   }
00135   else
00136     visualObject = VisualObjectFactory::getInstance().create( event->value.getClassId() )
00137     ( *this, event->value.getObjectName(), event->value.getConfigParam()  ) ;
00138   
00139   if( visualObject )
00140   { // Add the new visual object to the visualisation
00141     addNewVisualObject( visualObject ) ;
00142     OMTRACEID( OMK_DEBUG_VIS_EXEC, ">>> New size " << _visualObjects.size() << std::endl ) ; 
00143     ok = true ;
00144 
00145     // Read the parameters for animators
00146     const ConfigurationParameterDescriptor * animatorNode = event->value.getConfigParam() ? event->value.getConfigParam()->getSubDescriptorByName( "Animator" ) : 0 ;
00147 
00148     AnimatorsToCreateMap::iterator entry = _animatorsToCreate.find( event->value.getObjectName() ) ;
00149     if( entry != _animatorsToCreate.end() )
00150     {
00151       for( std::vector< AddAnimatorPrm >::iterator prm = entry->second.begin() ;
00152            prm != entry->second.end() ;
00153            ++prm )
00154       {
00155         // sends the appropriate event to create the visual objects
00156         if( prm->getVisualObjectName() != Name() )
00157         { // A name was in arg or was found in parameters node
00158           // Send directly to the visualisation
00159           sendValuedEvent( prm->getVisualObjectName(), VisBase::ADD_ANIMATOR_EVENT_ID, *prm ) ;
00160           OMTRACEID( OMK_DEBUG_VIS_EXEC, ">>> :-) Send again the event to create the animator to \"" << prm->getVisualObjectName() << "\"" ) ;
00161         }
00162         else
00163         { // No name was found
00164           // Fire signal to any visualisation which listens 
00165           // => each will creates its own visual object
00166           fireValuedSignal( VisBase::ADD_ANIMATOR_EVENT_ID, *prm ) ;
00167           OMTRACEID( OMK_DEBUG_VIS_EXEC, ">>> :-) Fire again the signal to create the animator" ) ;
00168         }
00169       }
00170     }
00171 
00172     if( animatorNode )
00173     {
00174       int nbItems = animatorNode->getNumberOfSubItems() ;
00175       for( int i = 0 ; i < nbItems ; ++i )
00176       { 
00177         // Retrieve parameters sets for each animator
00178         std::string name = dynamic_cast<const MultipleConfigurationParameter *>( animatorNode )->getNameOfSubDescriptor( i ) ;
00179         const ConfigurationParameterDescriptor * param = animatorNode->getSubDescriptorByPosition( i ) ;
00180         OMTRACEID( OMK_DEBUG_VIS_EXEC, ">>> item #" << i << "/" << nbItems << std::endl 
00181           << ">>> " << name << std::endl ) ;
00182         ok = VisObjectExtension::loadAnimatorParameters( name, param, this, getName(), &event->value.getObjectName() ) && ok ;
00183         OMTRACEID( OMK_DEBUG_VIS_EXEC, ">>> :-) " << name << " loaded" ) ;
00184       }
00185     }
00186     else
00187     {
00188       OMTRACEID( OMK_DEBUG_VIS_EXEC, "Warning for visualisation \"" << getName().getString() << "\" (class "
00189         << getObjectDescriptor().getClass().getString() << ")" << std::endl
00190         << ">>> :-| No Animator to create !" ) ; 
00191     }
00192   }
00193   else if( errorMsg == "" )
00194   {
00195     errorMsg = "Unable to create the visual object" ;
00196   }
00197   // Unable to process event => error message
00198   if( !ok )
00199   {
00200     OMTRACE(    "Error for object \"" << getName().getString() << "\" (class " 
00201       << getObjectDescriptor().getClass().getString() << ")" << std::endl
00202       << ">>> :-( " << errorMsg << "." << std::endl ) ; 
00203   }
00204   // allow ProcessEvent by other listener 
00205  return false ;
00206 }
00207 //-------------------------------------------------------------------------
00208 // processDeleteVisualObjectEvent
00209 //-------------------------------------------------------------------------
00210 bool VisBase::processDeleteVisualObjectEvent( DeleteVisualObjectEvent *event ) 
00211 {
00212   OMASSERTM( event->eventId == DELETE_VISUAL_OBJECT_EVENT_ID, "Not the good event" ) ;
00213   deleteVisualObject( event->value ) ;
00214   // allow ProcessEvent by other listener 
00215   return false ;
00216 }
00217 
00218 //-------------------------------------------------------------------------
00219 // processAddAnimatorEvent
00220 //-------------------------------------------------------------------------
00221 bool VisBase::processAddAnimatorEvent( AddAnimatorEvent *event ) 
00222 {
00223   OMASSERTM( event->eventId == ADD_ANIMATOR_EVENT_ID, "Not the good event" ) ;
00224   std::string errorMsg ;
00225   // Get the visual object
00226   Name visualObjectName( event->value.getVisualObjectName() ) ; 
00227   VisualObject* visualObject = getVisualObject( visualObjectName ) ;
00228   Animator* animator = 0 ;
00229   // return value
00230   bool ok = false ;
00231   if( !visualObject )
00232   {
00233     AnimatorsToCreateMap::iterator entry = _animatorsToCreate.find( visualObjectName ) ;
00234     if( entry == _animatorsToCreate.end() )
00235     {
00236       _animatorsToCreate[ visualObjectName ] = std::vector< AddAnimatorPrm >() ;
00237       entry = _animatorsToCreate.find( visualObjectName ) ;
00238     }
00239     entry->second.push_back( event->value ) ;
00240     errorMsg += "Cannot find the visual object named \"" + event->value.getVisualObjectName().getString() + "\" for the moment, stores the event for later" ; 
00241   }
00242   else
00243   { // Parameters are ok => Create the animator
00244     animator = AnimatorFactory::getInstance().create( event->value.getClassId() )
00245       ( *visualObject, event->value.getObjectName(), *event->value.getConfigParam()->clone() ) ;
00246   }
00247 
00248   if( animator )
00249   { // Add the new animator to the visual object
00250     visualObject->addAnimator( animator ) ;
00251     ok = true ;
00252   }
00253   else
00254   {
00255     errorMsg += " so unable to create the animator now" ;
00256     OMTRACEID( OMK_DEBUG_VIS_EXEC, "Warning for " << debugMsg( this ) << std::endl
00257         << ">>> :-| " << errorMsg << "." << std::endl 
00258         << " Event : " << * event ) ; 
00259   }
00260   // Unable to process event => error message
00261   // allow ProcessEvent by other listener 
00262   return false ;
00263 }
00264 
00265 //-------------------------------------------------------------------------
00266 // processDeleteAniamtorEvent
00267 //-------------------------------------------------------------------------
00268 bool VisBase::processDeleteAnimatorEvent( DeleteAnimatorEvent *event )
00269 {
00270   OMASSERTM( event->eventId == DELETE_ANIMATOR_EVENT_ID, "Not the good event" ) ;
00271   const DeleteAnimatorPrm& prm = event->value.getValue() ;
00272   VisualObjectMap::iterator i = _visualObjects.find( prm.first ) ;
00273   if( i != _visualObjects.end() )
00274   {
00275     i->second->deleteAnimator( prm.second ) ;
00276   }
00277   // allow ProcessEvent by other listener 
00278   return false;
00279 }
00280 
00281 //-------------------------------------------------------------------------
00282 // registerEventPlug
00283 //-------------------------------------------------------------------------
00284 void 
00285 VisBase::registerEventPlug( const EventIdentifier& eventId, 
00286                             const std::string& emitter, 
00287                             OMK::Vis::IAnimatorEventPlug* plug ) 
00288 {
00289   // Find the list
00290   EventPlugMap::iterator eventPlugs =_eventIdAndPlug.find( eventId ) ;
00291   if( eventPlugs == _eventIdAndPlug.end() )
00292   {
00293     _eventIdAndPlug[ eventId ] = EventPlugList() ;
00294   }
00295   eventPlugs =_eventIdAndPlug.find( eventId ) ;
00296   OMASSERT( eventPlugs != _eventIdAndPlug.end() ) ;
00297   // Register the plug associated to the event in the list
00298   eventPlugs->second.push_back( plug ) ;
00299   // Register the visualisation as a listener
00300   SimulatedObject* emitterPtr = _controller.getPointerToSimulatedObjectNamed ( emitter );
00301   if( emitterPtr ) 
00302   { // The emitter exists => register for the signal form this emitter
00303     registerForSignalBy( eventId, emitterPtr, eventId ) ;
00304   }
00305   else
00306   { // The emitter doesn't exist => register for the signal from any objectDescriptor
00307     registerForSignal( eventId ) ;
00308   }
00309 }
00310 
00311 //-------------------------------------------------------------------------
00312 // removeEventPlug
00313 //-------------------------------------------------------------------------
00314 void VisBase::removeEventPlug( const EventIdentifier& eventId, OMK::Vis::IAnimatorEventPlug *plug )
00315 {
00316   // Find the list
00317   EventPlugMap::iterator eventPlugs =_eventIdAndPlug.find( eventId ) ;
00318   if( eventPlugs != _eventIdAndPlug.end() )
00319   {
00320     _eventIdAndPlug[ eventId ].remove( plug ) ;
00321   }
00322   else
00323   {
00324     OMERROR( ">>> :-( The event id \"" << eventId << "\" is not present => cannot delete the plug " << plug ) ;
00325   }
00326 }
00327 
00328 //-------------------------------------------------------------------------
00329 // addNewVisualObject
00330 //-------------------------------------------------------------------------
00331 void 
00332 VisBase::addNewVisualObject( OMK::Vis::VisualObject* visualObject )
00333 {
00334   OMASSERTM( visualObject, "Must be a valid pointer" ) ;
00335   // Add the partner in the list
00336   _visualObjects[ visualObject->getId() ] = visualObject ;
00337 }
00338 
00339 //-------------------------------------------------------------------------
00340 // deleteVisualObject
00341 //-------------------------------------------------------------------------
00342 void VisBase::deleteVisualObject( const Name& id )
00343 {
00344   VisualObjectMap::iterator i = _visualObjects.find( id ) ;
00345   OMASSERTM( i != _visualObjects.end(), "The visual object named \"" << id << "\" is not present !" ) ;
00346   // delete the visual object
00347   delete i->second ;
00348   _visualObjects.erase( i ) ;
00349   OMTRACEID( OMK_DEBUG_VIS_EXEC, ">>> :-) The visual object named \"" << id << "\" is deleted" ) ;
00350 }
00351 
00352 //-------------------------------------------------------------------------
00353 // getVisualObject
00354 //-------------------------------------------------------------------------
00355 VisualObject* 
00356 VisBase::getVisualObject( const Name& id ) const 
00357 {
00358   VisualObjectMap::const_iterator i = _visualObjects.find( id ) ;
00359   return i != _visualObjects.end() ? i->second : 0 ;
00360 }
00361 
00362 //-------------------------------------------------------------------------
00363 // compute
00364 //-------------------------------------------------------------------------
00365 void 
00366 VisBase::computeParameters() 
00367 {
00368   for ( VisualObjectMap::iterator i = _visualObjects.begin() ;
00369     i != _visualObjects.end() ;
00370     ++i )
00371   {
00372     i->second->update() ;
00373   }
00374 }
00375 

logo OpenMask

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

Generated with doxygen by Dimitri van Heesch ,   1997-2007