Project

General

Profile

SO3Engine
SO3SequenceAnimation.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
34
35namespace SO3
36{
37
38SSequenceAnimation::SSequenceAnimation(SScene* scene, const std::string& animationName, const unsigned short& id) : SAnim(scene, animationName, 0, id, SAnim::SO3_SEQUENCE_ANIM)
39{
40 ogreAnimation = mScene->GetOgreScenePointer()->createAnimation(animationName, 0.0f);
41 ogreAnimationState = mScene->GetOgreScenePointer()->createAnimationState(animationName);
42 mLastPos = 0.0f;
43 mState = false;
44}
45
46SSequenceAnimation::SSequenceAnimation() : SAnim(0, "", 0, 0, SO3_UNKNOWN_ANIM)
47{
48 // Forbiden (private)
49}
50
52{
53 if (mScene->GetOgreScenePointer()->hasAnimationState(name))
54 mScene->GetOgreScenePointer()->destroyAnimationState(name);
55 ogreAnimationState = 0;
56 if (mScene->GetOgreScenePointer()->hasAnimation(name))
57 mScene->GetOgreScenePointer()->destroyAnimation(name);
58}
59
60SAnimTrack* SSequenceAnimation::CreateAnimationTrackImpl(const std::string& newAnimationTrackName)
61{
62 return new SSequenceAnimationTrack(newAnimationTrackName, this, 0);
63}
64
66{
67 return ogreAnimationState;
68}
69
70void SSequenceAnimation::SetTimePosition(const float& timePosition)
71{
73 _GetOgreAnimationState()->setTimePosition(timePosition);
74
75 //update tracks
76 if (GetEnable())
77 {
78 SAnimTrackList::iterator iAnimationTrack = animationTrackList.begin();
79 while (iAnimationTrack != animationTrackList.end())
80 {
81 (*iAnimationTrack)->Update(timePosition);
82 iAnimationTrack++;
83 }
84 //force update current animations
85 //GetParentScene()->GetOgreScenePointer()->_applySceneAnimations();
86 }
87
88 mLastPos = timePosition;
89}
90
91void SSequenceAnimation::SetEnable(const bool& enable)
92{
93 //if (ogreAnimationState)
94 // ogreAnimationState->setEnabled(enable);
95 mState = enable;
96
97 SetPaused(!enable);
98
99 const SAnimTrackList animationTrackCopy = animationTrackList;
100 SAnimTrackList::const_iterator iAnimationTrack = animationTrackCopy.begin();
101 while (iAnimationTrack != animationTrackCopy.end())
102 {
103 static_cast<SSequenceAnimationTrack*>(*iAnimationTrack)->EnableTrackKeys(enable);
104 iAnimationTrack++;
105 }
106}
107
109{
110 return mState;
111}
112
114{
115 float length = 0.0f;
116 const SAnimTrackList animationTrackCopy = animationTrackList;
117 SAnimTrackList::const_iterator iAnimationTrack = animationTrackCopy.begin();
118 while (iAnimationTrack != animationTrackCopy.end())
119 {
120 if (static_cast<SSequenceAnimationTrack*>(*iAnimationTrack)->GetLength() > length)
121 length = static_cast<SSequenceAnimationTrack*>(*iAnimationTrack)->GetLength();
122
123 iAnimationTrack++;
124 }
125 SetLength(length);
126}
127
129{
130 return mLastPos;
131}
132
133void SSequenceAnimation::_UpdateImpl(const Ogre::FrameEvent &evt)
134{
135 //$BB max time is set to 16 millisecond (one frame / 60)
136 Ogre::Real maxtime = (evt.timeSinceLastFrame <= 0.016f) ? evt.timeSinceLastFrame : (evt.timeSinceLastFrame * 0.016f / evt.timeSinceLastFrame);
137
138 if (SkipFrame())
139 AddTime((float)evt.timeSinceLastFrame * GetSpeed());
140 else
141 AddTime(maxtime * GetSpeed());
142
143 float currentPos = GetTimePosition();
144
145 SAnimTrackList::iterator iAnimationTrack = animationTrackList.begin();
146 while (iAnimationTrack != animationTrackList.end())
147 {
148 (*iAnimationTrack)->Update(GetTimePosition());
149 iAnimationTrack++;
150 }
151
152 mLastPos = currentPos;
153}
154
155}
void AddTime(const float &time)
Definition SO3Anim.cpp:351
void SetPaused(const bool &pauseState)
Definition SO3Anim.cpp:357
Ogre::Animation * ogreAnimation
Definition SO3Anim.h:65
void SetLength(const float &length)
Definition SO3Anim.cpp:199
bool SkipFrame()
Definition SO3Anim.cpp:135
SScene * mScene
Definition SO3Anim.h:67
float GetTimePosition()
Definition SO3Anim.cpp:228
SAnimTrackList animationTrackList
Definition SO3Anim.h:66
float GetSpeed()
Definition SO3Anim.cpp:337
std::string name
Definition SO3DataScol.h:44
Ogre::SceneManager * GetOgreScenePointer()
Definition SO3Scene.cpp:449
virtual void SetEnable(const bool &enable)
virtual Ogre::AnimationState * _GetOgreAnimationState()
virtual void _UpdateImpl(const Ogre::FrameEvent &evt)
virtual SAnimTrack * CreateAnimationTrackImpl(const std::string &newAnimationTrackName)
virtual void SetTimePosition(const float &timePosition)
std::vector< SAnimTrack * > SAnimTrackList