bool ValueAnimation::LoadXML(const XMLElement& source) { valueType_ = VAR_NONE; eventFrames_.Clear(); String interpMethodString = source.GetAttribute("interpolationmethod"); InterpMethod method = (InterpMethod)GetStringListIndex(interpMethodString.CString(), interpMethodNames, IM_LINEAR); SetInterpolationMethod(method); if (interpolationMethod_ == IM_SPLINE) splineTension_ = source.GetFloat("splinetension"); XMLElement keyFrameElem = source.GetChild("keyframe"); while (keyFrameElem) { float time = keyFrameElem.GetFloat("time"); Variant value = keyFrameElem.GetVariant(); SetKeyFrame(time, value); keyFrameElem = keyFrameElem.GetNext("keyframe"); } XMLElement eventFrameElem = source.GetChild("eventframe"); while (eventFrameElem) { float time = eventFrameElem.GetFloat("time"); unsigned eventType = eventFrameElem.GetUInt("eventtype"); VariantMap eventData = eventFrameElem.GetChild("eventdata").GetVariantMap(); SetEventFrame(time, StringHash(eventType), eventData); eventFrameElem = eventFrameElem.GetNext("eventframe"); } return true; }
VariantMap XMLElement::GetVariantMap() const { VariantMap ret; XMLElement variantElem = GetChild("variant"); while (variantElem) { StringHash key(variantElem.GetUInt("hash")); ret[key] = variantElem.GetVariant(); variantElem = variantElem.GetNext("variant"); } return ret; }
VariantMap XMLElement::GetVariantMap() const { VariantMap ret; XMLElement variantElem = GetChild("variant"); while (variantElem) { // If this is a manually edited map, user can not be expected to calculate hashes manually. Also accept "name" attribute if (variantElem.HasAttribute("name")) ret[StringHash(variantElem.GetAttribute("name"))] = variantElem.GetVariant(); else if (variantElem.HasAttribute("hash")) ret[StringHash(variantElem.GetUInt("hash"))] = variantElem.GetVariant(); variantElem = variantElem.GetNext("variant"); } return ret; }
bool Menu::LoadXML(const XMLElement& source, XMLFile* styleFile, bool setInstanceDefault) { // Get style override if defined String styleName = source.GetAttribute("style"); // Apply the style first, if the style file is available if (styleFile) { // If not defined, use type name if (styleName.Empty()) styleName = GetTypeName(); SetStyle(styleName, styleFile); } // The 'style' attribute value in the style file cannot be equals to original's applied style to prevent infinite loop else if (!styleName.Empty() && styleName != appliedStyle_) { // Attempt to use the default style file styleFile = GetDefaultStyle(); if (styleFile) { // Remember the original applied style String appliedStyle(appliedStyle_); SetStyle(styleName, styleFile); appliedStyle_ = appliedStyle; } } // Then load rest of the attributes from the source if (!Serializable::LoadXML(source, setInstanceDefault)) return false; unsigned nextInternalChild = 0; // Load child elements. Internal elements are not to be created as they already exist XMLElement childElem = source.GetChild("element"); while (childElem) { bool internalElem = childElem.GetBool("internal"); bool popupElem = childElem.GetBool("popup"); String typeName = childElem.GetAttribute("type"); if (typeName.Empty()) typeName = "UIElement"; unsigned index = childElem.HasAttribute("index") ? childElem.GetUInt("index") : M_MAX_UNSIGNED; UIElement* child = 0; if (!internalElem) { if (!popupElem) child = CreateChild(typeName, String::EMPTY, index); else { // Do not add the popup element as a child even temporarily, as that can break layouts SharedPtr<UIElement> popup = DynamicCast<UIElement>(context_->CreateObject(typeName)); if (!popup) URHO3D_LOGERROR("Could not create popup element type " + typeName); else { child = popup; SetPopup(popup); } } } else { // An internal popup element should already exist if (popupElem) child = popup_; else { for (unsigned i = nextInternalChild; i < children_.Size(); ++i) { if (children_[i]->IsInternal() && children_[i]->GetTypeName() == typeName) { child = children_[i]; nextInternalChild = i + 1; break; } } if (!child) URHO3D_LOGWARNING("Could not find matching internal child element of type " + typeName + " in " + GetTypeName()); } } if (child) { if (!styleFile) styleFile = GetDefaultStyle(); // As popup is not a child element in itself, the parental chain to acquire the default style file is broken for popup's child elements // To recover from this, popup needs to have the default style set in its own instance so the popup's child elements can find it later if (popupElem) child->SetDefaultStyle(styleFile); if (!child->LoadXML(childElem, styleFile, setInstanceDefault)) return false; } childElem = childElem.GetNext("element"); } ApplyAttributes(); return true; }
bool Scene::LoadAsyncXML(File* file, LoadMode mode) { if (!file) { URHO3D_LOGERROR("Null file for async loading"); return false; } StopAsyncLoading(); SharedPtr<XMLFile> xml(new XMLFile(context_)); if (!xml->Load(*file)) return false; if (mode > LOAD_RESOURCES_ONLY) { URHO3D_LOGINFO("Loading scene from " + file->GetName()); Clear(); } asyncLoading_ = true; asyncProgress_.xmlFile_ = xml; asyncProgress_.file_ = file; asyncProgress_.mode_ = mode; asyncProgress_.loadedNodes_ = asyncProgress_.totalNodes_ = asyncProgress_.loadedResources_ = asyncProgress_.totalResources_ = 0; asyncProgress_.resources_.Clear(); if (mode > LOAD_RESOURCES_ONLY) { XMLElement rootElement = xml->GetRoot(); // Preload resources if appropriate if (mode != LOAD_SCENE) { URHO3D_PROFILE(FindResourcesToPreload); PreloadResourcesXML(rootElement); } // Store own old ID for resolving possible root node references unsigned nodeID = rootElement.GetUInt("id"); resolver_.AddNode(nodeID, this); // Load the root level components first if (!Node::LoadXML(rootElement, resolver_, false)) return false; // Then prepare for loading all root level child nodes in the async update XMLElement childNodeElement = rootElement.GetChild("node"); asyncProgress_.xmlElement_ = childNodeElement; // Count the amount of child nodes while (childNodeElement) { ++asyncProgress_.totalNodes_; childNodeElement = childNodeElement.GetNext("node"); } } else { URHO3D_PROFILE(FindResourcesToPreload); URHO3D_LOGINFO("Preloading resources from " + file->GetName()); PreloadResourcesXML(xml->GetRoot()); } return true; }
bool AnimationSet2D::LoadAnimation(const XMLElement& animationElem) { SharedPtr<Animation2D> animation(new Animation2D(this)); String name = animationElem.GetAttribute("name"); animation->SetName(name); float length = animationElem.GetFloat("length") * 0.001f; animation->SetLength(length); bool looped = true; if (animationElem.HasAttribute("looping")) looped = animationElem.GetBool("looping"); animation->SetLooped(looped); // Load timelines for (XMLElement timelineElem = animationElem.GetChild("timeline"); timelineElem; timelineElem = timelineElem.GetNext("timeline")) { Timeline2D timeline; timeline.name_ = timelineElem.GetAttribute("name"); if (timelineElem.GetAttribute("object_type") == "bone") timeline.type_ = OT_BONE; else timeline.type_ = OT_SPRITE; for (XMLElement keyElem = timelineElem.GetChild("key"); keyElem; keyElem = keyElem.GetNext("key")) { TimelineKey2D key; key.time_ = keyElem.GetFloat("time") * 0.001f; key.spin_ = 1; if (keyElem.HasAttribute("spin")) key.spin_ = keyElem.GetInt("spin"); XMLElement childElem = keyElem.GetChild(); Vector2 position; position.x_ = childElem.GetFloat("x"); position.y_ = childElem.GetFloat("y"); float angle = childElem.GetFloat("angle"); Vector2 scale(Vector2::ONE); if (childElem.HasAttribute("scale_x")) scale.x_ = childElem.GetFloat("scale_x"); if (childElem.HasAttribute("scale_y")) scale.y_ = childElem.GetFloat("scale_y"); key.transform_ = Transform2D(position, angle, scale); if (timeline.type_ == OT_SPRITE) { int folder = childElem.GetUInt("folder"); int file = childElem.GetUInt("file"); key.sprite_ = GetSprite(folder, file); if (!key.sprite_) { LOGERROR("Could not find sprite"); return false; } if (childElem.HasAttribute("pivot_x")) key.hotSpot_.x_ = childElem.GetFloat("pivot_x"); else key.hotSpot_.x_ = key.sprite_->GetHotSpot().x_; if (childElem.HasAttribute("pivot_y")) key.hotSpot_.y_ = childElem.GetFloat("pivot_y"); else key.hotSpot_.y_ = key.sprite_->GetHotSpot().y_; if (childElem.HasAttribute("a")) key.alpha_ = childElem.GetFloat("a"); } timeline.timelineKeys_.Push(key); } // Add end key for looped animation if (looped && timeline.timelineKeys_.Back().time_ != length) { TimelineKey2D key = timeline.timelineKeys_.Front(); key.time_ = length; timeline.timelineKeys_.Push(key); } animation->AddTimeline(timeline); } // Load main line XMLElement mainlineElem = animationElem.GetChild("mainline"); for (XMLElement keyElem = mainlineElem.GetChild("key"); keyElem; keyElem = keyElem.GetNext("key")) { MainlineKey2D mainlineKey; int id = keyElem.GetInt("id"); mainlineKey.time_ = keyElem.GetFloat("time") * 0.001f; for (XMLElement refElem = keyElem.GetChild(); refElem; refElem = refElem.GetNext()) { Reference2D ref; int refId = refElem.GetInt("id"); if (refElem.GetName() == "bone_ref") ref.type_ = OT_BONE; else ref.type_ = OT_SPRITE; ref.timeline_ = refElem.GetInt("timeline"); if (refElem.HasAttribute("parent")) { int parent = refElem.GetInt("parent"); int parentTimeline = mainlineKey.references_[parent].timeline_; animation->SetTimelineParent(ref.timeline_, parentTimeline); } if (refElem.GetName() == "object_ref") ref.zIndex_ = refElem.GetInt("z_index"); mainlineKey.references_.Push(ref); } animation->AddMainlineKey(mainlineKey); } animations_.Push(animation); return true; }
bool AnimationSet2D::LoadFolders(const XMLElement& rootElem) { ResourceCache* cache = GetSubsystem<ResourceCache>(); bool async = GetAsyncLoadState() == ASYNC_LOADING; String parentPath = GetParentPath(GetName()); String spriteSheetFilePath = parentPath + GetFileName(GetName()) + ".xml"; SpriteSheet2D* spriteSheet = 0; bool hasSpriteSheet = false; // When async loading, request the sprite sheet for background loading but do not actually get it if (!async) spriteSheet = cache->GetResource<SpriteSheet2D>(spriteSheetFilePath, false); else { hasSpriteSheet = cache->Exists(spriteSheetFilePath); if (hasSpriteSheet) cache->BackgroundLoadResource<SpriteSheet2D>(spriteSheetFilePath, false, this); } for (XMLElement folderElem = rootElem.GetChild("folder"); folderElem; folderElem = folderElem.GetNext("folder")) { unsigned folderId = folderElem.GetUInt("id"); for (XMLElement fileElem = folderElem.GetChild("file"); fileElem; fileElem = fileElem.GetNext("file")) { unsigned fileId = fileElem.GetUInt("id"); String fileName = fileElem.GetAttribute("name"); // When async loading, request the sprites for background loading but do not actually get them if (!async) { SharedPtr<Sprite2D> sprite; if (spriteSheet) sprite = spriteSheet->GetSprite(GetFileName(fileName)); else sprite = (cache->GetResource<Sprite2D>(parentPath + fileName)); if (!sprite) { LOGERROR("Could not load sprite " + fileName); return false; } Vector2 hotSpot(0.0f, 1.0f); if (fileElem.HasAttribute("pivot_x")) hotSpot.x_ = fileElem.GetFloat("pivot_x"); if (fileElem.HasAttribute("pivot_y")) hotSpot.y_ = fileElem.GetFloat("pivot_y"); // If sprite is trimmed, recalculate hot spot const IntVector2& offset = sprite->GetOffset(); if (offset != IntVector2::ZERO) { int width = fileElem.GetInt("width"); int height = fileElem.GetInt("height"); float pivotX = width * hotSpot.x_; float pivotY = height * (1.0f - hotSpot.y_); const IntRect& rectangle = sprite->GetRectangle(); hotSpot.x_ = (offset.x_ + pivotX) / rectangle.Width(); hotSpot.y_ = 1.0f - (offset.y_ + pivotY) / rectangle.Height(); } sprite->SetHotSpot(hotSpot); sprites_[(folderId << 16) + fileId] = sprite; } else if (!hasSpriteSheet) cache->BackgroundLoadResource<Sprite2D>(parentPath + fileName, true, this); } } return true; }