| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- //
- // Copyright (c) 2008-2013 the Urho3D project.
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include "Precompiled.h"
- #include "Animation.h"
- #include "Context.h"
- #include "Deserializer.h"
- #include "FileSystem.h"
- #include "Log.h"
- #include "Profiler.h"
- #include "ResourceCache.h"
- #include "Serializer.h"
- #include "XMLFile.h"
- #include "DebugNew.h"
- namespace Urho3D
- {
- inline bool CompareTriggers(AnimationTriggerPoint& lhs, AnimationTriggerPoint& rhs)
- {
- return lhs.time_ < rhs.time_;
- }
- void AnimationTrack::GetKeyFrameIndex(float time, unsigned& index) const
- {
- if (time < 0.0f)
- time = 0.0f;
-
- if (index >= keyFrames_.Size())
- index = keyFrames_.Size() - 1;
-
- // Check for being too far ahead
- while (index && time < keyFrames_[index].time_)
- --index;
-
- // Check for being too far behind
- while (index < keyFrames_.Size() - 1 && time >= keyFrames_[index + 1].time_)
- ++index;
- }
- Animation::Animation(Context* context) :
- Resource(context),
- length_(0.f)
- {
- }
- Animation::~Animation()
- {
- }
- void Animation::RegisterObject(Context* context)
- {
- context->RegisterFactory<Animation>();
- }
- bool Animation::Load(Deserializer& source)
- {
- PROFILE(LoadAnimation);
-
- unsigned memoryUse = sizeof(Animation);
-
- // Check ID
- if (source.ReadFileID() != "UANI")
- {
- LOGERROR(source.GetName() + " is not a valid animation file");
- return false;
- }
-
- // Read name and length
- animationName_ = source.ReadString();
- animationNameHash_ = animationName_;
- length_ = source.ReadFloat();
- tracks_.Clear();
-
- unsigned tracks = source.ReadUInt();
- tracks_.Resize(tracks);
- memoryUse += tracks * sizeof(AnimationTrack);
-
- // Read tracks
- for (unsigned i = 0; i < tracks; ++i)
- {
- AnimationTrack& newTrack = tracks_[i];
- newTrack.name_ = source.ReadString();
- newTrack.nameHash_ = newTrack.name_;
- newTrack.channelMask_ = source.ReadUByte();
-
- unsigned keyFrames = source.ReadUInt();
- newTrack.keyFrames_.Resize(keyFrames);
- memoryUse += keyFrames * sizeof(AnimationKeyFrame);
-
- // Read keyframes of the track
- for (unsigned j = 0; j < keyFrames; ++j)
- {
- AnimationKeyFrame& newKeyFrame = newTrack.keyFrames_[j];
- newKeyFrame.time_ = source.ReadFloat();
- if (newTrack.channelMask_ & CHANNEL_POSITION)
- newKeyFrame.position_ = source.ReadVector3();
- if (newTrack.channelMask_ & CHANNEL_ROTATION)
- newKeyFrame.rotation_ = source.ReadQuaternion();
- if (newTrack.channelMask_ & CHANNEL_SCALE)
- newKeyFrame.scale_ = source.ReadVector3();
- }
- }
-
- // Optionally read triggers from an XML file
- ResourceCache* cache = GetSubsystem<ResourceCache>();
- String xmlName = ReplaceExtension(GetName(), ".xml");
-
- if (cache->Exists(xmlName))
- {
- XMLFile* file = cache->GetResource<XMLFile>(xmlName);
- if (file)
- {
- XMLElement rootElem = file->GetRoot();
- XMLElement triggerElem = rootElem.GetChild("trigger");
- while (triggerElem)
- {
- if (triggerElem.HasAttribute("normalizedtime"))
- AddTrigger(triggerElem.GetFloat("normalizedtime"), true, triggerElem.GetVariant());
- else if (triggerElem.HasAttribute("time"))
- AddTrigger(triggerElem.GetFloat("time"), false, triggerElem.GetVariant());
-
- triggerElem = triggerElem.GetNext("trigger");
- }
-
- memoryUse += triggers_.Size() * sizeof(AnimationTriggerPoint);
- }
- }
-
- SetMemoryUse(memoryUse);
- return true;
- }
- bool Animation::Save(Serializer& dest) const
- {
- // Write ID, name and length
- dest.WriteFileID("UANI");
- dest.WriteString(animationName_);
- dest.WriteFloat(length_);
-
- // Write tracks
- dest.WriteUInt(tracks_.Size());
- for (unsigned i = 0; i < tracks_.Size(); ++i)
- {
- const AnimationTrack& track = tracks_[i];
- dest.WriteString(track.name_);
- dest.WriteUByte(track.channelMask_);
- dest.WriteUInt(track.keyFrames_.Size());
-
- // Write keyframes of the track
- for (unsigned j = 0; j < track.keyFrames_.Size(); ++j)
- {
- const AnimationKeyFrame& keyFrame = track.keyFrames_[j];
- dest.WriteFloat(keyFrame.time_);
- if (track.channelMask_ & CHANNEL_POSITION)
- dest.WriteVector3(keyFrame.position_);
- if (track.channelMask_ & CHANNEL_ROTATION)
- dest.WriteQuaternion(keyFrame.rotation_);
- if (track.channelMask_ & CHANNEL_SCALE)
- dest.WriteVector3(keyFrame.scale_);
- }
- }
-
- // If triggers have been defined, write an XML file for them
- if (triggers_.Size())
- {
- File* destFile = dynamic_cast<File*>(&dest);
- if (destFile)
- {
- String xmlName = ReplaceExtension(destFile->GetName(), ".xml");
-
- SharedPtr<XMLFile> xml(new XMLFile(context_));
- XMLElement rootElem = xml->CreateRoot("animation");
-
- for (unsigned i = 0; i < triggers_.Size(); ++i)
- {
- XMLElement triggerElem = rootElem.CreateChild("trigger");
- triggerElem.SetFloat("time", triggers_[i].time_);
- triggerElem.SetVariant(triggers_[i].data_);
- }
-
- File xmlFile(context_, xmlName, FILE_WRITE);
- xml->Save(xmlFile);
- }
- else
- LOGWARNING("Can not save animation trigger data when not saving into a file");
- }
-
- return true;
- }
- void Animation::SetAnimationName(const String& name)
- {
- animationName_ = name;
- animationNameHash_ = StringHash(name);
- }
- void Animation::SetLength(float length)
- {
- length_ = Max(length, 0.0f);
- }
- void Animation::SetTracks(const Vector<AnimationTrack>& tracks)
- {
- tracks_ = tracks;
- }
- void Animation::AddTrigger(float time, bool timeIsNormalized, const Variant& data)
- {
- AnimationTriggerPoint newTrigger;
- newTrigger.time_ = timeIsNormalized ? time * length_ : time;
- newTrigger.data_ = data;
- triggers_.Push(newTrigger);
-
- Sort(triggers_.Begin(), triggers_.End(), CompareTriggers);
- }
- void Animation::RemoveTrigger(unsigned index)
- {
- if (index < triggers_.Size())
- triggers_.Erase(index);
- }
- void Animation::RemoveAllTriggers()
- {
- triggers_.Clear();
- }
- void Animation::SetNumTriggers(unsigned num)
- {
- triggers_.Resize(num);
- }
- const AnimationTrack* Animation::GetTrack(unsigned index) const
- {
- return index < tracks_.Size() ? &tracks_[index] : 0;
- }
- const AnimationTrack* Animation::GetTrack(const String& name) const
- {
- for (Vector<AnimationTrack>::ConstIterator i = tracks_.Begin(); i != tracks_.End(); ++i)
- {
- if (i->name_ == name)
- return &(*i);
- }
-
- return 0;
- }
- const AnimationTrack* Animation::GetTrack(StringHash nameHash) const
- {
- for (Vector<AnimationTrack>::ConstIterator i = tracks_.Begin(); i != tracks_.End(); ++i)
- {
- if (i->nameHash_ == nameHash)
- return &(*i);
- }
-
- return 0;
- }
- }
|