Panda3D
Loading...
Searching...
No Matches
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#include "extension.h"
23
25
26/**
27 * The base class for timeline components. A CInterval represents a single
28 * action, event, or collection of nested intervals that will be performed at
29 * some specific time or over a period of time.
30 *
31 * This is essentially similar to the Python "Interval" class, but it is
32 * implemented in C++ (hence the name). Intervals that may be implemented in
33 * C++ will inherit from this class; Intervals that must be implemented in
34 * Python will inherit from the similar Python class.
35 */
36class EXPCL_DIRECT_INTERVAL CInterval : public TypedReferenceCount {
37public:
38 CInterval(const std::string &name, double duration, bool open_ended);
39 virtual ~CInterval();
40
41PUBLISHED:
42 INLINE const std::string &get_name() const;
43 INLINE double get_duration() const;
44 INLINE bool get_open_ended() const;
45
46 enum EventType {
47 ET_initialize,
48 ET_instant,
49 ET_step,
50 ET_finalize,
51 ET_reverse_initialize,
52 ET_reverse_instant,
53 ET_reverse_finalize,
54 ET_interrupt
55 };
56
57 enum State {
58 S_initial,
59 S_started,
60 S_paused,
61 S_final
62 };
63
64 INLINE State get_state() const;
65 INLINE bool is_stopped() const;
66
67 INLINE void set_done_event(const std::string &event);
68 INLINE const std::string &get_done_event() const;
69
70 void set_t(double t);
71 INLINE double get_t() const;
72
73 INLINE void set_auto_pause(bool auto_pause);
74 INLINE bool get_auto_pause() const;
75 INLINE void set_auto_finish(bool auto_finish);
76 INLINE bool get_auto_finish() const;
77
78 INLINE void set_wants_t_callback(bool wants_t_callback);
79 INLINE bool get_wants_t_callback() const;
80
81 INLINE void set_manager(CIntervalManager *manager);
82 INLINE CIntervalManager *get_manager() const;
83
84 void start(double start_t = 0.0, double end_t = -1.0, double play_rate = 1.0);
85 void loop(double start_t = 0.0, double end_t = -1.0, double play_rate = 1.0);
86 double pause();
87 void resume();
88 void resume(double start_t);
89 void resume_until(double end_t);
90 void finish();
91 void clear_to_initial();
92 bool is_playing() const;
93
94 double get_play_rate() const;
95 void set_play_rate(double play_rate);
96
97 // These functions control the actual playback of the interval. Don't call
98 // them directly; they're intended to be called from a supervising object,
99 // e.g. the Python start() .. finish() interface.
100
101 // These cannot be declared private because they must be accessible to
102 // Python, but the method names are prefixed with priv_ to remind you that
103 // you probably don't want to be using them directly.
104 void priv_do_event(double t, EventType event);
105 virtual void priv_initialize(double t);
106 virtual void priv_instant();
107 virtual void priv_step(double t);
108 virtual void priv_finalize();
109 virtual void priv_reverse_initialize(double t);
110 virtual void priv_reverse_instant();
111 virtual void priv_reverse_finalize();
112 virtual void priv_interrupt();
113
114 virtual void output(std::ostream &out) const;
115 virtual void write(std::ostream &out, int indent_level) const;
116
117 void setup_play(double start_time, double end_time, double play_rate,
118 bool do_loop);
119 void setup_resume();
120 void setup_resume_until(double end_t);
121 bool step_play();
122
123PUBLISHED:
124 EXTENSION(PyObject *__await__(PyObject *self));
125
126 MAKE_PROPERTY(name, get_name);
127 MAKE_PROPERTY(duration, get_duration);
128 MAKE_PROPERTY(open_ended, get_open_ended);
129 MAKE_PROPERTY(state, get_state);
130 MAKE_PROPERTY(stopped, is_stopped);
131 MAKE_PROPERTY(done_event, get_done_event, set_done_event);
132 MAKE_PROPERTY(t, get_t, set_t);
133 MAKE_PROPERTY(auto_pause, get_auto_pause, set_auto_pause);
134 MAKE_PROPERTY(auto_finish, get_auto_finish, set_auto_finish);
135 MAKE_PROPERTY(manager, get_manager, set_manager);
136 MAKE_PROPERTY(play_rate, get_play_rate, set_play_rate);
137 MAKE_PROPERTY(playing, is_playing);
138
139public:
140 void mark_dirty();
141 INLINE bool check_t_callback();
142
143protected:
144 void interval_done();
145
146 INLINE void recompute() const;
147 virtual void do_recompute();
148 INLINE void check_stopped(TypeHandle type, const char *method_name) const;
149 INLINE void check_started(TypeHandle type, const char *method_name) const;
150
151 State _state;
152 double _curr_t;
153 std::string _name;
154 std::string _pname;
155 std::string _done_event;
156 double _duration;
157
158 bool _auto_pause;
159 bool _auto_finish;
160 bool _wants_t_callback;
161 double _last_t_callback;
162 CIntervalManager *_manager;
163
164 // For setup_play() and step_play().
165 double _clock_start;
166 double _start_t;
167 double _end_t;
168 bool _end_t_at_end;
169 bool _start_t_at_start;
170 double _play_rate;
171 bool _do_loop;
172 int _loop_count;
173
174private:
175 bool _open_ended;
176 bool _dirty;
177
178 // We keep a record of the "parent" intervals (that is, any CMetaInterval
179 // objects that keep a pointer to this one) strictly so we can mark all of
180 // our parents dirty when this interval gets dirty.
181 typedef pvector<CInterval *> Parents;
182 Parents _parents;
183
184 static PStatCollector _root_pcollector;
185 PStatCollector _ival_pcollector;
186
187public:
188 static TypeHandle get_class_type() {
189 return _type_handle;
190 }
191 static void init_type() {
192 TypedReferenceCount::init_type();
193 register_type(_type_handle, "CInterval",
194 TypedReferenceCount::get_class_type());
195 }
196 virtual TypeHandle get_type() const {
197 return get_class_type();
198 }
199 virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
200
201private:
202 static TypeHandle _type_handle;
203
204 friend class CMetaInterval;
205};
206
207INLINE std::ostream &operator << (std::ostream &out, const CInterval &ival);
208EXPCL_DIRECT_INTERVAL std::ostream &operator << (std::ostream &out, CInterval::State state);
209
210#include "cInterval.I"
211
212#endif
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This object holds a number of currently-playing intervals and is responsible for advancing them each ...
The base class for timeline components.
Definition cInterval.h:36
is_playing
Returns true if the interval is currently playing, false otherwise.
Definition cInterval.h:137
bool check_t_callback()
Returns true if the wants_t_callback() flag is true and the interval's t value has changed since the ...
Definition cInterval.I:178
get_manager
Returns the CIntervalManager object which will be responsible for playing this interval.
Definition cInterval.h:135
set_play_rate
Changes the play rate of the interval.
Definition cInterval.h:136
bool get_wants_t_callback() const
Returns the state of the 'wants_t_callback' flag.
Definition cInterval.I:145
virtual void priv_reverse_finalize()
Called generally following a priv_reverse_initialize(), this indicates the interval should set itself...
void resume()
Restarts the interval from its current point after a previous call to pause().
bool step_play()
Should be called once per frame to execute the automatic timed playback begun with setup_play().
virtual void priv_step(double t)
Advances the time on the interval.
void setup_play(double start_time, double end_time, double play_rate, bool do_loop)
Called to prepare the interval for automatic timed playback, e.g.
void loop(double start_t=0.0, double end_t=-1.0, double play_rate=1.0)
Starts the interval playing by registering it with the current CIntervalManager.
void set_wants_t_callback(bool wants_t_callback)
Changes the state of the 'wants_t_callback' flag.
Definition cInterval.I:135
set_manager
Indicates the CIntervalManager object which will be responsible for playing this interval.
Definition cInterval.h:135
get_name
Returns the interval's name.
Definition cInterval.h:126
get_auto_pause
Returns the state of the 'auto_pause' flag.
Definition cInterval.h:133
set_t
Explicitly sets the time within the interval.
Definition cInterval.h:132
get_duration
Returns the duration of the interval in seconds.
Definition cInterval.h:127
is_stopped
Returns true if the interval is in either its initial or final states (but not in a running or paused...
Definition cInterval.h:130
set_done_event
Sets the event that is generated whenever the interval reaches its final state, whether it is explici...
Definition cInterval.h:131
set_auto_pause
Changes the state of the 'auto_pause' flag.
Definition cInterval.h:133
get_open_ended
Returns the state of the "open_ended" flag.
Definition cInterval.h:128
get_state
Indicates the state the interval believes it is in: whether it has been started, is currently in the ...
Definition cInterval.h:129
get_done_event
Returns the event that is generated whenever the interval reaches its final state,...
Definition cInterval.h:131
virtual void priv_initialize(double t)
This replaces the first call to priv_step(), and indicates that the interval has just begun.
void mark_dirty()
Called by a derived class to indicate the interval has been changed internally and must be recomputed...
virtual void priv_reverse_initialize(double t)
Similar to priv_initialize(), but this is called when the interval is being played backwards; it indi...
get_play_rate
Returns the play rate as set by the last call to start(), loop(), or set_play_rate().
Definition cInterval.h:136
void resume_until(double end_t)
Restarts the interval from the current point after a previous call to pause() (or a previous play-to-...
void priv_do_event(double t, EventType event)
Calls the appropriate event function indicated by the EventType.
virtual void priv_finalize()
This is called to stop an interval, forcing it to whatever state it would be after it played all the ...
get_t
Returns the current time of the interval: the last value of t passed to priv_initialize(),...
Definition cInterval.h:132
set_auto_finish
Changes the state of the 'auto_finish' flag.
Definition cInterval.h:134
void clear_to_initial()
Pauses the interval, if it is playing, and resets its state to its initial state, abandoning any stat...
virtual void priv_reverse_instant()
This is called in lieu of priv_reverse_initialize() .
virtual void priv_interrupt()
This is called while the interval is playing to indicate that it is about to be interrupted; that is,...
void setup_resume()
Called to prepare the interval for restarting at the current point within the interval after an inter...
get_auto_finish
Returns the state of the 'auto_finish' flag.
Definition cInterval.h:134
virtual void priv_instant()
This is called in lieu of priv_initialize() .
void setup_resume_until(double end_t)
Called to prepare the interval for restarting from the current point after a previous call to pause()...
void start(double start_t=0.0, double end_t=-1.0, double play_rate=1.0)
Starts the interval playing by registering it with the current CIntervalManager.
void finish()
Stops the interval from playing and sets it to its final state.
double pause()
Stops the interval from playing but leaves it in its current state.
A lightweight class that represents a single element that may be timed and/or counted via stats.
TypeHandle is the identifier used to differentiate C++ class types.
Definition typeHandle.h:81
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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(),...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.