| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- //
- // Copyright (c) 2008-2014 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 "Animatable.h"
- #include "AttributeAnimation.h"
- #include "AttributeAnimationInstance.h"
- #include "Context.h"
- #include "Log.h"
- #include "ObjectAnimation.h"
- #include "ResourceCache.h"
- #include "XMLElement.h"
- #include "DebugNew.h"
- namespace Urho3D
- {
- extern const char* wrapModeNames[];
- Animatable::Animatable(Context* context) :
- Serializable(context),
- animationEnabled_(true)
- {
- }
- Animatable::~Animatable()
- {
- }
- void Animatable::RegisterObject(Context* context)
- {
- ACCESSOR_ATTRIBUTE(Animatable, VAR_RESOURCEREF, "Object Animation", GetObjectAnimationAttr, SetObjectAnimationAttr, ResourceRef, ResourceRef(ObjectAnimation::GetTypeStatic()), AM_DEFAULT);
- }
- bool Animatable::LoadXML(const XMLElement& source, bool setInstanceDefault)
- {
- if (!Serializable::LoadXML(source, setInstanceDefault))
- return false;
- SetObjectAnimation(0);
- attributeAnimationInstances_.Clear();
- XMLElement elem = source.GetChild("objectAnimation");
- if (elem)
- {
- SharedPtr<ObjectAnimation> objectAnimation(new ObjectAnimation(context_));
- if (!objectAnimation->LoadXML(elem))
- return false;
- SetObjectAnimation(objectAnimation);
- }
- elem = source.GetChild("attributeAnimation");
- while (elem)
- {
- String name = elem.GetAttribute("name");
- SharedPtr<AttributeAnimation> attributeAnimation(new AttributeAnimation(context_));
- if (!attributeAnimation->LoadXML(elem))
- return false;
- String wrapModeString = source.GetAttribute("wrapMode");
- WrapMode wrapMode = WM_LOOP;
- for (int i = 0; i <= WM_CLAMP; ++i)
- {
- if (wrapModeString == wrapModeNames[i])
- {
- wrapMode = (WrapMode)i;
- break;
- }
- }
- float speed = elem.GetFloat("speed");
- SetAttributeAnimation(name, attributeAnimation, wrapMode, speed);
- elem = elem.GetNext("attributeAnimation");
- }
- return true;
- }
- bool Animatable::SaveXML(XMLElement& dest) const
- {
- if (!Serializable::SaveXML(dest))
- return false;
- // Object animation without name
- if (objectAnimation_ && objectAnimation_->GetName().Empty())
- {
- XMLElement elem = dest.CreateChild("objectAnimation");
- if (!objectAnimation_->SaveXML(elem))
- return false;
- }
- for (HashMap<String, SharedPtr<AttributeAnimationInstance> >::ConstIterator i = attributeAnimationInstances_.Begin(); i != attributeAnimationInstances_.End(); ++i)
- {
- AttributeAnimation* attributeAnimation = i->second_->GetAttributeAnimation();
- if (attributeAnimation->GetObjectAnimation())
- continue;
- const AttributeInfo& attr = i->second_->GetAttributeInfo();
- XMLElement elem = dest.CreateChild("attributeAnimation");
- elem.SetAttribute("name", attr.name_);
- if (!attributeAnimation->SaveXML(elem))
- return false;
- elem.SetAttribute("wrapMode", wrapModeNames[i->second_->GetWrapMode()]);
- elem.SetFloat("speed", i->second_->GetSpeed());
- }
- return true;
- }
- void Animatable::SetAnimationEnabled(bool animationEnabled)
- {
- animationEnabled_ = animationEnabled;
- }
- void Animatable::SetObjectAnimation(ObjectAnimation* objectAnimation)
- {
- if (objectAnimation == objectAnimation_)
- return;
- if (objectAnimation_)
- OnObjectAnimationRemoved(objectAnimation_);
- objectAnimation_ = objectAnimation;
- if (objectAnimation_)
- OnObjectAnimationAdded(objectAnimation_);
- }
- void Animatable::SetAttributeAnimation(const String& name, AttributeAnimation* attributeAnimation, WrapMode wrapMode, float speed)
- {
- AttributeAnimationInstance* currentInstance = GetAttributeAnimationInstance(name);
- if (attributeAnimation)
- {
- if (currentInstance && attributeAnimation == currentInstance->GetAttributeAnimation())
- {
- currentInstance->SetWrapMode(wrapMode);
- currentInstance->SetSpeed(speed);
- return;
- }
- // Get attribute info
- const AttributeInfo* attributeInfo = 0;
- if (currentInstance)
- attributeInfo = ¤tInstance->GetAttributeInfo();
- else
- {
- const Vector<AttributeInfo>* attributes = GetAttributes();
- if (!attributes)
- {
- LOGERROR(GetTypeName() + " has no attributes");
- return;
- }
- for (Vector<AttributeInfo>::ConstIterator i = attributes->Begin(); i != attributes->End(); ++i)
- {
- if (name == (*i).name_)
- {
- attributeInfo = &(*i);
- break;
- }
- }
- }
- if (!attributeInfo)
- {
- LOGERROR("Invalid name: " + name);
- return;
- }
- // Check value type is same with attribute type
- if (attributeAnimation->GetValueType() != attributeInfo->type_)
- {
- LOGERROR("Invalid value type");
- return;
- }
- // Add network attribute to set
- if (attributeInfo->mode_ & AM_NET)
- {
- const Vector<AttributeInfo>* networkAttributes = GetNetworkAttributes();
- for (Vector<AttributeInfo>::ConstIterator i = networkAttributes->Begin(); i != networkAttributes->End(); ++i)
- {
- if (name == (*i).name_)
- {
- animatedNetworkAttributes_.Insert(&(*i));
- break;
- }
- }
- }
- attributeAnimationInstances_[name] = new AttributeAnimationInstance(this, *attributeInfo, attributeAnimation, wrapMode, speed);
- if (!currentInstance)
- OnAttributeAnimationAdded();
- }
- else
- {
- if (!currentInstance)
- return;
- // Remove network attribute from set
- if (currentInstance->GetAttributeInfo().mode_ & AM_NET)
- {
- const Vector<AttributeInfo>* networkAttributes = GetNetworkAttributes();
- for (Vector<AttributeInfo>::ConstIterator i = networkAttributes->Begin(); i != networkAttributes->End(); ++i)
- {
- if (name == (*i).name_)
- {
- animatedNetworkAttributes_.Erase(&(*i));
- break;
- }
- }
- }
- attributeAnimationInstances_.Erase(name);
- OnAttributeAnimationRemoved();
- }
- }
- void Animatable::SetAttributeAnimationWrapMode(const String& name, WrapMode wrapMode)
- {
- AttributeAnimationInstance* currentInstance = GetAttributeAnimationInstance(name);
- if (currentInstance)
- currentInstance->SetWrapMode(wrapMode);
- }
- void Animatable::SetAttributeAnimationSpeed(const String& name, float speed)
- {
- AttributeAnimationInstance* currentInstance = GetAttributeAnimationInstance(name);
- if (currentInstance)
- currentInstance->SetSpeed(speed);
- }
- ObjectAnimation* Animatable::GetObjectAnimation() const
- {
- return objectAnimation_;
- }
- AttributeAnimation* Animatable::GetAttributeAnimation(const String& name) const
- {
- const AttributeAnimationInstance* instance = GetAttributeAnimationInstance(name);
- return instance ? instance->GetAttributeAnimation() : 0;
- }
- WrapMode Animatable::GetAttributeAnimationWrapMode(const String& name) const
- {
- const AttributeAnimationInstance* instance = GetAttributeAnimationInstance(name);
- return instance ? instance->GetWrapMode() : WM_LOOP;
- }
- float Animatable::GetAttributeAnimationSpeed(const String& name) const
- {
- const AttributeAnimationInstance* instance = GetAttributeAnimationInstance(name);
- return instance ? instance->GetSpeed() : 1.0f;
- }
- void Animatable::SetObjectAnimationAttr(ResourceRef value)
- {
- if (!value.name_.Empty())
- {
- ResourceCache* cache = GetSubsystem<ResourceCache>();
- SetObjectAnimation(cache->GetResource<ObjectAnimation>(value.name_));
- }
- }
- ResourceRef Animatable::GetObjectAnimationAttr() const
- {
- return GetResourceRef(objectAnimation_, ObjectAnimation::GetTypeStatic());
- }
- void Animatable::OnObjectAnimationAdded(ObjectAnimation* objectAnimation)
- {
- if (!objectAnimation)
- return;
- // Set all attribute animations from the object animation
- HashMap<String, SharedPtr<AttributeAnimation> > attributeAnimations = objectAnimation->GetAttributeAnimations();
- for (HashMap<String, SharedPtr<AttributeAnimation> >::Iterator i = attributeAnimations.Begin(); i != attributeAnimations.End(); ++i)
- SetAttributeAnimation(i->first_, i->second_, objectAnimation->GetAttributeAnimationWrapMode(i->first_), objectAnimation->GetAttributeAnimationSpeed(i->first_));
- }
- void Animatable::OnObjectAnimationRemoved(ObjectAnimation* objectAnimation)
- {
- if (!objectAnimation)
- return;
- // Just remove all attribute animations from the object animation
- Vector<String> names;
- for (HashMap<String, SharedPtr<AttributeAnimationInstance> >::Iterator i = attributeAnimationInstances_.Begin(); i != attributeAnimationInstances_.End(); ++i)
- {
- if (i->second_->GetAttributeAnimation()->GetObjectAnimation() == objectAnimation)
- names.Push(i->first_);
- }
- for (unsigned int i = 0; i < names.Size(); ++i)
- SetAttributeAnimation(names[i], 0);
- }
- void Animatable::UpdateAttributeAnimations(float timeStep)
- {
- if (!animationEnabled_)
- return;
- Vector<String> finishedNames;
- for (HashMap<String, SharedPtr<AttributeAnimationInstance> >::ConstIterator i = attributeAnimationInstances_.Begin(); i != attributeAnimationInstances_.End(); ++i)
- {
- if (i->second_->Update(timeStep))
- finishedNames.Push(i->second_->GetAttributeInfo().name_);
- }
- for (unsigned i = 0; i < finishedNames.Size(); ++i)
- SetAttributeAnimation(finishedNames[i], 0);
- }
- bool Animatable::IsAnimatedNetworkAttribute(const AttributeInfo& attrInfo) const
- {
- return animatedNetworkAttributes_.Find(&attrInfo) != animatedNetworkAttributes_.End();
- }
- AttributeAnimationInstance* Animatable::GetAttributeAnimationInstance(const String& name) const
- {
- HashMap<String, SharedPtr<AttributeAnimationInstance> >::ConstIterator i = attributeAnimationInstances_.Find(name);
- if (i != attributeAnimationInstances_.End())
- return i->second_;
- return 0;
- }
- }
|