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  */
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  */
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  */
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  */
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  */
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  */
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  */
147  _controls.clear();
148  _controls_by_name.clear();
149 }
150 
151 /**
152  * Starts all animations playing.
153  */
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  */
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  */
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  */
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  */
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  */
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  */
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 }
get_anim
Returns the nth AnimControl associated with this collection.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void loop_all(bool restart)
Starts all animations looping.
void play_all()
Starts all animations playing.
AnimControlCollection()
Returns the AnimControl associated with the given name, or NULL if no such control has been associate...
void clear_anims()
Disassociates all anims from this collection.
void pose_all(double frame)
Sets all animations to the indicated frame.
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...
bool unbind_anim(const std::string &name)
Removes the AnimControl associated with the given name, if any.
bool stop_all()
Stops all currently playing animations.
void store_anim(AnimControl *control, const std::string &name)
Associates the given AnimControl with this collection under the given name.
Controls the timing of a character animation.
Definition: animControl.h:38
std::string which_anim_playing() const
Returns the name of the bound AnimControl currently playing, if any.
get_anim_name
Returns the name of the nth AnimControl associated with this collection.