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

Tekkotsu v2.4.1
Generated Tue Aug 16 16:32:48 2005 by Doxygen 1.4.4