00001
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
00010
00011
00012 template<unsigned int SIZE>
00013 class PlayMotionSequenceNode : public StateNode {
00014 public:
00015
00016 PlayMotionSequenceNode()
00017 : StateNode("PlayMotionSequenceNode"), msid(MotionManager::invalid_MC_ID), msidIsMine(false), looping(false), filename()
00018 {}
00019
00020
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
00031 updateMS(filename);
00032 erouter->addListener(this, EventBase::motmanEGID);
00033 StateNode::DoStart();
00034 }
00035
00036 virtual void DoStop() {
00037
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
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 virtual void setFile(const std::string& file) {
00065 if(isActive())
00066 updateMS(file);
00067 else
00068 filename=file;
00069 }
00070
00071
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,(unsigned int)this,EventBase::statusETID,0);
00082 }
00083 }
00084
00085
00086 virtual bool getLooping() { return looping; }
00087
00088
00089 virtual MotionManager::MC_ID getMSid() { return msid; }
00090
00091
00092 virtual MMAccessor<MotionSequenceMC<SIZE> > getMSAccessor() { return MMAccessor<MotionSequenceMC<SIZE> >(msid); }
00093
00094
00095 virtual bool ownsMSid() { return msidIsMine; }
00096
00097 protected:
00098
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;
00113 bool msidIsMine;
00114 bool looping;
00115 std::string filename;
00116 };
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129 #endif