Panda3D

cInterval.h

00001 // Filename: cInterval.h
00002 // Created by:  drose (27Aug02)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #ifndef CINTERVAL_H
00016 #define CINTERVAL_H
00017 
00018 #include "directbase.h"
00019 #include "typedReferenceCount.h"
00020 #include "pvector.h"
00021 #include "config_interval.h"
00022 #include "pStatCollector.h"
00023 
00024 class CIntervalManager;
00025 
00026 ////////////////////////////////////////////////////////////////////
00027 //       Class : CInterval
00028 // Description : The base class for timeline components.  A CInterval
00029 //               represents a single action, event, or collection of
00030 //               nested intervals that will be performed at some
00031 //               specific time or over a period of time.
00032 //
00033 //               This is essentially similar to the Python "Interval"
00034 //               class, but it is implemented in C++ (hence the name).
00035 //               Intervals that may be implemented in C++ will inherit
00036 //               from this class; Intervals that must be implemented
00037 //               in Python will inherit from the similar Python class.
00038 ////////////////////////////////////////////////////////////////////
00039 class EXPCL_DIRECT CInterval : public TypedReferenceCount {
00040 public:
00041   CInterval(const string &name, double duration, bool open_ended);
00042   virtual ~CInterval();
00043 
00044 PUBLISHED:
00045   INLINE const string &get_name() const;
00046   INLINE double get_duration() const;
00047   INLINE bool get_open_ended() const;
00048 
00049   enum EventType {
00050     ET_initialize,
00051     ET_instant,
00052     ET_step,
00053     ET_finalize,
00054     ET_reverse_initialize,
00055     ET_reverse_instant,
00056     ET_reverse_finalize,
00057     ET_interrupt
00058   };
00059 
00060   enum State {
00061     S_initial,
00062     S_started,
00063     S_paused,
00064     S_final
00065   };
00066 
00067   INLINE State get_state() const;
00068   INLINE bool is_stopped() const;
00069 
00070   INLINE void set_done_event(const string &event);
00071   INLINE const string &get_done_event() const;
00072 
00073   void set_t(double t);
00074   INLINE double get_t() const;
00075 
00076   INLINE void set_auto_pause(bool auto_pause);
00077   INLINE bool get_auto_pause() const;
00078   INLINE void set_auto_finish(bool auto_finish);
00079   INLINE bool get_auto_finish() const;
00080 
00081   INLINE void set_wants_t_callback(bool wants_t_callback);
00082   INLINE bool get_wants_t_callback() const;
00083 
00084   INLINE void set_manager(CIntervalManager *manager);
00085   INLINE CIntervalManager *get_manager() const;
00086 
00087   void start(double start_t = 0.0, double end_t = -1.0, double play_rate = 1.0);
00088   void loop(double start_t = 0.0, double end_t = -1.0, double play_rate = 1.0);
00089   double pause();
00090   void resume();
00091   void resume(double start_t);
00092   void resume_until(double end_t);
00093   void finish();
00094   void clear_to_initial();
00095   bool is_playing() const;
00096 
00097   double get_play_rate() const;
00098   void set_play_rate(double play_rate);
00099 
00100   // These functions control the actual playback of the interval.
00101   // Don't call them directly; they're intended to be called from a
00102   // supervising object, e.g. the Python start() .. finish()
00103   // interface.
00104 
00105   // These cannot be declared private because they must be accessible
00106   // to Python, but the method names are prefixed with priv_ to remind
00107   // you that you probably don't want to be using them directly.
00108   void priv_do_event(double t, EventType event);
00109   virtual void priv_initialize(double t);
00110   virtual void priv_instant();
00111   virtual void priv_step(double t);
00112   virtual void priv_finalize();
00113   virtual void priv_reverse_initialize(double t);
00114   virtual void priv_reverse_instant();
00115   virtual void priv_reverse_finalize();
00116   virtual void priv_interrupt();
00117 
00118   virtual void output(ostream &out) const;
00119   virtual void write(ostream &out, int indent_level) const;
00120 
00121   void setup_play(double start_time, double end_time, double play_rate,
00122                   bool do_loop);
00123   void setup_resume();
00124   void setup_resume_until(double end_t);
00125   bool step_play();
00126 
00127 public:
00128   void mark_dirty();
00129   INLINE bool check_t_callback();
00130 
00131 protected:
00132   void interval_done();
00133 
00134   INLINE void recompute() const;
00135   virtual void do_recompute();
00136   INLINE void check_stopped(TypeHandle type, const char *method_name) const;
00137   INLINE void check_started(TypeHandle type, const char *method_name) const;
00138 
00139   State _state;
00140   double _curr_t;
00141   string _name;
00142   string _pname;
00143   string _done_event;
00144   double _duration;
00145 
00146   bool _auto_pause;
00147   bool _auto_finish;
00148   bool _wants_t_callback;
00149   double _last_t_callback;
00150   CIntervalManager *_manager;
00151 
00152   // For setup_play() and step_play().
00153   double _clock_start;
00154   double _start_t;
00155   double _end_t;
00156   bool _end_t_at_end;
00157   bool _start_t_at_start;
00158   double _play_rate;
00159   bool _do_loop;
00160   int _loop_count;
00161   
00162 private:
00163   bool _open_ended;
00164   bool _dirty;
00165 
00166   // We keep a record of the "parent" intervals (that is, any
00167   // CMetaInterval objects that keep a pointer to this one) strictly
00168   // so we can mark all of our parents dirty when this interval gets
00169   // dirty.
00170   typedef pvector<CInterval *> Parents;
00171   Parents _parents;
00172 
00173   static PStatCollector _root_pcollector;
00174   PStatCollector _ival_pcollector;
00175   
00176 public:
00177   static TypeHandle get_class_type() {
00178     return _type_handle;
00179   }
00180   static void init_type() {
00181     TypedReferenceCount::init_type();
00182     register_type(_type_handle, "CInterval",
00183                   TypedReferenceCount::get_class_type());
00184   }
00185   virtual TypeHandle get_type() const {
00186     return get_class_type();
00187   }
00188   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00189 
00190 private:
00191   static TypeHandle _type_handle;
00192 
00193   friend class CMetaInterval;
00194 };
00195 
00196 INLINE ostream &operator << (ostream &out, const CInterval &ival);
00197 EXPCL_DIRECT ostream &operator << (ostream &out, CInterval::State state);
00198 
00199 #include "cInterval.I"
00200 
00201 #endif
00202 
00203 
00204 
 All Classes Functions Variables Enumerations