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
00028 #include "../SO3Animation/SO3SequenceAnimation.h"
00029 #include "../SO3Animation/SO3SequenceAnimationTrack.h"
00030
00031 namespace SO3
00032 {
00033
00034 SSequenceAnimationTrack::SSequenceAnimationTrack(std::string animationTrackName, SSequenceAnimation* animation, unsigned short trackAnimationId) : SAnimTrack(animationTrackName, animation, SAnimTrack::SO3_SEQUENCE_TRACK)
00035 {
00036 mLength = 0.0f;
00037 }
00038
00039 SSequenceAnimationTrack::~SSequenceAnimationTrack()
00040 {
00041 EnableTrackKeys(false);
00042 const SAnimSequenceKeyList keylistCpy = keyList;
00043 SAnimSequenceKeyList::const_iterator it = keylistCpy.begin();
00044 while (it != keylistCpy.end())
00045 {
00046 RemoveKey(*it, false);
00047 it++;
00048 }
00049 }
00050
00051 SSequenceAnimationTrack::SSequenceAnimationTrack() : SAnimTrack("", 0, SAnimTrack::SO3_NODE_TRACK)
00052 {
00053 }
00054
00055 SSequenceAnimationKey* SSequenceAnimationTrack::AddKey(SAnim* anim, float length, float transtime, float decaltime)
00056 {
00057
00058 if (anim->GetParentScene() != GetParentAnimation()->GetParentScene())
00059 return 0;
00060
00061 SSequenceAnimationKey* key = new SSequenceAnimationKey(anim, GetLength() - transtime + decaltime, length, transtime, decaltime);
00062 SetLength(GetLength() + length - transtime + decaltime);
00063
00064
00065 static_cast<SSequenceAnimation*>(GetParentAnimation())->UpdateAnimationLength();
00066
00067 keyList.push_back(key);
00068 return key;
00069 }
00070
00071 void SSequenceAnimationTrack::UpdateTrackTime()
00072 {
00073 float length = 0.0f;
00074 SAnimSequenceKeyList::iterator it = keyList.begin();
00075
00076 while (it != keyList.end())
00077 {
00078 SAnim* anim = (*it)->GetAnim();
00079 float klength = (*it)->GetLength();
00080 float decaltime = (*it)->GetDecalTime();
00081 float transtime = (*it)->GetTransitionTime();
00082 (*it)->SetPosition(length - transtime + decaltime);
00083 length = length + klength - transtime + decaltime;
00084 it++;
00085 }
00086
00087
00088 SetLength(length);
00089
00090
00091 static_cast<SSequenceAnimation*>(GetParentAnimation())->UpdateAnimationLength();
00092 }
00093
00094 void SSequenceAnimationTrack::RemoveKey(SSequenceAnimationKey* key, bool reset)
00095 {
00096
00097 if (reset && key->IsUsed())
00098 {
00099 SAnim* anim = key->GetAnim();
00100 anim->SetSequenceUpdated(false);
00101 key->SetUsed(false);
00102
00103 if (!anim->IsSequenceUpdated())
00104 {
00105 anim->SetEnable(false);
00106 anim->SetLoop(anim->GetInitialLoop());
00107 anim->SetTimePosition(0.0f);
00108 anim->SetWeight(anim->GetInitialWeight());
00109 }
00110 }
00111
00112 SAnimSequenceKeyList::iterator it = std::find(keyList.begin(), keyList.end(), key);
00113 if (it != keyList.end())
00114 keyList.erase(it);
00115
00116 SAFE_DELETE(key);
00117 UpdateTrackTime();
00118 }
00119
00120 void SSequenceAnimationTrack::SetKey(unsigned int index, float length, float transition, float decaltime)
00121 {
00122 SSequenceAnimationKey* key = GetKeyFromIndex(index);
00123 if (key)
00124 {
00125 key->SetLength(length);
00126 key->SetTransitionTime(transition);
00127 key->SetDecalTime(decaltime);
00128 UpdateTrackTime();
00129 }
00130 }
00131
00132 SSequenceAnimationKey* SSequenceAnimationTrack::GetKeyFromIndex(unsigned int index)
00133 {
00134 if (((int)(keyList.size()) - 1) < (int)index)
00135 return 0;
00136
00137 return keyList.at(index);
00138 }
00139
00140 bool SSequenceAnimationTrack::MoveKeyFromIndex(unsigned int index, unsigned int newindex)
00141 {
00142 if (((keyList.size() - 1) < index) || ((keyList.size() - 1) < newindex) || (index == newindex))
00143 return false;
00144
00145 SSequenceAnimationKey* mvkey = keyList[newindex];
00146 keyList[newindex] = keyList.at(index);
00147 keyList[index] = mvkey;
00148
00149 UpdateTrackTime();
00150 return true;
00151 }
00152
00153 SAnimSequenceKeyList SSequenceAnimationTrack::GetKeysFromAnim(SAnim* anim)
00154 {
00155 SAnimSequenceKeyList list;
00156
00157 SAnimSequenceKeyList::iterator it = keyList.begin();
00158 while (it != keyList.end())
00159 {
00160 if ((*it)->GetAnim() == anim)
00161 list.push_back(*it);
00162 it++;
00163 }
00164
00165 return list;
00166 }
00167
00168 void SSequenceAnimationTrack::RemoveKeysWithAnim(SAnim* anim, bool reset)
00169 {
00170 const SAnimSequenceKeyList keylistCpy = keyList;
00171 SAnimSequenceKeyList::const_iterator it = keylistCpy.begin();
00172 while (it != keylistCpy.end())
00173 {
00174 if ((*it)->GetAnim() == anim)
00175 RemoveKey(*it, reset);
00176 it++;
00177 }
00178 }
00179
00180 void SSequenceAnimationTrack::SetLength(float length)
00181 {
00182 mLength = length;
00183 }
00184
00185 float SSequenceAnimationTrack::GetLength()
00186 {
00187 return mLength;
00188 }
00189
00190 void SSequenceAnimationTrack::EnableTrackKeys(bool enable)
00191 {
00192 SAnimSequenceKeyList::iterator ikey = keyList.begin();
00193 while (ikey != keyList.end())
00194 {
00195 SAnim* anim = (*ikey)->GetAnim();
00196 if (!enable && ((*ikey)->IsUsed() == true))
00197 {
00198 anim->SetSequenceUpdated(false);
00199 (*ikey)->SetUsed(false);
00200
00201 if (!anim->IsSequenceUpdated())
00202 {
00203 anim->SetEnable(false);
00204 anim->SetLoop(anim->GetInitialLoop());
00205 anim->SetTimePosition(0.0f);
00206 anim->SetWeight(anim->GetInitialWeight());
00207 }
00208 }
00209
00210 ikey++;
00211 }
00212 }
00213
00214 unsigned short SSequenceAnimationTrack::GetNumKeyFrames()
00215 {
00216 return (unsigned short)keyList.size();
00217 }
00218
00219 float SSequenceAnimationTrack::GetKeyPositionTime(unsigned int keyIndex)
00220 {
00221 float postime = 0.0f;
00222 SSequenceAnimationKey* key = 0;
00223 if(((int)GetNumKeyFrames() - 1) >= (int)keyIndex)
00224 key = GetKeyFromIndex(keyIndex);
00225
00226 if (key)
00227 postime = key->GetPosition();
00228
00229 return postime;
00230 }
00231
00232 void SSequenceAnimationTrack::RemoveKey(unsigned int keyIndex)
00233 {
00234 if(((int)GetNumKeyFrames() - 1) >= (int)keyIndex)
00235 {
00236 SSequenceAnimationKey* key = GetKeyFromIndex(keyIndex);
00237 RemoveKey(key, true);
00238 }
00239 }
00240
00241 void SSequenceAnimationTrack::RemoveAllKeyFrames()
00242 {
00243 EnableTrackKeys(false);
00244
00245 const SAnimSequenceKeyList keylistCpy = keyList;
00246 SAnimSequenceKeyList::const_iterator it = keylistCpy.begin();
00247 while (it != keylistCpy.end())
00248 {
00249 RemoveKey(*it, true);
00250 it++;
00251 }
00252 }
00253
00254 void SSequenceAnimationTrack::Update(float pos)
00255 {
00256 float lastPos = static_cast<SSequenceAnimation*>(GetParentAnimation())->GetLastPos();
00257 bool stopIterate = false;
00258
00259
00260
00261 if (pos < lastPos)
00262 {
00263 SAnimSequenceKeyList::reverse_iterator nextKeyIt = keyList.rbegin();
00264 while ((nextKeyIt != keyList.rend()) && !stopIterate)
00265 {
00266 SAnim* anim = (*nextKeyIt)->GetAnim();
00267 float spos = (*nextKeyIt)->GetPosition();
00268 float klength = (*nextKeyIt)->GetLength();
00269 if ((spos > pos) && (spos < lastPos))
00270 {
00271 anim->SetEnable(true);
00272 anim->Apply(0.0f);
00273
00274 if (((*nextKeyIt)->IsUsed() == true) && anim->IsSequenceUpdated())
00275 {
00276 anim->SetSequenceUpdated(false);
00277 (*nextKeyIt)->SetUsed(false);
00278 }
00279
00280 if (!anim->IsSequenceUpdated())
00281 {
00282 anim->SetEnable(false);
00283 anim->SetLoop(anim->GetInitialLoop());
00284
00285 anim->SetWeight(anim->GetInitialWeight());
00286 }
00287 }
00288 else if (spos <= pos)
00289 {
00290 stopIterate = true;
00291 }
00292
00293 if (((*nextKeyIt)->IsUsed() == true) && anim->IsSequenceUpdated())
00294 {
00295 anim->SetSequenceUpdated(false);
00296 (*nextKeyIt)->SetUsed(false);
00297
00298 if (!anim->IsSequenceUpdated())
00299 {
00300 anim->SetEnable(false);
00301 anim->SetLoop(anim->GetInitialLoop());
00302
00303 anim->SetWeight(anim->GetInitialWeight());
00304 }
00305 }
00306
00307 nextKeyIt++;
00308 }
00309
00310 stopIterate = false;
00311 }
00312
00313 else if (pos > lastPos)
00314 {
00315 SAnimSequenceKeyList::iterator prevKeyIt = keyList.begin();
00316 while ((prevKeyIt != keyList.end()) && !stopIterate)
00317 {
00318 SAnim* anim = (*prevKeyIt)->GetAnim();
00319 float spos = (*prevKeyIt)->GetPosition();
00320 float klength = (*prevKeyIt)->GetLength();
00321 if (((spos + klength) > lastPos) && ((spos + klength) < pos))
00322 {
00323 anim->SetEnable(true);
00324
00325
00326 if ((prevKeyIt + 1) != keyList.end())
00327 {
00328 prevKeyIt++;
00329 SSequenceAnimationKey* nextkey = (*prevKeyIt);
00330 if (nextkey->GetTransitionTime() != 0.0f)
00331 anim->SetWeight(0.0);
00332 prevKeyIt--;
00333 }
00334
00335 if (anim->GetLength() <= klength)
00336 anim->SetLoop(false);
00337
00338 anim->Apply(klength);
00339
00340 if (((*prevKeyIt)->IsUsed() == true) && anim->IsSequenceUpdated())
00341 {
00342 anim->SetSequenceUpdated(false);
00343 (*prevKeyIt)->SetUsed(false);
00344 }
00345
00346 if (!anim->IsSequenceUpdated())
00347 {
00348 anim->SetEnable(false);
00349 anim->SetLoop(anim->GetInitialLoop());
00350
00351 anim->SetWeight(anim->GetInitialWeight());
00352 }
00353 }
00354 else if ((spos + klength) >= pos)
00355 {
00356 stopIterate = true;
00357 }
00358
00359 if (((*prevKeyIt)->IsUsed() == true) && anim->IsSequenceUpdated())
00360 {
00361 anim->SetSequenceUpdated(false);
00362 (*prevKeyIt)->SetUsed(false);
00363
00364 if (!anim->IsSequenceUpdated())
00365 {
00366 anim->SetEnable(false);
00367 anim->SetLoop(anim->GetInitialLoop());
00368
00369 anim->SetWeight(anim->GetInitialWeight());
00370 }
00371 }
00372
00373 prevKeyIt++;
00374 }
00375
00376 stopIterate = false;
00377 }
00378
00379
00380 SAnimSequenceKeyList::iterator keyIt = keyList.begin();
00381 while ((keyIt != keyList.end()) && !stopIterate)
00382 {
00383 SAnim* anim = (*keyIt)->GetAnim();
00384 float spos = (*keyIt)->GetPosition();
00385 float klength = (*keyIt)->GetLength();
00386 float transtime = (*keyIt)->GetTransitionTime();
00387 float endpos = spos + klength;
00388 float updpos = pos - spos;
00389 float transStep = 1.0f;
00390
00391 if ((pos >= spos) && (pos <= endpos))
00392 {
00393 if ((*keyIt)->IsUsed() == false)
00394 {
00395 anim->SetSequenceUpdated(true);
00396 (*keyIt)->SetUsed(true);
00397 }
00398
00399 anim->SetEnable(true);
00400 anim->SetLoop(true);
00401
00402
00403 if (((pos - spos) < transtime) && (transtime != 0.0f))
00404 {
00405 transStep = (pos - spos) / transtime;
00406
00407
00408 if (keyIt != keyList.begin())
00409 {
00410 keyIt--;
00411 SSequenceAnimationKey* prevkey = (*keyIt);
00412 if ((pos >= prevkey->GetPosition()) && (pos < (prevkey->GetPosition() + prevkey->GetLength())))
00413 {
00414 prevkey->GetAnim()->SetWeight(GetParentAnimation()->GetWeight() * prevkey->GetAnim()->GetInitialWeight() * (1.0f - transStep));
00415
00416 prevkey->GetAnim()->SetTimePosition(prevkey->GetAnim()->GetTimePosition());
00417 }
00418 keyIt++;
00419 }
00420 }
00421
00422 anim->SetWeight(GetParentAnimation()->GetWeight() * anim->GetInitialWeight() * transStep);
00423 anim->SetTimePosition(updpos);
00424 }
00425 else if (spos + klength > pos)
00426 {
00427 stopIterate = true;
00428 }
00429
00430 keyIt++;
00431 }
00432 }
00433
00434 }