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