Example #1
0
bool XMLElement::SetVariantValue(const Variant& value)
{
    switch (value.GetType())
    {
    case VAR_RESOURCEREF:
        return SetResourceRef(value.GetResourceRef());

    case VAR_RESOURCEREFLIST:
        return SetResourceRefList(value.GetResourceRefList());

    case VAR_VARIANTVECTOR:
        return SetVariantVector(value.GetVariantVector());

    case VAR_VARIANTMAP:
        return SetVariantMap(value.GetVariantMap());

    default:
        return SetAttribute("value", value.ToString().CString());
    }
}
Example #2
0
bool Serializer::WriteVariantData(const Variant& value)
{
    switch (value.GetType())
    {
    case VAR_NONE:
        return true;

    case VAR_INT:
        return WriteInt(value.GetInt());

    case VAR_BOOL:
        return WriteBool(value.GetBool());

    case VAR_FLOAT:
        return WriteFloat(value.GetFloat());

    case VAR_VECTOR2:
        return WriteVector2(value.GetVector2());

    case VAR_VECTOR3:
        return WriteVector3(value.GetVector3());

    case VAR_VECTOR4:
        return WriteVector4(value.GetVector4());

    case VAR_QUATERNION:
        return WriteQuaternion(value.GetQuaternion());

    case VAR_COLOR:
        return WriteColor(value.GetColor());

    case VAR_STRING:
        return WriteString(value.GetString());

    case VAR_BUFFER:
        return WriteBuffer(value.GetBuffer());

    // Serializing pointers is not supported. Write null
    case VAR_VOIDPTR:
    case VAR_PTR:
        return WriteUInt(0);

    case VAR_RESOURCEREF:
        return WriteResourceRef(value.GetResourceRef());

    case VAR_RESOURCEREFLIST:
        return WriteResourceRefList(value.GetResourceRefList());

    case VAR_VARIANTVECTOR:
        return WriteVariantVector(value.GetVariantVector());

    case VAR_VARIANTMAP:
        return WriteVariantMap(value.GetVariantMap());

    case VAR_INTRECT:
        return WriteIntRect(value.GetIntRect());

    case VAR_INTVECTOR2:
        return WriteIntVector2(value.GetIntVector2());

    case VAR_MATRIX3:
        return WriteMatrix3(value.GetMatrix3());

    case VAR_MATRIX3X4:
        return WriteMatrix3x4(value.GetMatrix3x4());

    case VAR_MATRIX4:
        return WriteMatrix4(value.GetMatrix4());

    default:
        return false;
    }
}
Example #3
0
void SceneResolver::Resolve()
{
    // Nodes do not have component or node ID attributes, so only have to go through components
    ea::hash_set<StringHash> noIDAttributes;
    for (auto i = components_.begin(); i !=
        components_.end(); ++i)
    {
        Component* component = i->second;
        if (!component || noIDAttributes.contains(component->GetType()))
            continue;

        bool hasIDAttributes = false;
        const ea::vector<AttributeInfo>* attributes = component->GetAttributes();
        if (!attributes)
        {
            noIDAttributes.insert(component->GetType());
            continue;
        }

        for (unsigned j = 0; j < attributes->size(); ++j)
        {
            const AttributeInfo& info = attributes->at(j);
            if (info.mode_ & AM_NODEID)
            {
                hasIDAttributes = true;
                unsigned oldNodeID = component->GetAttribute(j).GetUInt();

                if (oldNodeID)
                {
                    auto k = nodes_.find(oldNodeID);

                    if (k != nodes_.end() && k->second)
                    {
                        unsigned newNodeID = k->second->GetID();
                        component->SetAttribute(j, Variant(newNodeID));
                    }
                    else
                        URHO3D_LOGWARNING("Could not resolve node ID " + ea::to_string(oldNodeID));
                }
            }
            else if (info.mode_ & AM_COMPONENTID)
            {
                hasIDAttributes = true;
                unsigned oldComponentID = component->GetAttribute(j).GetUInt();

                if (oldComponentID)
                {
                    auto k = components_.find(
                        oldComponentID);

                    if (k != components_.end() && k->second)
                    {
                        unsigned newComponentID = k->second->GetID();
                        component->SetAttribute(j, Variant(newComponentID));
                    }
                    else
                        URHO3D_LOGWARNING("Could not resolve component ID " + ea::to_string(oldComponentID));
                }
            }
            else if (info.mode_ & AM_NODEIDVECTOR)
            {
                hasIDAttributes = true;
                Variant attrValue = component->GetAttribute(j);
                const VariantVector& oldNodeIDs = attrValue.GetVariantVector();

                if (oldNodeIDs.size())
                {
                    // The first index stores the number of IDs redundantly. This is for editing
                    unsigned numIDs = oldNodeIDs[0].GetUInt();
                    VariantVector newIDs;
                    newIDs.push_back(numIDs);

                    for (unsigned k = 1; k < oldNodeIDs.size(); ++k)
                    {
                        unsigned oldNodeID = oldNodeIDs[k].GetUInt();
                        auto l = nodes_.find(oldNodeID);

                        if (l != nodes_.end() && l->second)
                            newIDs.push_back(l->second->GetID());
                        else
                        {
                            // If node was not found, retain number of elements, just store ID 0
                            newIDs.push_back(0);
                            URHO3D_LOGWARNING("Could not resolve node ID " + ea::to_string(oldNodeID));
                        }
                    }

                    component->SetAttribute(j, newIDs);
                }
            }
        }

        // If component type had no ID attributes, cache this fact for optimization
        if (!hasIDAttributes)
            noIDAttributes.insert(component->GetType());
    }

    // Attributes have been resolved, so no need to remember the nodes after this
    Reset();
}
Example #4
0
void JSONValue::SetVariantValue(const Variant& variant, Context* context)
{
    if (!IsNull())
    {
        LOGWARNING("JsonValue is not null");
    }

    switch (variant.GetType())
    {
    case VAR_BOOL:
        *this = variant.GetBool();
        return;
    
    case VAR_INT:
        *this = variant.GetInt();
        return;

    case VAR_FLOAT:
        *this = variant.GetFloat();
        return;

    case VAR_DOUBLE:
        *this = variant.GetDouble();
        return;

    case VAR_STRING:
        *this = variant.GetString();
        return;

    case VAR_VARIANTVECTOR:
        SetVariantVector(variant.GetVariantVector(), context);
        return;

    case VAR_VARIANTMAP:
        SetVariantMap(variant.GetVariantMap(), context);
        return;

    case VAR_RESOURCEREF:
        {
            if (!context)
            {
                LOGERROR("Context must not null for ResourceRef");
                return;
            }

            const ResourceRef& ref = variant.GetResourceRef();
            *this = String(context->GetTypeName(ref.type_)) + ";" + ref.name_;
        }
        return;

    case VAR_RESOURCEREFLIST:
        {
            if (!context)
            {
                LOGERROR("Context must not null for ResourceRefList");
                return;
            }

            const ResourceRefList& refList = variant.GetResourceRefList();
            String str(context->GetTypeName(refList.type_));
            for (unsigned i = 0; i < refList.names_.Size(); ++i)
            {
                str += ";";
                str += refList.names_[i];
            }
            *this = str;
        }
        return;

    case VAR_STRINGVECTOR:
        {
            const StringVector& vector = variant.GetStringVector();
            Resize(vector.Size());
            for (unsigned i = 0; i < vector.Size(); ++i)
                (*this)[i] = vector[i];
        }
        return;

    default:
        *this = variant.ToString();
    }
}