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