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) animatedNetworkAttributes_.Insert(attributeInfo); 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) animatedNetworkAttributes_.Erase(¤tInstance->GetAttributeInfo()); attributeAnimationInstances_.Erase(name); OnAttributeAnimationRemoved(); } }
void Animatable::SetAttributeAnimation(const String& name, ValueAnimation* attributeAnimation, WrapMode wrapMode, float speed) { AttributeAnimationInfo* info = GetAttributeAnimationInfo(name); if (attributeAnimation) { if (info && attributeAnimation == info->GetAnimation()) { info->SetWrapMode(wrapMode); info->SetSpeed(speed); return; } // Use shared ptr to avoid memory leak SharedPtr<ValueAnimation> animationPtr(attributeAnimation); // Get attribute info const AttributeInfo* attributeInfo = 0; if (info) attributeInfo = &info->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 (animationPtr->GetValueType() != attributeInfo->type_) { LOGERROR("Invalid value type"); return; } // Add network attribute to set if (attributeInfo->mode_ & AM_NET) animatedNetworkAttributes_.Insert(attributeInfo); attributeAnimationInfos_[name] = new AttributeAnimationInfo(this, *attributeInfo, animationPtr, wrapMode, speed); if (!info) OnAttributeAnimationAdded(); } else { if (!info) return; // Remove network attribute from set if (info->GetAttributeInfo().mode_ & AM_NET) animatedNetworkAttributes_.Erase(&info->GetAttributeInfo()); attributeAnimationInfos_.Erase(name); OnAttributeAnimationRemoved(); } }