Panda3D

animControlCollection.cxx

00001 // Filename: animControlCollection.cxx
00002 // Created by:  drose (22Feb00)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 
00016 #include "animControlCollection.h"
00017 
00018 
00019 ////////////////////////////////////////////////////////////////////
00020 //     Function: AnimControlCollection::Constructor
00021 //       Access: Published
00022 //  Description: Returns the AnimControl associated with the given
00023 //               name, or NULL if no such control has been associated.
00024 ////////////////////////////////////////////////////////////////////
00025 AnimControlCollection::
00026 AnimControlCollection() {
00027   _last_started_control = (AnimControl *)NULL;
00028 }
00029 
00030 ////////////////////////////////////////////////////////////////////
00031 //     Function: AnimControlCollection::Destructor
00032 //       Access: Published
00033 //  Description:
00034 ////////////////////////////////////////////////////////////////////
00035 AnimControlCollection::
00036 ~AnimControlCollection() {
00037 }
00038 
00039 ////////////////////////////////////////////////////////////////////
00040 //     Function: AnimControlCollection::store_anim
00041 //       Access: Published
00042 //  Description: Associates the given AnimControl with this collection
00043 //               under the given name.  The AnimControl will remain
00044 //               associated until a new AnimControl is associated with
00045 //               the same name later, or until unbind_anim() is called
00046 //               with this name.
00047 ////////////////////////////////////////////////////////////////////
00048 void AnimControlCollection::
00049 store_anim(AnimControl *control, const string &name) {
00050   ControlsByName::iterator ci = _controls_by_name.find(name);
00051 
00052   if (ci == _controls_by_name.end()) {
00053     // Insert a new control.
00054     size_t index = _controls.size();
00055     ControlDef cdef;
00056     cdef._control = control;
00057     cdef._name = name;
00058     _controls.push_back(cdef);
00059     _controls_by_name.insert(ControlsByName::value_type(name, index));
00060 
00061   } else {
00062     // Replace an existing control.
00063     size_t index = (*ci).second;
00064     nassertv(index < _controls.size());
00065     nassertv(_controls[index]._name == name);
00066     if (_last_started_control == _controls[index]._control) {
00067       _last_started_control = (AnimControl *)NULL;
00068     }
00069     _controls[index]._control = control;
00070   }
00071 }
00072 
00073 ////////////////////////////////////////////////////////////////////
00074 //     Function: AnimControlCollection::find_anim
00075 //       Access: Published
00076 //  Description: Returns the AnimControl associated with the given
00077 //               name, or NULL if no such control has been associated.
00078 ////////////////////////////////////////////////////////////////////
00079 AnimControl *AnimControlCollection::
00080 find_anim(const string &name) const {
00081   ControlsByName::const_iterator ci = _controls_by_name.find(name);
00082   if (ci == _controls_by_name.end()) {
00083     return (AnimControl *)NULL;
00084   }
00085   size_t index = (*ci).second;
00086   nassertr(index < _controls.size(), NULL);
00087   nassertr(_controls[index]._name == name, NULL);
00088   return _controls[index]._control;
00089 }
00090 
00091 ////////////////////////////////////////////////////////////////////
00092 //     Function: AnimControlCollection::unbind_anim
00093 //       Access: Published
00094 //  Description: Removes the AnimControl associated with the given
00095 //               name, if any.  Returns true if an AnimControl was
00096 //               removed, false if there was no AnimControl with the
00097 //               indicated name.
00098 ////////////////////////////////////////////////////////////////////
00099 bool AnimControlCollection::
00100 unbind_anim(const string &name) {
00101   ControlsByName::iterator ci = _controls_by_name.find(name);
00102   if (ci == _controls_by_name.end()) {
00103     return false;
00104   }
00105   size_t index = (*ci).second;
00106   nassertr(index < _controls.size(), false);
00107   nassertr(_controls[index]._name == name, false);
00108 
00109   if (_last_started_control == _controls[index]._control) {
00110     _last_started_control = (AnimControl *)NULL;
00111   }
00112   _controls_by_name.erase(ci);
00113 
00114   _controls.erase(_controls.begin() + index);
00115 
00116   // Now slide all the index numbers down.
00117   for (ci = _controls_by_name.begin();
00118        ci != _controls_by_name.end();
00119        ++ci) {
00120     if ((*ci).second > index) {
00121       (*ci).second--;
00122     }
00123   }
00124 
00125   return true;
00126 }
00127 
00128 ////////////////////////////////////////////////////////////////////
00129 //     Function: AnimControlCollection::get_num_anims
00130 //       Access: Published
00131 //  Description: Returns the number of AnimControls associated with
00132 //               this collection.
00133 ////////////////////////////////////////////////////////////////////
00134 int AnimControlCollection::
00135 get_num_anims() const {
00136   return _controls.size();
00137 }
00138 
00139 ////////////////////////////////////////////////////////////////////
00140 //     Function: AnimControlCollection::get_anim
00141 //       Access: Published
00142 //  Description: Returns the nth AnimControl associated with
00143 //               this collection.
00144 ////////////////////////////////////////////////////////////////////
00145 AnimControl *AnimControlCollection::
00146 get_anim(int n) const {
00147   nassertr(n >= 0 && n < (int)_controls.size(), NULL);
00148   return _controls[n]._control;
00149 }
00150 
00151 ////////////////////////////////////////////////////////////////////
00152 //     Function: AnimControlCollection::get_anim_name
00153 //       Access: Published
00154 //  Description: Returns the name of the nth AnimControl associated
00155 //               with this collection.
00156 ////////////////////////////////////////////////////////////////////
00157 string AnimControlCollection::
00158 get_anim_name(int n) const {
00159   nassertr(n >= 0 && n < (int)_controls.size(), string());
00160   return _controls[n]._name;
00161 }
00162 
00163 ////////////////////////////////////////////////////////////////////
00164 //     Function: AnimControlCollection::clear_anims
00165 //       Access: Published
00166 //  Description: Disassociates all anims from this collection.
00167 ////////////////////////////////////////////////////////////////////
00168 void AnimControlCollection::
00169 clear_anims() {
00170   _controls.clear();
00171   _controls_by_name.clear();
00172 }
00173 
00174 ////////////////////////////////////////////////////////////////////
00175 //     Function: AnimControlCollection::play_all
00176 //       Access: Published
00177 //  Description: Starts all animations playing.
00178 ////////////////////////////////////////////////////////////////////
00179 void AnimControlCollection::
00180 play_all() {
00181   Controls::const_iterator ci;
00182   for (ci = _controls.begin(); ci != _controls.end(); ++ci) {
00183     (*ci)._control->play();
00184     _last_started_control = (*ci)._control;
00185   }
00186 }
00187 
00188 ////////////////////////////////////////////////////////////////////
00189 //     Function: AnimControlCollection::play_all
00190 //       Access: Published
00191 //  Description: Starts all animations playing.
00192 ////////////////////////////////////////////////////////////////////
00193 void AnimControlCollection::
00194 play_all(int from, int to) {
00195   Controls::const_iterator ci;
00196   for (ci = _controls.begin(); ci != _controls.end(); ++ci) {
00197     (*ci)._control->play(from, to);
00198     _last_started_control = (*ci)._control;
00199   }
00200 }
00201 
00202 ////////////////////////////////////////////////////////////////////
00203 //     Function: AnimControlCollection::loop_all
00204 //       Access: Published
00205 //  Description: Starts all animations looping.
00206 ////////////////////////////////////////////////////////////////////
00207 void AnimControlCollection::
00208 loop_all(bool restart) {
00209   Controls::const_iterator ci;
00210   for (ci = _controls.begin(); ci != _controls.end(); ++ci) {
00211     (*ci)._control->loop(restart);
00212     _last_started_control = (*ci)._control;
00213   }
00214 }
00215 
00216 ////////////////////////////////////////////////////////////////////
00217 //     Function: AnimControlCollection::loop_all
00218 //       Access: Published
00219 //  Description: Starts all animations looping.
00220 ////////////////////////////////////////////////////////////////////
00221 void AnimControlCollection::
00222 loop_all(bool restart, int from, int to) {
00223   Controls::const_iterator ci;
00224   for (ci = _controls.begin(); ci != _controls.end(); ++ci) {
00225     (*ci)._control->loop(restart, from, to);
00226     _last_started_control = (*ci)._control;
00227   }
00228 }
00229 
00230 ////////////////////////////////////////////////////////////////////
00231 //     Function: AnimControlCollection::stop_all
00232 //       Access: Published
00233 //  Description: Stops all currently playing animations.  Returns true
00234 //               if any animations were stopped, false if none were
00235 //               playing.
00236 ////////////////////////////////////////////////////////////////////
00237 bool AnimControlCollection::
00238 stop_all() {
00239   bool any = false;
00240   Controls::const_iterator ci;
00241   for (ci = _controls.begin(); ci != _controls.end(); ++ci) {
00242     if ((*ci)._control->is_playing()) {
00243       any = true;
00244       (*ci)._control->stop();
00245     }
00246   }
00247 
00248   return any;
00249 }
00250 
00251 ////////////////////////////////////////////////////////////////////
00252 //     Function: AnimControlCollection::pose_all
00253 //       Access: Published
00254 //  Description: Sets all animations to the indicated frame.
00255 ////////////////////////////////////////////////////////////////////
00256 void AnimControlCollection::
00257 pose_all(int frame) {
00258   Controls::const_iterator ci;
00259   for (ci = _controls.begin(); ci != _controls.end(); ++ci) {
00260     (*ci)._control->pose(frame);
00261     _last_started_control = (*ci)._control;
00262   }
00263 }
00264 
00265 ////////////////////////////////////////////////////////////////////
00266 //     Function: AnimControlCollection::which_anim_playing
00267 //       Access: Published
00268 //  Description: Returns the name of the bound AnimControl currently
00269 //               playing, if any.  If more than one AnimControl is
00270 //               currently playing, returns all of the names separated
00271 //               by spaces.
00272 ////////////////////////////////////////////////////////////////////
00273 string AnimControlCollection::
00274 which_anim_playing() const {
00275   string result;
00276 
00277   Controls::const_iterator ci;
00278   for (ci = _controls.begin(); 
00279        ci != _controls.end(); 
00280        ++ci) {
00281     if ((*ci)._control->is_playing()) {
00282       if (!result.empty()) {
00283         result += " ";
00284       }
00285       result += (*ci)._name;
00286     }
00287   }
00288 
00289   return result;
00290 }
00291 
00292 ////////////////////////////////////////////////////////////////////
00293 //     Function: AnimControlCollection::output
00294 //       Access: Published
00295 //  Description: 
00296 ////////////////////////////////////////////////////////////////////
00297 void AnimControlCollection::
00298 output(ostream &out) const {
00299   out << _controls.size() << " anims.";
00300 }
00301 
00302 ////////////////////////////////////////////////////////////////////
00303 //     Function: AnimControlCollection::write
00304 //       Access: Published
00305 //  Description: 
00306 ////////////////////////////////////////////////////////////////////
00307 void AnimControlCollection::
00308 write(ostream &out) const {
00309   ControlsByName::const_iterator ci;
00310   for (ci = _controls_by_name.begin(); 
00311        ci != _controls_by_name.end(); 
00312        ++ci) {
00313     out << (*ci).first << ": " << *_controls[(*ci).second]._control << "\n";
00314   }
00315 }
 All Classes Functions Variables Enumerations