Panda3D
cInterval.h
1 // Filename: cInterval.h
2 // Created by: drose (27Aug02)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef CINTERVAL_H
16 #define CINTERVAL_H
17 
18 #include "directbase.h"
19 #include "typedReferenceCount.h"
20 #include "pvector.h"
21 #include "config_interval.h"
22 #include "pStatCollector.h"
23 
24 class CIntervalManager;
25 
26 ////////////////////////////////////////////////////////////////////
27 // Class : CInterval
28 // Description : The base class for timeline components. A CInterval
29 // represents a single action, event, or collection of
30 // nested intervals that will be performed at some
31 // specific time or over a period of time.
32 //
33 // This is essentially similar to the Python "Interval"
34 // class, but it is implemented in C++ (hence the name).
35 // Intervals that may be implemented in C++ will inherit
36 // from this class; Intervals that must be implemented
37 // in Python will inherit from the similar Python class.
38 ////////////////////////////////////////////////////////////////////
39 class EXPCL_DIRECT CInterval : public TypedReferenceCount {
40 public:
41  CInterval(const string &name, double duration, bool open_ended);
42  virtual ~CInterval();
43 
44 PUBLISHED:
45  INLINE const string &get_name() const;
46  INLINE double get_duration() const;
47  INLINE bool get_open_ended() const;
48 
49  enum EventType {
50  ET_initialize,
51  ET_instant,
52  ET_step,
53  ET_finalize,
54  ET_reverse_initialize,
55  ET_reverse_instant,
56  ET_reverse_finalize,
57  ET_interrupt
58  };
59 
60  enum State {
61  S_initial,
62  S_started,
63  S_paused,
64  S_final
65  };
66 
67  INLINE State get_state() const;
68  INLINE bool is_stopped() const;
69 
70  INLINE void set_done_event(const string &event);
71  INLINE const string &get_done_event() const;
72 
73  void set_t(double t);
74  INLINE double get_t() const;
75 
76  INLINE void set_auto_pause(bool auto_pause);
77  INLINE bool get_auto_pause() const;
78  INLINE void set_auto_finish(bool auto_finish);
79  INLINE bool get_auto_finish() const;
80 
81  INLINE void set_wants_t_callback(bool wants_t_callback);
82  INLINE bool get_wants_t_callback() const;
83 
84  INLINE void set_manager(CIntervalManager *manager);
85  INLINE CIntervalManager *get_manager() const;
86 
87  void start(double start_t = 0.0, double end_t = -1.0, double play_rate = 1.0);
88  void loop(double start_t = 0.0, double end_t = -1.0, double play_rate = 1.0);
89  double pause();
90  void resume();
91  void resume(double start_t);
92  void resume_until(double end_t);
93  void finish();
94  void clear_to_initial();
95  bool is_playing() const;
96 
97  double get_play_rate() const;
98  void set_play_rate(double play_rate);
99 
100  // These functions control the actual playback of the interval.
101  // Don't call them directly; they're intended to be called from a
102  // supervising object, e.g. the Python start() .. finish()
103  // interface.
104 
105  // These cannot be declared private because they must be accessible
106  // to Python, but the method names are prefixed with priv_ to remind
107  // you that you probably don't want to be using them directly.
108  void priv_do_event(double t, EventType event);
109  virtual void priv_initialize(double t);
110  virtual void priv_instant();
111  virtual void priv_step(double t);
112  virtual void priv_finalize();
113  virtual void priv_reverse_initialize(double t);
114  virtual void priv_reverse_instant();
115  virtual void priv_reverse_finalize();
116  virtual void priv_interrupt();
117 
118  virtual void output(ostream &out) const;
119  virtual void write(ostream &out, int indent_level) const;
120 
121  void setup_play(double start_time, double end_time, double play_rate,
122  bool do_loop);
123  void setup_resume();
124  void setup_resume_until(double end_t);
125  bool step_play();
126 
127 public:
128  void mark_dirty();
129  INLINE bool check_t_callback();
130 
131 protected:
132  void interval_done();
133 
134  INLINE void recompute() const;
135  virtual void do_recompute();
136  INLINE void check_stopped(TypeHandle type, const char *method_name) const;
137  INLINE void check_started(TypeHandle type, const char *method_name) const;
138 
139  State _state;
140  double _curr_t;
141  string _name;
142  string _pname;
143  string _done_event;
144  double _duration;
145 
146  bool _auto_pause;
147  bool _auto_finish;
148  bool _wants_t_callback;
149  double _last_t_callback;
150  CIntervalManager *_manager;
151 
152  // For setup_play() and step_play().
153  double _clock_start;
154  double _start_t;
155  double _end_t;
156  bool _end_t_at_end;
157  bool _start_t_at_start;
158  double _play_rate;
159  bool _do_loop;
160  int _loop_count;
161 
162 private:
163  bool _open_ended;
164  bool _dirty;
165 
166  // We keep a record of the "parent" intervals (that is, any
167  // CMetaInterval objects that keep a pointer to this one) strictly
168  // so we can mark all of our parents dirty when this interval gets
169  // dirty.
171  Parents _parents;
172 
173  static PStatCollector _root_pcollector;
174  PStatCollector _ival_pcollector;
175 
176 public:
177  static TypeHandle get_class_type() {
178  return _type_handle;
179  }
180  static void init_type() {
181  TypedReferenceCount::init_type();
182  register_type(_type_handle, "CInterval",
183  TypedReferenceCount::get_class_type());
184  }
185  virtual TypeHandle get_type() const {
186  return get_class_type();
187  }
188  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
189 
190 private:
191  static TypeHandle _type_handle;
192 
193  friend class CMetaInterval;
194 };
195 
196 INLINE ostream &operator << (ostream &out, const CInterval &ival);
197 EXPCL_DIRECT ostream &operator << (ostream &out, CInterval::State state);
198 
199 #include "cInterval.I"
200 
201 #endif
202 
203 
204 
The base class for timeline components.
Definition: cInterval.h:39
A base class for things which need to inherit from both TypedObject and from ReferenceCount.
This interval contains a list of nested intervals, each of which has its own begin and end times...
Definition: cMetaInterval.h:34
A lightweight class that represents a single element that may be timed and/or counted via stats...
This object holds a number of currently-playing intervals and is responsible for advancing them each ...
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85