bool Animatable::LoadJSON(const JSONValue& source, bool setInstanceDefault) { if (!Serializable::LoadJSON(source, setInstanceDefault)) return false; SetObjectAnimation(0); attributeAnimationInfos_.Clear(); JSONValue value = source.Get("objectanimation"); if (!value.IsNull()) { SharedPtr<ObjectAnimation> objectAnimation(new ObjectAnimation(context_)); if (!objectAnimation->LoadJSON(value)) return false; SetObjectAnimation(objectAnimation); } JSONValue attributeAnimationValue = source.Get("attributeanimation"); if (attributeAnimationValue.IsNull()) return true; if (!attributeAnimationValue.IsObject()) { URHO3D_LOGWARNING("'attributeanimation' value is present in JSON data, but is not a JSON object; skipping it"); return true; } const JSONObject& attributeAnimationObject = attributeAnimationValue.GetObject(); for (JSONObject::ConstIterator it = attributeAnimationObject.Begin(); it != attributeAnimationObject.End(); it++) { String name = it->first_; JSONValue value = it->second_; SharedPtr<ValueAnimation> attributeAnimation(new ValueAnimation(context_)); if (!attributeAnimation->LoadJSON(it->second_)) return false; String wrapModeString = source.Get("wrapmode").GetString(); WrapMode wrapMode = WM_LOOP; for (int i = 0; i <= WM_CLAMP; ++i) { if (wrapModeString == wrapModeNames[i]) { wrapMode = (WrapMode)i; break; } } float speed = value.Get("speed").GetFloat(); SetAttributeAnimation(name, attributeAnimation, wrapMode, speed); it++; } return true; }
Variant JSONValue::GetVariant(unsigned index) const { // Get child for variant JSONValue child = GetChild(index, JSON_OBJECT); if (child.IsNull()) return Variant::EMPTY; // Get type VariantType type = Variant::GetTypeFromName(child.GetString("type")); // Get value return child.GetVariantValue("value", type); }
bool ObjectAnimation::LoadJSON(const JSONValue& source) { attributeAnimationInfos_.Clear(); JSONValue attributeAnimationsValue = source.Get("attributeanimations"); if (attributeAnimationsValue.IsNull()) return true; if (!attributeAnimationsValue.IsObject()) return true; const JSONObject& attributeAnimationsObject = attributeAnimationsValue.GetObject(); for (JSONObject::ConstIterator it = attributeAnimationsObject.Begin(); it != attributeAnimationsObject.End(); it++) { String name = it->first_; JSONValue value = it->second_; SharedPtr<ValueAnimation> animation(new ValueAnimation(context_)); if (!animation->LoadJSON(value)) return false; String wrapModeString = value.Get("wrapmode").GetString(); WrapMode wrapMode = WM_LOOP; for (int i = 0; i <= WM_CLAMP; ++i) { if (wrapModeString == wrapModeNames[i]) { wrapMode = (WrapMode)i; break; } } float speed = value.Get("speed").GetFloat(); AddAttributeAnimation(name, animation, wrapMode, speed); } return true; }
bool Animation::BeginLoad(Deserializer& source) { unsigned memoryUse = sizeof(Animation); // Check ID if (source.ReadFileID() != "UANI") { URHO3D_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(); memoryUse += tracks * sizeof(AnimationTrack); // Read tracks for (unsigned i = 0; i < tracks; ++i) { AnimationTrack* newTrack = CreateTrack(source.ReadString()); 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"); SharedPtr<XMLFile> file(cache->GetTempResource<XMLFile>(xmlName, false)); 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; } // Optionally read triggers from a JSON file String jsonName = ReplaceExtension(GetName(), ".json"); SharedPtr<JSONFile> jsonFile(cache->GetTempResource<JSONFile>(jsonName, false)); if (jsonFile) { const JSONValue& rootVal = jsonFile->GetRoot(); JSONArray triggerArray = rootVal.Get("triggers").GetArray(); for (unsigned i = 0; i < triggerArray.Size(); i++) { const JSONValue& triggerValue = triggerArray.At(i); JSONValue normalizedTimeValue = triggerValue.Get("normalizedTime"); if (!normalizedTimeValue.IsNull()) AddTrigger(normalizedTimeValue.GetFloat(), true, triggerValue.GetVariant()); else { JSONValue timeVal = triggerValue.Get("time"); if (!timeVal.IsNull()) AddTrigger(timeVal.GetFloat(), false, triggerValue.GetVariant()); } } memoryUse += triggers_.Size() * sizeof(AnimationTriggerPoint); SetMemoryUse(memoryUse); return true; } SetMemoryUse(memoryUse); return true; }