00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00034 #include "SO3Anim.h"
00035 #include "SO3AnimTrack.h"
00036 #include "SO3SequenceAnimationTrack.h"
00037 #include "../SO3SceneGraph/SO3NodeScol.h"
00038 #include "../SO3SceneGraph/SO3Scene.h"
00039 #include "../SO3SceneGraph/SO3Skeleton.h"
00040 #include "../SCOLPack/SO3SCOL.h"
00041
00042 namespace SO3
00043 {
00044
00045 SAnim::SAnim(SScene* scene, std::string animationName, SNode* animationAssociatedNode, unsigned short animationId, AnimType animationType) : SData(animationName), type(animationType)
00046 {
00047 ogreAnimation = 0;
00048 bUpdatedBySequence = 0;
00049
00050 mScene = scene;
00051 associatedNode = animationAssociatedNode;
00052 animationIndex = animationId;
00053 mSpeed = 1.0f;
00054 mWeight = 0.0f;
00055 isPaused = true;
00056 skipFrame = true;
00057 ogreAnimationFrameListener = new SAnimationFrameListener(this);
00058 Ogre::Root::getSingleton().addFrameListener(ogreAnimationFrameListener);
00059 }
00060
00061 SAnim::SAnim() : SData(""), type(SO3_UNKNOWN_ANIM)
00062 {
00063
00064 }
00065
00066 SAnim::~SAnim()
00067 {
00068 Ogre::Root::getSingleton().removeFrameListener(ogreAnimationFrameListener);
00069 delete(ogreAnimationFrameListener);
00070 ogreAnimationFrameListener = 0;
00071
00072
00073 const SAnimMap seqAnimCopy = mScene->GetAnimations();
00074 SAnimMap::const_iterator iSeqAnim = seqAnimCopy.begin();
00075 while(iSeqAnim != seqAnimCopy.end())
00076 {
00077 SAnim* seqanim = iSeqAnim->second;
00078
00079
00080 if ((seqanim != this) && (seqanim->GetType() == SAnim::SO3_SEQUENCE_ANIM))
00081 {
00082 const SAnimTrackList SeqTrackCopy = seqanim->GetAnimationsTracks();
00083 SAnimTrackList::const_iterator iSeqTrack = SeqTrackCopy.begin();
00084 while(iSeqTrack != SeqTrackCopy.end())
00085 {
00086 try
00087 {
00088 SSequenceAnimationTrack* seqtrack = static_cast<SSequenceAnimationTrack*>(*iSeqTrack);
00089 seqtrack->RemoveKeysWithAnim(this, false);
00090 }
00091 catch(Ogre::Exception)
00092 {
00093 }
00094 iSeqTrack++;
00095 }
00096 }
00097
00098 iSeqAnim++;
00099 }
00100
00101 SAnimTrackList animationTrackListCopy = animationTrackList;
00102 SAnimTrackList::iterator iAnimTrack = animationTrackListCopy.begin();
00103 while(iAnimTrack != animationTrackListCopy.end())
00104 {
00105 RemoveAnimationTrack(*iAnimTrack);
00106 SAFE_DELETE(*iAnimTrack);
00107 iAnimTrack++;
00108 }
00109
00110 associatedNode = 0;
00111 ogreAnimation = 0;
00112 }
00113
00114 SScene* SAnim::GetParentScene()
00115 {
00116 return mScene;
00117 }
00118
00119 Ogre::Animation* SAnim::GetOgreAnimationPointer()
00120 {
00121 return ogreAnimation;
00122 }
00123
00124 SNode* SAnim::GetParentNode()
00125 {
00126 return associatedNode;
00127 }
00128
00129 SAnim::AnimType SAnim::GetType()
00130 {
00131 return type;
00132 }
00133
00134 unsigned short SAnim::GetIndex()
00135 {
00136 return animationIndex;
00137 }
00138
00139 bool SAnim::SkipFrame()
00140 {
00141 return skipFrame;
00142 }
00143
00144 void SAnim::SetSkipFrame(bool state)
00145 {
00146 skipFrame = state;
00147 }
00148
00149 SAnimTrack* SAnim::CreateAnimationTrack(std::string newAnimationTrackName)
00150 {
00151 SAnimTrack* newAnimationTrack = this->CreateAnimationTrackImpl(newAnimationTrackName);
00152
00153
00154
00155 if(newAnimationTrack == 0)
00156 OGRE_EXCEPT(Ogre::Exception::ERR_NOT_IMPLEMENTED, "Can not create animation track named \""+ newAnimationTrackName +"\", this animation type may not handle user creation!", "SAnim::CreateAnimationTrack");
00157
00158 AddAnimationTrack(newAnimationTrack);
00159 return newAnimationTrack;
00160 }
00161
00162 SAnimTrack* SAnim::CreateAnimationTrackImpl(std::string newAnimationTrackName)
00163 {
00164 return 0;
00165 }
00166
00167 void SAnim::DeleteAnimationTrack(SAnimTrack* existingAnimationTrack)
00168 {
00169 RemoveAnimationTrack(existingAnimationTrack);
00170 SAFE_DELETE(existingAnimationTrack);
00171 }
00172
00173 unsigned short SAnim::GetNumAnimationsTracks()
00174 {
00175 return animationTrackList.size();
00176 }
00177
00178 SAnimTrack* SAnim::GetAnimationTrack(unsigned short index)
00179 {
00180 if (((int)(animationTrackList.size()) - 1) < (int)index)
00181 return 0;
00182
00183 return animationTrackList.at(index);
00184 }
00185
00186 SAnimTrackList SAnim::GetAnimationsTracks() const
00187 {
00188 return animationTrackList;
00189 }
00190
00191 void SAnim::AddAnimationTrack(SAnimTrack* existingAnimationTrack)
00192 {
00193 animationTrackList.push_back(existingAnimationTrack);
00194 }
00195
00196 void SAnim::RemoveAnimationTrack(SAnimTrack* existingAnimationTrack)
00197 {
00198 SAnimTrackList::iterator it = std::find(animationTrackList.begin(), animationTrackList.end(), existingAnimationTrack);
00199 if (it != animationTrackList.end())
00200 animationTrackList.erase(it);
00201 }
00202
00203 void SAnim::SetLength(float length)
00204 {
00205 if(_GetOgreAnimationState())
00206 _GetOgreAnimationState()->setLength(length);
00207 }
00208
00209 float SAnim::GetLength()
00210 {
00211 float length = 0.0f;
00212 if(_GetOgreAnimationState())
00213 length = _GetOgreAnimationState()->getLength();
00214
00215 return length;
00216 }
00217
00218 void SAnim::SetTimePosition(float timePosition)
00219 {
00220 if(_GetOgreAnimationState())
00221 _GetOgreAnimationState()->setTimePosition(timePosition);
00222 }
00223
00224 void SAnim::Apply(float timePosition)
00225 {
00226 SetTimePosition(timePosition);
00227
00228
00229 GetParentScene()->GetOgreScenePointer()->_applySceneAnimations();
00230 }
00231
00232 float SAnim::GetTimePosition()
00233 {
00234 float pos = 0.0f;
00235 if(_GetOgreAnimationState())
00236 pos = _GetOgreAnimationState()->getTimePosition();
00237
00238 return pos;
00239 }
00240
00241 void SAnim::SetWeight(float weight)
00242 {
00243 if(_GetOgreAnimationState())
00244 _GetOgreAnimationState()->setWeight(weight);
00245 }
00246
00247 float SAnim::GetWeight()
00248 {
00249 float weight = 0.0f;
00250 if(_GetOgreAnimationState())
00251 weight = _GetOgreAnimationState()->getWeight();
00252
00253 return weight;
00254 }
00255
00256 void SAnim::SetInitialWeight(float weight)
00257 {
00258 SetWeight(weight);
00259 mWeight = weight;
00260 }
00261
00262 float SAnim::GetInitialWeight()
00263 {
00264 return mWeight;
00265 }
00266
00267 void SAnim::SetInitialLoop(bool loop)
00268 {
00269 if(_GetOgreAnimationState())
00270 _GetOgreAnimationState()->setLoop(loop);
00271 mLoop = loop;
00272 }
00273
00274 bool SAnim::GetInitialLoop()
00275 {
00276 return mLoop;
00277 }
00278
00279 void SAnim::SetInterpolationMode(SAnim::AnimInterpolationMode interpolationMode)
00280 {
00281 ogreAnimation->setInterpolationMode(static_cast <Ogre::Animation::InterpolationMode> (static_cast<int> (interpolationMode)));
00282 }
00283
00284 SAnim::AnimInterpolationMode SAnim::GetInterpolationMode()
00285 {
00286 return static_cast <AnimInterpolationMode> (ogreAnimation->getInterpolationMode());
00287 }
00288
00289 void SAnim::SetRotationInterpolationMode(SAnim::AnimRotationInterpolationMode interpolationMode)
00290 {
00291 ogreAnimation->setRotationInterpolationMode(static_cast <Ogre::Animation::RotationInterpolationMode> (static_cast<int> (interpolationMode)));
00292 }
00293
00294 SAnim::AnimRotationInterpolationMode SAnim::GetRotationInterpolationMode()
00295 {
00296 return static_cast <AnimRotationInterpolationMode> (ogreAnimation->getRotationInterpolationMode());
00297 }
00298
00299 void SAnim::SetEnable(bool enable)
00300 {
00301 if(_GetOgreAnimationState())
00302 _GetOgreAnimationState()->setEnabled(enable);
00303 SetPaused(!enable);
00304 }
00305
00306 bool SAnim::GetEnable()
00307 {
00308 bool ret = false;
00309 if(_GetOgreAnimationState())
00310 ret = _GetOgreAnimationState()->getEnabled();
00311
00312 return ret;
00313 }
00314
00315 void SAnim::SetLoop(bool loop)
00316 {
00317 if(_GetOgreAnimationState())
00318 _GetOgreAnimationState()->setLoop(loop);
00319 }
00320
00321 bool SAnim::GetLoop()
00322 {
00323 bool ret = false;
00324 if(_GetOgreAnimationState())
00325 ret = _GetOgreAnimationState()->getLoop();
00326
00327 return ret;
00328 }
00329
00330 void SAnim::SetOptimise(bool optimize)
00331 {
00332 if (ogreAnimation)
00333 ogreAnimation->optimise(optimize);
00334 }
00335
00336 void SAnim::SetSpeed(float newSpeed)
00337 {
00338 mSpeed = newSpeed;
00339 }
00340
00341 float SAnim::GetSpeed()
00342 {
00343 return mSpeed;
00344 }
00345
00346 bool SAnim::HasEnded()
00347 {
00348 bool ret = true;
00349 if(_GetOgreAnimationState())
00350 ret = _GetOgreAnimationState()->hasEnded();
00351
00352 return ret;
00353 }
00354
00355 void SAnim::AddTime(float time)
00356 {
00357 if(_GetOgreAnimationState())
00358 _GetOgreAnimationState()->addTime(time);
00359 }
00360
00361 void SAnim::SetPaused(bool pauseState)
00362 {
00363 isPaused = pauseState;
00364 }
00365
00366 bool SAnim::GetPaused()
00367 {
00368 return isPaused;
00369 }
00370
00371 SAnimationFrameListener::SAnimationFrameListener(SAnim* animationListened) : FrameListener()
00372 {
00373 animation = animationListened;
00374 }
00375
00376 SAnimationFrameListener::~SAnimationFrameListener(void)
00377 {
00378 animation = 0;
00379 }
00380
00381 void SAnim::_UpdateImpl(const Ogre::FrameEvent &evt)
00382 {
00383
00384 Ogre::Real maxtime = (evt.timeSinceLastFrame <= 0.016f) ? evt.timeSinceLastFrame : (evt.timeSinceLastFrame * 0.016f / evt.timeSinceLastFrame);
00385
00386 if (SkipFrame())
00387 AddTime((float)evt.timeSinceLastFrame * GetSpeed());
00388 else
00389 AddTime(maxtime * GetSpeed());
00390 }
00391
00392 bool SAnim::IsSequenceUpdated()
00393 {
00394 return (bUpdatedBySequence > 0);
00395 }
00396
00397 void SAnim::SetSequenceUpdated(bool value)
00398 {
00399 if (value)
00400 bUpdatedBySequence ++;
00401 else
00402 bUpdatedBySequence --;
00403 }
00404
00405 bool SAnimationFrameListener::frameStarted(const Ogre::FrameEvent &evt)
00406 {
00407 if(animation && !animation->IsSequenceUpdated() && animation->GetEnable() && !animation->GetPaused())
00408 animation->_UpdateImpl(evt);
00409
00410 return true;
00411 }
00412
00413 }