Search in sources :

Example 41 with JsonValue

use of com.badlogic.gdx.utils.JsonValue in project gaiasky by langurmonkey.

the class DatasetDesc method checkJsonVersion.

/**
 * Checks the version file of the given path, if it is a correct JSON
 * file and contains a top-level "version" attribute. Otherwise, it
 * returns the default lowest version (0)
 *
 * @param path The path with the file to check
 *
 * @return The version, if it exists, or 0
 */
private int checkJsonVersion(Path path) throws RuntimeException {
    if (path != null) {
        File file = path.toFile();
        if (file.exists() && file.canRead() && file.isFile()) {
            String fname = file.getName();
            String extension = fname.substring(fname.lastIndexOf(".") + 1);
            if (extension.equalsIgnoreCase("json")) {
                JsonValue jf = reader.parse(Gdx.files.absolute(file.getAbsolutePath()));
                if (jf.has("version")) {
                    try {
                        return jf.getInt("version", 0);
                    } catch (Exception e) {
                        logger.error(e, "The 'version' attribute must be an integer: " + path);
                    }
                }
            }
        }
        return 0;
    } else {
        throw new RuntimeException("Path is null");
    }
}
Also used : JsonValue(com.badlogic.gdx.utils.JsonValue) File(java.io.File)

Example 42 with JsonValue

use of com.badlogic.gdx.utils.JsonValue in project Mundus by jpooleycodes.

the class MG3dModelLoader method parseModel.

public ModelData parseModel(FileHandle handle) {
    JsonValue json = reader.parse(handle);
    ModelData model = new ModelData();
    JsonValue version = json.require("version");
    model.version[0] = version.getShort(0);
    model.version[1] = version.getShort(1);
    if (model.version[0] != VERSION_HI || model.version[1] != VERSION_LO)
        throw new GdxRuntimeException("Model version not supported");
    model.id = json.getString("id", "");
    parseMeshes(model, json);
    parseMaterials(model, json, handle.parent().path());
    parseNodes(model, json);
    parseAnimations(model, json);
    return model;
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) ModelData(com.badlogic.gdx.graphics.g3d.model.data.ModelData) JsonValue(com.badlogic.gdx.utils.JsonValue)

Example 43 with JsonValue

use of com.badlogic.gdx.utils.JsonValue in project Mundus by jpooleycodes.

the class MG3dModelLoader method parseAttributes.

private VertexAttribute[] parseAttributes(JsonValue attributes) {
    Array<VertexAttribute> vertexAttributes = new Array<VertexAttribute>();
    int unit = 0;
    int blendWeightCount = 0;
    for (JsonValue value = attributes.child; value != null; value = value.next) {
        String attribute = value.asString();
        String attr = (String) attribute;
        if (attr.equals("POSITION")) {
            vertexAttributes.add(VertexAttribute.Position());
        } else if (attr.equals("NORMAL")) {
            vertexAttributes.add(VertexAttribute.Normal());
        } else if (attr.equals("COLOR")) {
            vertexAttributes.add(VertexAttribute.ColorUnpacked());
        } else if (attr.equals("COLORPACKED")) {
            vertexAttributes.add(VertexAttribute.ColorPacked());
        } else if (attr.equals("TANGENT")) {
            vertexAttributes.add(VertexAttribute.Tangent());
        } else if (attr.equals("BINORMAL")) {
            vertexAttributes.add(VertexAttribute.Binormal());
        } else if (attr.startsWith("TEXCOORD")) {
            vertexAttributes.add(VertexAttribute.TexCoords(unit++));
        } else if (attr.startsWith("BLENDWEIGHT")) {
            vertexAttributes.add(VertexAttribute.BoneWeight(blendWeightCount++));
        } else {
            throw new GdxRuntimeException("Unknown vertex attribute '" + attr + "', should be one of position, normal, uv, tangent or binormal");
        }
    }
    return vertexAttributes.toArray(VertexAttribute.class);
}
Also used : Array(com.badlogic.gdx.utils.Array) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) VertexAttribute(com.badlogic.gdx.graphics.VertexAttribute) JsonValue(com.badlogic.gdx.utils.JsonValue)

Example 44 with JsonValue

use of com.badlogic.gdx.utils.JsonValue in project Mundus by jpooleycodes.

the class MG3dModelLoader method parseMaterials.

private void parseMaterials(ModelData model, JsonValue json, String materialDir) {
    JsonValue materials = json.get("materials");
    if (materials == null) {
    // we should probably create some default material in this case
    } else {
        model.materials.ensureCapacity(materials.size);
        for (JsonValue material = materials.child; material != null; material = material.next) {
            ModelMaterial jsonMaterial = new ModelMaterial();
            String id = material.getString("id", null);
            if (id == null)
                throw new GdxRuntimeException("Material needs an id.");
            jsonMaterial.id = id;
            // Read material colors
            final JsonValue diffuse = material.get("diffuse");
            if (diffuse != null)
                jsonMaterial.diffuse = parseColor(diffuse);
            final JsonValue ambient = material.get("ambient");
            if (ambient != null)
                jsonMaterial.ambient = parseColor(ambient);
            final JsonValue emissive = material.get("emissive");
            if (emissive != null)
                jsonMaterial.emissive = parseColor(emissive);
            final JsonValue specular = material.get("specular");
            if (specular != null)
                jsonMaterial.specular = parseColor(specular);
            final JsonValue reflection = material.get("reflection");
            if (reflection != null)
                jsonMaterial.reflection = parseColor(reflection);
            // Read shininess
            jsonMaterial.shininess = material.getFloat("shininess", 0.0f);
            // Read opacity
            jsonMaterial.opacity = material.getFloat("opacity", 1.0f);
            // Read textures
            // JsonValue textures = material.get("textures");
            // if (textures != null) {
            // for (JsonValue texture = textures.child; texture != null;
            // texture = texture.next) {
            // ModelTexture jsonTexture = new ModelTexture();
            // 
            // String textureId = texture.getString("id", null);
            // if (textureId == null) throw new GdxRuntimeException("Texture
            // has no id.");
            // jsonTexture.id = textureId;
            // 
            // String fileName = texture.getString("filename", null);
            // if (fileName == null) throw new GdxRuntimeException("Texture
            // needs filename.");
            // jsonTexture.fileName = materialDir + (materialDir.length() ==
            // 0 || materialDir.endsWith("/") ? "" : "/")
            // + fileName;
            // 
            // jsonTexture.uvTranslation =
            // readVector2(texture.get("uvTranslation"), 0f, 0f);
            // jsonTexture.uvScaling = readVector2(texture.get("uvScaling"),
            // 1f, 1f);
            // 
            // String textureType = texture.getString("type", null);
            // if (textureType == null) throw new
            // GdxRuntimeException("Texture needs type.");
            // 
            // jsonTexture.usage = parseTextureUsage(textureType);
            // 
            // if (jsonMaterial.textures == null) jsonMaterial.textures =
            // new Array<ModelTexture>();
            // jsonMaterial.textures.add(jsonTexture);
            // }
            // }
            model.materials.add(jsonMaterial);
        }
    }
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) JsonValue(com.badlogic.gdx.utils.JsonValue) ModelMaterial(com.badlogic.gdx.graphics.g3d.model.data.ModelMaterial)

Example 45 with JsonValue

use of com.badlogic.gdx.utils.JsonValue in project Mundus by jpooleycodes.

the class MetaLoader method load.

public Meta load(FileHandle file) throws MetaFileParseException {
    Meta meta = new Meta(file);
    JsonValue json = reader.parse(file);
    parseBasics(meta, json);
    if (meta.getType() == AssetType.TERRAIN) {
        parseTerrain(meta, json.get(Meta.JSON_TERRAIN));
    } else if (meta.getType() == AssetType.MODEL) {
        parseModel(meta, json.get(Meta.JSON_MODEL));
    }
    return meta;
}
Also used : JsonValue(com.badlogic.gdx.utils.JsonValue)

Aggregations

JsonValue (com.badlogic.gdx.utils.JsonValue)148 JsonReader (com.badlogic.gdx.utils.JsonReader)27 IOException (java.io.IOException)21 Array (com.badlogic.gdx.utils.Array)20 Json (com.badlogic.gdx.utils.Json)15 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)14 FileHandle (com.badlogic.gdx.files.FileHandle)11 ReflectionException (com.badlogic.gdx.utils.reflect.ReflectionException)10 BladeJson (com.bladecoder.engine.serialization.BladeJson)9 HashMap (java.util.HashMap)8 Color (com.badlogic.gdx.graphics.Color)7 GraphBoxImpl (com.gempukku.libgdx.graph.ui.graph.GraphBoxImpl)7 ArrayList (java.util.ArrayList)7 Vector2 (com.badlogic.gdx.math.Vector2)6 Actor (com.badlogic.gdx.scenes.scene2d.Actor)6 ChangeListener (com.badlogic.gdx.scenes.scene2d.utils.ChangeListener)6 Action (com.bladecoder.engine.actions.Action)6 File (java.io.File)6 ObjectMap (com.badlogic.gdx.utils.ObjectMap)5 SerializationException (com.badlogic.gdx.utils.SerializationException)5