source: OGRE/trunk/ogrenew/OgreMain/src/OgreAnimationState.cpp @ 692

Revision 692, 12.3 KB checked in by mattausch, 19 years ago (diff)

adding ogre 1.2 and dependencies

Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4    (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2005 The OGRE Team
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23-----------------------------------------------------------------------------
24*/
25#include "OgreStableHeaders.h"
26
27#include "OgreAnimationState.h"
28#include "OgreException.h"
29#include "OgreRoot.h"
30
31namespace Ogre
32{
33
34        //---------------------------------------------------------------------
35        AnimationState::AnimationState(AnimationStateSet* parent, const AnimationState &rhs)
36        {
37                mAnimationName = rhs.mAnimationName;
38                mTimePos = rhs.mTimePos;
39                mLoop = rhs.mLoop;
40                setLength(rhs.mLength);
41                mWeight = rhs.mWeight;
42                mParent = parent;
43
44                mParent->_notifyDirty();
45
46        }
47        //---------------------------------------------------------------------
48        AnimationState::~AnimationState()
49        {
50        }
51    //---------------------------------------------------------------------
52    AnimationState::AnimationState(const String& animName,
53                AnimationStateSet *parent, Real timePos, Real length, Real weight,
54                bool enabled)
55        : mAnimationName(animName), mParent(parent), mTimePos(timePos),
56                mWeight(weight), mEnabled(enabled)
57    {
58        mLoop = true;
59        setLength(length);
60                mParent->_notifyDirty();
61    }
62    //---------------------------------------------------------------------
63    const String& AnimationState::getAnimationName() const
64    {
65        return mAnimationName;
66    }
67    //---------------------------------------------------------------------
68    Real AnimationState::getTimePosition(void) const
69    {
70        return mTimePos;
71    }
72    //---------------------------------------------------------------------
73    void AnimationState::setTimePosition(Real timePos)
74    {
75                if (timePos != mTimePos)
76                {
77                        mTimePos = timePos;
78                        if (mLoop)
79                        {
80                                // Wrap
81                                mTimePos = fmod(mTimePos, mLength);
82                                if(mTimePos < 0)
83                                        mTimePos += mLength;     
84                        }
85                        else
86                        {
87                                // Clamp
88                                if(mTimePos < 0)
89                                        mTimePos = 0;
90                                else if (mTimePos > mLength)
91                                        mTimePos = mLength;
92                        }
93
94            if (mEnabled)
95                mParent->_notifyDirty();
96        }
97
98    }
99    //---------------------------------------------------------------------
100    Real AnimationState::getLength() const
101    {
102        return mLength;
103    }
104    //---------------------------------------------------------------------
105    void AnimationState::setLength(Real len)
106    {
107        mLength = len;
108        if (len != 0)
109        {
110            mInvLength = 1/len;
111        }
112        else
113        {
114            mInvLength = 0;
115        }
116    }
117    //---------------------------------------------------------------------
118    Real AnimationState::getWeight(void) const
119    {
120        return mWeight;
121    }
122    //---------------------------------------------------------------------
123    void AnimationState::setWeight(Real weight)
124    {
125        mWeight = weight;
126
127        if (mEnabled)
128            mParent->_notifyDirty();
129    }
130    //---------------------------------------------------------------------
131    void AnimationState::addTime(Real offset)
132    {
133        setTimePosition(mTimePos + offset);
134    }
135    //---------------------------------------------------------------------
136    bool AnimationState::getEnabled(void) const
137    {
138        return mEnabled;
139    }
140    //---------------------------------------------------------------------
141    void AnimationState::setEnabled(bool enabled)
142    {
143        mEnabled = enabled;
144        mParent->_notifyAnimationStateEnabled(this, enabled);
145    }
146    //---------------------------------------------------------------------
147    bool AnimationState::operator==(const AnimationState& rhs) const
148    {
149        if (mAnimationName == rhs.mAnimationName &&
150            mEnabled == rhs.mEnabled &&
151            mTimePos == rhs.mTimePos &&
152            mWeight == rhs.mWeight &&
153            mLength == rhs.mLength &&
154            mLoop == rhs.mLoop)
155        {
156            return true;
157        }
158        else
159        {
160            return false;
161        }
162    }
163    //---------------------------------------------------------------------
164    bool AnimationState::operator!=(const AnimationState& rhs) const
165    {
166        return !(*this == rhs);
167    }
168    //---------------------------------------------------------------------
169    void AnimationState::copyStateFrom(const AnimationState& animState)
170    {
171        mTimePos = animState.mTimePos;
172        mLength = animState.mLength;
173        mInvLength = animState.mInvLength;
174        mWeight = animState.mWeight;
175        mEnabled = animState.mEnabled;
176        mLoop = animState.mLoop;
177                mParent->_notifyDirty();
178
179    }
180        //---------------------------------------------------------------------
181        //---------------------------------------------------------------------
182        AnimationStateSet::AnimationStateSet()
183                : mDirtyFrameNumber(std::numeric_limits<unsigned long>::max())
184        {
185        }
186        //---------------------------------------------------------------------
187        AnimationStateSet::AnimationStateSet(const AnimationStateSet& rhs)
188                : mDirtyFrameNumber(std::numeric_limits<unsigned long>::max())
189        {
190                for (AnimationStateMap::const_iterator i = rhs.mAnimationStates.begin();
191                        i != rhs.mAnimationStates.end(); ++i)
192                {
193                        AnimationState* src = i->second;
194                        mAnimationStates[src->getAnimationName()] =
195                                new AnimationState(this, *src);
196                }
197
198        // Clone enabled animation state list
199        for (EnabledAnimationStateList::const_iterator it = rhs.mEnabledAnimationStates.begin();
200            it != rhs.mEnabledAnimationStates.end(); ++it)
201        {
202            const AnimationState* src = *it;
203            mEnabledAnimationStates.push_back(getAnimationState(src->getAnimationName()));
204        }
205        }
206        //---------------------------------------------------------------------
207        AnimationStateSet::~AnimationStateSet()
208        {
209                // Destroy
210                removeAllAnimationStates();
211        }
212        //---------------------------------------------------------------------
213        void AnimationStateSet::removeAnimationState(const String& name)
214        {
215                AnimationStateMap::iterator i = mAnimationStates.find(name);
216                if (i != mAnimationStates.end())
217                {
218            mEnabledAnimationStates.remove(i->second);
219
220                        delete i->second;
221                        mAnimationStates.erase(i);
222                }
223        }
224        //---------------------------------------------------------------------
225        void AnimationStateSet::removeAllAnimationStates(void)
226        {
227                for (AnimationStateMap::iterator i = mAnimationStates.begin();
228                        i != mAnimationStates.end(); ++i)
229                {
230                        delete i->second;
231                }
232                mAnimationStates.clear();
233        mEnabledAnimationStates.clear();
234
235        }
236        //---------------------------------------------------------------------
237        AnimationState* AnimationStateSet::createAnimationState(const String& name, 
238                Real timePos, Real length, Real weight, bool enabled)
239        {
240                AnimationStateMap::iterator i = mAnimationStates.find(name);
241                if (i != mAnimationStates.end())
242                {
243                        OGRE_EXCEPT(Exception::ERR_DUPLICATE_ITEM,
244                                "State for animation named '" + name + "' already exists.",
245                                "AnimationStateSet::createAnimationState");
246                }
247
248                AnimationState* newState = new AnimationState(name, this, timePos,
249                        length, weight, enabled);
250                mAnimationStates[name] = newState;
251
252                return newState;
253
254        }
255        //---------------------------------------------------------------------
256        AnimationState* AnimationStateSet::getAnimationState(const String& name) const
257        {
258                AnimationStateMap::const_iterator i = mAnimationStates.find(name);
259                if (i == mAnimationStates.end())
260                {
261                        OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND,
262                                "No state found for animation named '" + name + "'",
263                                "AnimationStateSet::getAnimationState");
264                }
265                return i->second;
266        }
267        //---------------------------------------------------------------------
268        bool AnimationStateSet::hasAnimationState(const String& name) const
269        {
270                return mAnimationStates.find(name) != mAnimationStates.end();
271        }
272        //---------------------------------------------------------------------
273        AnimationStateIterator AnimationStateSet::getAnimationStateIterator(void)
274        {
275                return AnimationStateIterator(
276                        mAnimationStates.begin(), mAnimationStates.end());
277        }
278        //---------------------------------------------------------------------
279        ConstAnimationStateIterator AnimationStateSet::getAnimationStateIterator(void) const
280        {
281                return ConstAnimationStateIterator(
282                        mAnimationStates.begin(), mAnimationStates.end());
283        }
284        //---------------------------------------------------------------------
285        void AnimationStateSet::copyMatchingState(AnimationStateSet* target) const
286        {
287        AnimationStateMap::iterator i, iend;
288        iend = target->mAnimationStates.end();
289        for (i = target->mAnimationStates.begin(); i != iend; ++i) {
290            AnimationStateMap::const_iterator iother = mAnimationStates.find(i->first);
291            if (iother == mAnimationStates.end()) {
292                OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND, "No animation entry found named " + i->first,
293                    "AnimationStateSet::copyMatchingState");
294            } else {
295                i->second->copyStateFrom(*(iother->second));
296            }
297        }
298
299        // Copy matching enabled animation state list
300        target->mEnabledAnimationStates.clear();
301
302        EnabledAnimationStateList::const_iterator it, itend;
303        itend = mEnabledAnimationStates.end();
304        for (it = mEnabledAnimationStates.begin(); it != itend; ++it)
305        {
306            const AnimationState* src = *it;
307            AnimationStateMap::const_iterator itarget = target->mAnimationStates.find(src->getAnimationName());
308            if (itarget != target->mAnimationStates.end())
309            {
310                target->mEnabledAnimationStates.push_back(itarget->second);
311            }
312        }
313
314        target->mDirtyFrameNumber = mDirtyFrameNumber;
315    }
316    //---------------------------------------------------------------------
317    void AnimationStateSet::_notifyDirty(void)
318    {
319        mDirtyFrameNumber = Root::getSingleton().getCurrentFrameNumber();
320    }
321    //---------------------------------------------------------------------
322    void AnimationStateSet::_notifyAnimationStateEnabled(AnimationState* target, bool enabled)
323    {
324        // Remove from enabled animation state list first
325        mEnabledAnimationStates.remove(target);
326
327        // Add to enabled animation state list if need
328        if (enabled)
329        {
330            mEnabledAnimationStates.push_back(target);
331        }
332
333        // Set the dirty frame number
334        _notifyDirty();
335    }
336    //---------------------------------------------------------------------
337    ConstEnabledAnimationStateIterator AnimationStateSet::getEnabledAnimationStateIterator(void) const
338    {
339        return ConstEnabledAnimationStateIterator(
340            mEnabledAnimationStates.begin(), mEnabledAnimationStates.end());
341    }
342        //---------------------------------------------------------------------
343        //---------------------------------------------------------------------
344        Real AnimationStateControllerValue::getValue(void) const
345        {
346                return mTargetAnimationState->getTimePosition() / mTargetAnimationState->getLength();
347        }
348        //---------------------------------------------------------------------
349        void AnimationStateControllerValue::setValue(Real value)
350        {
351                mTargetAnimationState->setTimePosition(value * mTargetAnimationState->getLength());
352        }
353
354
355}
356
Note: See TracBrowser for help on using the repository browser.