Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

Transition.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_Transition_h_
00003 #define INCLUDED_Transition_h_
00004 
00005 #include "BehaviorBase.h"
00006 #include <vector>
00007 #include <list>
00008 
00009 class StateNode;
00010 
00011 //! Represents a transition between StateNodes.
00012 /*! This is an abstract class - you'll want to subclass it to put
00013  *  conditions on the transitions.  Transitions are "smart" - they are
00014  *  behaviors in and of themselves and can listen for events and use
00015  *  the standard doStart/doStop interface.  Based on the events they
00016  *  receive, they can fire() themselves and cause a state
00017  *  transition.
00018  *
00019  *  doStart() will be called when this transition is 'active' - it
00020  *  should listen/monitor for the transition it represents until
00021  *  doStop() is called.
00022  *  
00023  *  If the conditions are satisified for a transition, you should call
00024  *  fire() to do the appropriate notifications.
00025  *
00026  *  Also note that a transition can have multiple sources and
00027  *  destinations.  See fire().
00028  *
00029  *  When setting up, the flow of additions follows the flow of
00030  *  control.  In other words, you add a transition to a source, and
00031  *  you add a destination to a transition.  This makes the code
00032  *  simpler because it doesn't have to worry about recursive looping
00033  *  depending whether the source was added to the transition or the
00034  *  transition was added to the source.  Confusing?  Exactly.
00035  *
00036  *  A template file is available at <a href="http://cvs.tekkotsu.org/cgi/viewcvs.cgi/Tekkotsu/project/templates/transition.h?rev=HEAD&content-type=text/vnd.viewcvs-markup"><i>project</i><tt>/templates/transition.h</tt></a>,
00037  *  which will help you get moving faster.
00038  */
00039 
00040 class Transition : public BehaviorBase {
00041   friend class StateNode;
00042 public:
00043   //!destructor
00044   virtual ~Transition();
00045   
00046   //! Call this when the transition should be made, will forward on to doFire()
00047   virtual void fire();
00048   
00049   //! Call this to fire with an event to be stored into the destinations' 'event' member while their start() is called
00050   virtual void fire(const EventBase& ev);
00051   
00052   virtual std::vector<StateNode*>& getSources() { return srcs; }  //!< returns a user-modifiable reference to the current source list
00053   virtual const std::vector<StateNode*>& getSources() const { return srcs; } //!< returns a const reference to the current source list
00054 
00055   virtual void addDestination(StateNode* destination) { if(destination!=NULL) dsts.push_back(destination); } //!< if @a destination is non-null, add it to the destination list
00056   virtual std::vector<StateNode*>& getDestinations() { return dsts; } //!< returns a user-modifiable reference to the current destination list
00057   virtual const std::vector<StateNode*>& getDestinations() const { return dsts; } //!< returns a const reference to the current destination list
00058 
00059   virtual void setSound(const std::string& snd) { soundFile=snd; } //!< set a sound file to be played upon activation; you might want to preload this in the parent node; empty string to turn off
00060   virtual std::string getSound() { return soundFile; } //!< returns the current sound file
00061   
00062   virtual void setSpeechText(const std::string& text) { speechText=text; } //!< set the text to be spoken upon activation
00063 
00064   //! If #instanceName == #className, will autogenerate a name incorporating source and destination names
00065   virtual std::string getName() const;
00066 
00067   static void showFiring();
00068 
00069 protected:
00070   //! cancel buffered firing if deactivated (perhaps by transition which fired from earlier in the queue
00071   virtual void stop();
00072   
00073   //! performs firing, stopping sources and activating destination
00074   virtual void doFire();
00075 
00076   //!List of currently active transitions. Hopefully prevents re-entrant issues.
00077   static std::list<Transition*> firing;
00078   
00079   void fireDownCallStack();
00080   
00081   //!constructor, specify destination StateNode (ignores NULL)
00082   Transition(StateNode* destination) : BehaviorBase(), srcs(), dsts(), soundFile(), speechText(), eventData(NULL) {
00083     addDestination(destination);
00084   }
00085   //!constructor, pass your subclass type name as a string for the default name, and a separate instance name
00086   Transition(const std::string& instancename) : BehaviorBase(instancename), srcs(), dsts(), soundFile(), speechText(), eventData(NULL) {}
00087   //!constructor, specify names and destination StateNode (ignores NULL)
00088   Transition(const std::string& instancename, StateNode* destination) : BehaviorBase(instancename), srcs(), dsts(), soundFile(), speechText(), eventData(NULL) {
00089     addDestination(destination);
00090   }
00091   //!copy constructor, just in case you need it
00092   Transition(const Transition& t) : BehaviorBase(t), srcs(t.srcs), dsts(t.dsts), soundFile(t.soundFile), speechText(t.speechText), eventData(NULL) {}
00093 
00094   //!assignment operator (only does shallow copy)
00095   Transition& operator=(const Transition& t) { BehaviorBase::operator=(t); srcs=t.srcs; dsts=t.dsts; 
00096     soundFile=t.soundFile; speechText=t.speechText; return *this; }
00097   
00098   //! if @a source is non-null, add it to the source list
00099   /*! Only StateNodes should be calling this - you add a transition to a source, not a source to a transition.
00100    *  @see StateNode::addTransition() */
00101   virtual void addSource(StateNode* source) { if(source!=NULL) srcs.push_back(source); } 
00102 
00103   std::vector<StateNode*> srcs; //!< the node being transitioned from
00104   std::vector<StateNode*> dsts; //!< the node being transitioned to
00105   std::string soundFile;        //!< sound to play on transitioning
00106   std::string speechText;       //!< text to speak on transitioning
00107   const EventBase* eventData;
00108 };
00109 
00110 /*! @file
00111  * @brief Describes Transition, represents a transition between StateNodes.
00112  * @author ejt (Creator)
00113  */
00114 
00115 #endif

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:52 2016 by Doxygen 1.6.3