Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

MotionSequenceNode.h

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

Tekkotsu v4.0
Generated Thu Nov 22 00:54:54 2007 by Doxygen 1.5.4