Search in sources :

Example 51 with JsonValue

use of com.badlogic.gdx.utils.JsonValue in project libgdx by libgdx.

the class G3dModelLoader 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 52 with JsonValue

use of com.badlogic.gdx.utils.JsonValue in project libgdx by libgdx.

the class G3dModelLoader method parseAnimations.

private void parseAnimations(ModelData model, JsonValue json) {
    JsonValue animations = json.get("animations");
    if (animations == null)
        return;
    model.animations.ensureCapacity(animations.size);
    for (JsonValue anim = animations.child; anim != null; anim = anim.next) {
        JsonValue nodes = anim.get("bones");
        if (nodes == null)
            continue;
        ModelAnimation animation = new ModelAnimation();
        model.animations.add(animation);
        animation.nodeAnimations.ensureCapacity(nodes.size);
        animation.id = anim.getString("id");
        for (JsonValue node = nodes.child; node != null; node = node.next) {
            ModelNodeAnimation nodeAnim = new ModelNodeAnimation();
            animation.nodeAnimations.add(nodeAnim);
            nodeAnim.nodeId = node.getString("boneId");
            // For backwards compatibility (version 0.1):
            JsonValue keyframes = node.get("keyframes");
            if (keyframes != null && keyframes.isArray()) {
                for (JsonValue keyframe = keyframes.child; keyframe != null; keyframe = keyframe.next) {
                    final float keytime = keyframe.getFloat("keytime", 0f) / 1000.f;
                    JsonValue translation = keyframe.get("translation");
                    if (translation != null && translation.size == 3) {
                        if (nodeAnim.translation == null)
                            nodeAnim.translation = new Array<ModelNodeKeyframe<Vector3>>();
                        ModelNodeKeyframe<Vector3> tkf = new ModelNodeKeyframe<Vector3>();
                        tkf.keytime = keytime;
                        tkf.value = new Vector3(translation.getFloat(0), translation.getFloat(1), translation.getFloat(2));
                        nodeAnim.translation.add(tkf);
                    }
                    JsonValue rotation = keyframe.get("rotation");
                    if (rotation != null && rotation.size == 4) {
                        if (nodeAnim.rotation == null)
                            nodeAnim.rotation = new Array<ModelNodeKeyframe<Quaternion>>();
                        ModelNodeKeyframe<Quaternion> rkf = new ModelNodeKeyframe<Quaternion>();
                        rkf.keytime = keytime;
                        rkf.value = new Quaternion(rotation.getFloat(0), rotation.getFloat(1), rotation.getFloat(2), rotation.getFloat(3));
                        nodeAnim.rotation.add(rkf);
                    }
                    JsonValue scale = keyframe.get("scale");
                    if (scale != null && scale.size == 3) {
                        if (nodeAnim.scaling == null)
                            nodeAnim.scaling = new Array<ModelNodeKeyframe<Vector3>>();
                        ModelNodeKeyframe<Vector3> skf = new ModelNodeKeyframe();
                        skf.keytime = keytime;
                        skf.value = new Vector3(scale.getFloat(0), scale.getFloat(1), scale.getFloat(2));
                        nodeAnim.scaling.add(skf);
                    }
                }
            } else {
                // Version 0.2:
                JsonValue translationKF = node.get("translation");
                if (translationKF != null && translationKF.isArray()) {
                    nodeAnim.translation = new Array<ModelNodeKeyframe<Vector3>>();
                    nodeAnim.translation.ensureCapacity(translationKF.size);
                    for (JsonValue keyframe = translationKF.child; keyframe != null; keyframe = keyframe.next) {
                        ModelNodeKeyframe<Vector3> kf = new ModelNodeKeyframe<Vector3>();
                        nodeAnim.translation.add(kf);
                        kf.keytime = keyframe.getFloat("keytime", 0f) / 1000.f;
                        JsonValue translation = keyframe.get("value");
                        if (translation != null && translation.size >= 3)
                            kf.value = new Vector3(translation.getFloat(0), translation.getFloat(1), translation.getFloat(2));
                    }
                }
                JsonValue rotationKF = node.get("rotation");
                if (rotationKF != null && rotationKF.isArray()) {
                    nodeAnim.rotation = new Array<ModelNodeKeyframe<Quaternion>>();
                    nodeAnim.rotation.ensureCapacity(rotationKF.size);
                    for (JsonValue keyframe = rotationKF.child; keyframe != null; keyframe = keyframe.next) {
                        ModelNodeKeyframe<Quaternion> kf = new ModelNodeKeyframe<Quaternion>();
                        nodeAnim.rotation.add(kf);
                        kf.keytime = keyframe.getFloat("keytime", 0f) / 1000.f;
                        JsonValue rotation = keyframe.get("value");
                        if (rotation != null && rotation.size >= 4)
                            kf.value = new Quaternion(rotation.getFloat(0), rotation.getFloat(1), rotation.getFloat(2), rotation.getFloat(3));
                    }
                }
                JsonValue scalingKF = node.get("scaling");
                if (scalingKF != null && scalingKF.isArray()) {
                    nodeAnim.scaling = new Array<ModelNodeKeyframe<Vector3>>();
                    nodeAnim.scaling.ensureCapacity(scalingKF.size);
                    for (JsonValue keyframe = scalingKF.child; keyframe != null; keyframe = keyframe.next) {
                        ModelNodeKeyframe<Vector3> kf = new ModelNodeKeyframe<Vector3>();
                        nodeAnim.scaling.add(kf);
                        kf.keytime = keyframe.getFloat("keytime", 0f) / 1000.f;
                        JsonValue scaling = keyframe.get("value");
                        if (scaling != null && scaling.size >= 3)
                            kf.value = new Vector3(scaling.getFloat(0), scaling.getFloat(1), scaling.getFloat(2));
                    }
                }
            }
        }
    }
}
Also used : Array(com.badlogic.gdx.utils.Array) ModelNodeKeyframe(com.badlogic.gdx.graphics.g3d.model.data.ModelNodeKeyframe) Quaternion(com.badlogic.gdx.math.Quaternion) ModelAnimation(com.badlogic.gdx.graphics.g3d.model.data.ModelAnimation) JsonValue(com.badlogic.gdx.utils.JsonValue) Vector3(com.badlogic.gdx.math.Vector3) ModelNodeAnimation(com.badlogic.gdx.graphics.g3d.model.data.ModelNodeAnimation)

Example 53 with JsonValue

use of com.badlogic.gdx.utils.JsonValue in project libgdx by libgdx.

the class Skin method getJsonLoader.

protected Json getJsonLoader(final FileHandle skinFile) {
    final Skin skin = this;
    final Json json = new Json() {

        public <T> T readValue(Class<T> type, Class elementType, JsonValue jsonData) {
            // If the JSON is a string but the type is not, look up the actual value by name.
            if (jsonData.isString() && !ClassReflection.isAssignableFrom(CharSequence.class, type))
                return get(jsonData.asString(), type);
            return super.readValue(type, elementType, jsonData);
        }
    };
    json.setTypeName(null);
    json.setUsePrototypes(false);
    json.setSerializer(Skin.class, new ReadOnlySerializer<Skin>() {

        public Skin read(Json json, JsonValue typeToValueMap, Class ignored) {
            for (JsonValue valueMap = typeToValueMap.child; valueMap != null; valueMap = valueMap.next) {
                try {
                    readNamedObjects(json, ClassReflection.forName(valueMap.name()), valueMap);
                } catch (ReflectionException ex) {
                    throw new SerializationException(ex);
                }
            }
            return skin;
        }

        private void readNamedObjects(Json json, Class type, JsonValue valueMap) {
            Class addType = type == TintedDrawable.class ? Drawable.class : type;
            for (JsonValue valueEntry = valueMap.child; valueEntry != null; valueEntry = valueEntry.next) {
                Object object = json.readValue(type, valueEntry);
                if (object == null)
                    continue;
                try {
                    add(valueEntry.name, object, addType);
                    if (addType != Drawable.class && ClassReflection.isAssignableFrom(Drawable.class, addType))
                        add(valueEntry.name, object, Drawable.class);
                } catch (Exception ex) {
                    throw new SerializationException("Error reading " + ClassReflection.getSimpleName(type) + ": " + valueEntry.name, ex);
                }
            }
        }
    });
    json.setSerializer(BitmapFont.class, new ReadOnlySerializer<BitmapFont>() {

        public BitmapFont read(Json json, JsonValue jsonData, Class type) {
            String path = json.readValue("file", String.class, jsonData);
            int scaledSize = json.readValue("scaledSize", int.class, -1, jsonData);
            Boolean flip = json.readValue("flip", Boolean.class, false, jsonData);
            Boolean markupEnabled = json.readValue("markupEnabled", Boolean.class, false, jsonData);
            FileHandle fontFile = skinFile.parent().child(path);
            if (!fontFile.exists())
                fontFile = Gdx.files.internal(path);
            if (!fontFile.exists())
                throw new SerializationException("Font file not found: " + fontFile);
            // Use a region with the same name as the font, else use a PNG file in the same directory as the FNT file.
            String regionName = fontFile.nameWithoutExtension();
            try {
                BitmapFont font;
                Array<TextureRegion> regions = skin.getRegions(regionName);
                if (regions != null)
                    font = new BitmapFont(new BitmapFontData(fontFile, flip), regions, true);
                else {
                    TextureRegion region = skin.optional(regionName, TextureRegion.class);
                    if (region != null)
                        font = new BitmapFont(fontFile, region, flip);
                    else {
                        FileHandle imageFile = fontFile.parent().child(regionName + ".png");
                        if (imageFile.exists())
                            font = new BitmapFont(fontFile, imageFile, flip);
                        else
                            font = new BitmapFont(fontFile, flip);
                    }
                }
                font.getData().markupEnabled = markupEnabled;
                // Scaled size is the desired cap height to scale the font to.
                if (scaledSize != -1)
                    font.getData().setScale(scaledSize / font.getCapHeight());
                return font;
            } catch (RuntimeException ex) {
                throw new SerializationException("Error loading bitmap font: " + fontFile, ex);
            }
        }
    });
    json.setSerializer(Color.class, new ReadOnlySerializer<Color>() {

        public Color read(Json json, JsonValue jsonData, Class type) {
            if (jsonData.isString())
                return get(jsonData.asString(), Color.class);
            String hex = json.readValue("hex", String.class, (String) null, jsonData);
            if (hex != null)
                return Color.valueOf(hex);
            float r = json.readValue("r", float.class, 0f, jsonData);
            float g = json.readValue("g", float.class, 0f, jsonData);
            float b = json.readValue("b", float.class, 0f, jsonData);
            float a = json.readValue("a", float.class, 1f, jsonData);
            return new Color(r, g, b, a);
        }
    });
    json.setSerializer(TintedDrawable.class, new ReadOnlySerializer() {

        public Object read(Json json, JsonValue jsonData, Class type) {
            String name = json.readValue("name", String.class, jsonData);
            Color color = json.readValue("color", Color.class, jsonData);
            Drawable drawable = newDrawable(name, color);
            if (drawable instanceof BaseDrawable) {
                BaseDrawable named = (BaseDrawable) drawable;
                named.setName(jsonData.name + " (" + name + ", " + color + ")");
            }
            return drawable;
        }
    });
    return json;
}
Also used : ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException) SerializationException(com.badlogic.gdx.utils.SerializationException) BitmapFontData(com.badlogic.gdx.graphics.g2d.BitmapFont.BitmapFontData) FileHandle(com.badlogic.gdx.files.FileHandle) Color(com.badlogic.gdx.graphics.Color) BaseDrawable(com.badlogic.gdx.scenes.scene2d.utils.BaseDrawable) JsonValue(com.badlogic.gdx.utils.JsonValue) TextureRegionDrawable(com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable) Drawable(com.badlogic.gdx.scenes.scene2d.utils.Drawable) BaseDrawable(com.badlogic.gdx.scenes.scene2d.utils.BaseDrawable) SpriteDrawable(com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable) NinePatchDrawable(com.badlogic.gdx.scenes.scene2d.utils.NinePatchDrawable) TiledDrawable(com.badlogic.gdx.scenes.scene2d.utils.TiledDrawable) Json(com.badlogic.gdx.utils.Json) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) SerializationException(com.badlogic.gdx.utils.SerializationException) ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException) Array(com.badlogic.gdx.utils.Array) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) ReadOnlySerializer(com.badlogic.gdx.utils.Json.ReadOnlySerializer) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont)

Example 54 with JsonValue

use of com.badlogic.gdx.utils.JsonValue in project bdx by GoranM.

the class Scene method createModel.

public Model createModel(JsonValue model) {
    ModelBuilder builder = new ModelBuilder();
    builder.begin();
    short idx = 0;
    for (JsonValue mat : model) {
        Material m = materials.get(mat.name);
        if (mat.name.equals(defaultMaterial.id))
            m = new Material(m);
        MeshPartBuilder mpb = builder.part(model.name, GL20.GL_TRIANGLES, Usage.Position | Usage.Normal | Usage.TextureCoordinates, m);
        float[] verts = mat.asFloatArray();
        mpb.vertex(verts);
        int len = verts.length / Bdx.VERT_STRIDE;
        try {
            for (short i = 0; i < len; ++i) {
                mpb.index(idx);
                idx += 1;
            }
        } catch (Error e) {
            throw new RuntimeException("MODEL ERROR: Models with more than 32767 vertices are not supported. " + model.name + " has " + Integer.toString(len) + " vertices.");
        }
    }
    return builder.end();
}
Also used : ModelBuilder(com.badlogic.gdx.graphics.g3d.utils.ModelBuilder) JsonValue(com.badlogic.gdx.utils.JsonValue) MeshPartBuilder(com.badlogic.gdx.graphics.g3d.utils.MeshPartBuilder)

Example 55 with JsonValue

use of com.badlogic.gdx.utils.JsonValue in project gdx-skineditor by cobolfoo.

the class Skin method getJsonLoader.

protected Json getJsonLoader(final FileHandle skinFile) {
    final Skin skin = this;
    final Json json = new Json() {

        public <T> T readValue(Class<T> type, Class elementType, JsonValue jsonData) {
            // actual value by name.
            if (jsonData.isString() && !ClassReflection.isAssignableFrom(CharSequence.class, type))
                return get(jsonData.asString(), type);
            return super.readValue(type, elementType, jsonData);
        }
    };
    json.setTypeName(null);
    json.setUsePrototypes(false);
    json.setSerializer(Skin.class, new ReadOnlySerializer<Skin>() {

        public Skin read(Json json, JsonValue typeToValueMap, Class ignored) {
            for (JsonValue valueMap = typeToValueMap.child; valueMap != null; valueMap = valueMap.next) {
                try {
                    readNamedObjects(json, ClassReflection.forName(valueMap.name()), valueMap);
                } catch (ReflectionException ex) {
                    throw new SerializationException(ex);
                }
            }
            return skin;
        }

        private void readNamedObjects(Json json, Class type, JsonValue valueMap) {
            for (JsonValue valueEntry = valueMap.child; valueEntry != null; valueEntry = valueEntry.next) {
                Object object = json.readValue(type, valueEntry);
                if (object == null)
                    continue;
                try {
                    add(valueEntry.name(), object, type);
                } catch (Exception ex) {
                    throw new SerializationException("Error reading " + ClassReflection.getSimpleName(type) + ": " + valueEntry.name(), ex);
                }
            }
        }
    });
    json.setSerializer(BitmapFont.class, new ReadOnlySerializer<BitmapFont>() {

        public BitmapFont read(Json json, JsonValue jsonData, Class type) {
            String path = json.readValue("file", String.class, jsonData);
            int scaledSize = json.readValue("scaledSize", int.class, -1, jsonData);
            Boolean flip = json.readValue("flip", Boolean.class, false, jsonData);
            FileHandle fontFile = skinFile.parent().child(path);
            if (!fontFile.exists())
                fontFile = Gdx.files.internal(path);
            if (!fontFile.exists())
                throw new SerializationException("Font file not found: " + fontFile);
            // Use a region with the same name as the font, else use
            // a PNG file in the same directory as the FNT file.
            String regionName = fontFile.nameWithoutExtension();
            try {
                BitmapFont font;
                TextureRegion region = skin.optional(regionName, TextureRegion.class);
                if (region != null)
                    font = new BitmapFont(fontFile, region, flip);
                else {
                    FileHandle imageFile = fontFile.parent().child(regionName + ".png");
                    if (imageFile.exists())
                        font = new BitmapFont(fontFile, imageFile, flip);
                    else
                        font = new BitmapFont(fontFile, flip);
                }
                // the font to.
                if (scaledSize != -1)
                    font.setScale(scaledSize / font.getCapHeight());
                return font;
            } catch (RuntimeException ex) {
                throw new SerializationException("Error loading bitmap font: " + fontFile, ex);
            }
        }
    });
    json.setSerializer(Color.class, new ReadOnlySerializer<Color>() {

        public Color read(Json json, JsonValue jsonData, Class type) {
            if (jsonData.isString())
                return get(jsonData.asString(), Color.class);
            String hex = json.readValue("hex", String.class, (String) null, jsonData);
            if (hex != null)
                return Color.valueOf(hex);
            float r = json.readValue("r", float.class, 0f, jsonData);
            float g = json.readValue("g", float.class, 0f, jsonData);
            float b = json.readValue("b", float.class, 0f, jsonData);
            float a = json.readValue("a", float.class, 1f, jsonData);
            return new Color(r, g, b, a);
        }
    });
    json.setSerializer(TintedDrawable.class, new ReadOnlySerializer() {

        public Object read(Json json, JsonValue jsonData, Class type) {
            String name = json.readValue("name", String.class, jsonData);
            Color color = json.readValue("color", Color.class, jsonData);
            TintedDrawable td = new TintedDrawable();
            td.name = name;
            td.color = color;
            td.drawable = newDrawable(name, color);
            return td;
        // return newDrawable(name, color);
        }
    });
    return json;
}
Also used : ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException) SerializationException(com.badlogic.gdx.utils.SerializationException) FileHandle(com.badlogic.gdx.files.FileHandle) Color(com.badlogic.gdx.graphics.Color) JsonValue(com.badlogic.gdx.utils.JsonValue) Json(com.badlogic.gdx.utils.Json) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) SerializationException(com.badlogic.gdx.utils.SerializationException) ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) ReadOnlySerializer(com.badlogic.gdx.utils.Json.ReadOnlySerializer) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont)

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