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"), 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(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->forgetListener(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     }
00082   }
00083 
00084   //! returns true if currently looping
00085   virtual bool getLooping() { return looping; }
00086 
00087   //! use this to access the MS that the PlayMotionSequenceNode is using
00088   virtual MotionManager::MC_ID getMSid() { return msid; }
00089 
00090   //! use this to access the MS that the PlayMotionSequenceNode is using
00091   virtual MMAccessor<MotionSequenceMC<SIZE> > getMSAccessor() { return MMAccessor<MotionSequenceMC<SIZE> >(msid); }
00092 
00093   //! returns true if #msid was created (and will be destroyed) by this PlayMotionSequenceNode - false if assigned by setMsid()
00094   virtual bool ownsMSid() { return msidIsMine; }
00095 
00096 protected:
00097   //! resets the motion command and starts it playing
00098   void updateMS(const std::string& file) {
00099     if(msid==MotionManager::invalid_MC_ID) {
00100       msid=motman->addMotion(SharedObject<MotionSequenceMC<SIZE> >(file.c_str()));
00101       msidIsMine=true;
00102     } else {
00103       MMAccessor<MotionSequenceMC<SIZE> > ms(msid);
00104       ms->clear();
00105       ms->LoadFile(file.c_str());
00106       ms->setPlayTime(1);
00107     }
00108     filename=file;
00109   }
00110 
00111   MotionManager::MC_ID msid; //!< id of the motion command
00112   bool msidIsMine; //!< true if this class created the current motion command (and therefore should delete it when done)
00113   bool looping; //!< true if we should loop
00114   std::string filename; //!< filename of current motion sequence
00115 };
00116 
00117 /*! @file
00118  * @brief Describes PlayMotionSequenceNode, a StateNode for playing a MotionSequence (and looping it if desired)
00119  * @author ejt (Creator)
00120  *
00121  * $Author: ejt $
00122  * $Name: tekkotsu-2_1 $
00123  * $Revision: 1.2 $
00124  * $State: Exp $
00125  * $Date: 2004/01/18 10:16:57 $
00126  */
00127 
00128 #endif

Tekkotsu v2.1
Generated Tue Mar 16 23:19:14 2004 by Doxygen 1.3.5