Ejemplo n.º 1
0
Variant JSONValue::GetVariantValue(unsigned index, VariantType type) const
{
    Variant ret;

    if (type == VAR_RESOURCEREF)
        ret = GetResourceRef(index);
    else if (type == VAR_RESOURCEREFLIST)
        ret = GetResourceRefList(index);
    else if (type == VAR_VARIANTVECTOR || type == VAR_VARIANTMAP)
        LOGERROR("Unsupported value type");
    else
        ret.FromString(type, GetCString(index));

    return ret;
}
Ejemplo n.º 2
0
Variant XMLElement::GetVariantValue(VariantType type) const
{
    Variant ret;

    if (type == VAR_RESOURCEREF)
        ret = GetResourceRef();
    else if (type == VAR_RESOURCEREFLIST)
        ret = GetResourceRefList();
    else if (type == VAR_VARIANTVECTOR)
        ret = GetVariantVector();
    else if (type == VAR_VARIANTMAP)
        ret = GetVariantMap();
    else
        ret.FromString(type, GetAttributeCString("value"));

    return ret;
}
static int Material_SetShaderParameter(duk_context* ctx)
{
    duk_push_this(ctx);
    Material* material = js_to_class_instance<Material>(ctx, -1, 0);

    const char* name = duk_require_string(ctx, 0);
    String value = duk_require_string(ctx, 1);

    const Variant& v = material->GetShaderParameter(name);

    if (v == Variant::EMPTY)
        return 0;

    Variant vset;
    vset.FromString(v.GetType(), value);

    material->SetShaderParameter(name, vset);

    return 0;

}
Ejemplo n.º 4
0
Variant ToVectorVariant(const char* source)
{
    Variant ret;
    unsigned elements = CountElements(source, ' ');

    switch (elements)
    {
    case 1:
        ret.FromString(VAR_FLOAT, source);
        break;

    case 2:
        ret.FromString(VAR_VECTOR2, source);
        break;

    case 3:
        ret.FromString(VAR_VECTOR3, source);
        break;

    case 4:
        ret.FromString(VAR_VECTOR4, source);
        break;

    case 9:
        ret.FromString(VAR_MATRIX3, source);
        break;

    case 12:
        ret.FromString(VAR_MATRIX3X4, source);
        break;

    case 16:
        ret.FromString(VAR_MATRIX4, source);
        break;

    default:
        assert(false);  // Should not get here
        break;
    }

    return ret;
}
Ejemplo n.º 5
0
Variant ToVectorVariant(const char* source)
{
    Variant ret;
    unsigned elements = CountElements(source, ' ');

    switch (elements)
    {
    case 1:
        ret.FromString(VAR_FLOAT, source);
        break;

    case 2:
        ret.FromString(VAR_VECTOR2, source);
        break;

    case 3:
        ret.FromString(VAR_VECTOR3, source);
        break;

    case 4:
        ret.FromString(VAR_VECTOR4, source);
        break;

    case 9:
        ret.FromString(VAR_MATRIX3, source);
        break;

    case 12:
        ret.FromString(VAR_MATRIX3X4, source);
        break;

    case 16:
        ret.FromString(VAR_MATRIX4, source);
        break;

    default:
        // Illegal input. Return variant remains empty
        break;
    }

    return ret;
}
    bool CSComponentAssembly::ParseComponentClassJSON(const JSONValue& json)
    {
        if (!typeMap_.Size())
            InitTypeMap();

        String className = json.Get("name").GetString();

        classNames_.Push(className);

        const JSONValue& jfields = json.Get("fields");

        PODVector<StringHash> enumsAdded;

        if (jfields.IsArray())
        {
            for (unsigned i = 0; i < jfields.GetArray().Size(); i++)
            {
                const JSONValue& jfield = jfields.GetArray().At(i);

                VariantType varType = VAR_NONE;

                bool isEnum = jfield.Get("isEnum").GetBool();
                String typeName = jfield.Get("typeName").GetString();
                String fieldName = jfield.Get("name").GetString();
                String defaultValue = jfield.Get("defaultValue").GetString();

                if (!defaultValue.Length())
                {
                    JSONArray caPos = jfield.Get("caPos").GetArray();
                    if (caPos.Size())
                        defaultValue = caPos[0].GetString();
                }

                if (!defaultValue.Length())
                {
                    JSONObject caNamed = jfield.Get("caNamed").GetObject();
                    if (caNamed.Contains("DefaultValue"))
                        defaultValue = caNamed["DefaultValue"].GetString();
                }

                if (isEnum && assemblyEnums_.Contains(typeName) && !enumsAdded.Contains(fieldName))
                {
                    varType = VAR_INT;
                    enumsAdded.Push(fieldName);
                    const Vector<EnumInfo>& einfos = assemblyEnums_[typeName];
                    for (unsigned i = 0; i < einfos.Size(); i++)
                        AddEnum(/*typeName*/fieldName, einfos[i], className);
                }

                if (varType == VAR_NONE && typeMap_.Contains(typeName))
                    varType = typeMap_[typeName];

                if (varType == VAR_NONE)
                {
                    // FIXME: We need to be able to test if a type is a ResourceRef, this isn't really the way to achieve that
                    const HashMap<StringHash, SharedPtr<ObjectFactory>>& factories = context_->GetObjectFactories();
                    HashMap<StringHash, SharedPtr<ObjectFactory>>::ConstIterator itr = factories.Begin();

                    while (itr != factories.End())
                    {
                        if (itr->second_->GetTypeName() == typeName)
                        {
                            varType = VAR_RESOURCEREF;
                            break;
                        }

                        itr++;
                    }

                    if (varType == VAR_NONE)
                    {
                        ATOMIC_LOGERRORF("Component Class %s contains unmappable type %s in field %s",
                            className.CString(), typeName.CString(), fieldName.CString());

                        continue;
                    }

                }

                if (!defaultValue.Length() && varType == VAR_RESOURCEREF)
                {
                    // We still need a default value for ResourceRef's so we know the classtype
                    AddDefaultValue(fieldName, ResourceRef(typeName), className);
                }
                else
                {
                    Variant value;

                    if (varType == VAR_RESOURCEREF)
                    {
                        ResourceRef rref(typeName);
                        rref.name_ = defaultValue;
                        value = rref;
                    }
                    else
                    {
                        value.FromString(varType, defaultValue);
                    }

                    AddDefaultValue(fieldName, value, className);
                }

                AddField(fieldName, varType, className);

            }

        }

        return true;
    }
Ejemplo n.º 7
0
void JSONValue::GetVariantValue(Variant& variant, VariantType type) const
{
    switch (type)
    {
    case VAR_BOOL:
        variant = GetBool();
        return;

    case VAR_INT:
        variant = GetInt();
        return;

    case VAR_FLOAT:
        variant = GetFloat();
        return;

    case VAR_DOUBLE:
        variant = GetDouble();
        return;

    case VAR_STRING:
        variant = GetString();
        return;

    case VAR_VARIANTVECTOR:
        {
            VariantVector vector;
            GetVariantVector(vector);
            variant = vector;
        }
        return;

    case VAR_VARIANTMAP:
        {
            VariantMap map;
            GetVariantMap(map);
            variant = map;
        }
        return;

    case VAR_RESOURCEREF:
        {
            ResourceRef ref;
            Vector<String> values = GetString().Split(';');
            if (values.Size() == 2)
            {
                ref.type_ = values[0];
                ref.name_ = values[1];
            }
            variant = ref;
        }
        return;

    case VAR_RESOURCEREFLIST:
        {
            ResourceRefList refList;
            Vector<String> values = GetString().Split(';');
            if (values.Size() >= 1)
            {
                refList.type_ = values[0];
                refList.names_.Resize(values.Size() - 1);
                for (unsigned i = 1; i < values.Size(); ++i)
                    refList.names_[i - 1] = values[i];
            }
            variant = refList;
        }
        return;

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

    default:
        variant.FromString(type, GetString());
        return;
    }
}