SO3Engine
SO3Anim.cpp
Go to the documentation of this file.
1/*
2-----------------------------------------------------------------------------
3This source file is part of OpenSpace3D
4For the latest info, see http://www.openspace3d.com
5
6Copyright (c) 2012 I-maginer
7
8This program is free software; you can redistribute it and/or modify it under
9the terms of the GNU Lesser General Public License as published by the Free Software
10Foundation; either version 2 of the License, or (at your option) any later
11version.
12
13This program is distributed in the hope that it will be useful, but WITHOUT
14ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16
17You should have received a copy of the GNU Lesser General Public License along with
18this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19Place - Suite 330, Boston, MA 02111-1307, USA, or go to
20http://www.gnu.org/copyleft/lesser.txt
21
22-----------------------------------------------------------------------------
23*/
24
37
38namespace SO3
39{
40
41SAnim::SAnim(SScene* scene, const std::string& animationName, SNode* animationAssociatedNode, const unsigned short& animationId, const AnimType& animationType) : SData(animationName), type(animationType)
42{
43 ogreAnimation = 0;
44 bUpdatedBySequence = 0;
45
46 mScene = scene;
47 associatedNode = animationAssociatedNode;
48 animationIndex = animationId;
49 mSpeed = 1.0f;
50 mWeight = 0.0f;
51 isPaused = true;
52 skipFrame = true;
53 ogreAnimationFrameListener = new SAnimationFrameListener(this);
54 Ogre::Root::getSingleton().addFrameListener(ogreAnimationFrameListener);
55}
56
57SAnim::SAnim() : SData(""), type(SO3_UNKNOWN_ANIM)
58{
59 // Forbiden (private)
60}
61
63{
64 Ogre::Root::getSingleton().removeFrameListener(ogreAnimationFrameListener);
65 delete(ogreAnimationFrameListener);
66 ogreAnimationFrameListener = 0;
67
68 //remove sequence animation keys containing this animation
69 const SAnimMap seqAnimCopy = mScene->GetAnimations();
70 SAnimMap::const_iterator iSeqAnim = seqAnimCopy.begin();
71 while(iSeqAnim != seqAnimCopy.end())
72 {
73 SAnim* seqanim = iSeqAnim->second;
74
75 //Do not treat the actual animation
76 if ((seqanim != this) && (seqanim->GetType() == SAnim::SO3_SEQUENCE_ANIM))
77 {
78 const SAnimTrackList SeqTrackCopy = seqanim->GetAnimationsTracks();
79 SAnimTrackList::const_iterator iSeqTrack = SeqTrackCopy.begin();
80 while(iSeqTrack != SeqTrackCopy.end())
81 {
82 try
83 {
84 SSequenceAnimationTrack* seqtrack = static_cast<SSequenceAnimationTrack*>(*iSeqTrack);
85 seqtrack->RemoveKeysWithAnim(this, false);
86 }
87 catch(Ogre::Exception)
88 {
89 }
90 iSeqTrack++;
91 }
92 }
93
94 iSeqAnim++;
95 }
96
97 SAnimTrackList animationTrackListCopy = animationTrackList;
98 SAnimTrackList::iterator iAnimTrack = animationTrackListCopy.begin();
99 while(iAnimTrack != animationTrackListCopy.end())
100 {
101 RemoveAnimationTrack(*iAnimTrack);
102 SO3_SAFE_DELETE(*iAnimTrack);
103 iAnimTrack++;
104 }
105
106 associatedNode = 0;
107 ogreAnimation = 0;
108}
109
111{
112 return mScene;
113}
114
116{
117 return ogreAnimation;
118}
119
121{
122 return associatedNode;
123}
124
126{
127 return type;
128}
129
130unsigned short SAnim::GetIndex()
131{
132 return animationIndex;
133}
134
136{
137 return skipFrame;
138}
139
140void SAnim::SetSkipFrame(const bool& state)
141{
142 skipFrame = state;
143}
144
145SAnimTrack* SAnim::CreateAnimationTrack(const std::string& newAnimationTrackName)
146{
147 SAnimTrack* newAnimationTrack = this->CreateAnimationTrackImpl(newAnimationTrackName);
148
149 /* Check if our anim track was really created. If not, it's certainly that the SAnim derived class do not
150 re-implement CreateAnimationTrackImpl function, and it's SAnim::CreateAnimationTrackImpl that was called. */
151 if(newAnimationTrack == 0)
152 OGRE_EXCEPT(Ogre::Exception::ERR_NOT_IMPLEMENTED, "Can not create animation track named \""+ newAnimationTrackName +"\", this animation type may not handle user creation!", "SAnim::CreateAnimationTrack");
153
154 AddAnimationTrack(newAnimationTrack);
155 return newAnimationTrack;
156}
157
158SAnimTrack* SAnim::CreateAnimationTrackImpl(const std::string& newAnimationTrackName)
159{
160 return 0;
161}
162
163void SAnim::DeleteAnimationTrack(SAnimTrack* existingAnimationTrack)
164{
165 RemoveAnimationTrack(existingAnimationTrack);
166 SO3_SAFE_DELETE(existingAnimationTrack);
167}
168
170{
171 return animationTrackList.size();
172}
173
174SAnimTrack* SAnim::GetAnimationTrack(const unsigned short& index)
175{
176 if (((int)(animationTrackList.size()) - 1) < (int)index)
177 return 0;
178
179 return animationTrackList.at(index);
180}
181
186
187void SAnim::AddAnimationTrack(SAnimTrack* existingAnimationTrack)
188{
189 animationTrackList.push_back(existingAnimationTrack);
190}
191
192void SAnim::RemoveAnimationTrack(SAnimTrack* existingAnimationTrack)
193{
194 SAnimTrackList::iterator it = std::find(animationTrackList.begin(), animationTrackList.end(), existingAnimationTrack);
195 if (it != animationTrackList.end())
196 animationTrackList.erase(it);
197}
198
199void SAnim::SetLength(const float& length)
200{
202 _GetOgreAnimationState()->setLength(length);
203}
204
206{
207 float length = 0.0f;
209 length = _GetOgreAnimationState()->getLength();
210
211 return length;
212}
213
214void SAnim::SetTimePosition(const float& timePosition)
215{
217 _GetOgreAnimationState()->setTimePosition(timePosition);
218}
219
220void SAnim::Apply(const float& timePosition)
221{
222 SetTimePosition(timePosition);
223
224 //force update current animations
225 GetParentScene()->GetOgreScenePointer()->_applySceneAnimations();
226}
227
229{
230 float pos = 0.0f;
232 pos = _GetOgreAnimationState()->getTimePosition();
233
234 return pos;
235}
236
237void SAnim::SetWeight(const float& weight)
238{
240 _GetOgreAnimationState()->setWeight(weight);
241}
242
244{
245 float weight = 0.0f;
247 weight = _GetOgreAnimationState()->getWeight();
248
249 return weight;
250}
251
252void SAnim::SetInitialWeight(const float& weight)
253{
254 SetWeight(weight);
255 mWeight = weight;
256}
257
259{
260 return mWeight;
261}
262
263void SAnim::SetInitialLoop(const bool& loop)
264{
266 _GetOgreAnimationState()->setLoop(loop);
267 mLoop = loop;
268}
269
271{
272 return mLoop;
273}
274
276{
277 ogreAnimation->setInterpolationMode(static_cast <Ogre::Animation::InterpolationMode> (static_cast<int> (interpolationMode)));
278}
279
281{
282 return static_cast <AnimInterpolationMode> (ogreAnimation->getInterpolationMode());
283}
284
286{
287 ogreAnimation->setRotationInterpolationMode(static_cast <Ogre::Animation::RotationInterpolationMode> (static_cast<int> (interpolationMode)));
288}
289
294
295void SAnim::SetEnable(const bool& enable)
296{
298 _GetOgreAnimationState()->setEnabled(enable);
299 SetPaused(!enable);
300}
301
303{
304 bool ret = false;
306 ret = _GetOgreAnimationState()->getEnabled();
307
308 return ret;
309}
310
311void SAnim::SetLoop(const bool& loop)
312{
314 _GetOgreAnimationState()->setLoop(loop);
315}
316
318{
319 bool ret = false;
321 ret = _GetOgreAnimationState()->getLoop();
322
323 return ret;
324}
325
326void SAnim::SetOptimise(const bool& optimize)
327{
328 if (ogreAnimation)
329 ogreAnimation->optimise(optimize);
330}
331
332void SAnim::SetSpeed(const float& newSpeed)
333{
334 mSpeed = newSpeed;
335}
336
338{
339 return mSpeed;
340}
341
343{
344 bool ret = true;
346 ret = _GetOgreAnimationState()->hasEnded();
347
348 return ret;
349}
350
351void SAnim::AddTime(const float& time)
352{
354 _GetOgreAnimationState()->addTime(time);
355}
356
357void SAnim::SetPaused(const bool& pauseState)
358{
359 isPaused = pauseState;
360}
361
363{
364 return isPaused;
365}
366
367void SAnim::Update(const Ogre::FrameEvent& evt)
368{
369 if(!IsSequenceUpdated() && GetEnable() && !isPaused)
370 _UpdateImpl(evt);
371}
372
373SAnimationFrameListener::SAnimationFrameListener(SAnim* animationListened) : FrameListener()
374{
375 animation = animationListened;
376}
377
379{
380 animation = 0;
381}
382
383void SAnim::_UpdateImpl(const Ogre::FrameEvent &evt)
384{
385 //$BB max time is set to 16 millisecond (one frame / 60)
386 if (SkipFrame())
387 AddTime((float)evt.timeSinceLastFrame * GetSpeed());
388 else
389 AddTime(std::min((float)evt.timeSinceLastFrame, 0.016f) * GetSpeed());
390}
391
393{
394 return (bUpdatedBySequence > 0);
395}
396
397void SAnim::SetSequenceUpdated(const bool& value)
398{
399 if (value)
400 bUpdatedBySequence ++;
401 else
402 bUpdatedBySequence --;
403}
404
405bool SAnimationFrameListener::frameStarted(const Ogre::FrameEvent &evt)
406{
407 return true;
408}
409
410}
SScene * GetParentScene()
Definition SO3Anim.cpp:110
void RemoveAnimationTrack(SAnimTrack *existingAnimationTrack)
Definition SO3Anim.cpp:192
virtual void SetTimePosition(const float &timePosition)
Definition SO3Anim.cpp:214
virtual void _UpdateImpl(const Ogre::FrameEvent &evt)
Definition SO3Anim.cpp:383
void AddTime(const float &time)
Definition SO3Anim.cpp:351
void SetInitialLoop(const bool &loop)
Definition SO3Anim.cpp:263
void SetOptimise(const bool &optimize)
Definition SO3Anim.cpp:326
void DeleteAnimationTrack(SAnimTrack *existingAnimationTrack)
Definition SO3Anim.cpp:163
bool GetInitialLoop()
Definition SO3Anim.cpp:270
virtual SAnimTrack * CreateAnimationTrackImpl(const std::string &newAnimationTrackName)
Definition SO3Anim.cpp:158
bool IsSequenceUpdated()
Definition SO3Anim.cpp:392
AnimRotationInterpolationMode
Definition SO3Anim.h:60
virtual void SetWeight(const float &weight)
Definition SO3Anim.cpp:237
void Update(const Ogre::FrameEvent &evt)
Definition SO3Anim.cpp:367
virtual Ogre::AnimationState * _GetOgreAnimationState()=0
void Apply(const float &timePosition)
Definition SO3Anim.cpp:220
SAnimTrack * GetAnimationTrack(const unsigned short &index)
Definition SO3Anim.cpp:174
AnimInterpolationMode
Definition SO3Anim.h:55
AnimType GetType()
Definition SO3Anim.cpp:125
Ogre::Animation * GetOgreAnimationPointer()
Definition SO3Anim.cpp:115
float GetInitialWeight()
Definition SO3Anim.cpp:258
unsigned short GetIndex()
Definition SO3Anim.cpp:130
AnimInterpolationMode GetInterpolationMode()
Definition SO3Anim.cpp:280
void SetLoop(const bool &loop)
Definition SO3Anim.cpp:311
SAnimTrack * CreateAnimationTrack(const std::string &newAnimationTrackName)
Definition SO3Anim.cpp:145
virtual void SetEnable(const bool &enable)
Definition SO3Anim.cpp:295
SNode * GetParentNode()
Definition SO3Anim.cpp:120
AnimRotationInterpolationMode GetRotationInterpolationMode()
Definition SO3Anim.cpp:290
void SetRotationInterpolationMode(const AnimRotationInterpolationMode &interpolationMode)
Definition SO3Anim.cpp:285
unsigned short GetNumAnimationsTracks()
Definition SO3Anim.cpp:169
void SetPaused(const bool &pauseState)
Definition SO3Anim.cpp:357
void AddAnimationTrack(SAnimTrack *existingAnimationTrack)
Definition SO3Anim.cpp:187
virtual bool GetEnable()
Definition SO3Anim.cpp:302
bool GetPaused()
Definition SO3Anim.cpp:362
@ SO3_SEQUENCE_ANIM
Definition SO3Anim.h:52
Ogre::Animation * ogreAnimation
Definition SO3Anim.h:65
void SetLength(const float &length)
Definition SO3Anim.cpp:199
bool SkipFrame()
Definition SO3Anim.cpp:135
void SetInitialWeight(const float &weight)
Definition SO3Anim.cpp:252
SScene * mScene
Definition SO3Anim.h:67
virtual float GetWeight()
Definition SO3Anim.cpp:243
virtual ~SAnim()
Definition SO3Anim.cpp:62
float GetLength()
Definition SO3Anim.cpp:205
float GetTimePosition()
Definition SO3Anim.cpp:228
SAnimTrackList animationTrackList
Definition SO3Anim.h:66
bool GetLoop()
Definition SO3Anim.cpp:317
void SetInterpolationMode(const AnimInterpolationMode &interpolationMode)
Definition SO3Anim.cpp:275
bool HasEnded()
Definition SO3Anim.cpp:342
float GetSpeed()
Definition SO3Anim.cpp:337
void SetSpeed(const float &newSpeed)
Definition SO3Anim.cpp:332
void SetSequenceUpdated(const bool &value)
Definition SO3Anim.cpp:397
SAnimTrackList GetAnimationsTracks() const
Definition SO3Anim.cpp:182
void SetSkipFrame(const bool &state)
Definition SO3Anim.cpp:140
SAnimationFrameListener(SAnim *animationListened)
Definition SO3Anim.cpp:373
bool frameStarted(const Ogre::FrameEvent &evt)
Definition SO3Anim.cpp:405
SAnimMap GetAnimations() const
Ogre::SceneManager * GetOgreScenePointer()
Definition SO3Scene.cpp:449
void RemoveKeysWithAnim(SAnim *anim, const bool &reset)
std::vector< SAnimTrack * > SAnimTrackList
std::unordered_map< std::string, SAnim * > SAnimMap