Homepage Demos Overview Downloads Tutorials Reference
Credits

PlayMotionSequenceNode.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_PlayMotionSequenceNode_h_
00003 #define INCLUDED_PlayMotionSequenceNode_h_
00004 
00005 #include "Behaviors/StateNode.h"
00006 #include "Motion/MotionManager.h"
00007 #include "Motion/MotionSequenceMC.h"
00008 
00009 //! A StateNode for playing a MotionSequence (and looping it if desired)
00010 /*! Eventually, i'd like to just build the looping functionality into
00011  *  MotionSequence, but in the mean time we have this. */
00012 template<unsigned int SIZE>
00013 class PlayMotionSequenceNode : public StateNode {
00014 public:
00015   //!constructor
00016   PlayMotionSequenceNode()
00017     : StateNode("PlayMotionSequenceNode","PlayMotionSequenceNode"), msid(MotionManager::invalid_MC_ID), msidIsMine(false), looping(false), filename()
00018   {}
00019 
00020   //!constructor
00021   PlayMotionSequenceNode(const std::string& nm, StateNode* par, const std::string& file, bool loop=false)
00022     : StateNode("PlayMotionSequenceNode",nm,par), msid(MotionManager::invalid_MC_ID), msidIsMine(false), looping(false), filename(file)
00023   {
00024     setLooping(loop);
00025   }
00026 
00027   virtual void setup() {}
00028 
00029   virtual void DoStart() {
00030     //std::cout << "PlayMotionSequenceNode::DoStart(); " << std::endl;
00031     updateMS(filename);
00032     erouter->addListener(this, EventBase::motmanEGID);//, msid, EventBase::deactivateETID);
00033     StateNode::DoStart();
00034   }
00035 
00036   virtual void DoStop() {
00037     //std::cout << "PlayMotionSequenceNode::DoStop(); " << std::endl;
00038     erouter->removeListener(this);
00039     motman->removeMotion(msid);
00040     msid=MotionManager::invalid_MC_ID;
00041     StateNode::DoStop();
00042   }
00043 
00044   virtual void teardown() {
00045     if(msidIsMine) {
00046       motman->removeMotion(msid);
00047       msid=MotionManager::invalid_MC_ID;
00048     }
00049   }
00050 
00051   /* not ready yet
00052   // ! use this to force the PlayMotionSequenceNode to use a shared MS - set to MotionManager::invalid_MC_ID to reset to internally generated MS
00053   virtual void setMSid(MotionManager::MC_ID id) {
00054   if(msidIsMine) {
00055   motman->removeMotion(msid);
00056   msid=MotionManager::invalid_MC_ID;
00057   }
00058   msid=id;
00059   msidIsMine=(id==MotionManager::invalid_MC_ID);
00060   }
00061   */
00062 
00063   //! sets the file to play
00064   virtual void setFile(const std::string& file) {
00065     if(isActive())
00066       updateMS(file);
00067     else
00068       filename=file;
00069   }
00070 
00071   //! turns looping on or off
00072   virtual void setLooping(bool loop) { looping=loop; }
00073 
00074   virtual void processEvent(const EventBase& e) {
00075     ASSERTRET(e.getGeneratorID()==EventBase::motmanEGID,"Unknown event");
00076     if(e==EventBase(EventBase::motmanEGID,msid,EventBase::deactivateETID)) {
00077       msid=MotionManager::invalid_MC_ID;
00078       if(looping) {
00079         updateMS(filename);
00080       }
00081       erouter->postEvent(EventBase::stateMachineEGID,reinterpret_cast<unsigned int>(this),EventBase::statusETID,0,getName(),1);
00082     }
00083   }
00084 
00085   //! returns true if currently looping
00086   virtual bool getLooping() { return looping; }
00087 
00088   //! use this to access the MS that the PlayMotionSequenceNode is using
00089   virtual MotionManager::MC_ID getMSid() { return msid; }
00090 
00091   //! use this to access the MS that the PlayMotionSequenceNode is using
00092   virtual MMAccessor<MotionSequenceMC<SIZE> > getMSAccessor() { return MMAccessor<MotionSequenceMC<SIZE> >(msid); }
00093 
00094   //! returns true if #msid was created (and will be destroyed) by this PlayMotionSequenceNode - false if assigned by setMsid()
00095   virtual bool ownsMSid() { return msidIsMine; }
00096 
00097 protected:
00098   //! resets the motion command and starts it playing
00099   void updateMS(const std::string& file) {
00100     if(msid==MotionManager::invalid_MC_ID) {
00101       msid=motman->addPrunableMotion(SharedObject<MotionSequenceMC<SIZE> >(file.c_str()));
00102       msidIsMine=true;
00103     } else {
00104       MMAccessor<MotionSequenceMC<SIZE> > ms(msid);
00105       ms->clear();
00106       ms->LoadFile(file.c_str());
00107       ms->setPlayTime(1);
00108     }
00109     filename=file;
00110   }
00111 
00112   MotionManager::MC_ID msid; //!< id of the motion command
00113   bool msidIsMine; //!< true if this class created the current motion command (and therefore should delete it when done)
00114   bool looping; //!< true if we should loop
00115   std::string filename; //!< filename of current motion sequence
00116 };
00117 
00118 /*! @file
00119  * @brief Describes PlayMotionSequenceNode, a StateNode for playing a MotionSequence (and looping it if desired)
00120  * @author ejt (Creator)
00121  *
00122  * $Author: ejt $
00123  * $Name: tekkotsu-2_2_1 $
00124  * $Revision: 1.9 $
00125  * $State: Exp $
00126  * $Date: 2004/11/11 21:32:14 $
00127  */
00128 
00129 #endif

Tekkotsu v2.2.1
Generated Tue Nov 23 16:36:39 2004 by Doxygen 1.3.9.1