Panda3D

animControlCollection.cxx
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 animControlCollection.cxx
10 * @author drose
11 * @date 2000-02-22
12 */
13
14 #include "animControlCollection.h"
15
16 using std::string;
17
18
19 /**
20 * Returns the AnimControl associated with the given name, or NULL if no such
21 * control has been associated.
22 */
23 AnimControlCollection::
24 AnimControlCollection() {
25 _last_started_control = nullptr;
26 }
27
28 /**
29 *
30 */
31 AnimControlCollection::
32 ~AnimControlCollection() {
33 }
34
35 /**
36 * Associates the given AnimControl with this collection under the given name.
37 * The AnimControl will remain associated until a new AnimControl is
38 * associated with the same name later, or until unbind_anim() is called with
39 * this name.
40 */
41 void AnimControlCollection::
42 store_anim(AnimControl *control, const string &name) {
43 ControlsByName::iterator ci = _controls_by_name.find(name);
44
45 if (ci == _controls_by_name.end()) {
46 // Insert a new control.
47 size_t index = _controls.size();
48 ControlDef cdef;
49 cdef._control = control;
50 cdef._name = name;
51 _controls.push_back(cdef);
52 _controls_by_name.insert(ControlsByName::value_type(name, index));
53
54 } else {
55 // Replace an existing control.
56 size_t index = (*ci).second;
57 nassertv(index < _controls.size());
58 nassertv(_controls[index]._name == name);
59 if (_last_started_control == _controls[index]._control) {
60 _last_started_control = nullptr;
61 }
62 _controls[index]._control = control;
63 }
64 }
65
66 /**
67 * Returns the AnimControl associated with the given name, or NULL if no such
68 * control has been associated.
69 */
70 AnimControl *AnimControlCollection::
71 find_anim(const string &name) const {
72 ControlsByName::const_iterator ci = _controls_by_name.find(name);
73 if (ci == _controls_by_name.end()) {
74 return nullptr;
75 }
76 size_t index = (*ci).second;
77 nassertr(index < _controls.size(), nullptr);
78 nassertr(_controls[index]._name == name, nullptr);
79 return _controls[index]._control;
80 }
81
82 /**
83 * Removes the AnimControl associated with the given name, if any. Returns
84 * true if an AnimControl was removed, false if there was no AnimControl with
85 * the indicated name.
86 */
87 bool AnimControlCollection::
88 unbind_anim(const string &name) {
89 ControlsByName::iterator ci = _controls_by_name.find(name);
90 if (ci == _controls_by_name.end()) {
91 return false;
92 }
93 size_t index = (*ci).second;
94 nassertr(index < _controls.size(), false);
95 nassertr(_controls[index]._name == name, false);
96
97 if (_last_started_control == _controls[index]._control) {
98 _last_started_control = nullptr;
99 }
100 _controls_by_name.erase(ci);
101
102 _controls.erase(_controls.begin() + index);
103
104 // Now slide all the index numbers down.
105 for (ci = _controls_by_name.begin();
106 ci != _controls_by_name.end();
107 ++ci) {
108 if ((*ci).second > index) {
109 (*ci).second--;
110 }
111 }
112
113 return true;
114 }
115
116 /**
117 * Returns the number of AnimControls associated with this collection.
118 */
119 int AnimControlCollection::
120 get_num_anims() const {
121 return _controls.size();
122 }
123
124 /**
125 * Returns the nth AnimControl associated with this collection.
126 */
127 AnimControl *AnimControlCollection::
128 get_anim(int n) const {
129 nassertr(n >= 0 && n < (int)_controls.size(), nullptr);
130 return _controls[n]._control;
131 }
132
133 /**
134 * Returns the name of the nth AnimControl associated with this collection.
135 */
136 string AnimControlCollection::
137 get_anim_name(int n) const {
138 nassertr(n >= 0 && n < (int)_controls.size(), string());
139 return _controls[n]._name;
140 }
141
142 /**
143 * Disassociates all anims from this collection.
144 */
145 void AnimControlCollection::
146 clear_anims() {
147 _controls.clear();
148 _controls_by_name.clear();
149 }
150
151 /**
152 * Starts all animations playing.
153 */
154 void AnimControlCollection::
155 play_all() {
156 Controls::const_iterator ci;
157 for (ci = _controls.begin(); ci != _controls.end(); ++ci) {
158 (*ci)._control->play();
159 _last_started_control = (*ci)._control;
160 }
161 }
162
163 /**
164 * Starts all animations playing.
165 */
166 void AnimControlCollection::
167 play_all(double from, double to) {
168 Controls::const_iterator ci;
169 for (ci = _controls.begin(); ci != _controls.end(); ++ci) {
170 (*ci)._control->play(from, to);
171 _last_started_control = (*ci)._control;
172 }
173 }
174
175 /**
176 * Starts all animations looping.
177 */
178 void AnimControlCollection::
179 loop_all(bool restart) {
180 Controls::const_iterator ci;
181 for (ci = _controls.begin(); ci != _controls.end(); ++ci) {
182 (*ci)._control->loop(restart);
183 _last_started_control = (*ci)._control;
184 }
185 }
186
187 /**
188 * Starts all animations looping.
189 */
190 void AnimControlCollection::
191 loop_all(bool restart, double from, double to) {
192 Controls::const_iterator ci;
193 for (ci = _controls.begin(); ci != _controls.end(); ++ci) {
194 (*ci)._control->loop(restart, from, to);
195 _last_started_control = (*ci)._control;
196 }
197 }
198
199 /**
200 * Stops all currently playing animations. Returns true if any animations
201 * were stopped, false if none were playing.
202 */
203 bool AnimControlCollection::
204 stop_all() {
205 bool any = false;
206 Controls::const_iterator ci;
207 for (ci = _controls.begin(); ci != _controls.end(); ++ci) {
208 if ((*ci)._control->is_playing()) {
209 any = true;
210 (*ci)._control->stop();
211 }
212 }
213
214 return any;
215 }
216
217 /**
218 * Sets all animations to the indicated frame.
219 */
220 void AnimControlCollection::
221 pose_all(double frame) {
222 Controls::const_iterator ci;
223 for (ci = _controls.begin(); ci != _controls.end(); ++ci) {
224 (*ci)._control->pose(frame);
225 _last_started_control = (*ci)._control;
226 }
227 }
228
229 /**
230 * Returns the name of the bound AnimControl currently playing, if any. If
231 * more than one AnimControl is currently playing, returns all of the names
232 * separated by spaces.
233 */
234 string AnimControlCollection::
235 which_anim_playing() const {
236 string result;
237
238 Controls::const_iterator ci;
239 for (ci = _controls.begin();
240 ci != _controls.end();
241 ++ci) {
242 if ((*ci)._control->is_playing()) {
243 if (!result.empty()) {
244 result += " ";
245 }
246 result += (*ci)._name;
247 }
248 }
249
250 return result;
251 }
252
253 /**
254 *
255 */
256 void AnimControlCollection::
257 output(std::ostream &out) const {
258 out << _controls.size() << " anims.";
259 }
260
261 /**
262 *
263 */
264 void AnimControlCollection::
265 write(std::ostream &out) const {
266 ControlsByName::const_iterator ci;
267 for (ci = _controls_by_name.begin();
268 ci != _controls_by_name.end();
269 ++ci) {
270 out << (*ci).first << ": " << *_controls[(*ci).second]._control << "\n";
271 }
272 }
animControlCollection.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
AnimControlCollection::get_num_anims
get_num_anims
Returns the number of AnimControls associated with this collection.
Definition: animControlCollection.h:45
AnimControlCollection::get_anim
get_anim
Returns the nth AnimControl associated with this collection.
Definition: animControlCollection.h:45
AnimControlCollection::stop_all
bool stop_all()
Stops all currently playing animations.
Definition: animControlCollection.cxx:204
AnimControlCollection::loop_all
void loop_all(bool restart)
Starts all animations looping.
Definition: animControlCollection.cxx:179
AnimControlCollection::clear_anims
void clear_anims()
Disassociates all anims from this collection.
Definition: animControlCollection.cxx:146
AnimControlCollection::AnimControlCollection
AnimControlCollection()
Returns the AnimControl associated with the given name, or NULL if no such control has been associate...
Definition: animControlCollection.cxx:24
AnimControlCollection::find_anim
AnimControl * find_anim(const std::string &name) const
Returns the AnimControl associated with the given name, or NULL if no such control has been associate...
Definition: animControlCollection.cxx:71
AnimControlCollection::store_anim
void store_anim(AnimControl *control, const std::string &name)
Associates the given AnimControl with this collection under the given name.
Definition: animControlCollection.cxx:42
AnimControlCollection::which_anim_playing
std::string which_anim_playing() const
Returns the name of the bound AnimControl currently playing, if any.
Definition: animControlCollection.cxx:235
AnimControlCollection::pose_all
void pose_all(double frame)
Sets all animations to the indicated frame.
Definition: animControlCollection.cxx:221
AnimControlCollection::get_anim_name
get_anim_name
Returns the name of the nth AnimControl associated with this collection.
Definition: animControlCollection.h:46
AnimControlCollection::unbind_anim
bool unbind_anim(const std::string &name)
Removes the AnimControl associated with the given name, if any.
Definition: animControlCollection.cxx:88
AnimControlCollection::play_all
void play_all()
Starts all animations playing.
Definition: animControlCollection.cxx:155
AnimControl
Controls the timing of a character animation.
Definition: animControl.h:38