Search in sources :

Example 1 with Json

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

the class TexturePacker method main.

public static void main(String[] args) throws Exception {
    Settings settings = null;
    String input = null, output = null, packFileName = "pack.atlas";
    switch(args.length) {
        case 4:
            settings = new Json().fromJson(Settings.class, new FileReader(args[3]));
        case 3:
            packFileName = args[2];
        case 2:
            output = args[1];
        case 1:
            input = args[0];
            break;
        default:
            System.out.println("Usage: inputDir [outputDir] [packFileName] [settingsFileName]");
            System.exit(0);
    }
    if (output == null) {
        File inputFile = new File(input);
        output = new File(inputFile.getParentFile(), inputFile.getName() + "-packed").getAbsolutePath();
    }
    if (settings == null)
        settings = new Settings();
    process(settings, input, output, packFileName);
}
Also used : FileReader(java.io.FileReader) Json(com.badlogic.gdx.utils.Json) File(java.io.File)

Example 2 with Json

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

the class ParticleEffectLoader method save.

/** Saves the effect to the given file contained in the passed in parameter. */
public void save(ParticleEffect effect, ParticleEffectSaveParameter parameter) throws IOException {
    ResourceData<ParticleEffect> data = new ResourceData<ParticleEffect>(effect);
    // effect assets
    effect.save(parameter.manager, data);
    // Batches configurations
    if (parameter.batches != null) {
        for (ParticleBatch<?> batch : parameter.batches) {
            boolean save = false;
            for (ParticleController controller : effect.getControllers()) {
                if (controller.renderer.isCompatible(batch)) {
                    save = true;
                    break;
                }
            }
            if (save)
                batch.save(parameter.manager, data);
        }
    }
    // save
    Json json = new Json();
    json.toJson(data, parameter.file);
}
Also used : Json(com.badlogic.gdx.utils.Json)

Example 3 with Json

use of com.badlogic.gdx.utils.Json 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 4 with Json

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

the class JsonTest method create.

public void create() {
    json = new Json();
    // json.fromJson(Test1.class, //
    // "{byteArrayField:[-1\n,-2]}"
    // );
    // if (true) return;
    Test1 test = new Test1();
    test.booleanField = true;
    test.byteField = 123;
    test.charField = 'Z';
    test.shortField = 12345;
    test.intField = 123456;
    test.longField = 123456789;
    test.floatField = 123.456f;
    test.doubleField = 1.23456d;
    test.BooleanField = true;
    test.ByteField = -12;
    test.CharacterField = 'X';
    test.ShortField = -12345;
    test.IntegerField = -123456;
    test.LongField = -123456789l;
    test.FloatField = -123.3f;
    test.DoubleField = -0.121231d;
    test.stringField = "stringvalue";
    test.byteArrayField = new byte[] { 2, 1, 0, -1, -2 };
    test.map = new ObjectMap();
    test.map.put("one", 1);
    test.map.put("two", 2);
    test.map.put("nine", 9);
    test.stringArray = new Array();
    test.stringArray.add("meow");
    test.stringArray.add("moo");
    test.objectArray = new Array();
    test.objectArray.add("meow");
    test.objectArray.add(new Test1());
    test.someEnum = SomeEnum.b;
    roundTrip(test);
    test.someEnum = null;
    roundTrip(test);
    test = new Test1();
    roundTrip(test);
    test.stringArray = new Array();
    roundTrip(test);
    test.stringArray.add("meow");
    roundTrip(test);
    test.stringArray.add("moo");
    roundTrip(test);
    TestMapGraph objectGraph = new TestMapGraph();
    testObjectGraph(objectGraph, "exoticTypeName");
    test = new Test1();
    test.map = new ObjectMap();
    roundTrip(test);
    test.map.put("one", 1);
    roundTrip(test);
    test.map.put("two", 2);
    test.map.put("nine", 9);
    roundTrip(test);
    test.map.put("\nst\nuff\n", 9);
    test.map.put("\r\nst\r\nuff\r\n", 9);
    roundTrip(test);
    equals(json.toJson("meow"), "meow");
    equals(json.toJson("meow "), "\"meow \"");
    equals(json.toJson(" meow"), "\" meow\"");
    equals(json.toJson(" meow "), "\" meow \"");
    equals(json.toJson("\nmeow\n"), "\\nmeow\\n");
    equals(json.toJson(Array.with(1, 2, 3), null, int.class), "[1,2,3]");
    equals(json.toJson(Array.with("1", "2", "3"), null, String.class), "[1,2,3]");
    equals(json.toJson(Array.with(" 1", "2 ", " 3 "), null, String.class), "[\" 1\",\"2 \",\" 3 \"]");
    equals(json.toJson(Array.with("1", "", "3"), null, String.class), "[1,\"\",3]");
    System.out.println();
    System.out.println("Success!");
}
Also used : Array(com.badlogic.gdx.utils.Array) ObjectMap(com.badlogic.gdx.utils.ObjectMap) Json(com.badlogic.gdx.utils.Json)

Example 5 with Json

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

the class JsonTest method testObjectGraph.

private void testObjectGraph(TestMapGraph object, String typeName) {
    Json json = new Json();
    json.setTypeName(typeName);
    json.setUsePrototypes(false);
    json.setIgnoreUnknownFields(true);
    json.setOutputType(OutputType.json);
    String text = json.prettyPrint(object);
    TestMapGraph object2 = json.fromJson(TestMapGraph.class, text);
    if (object2.map.size() != object.map.size()) {
        throw new RuntimeException("Too many items in deserialized json map.");
    }
    if (object2.objectMap.size != object.objectMap.size) {
        throw new RuntimeException("Too many items in deserialized json object map.");
    }
    if (object2.arrayMap.size != object.arrayMap.size) {
        throw new RuntimeException("Too many items in deserialized json map.");
    }
}
Also used : Json(com.badlogic.gdx.utils.Json)

Aggregations

Json (com.badlogic.gdx.utils.Json)12 FileHandle (com.badlogic.gdx.files.FileHandle)4 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)4 Array (com.badlogic.gdx.utils.Array)4 Color (com.badlogic.gdx.graphics.Color)3 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)3 SerializationException (com.badlogic.gdx.utils.SerializationException)3 ReflectionException (com.badlogic.gdx.utils.reflect.ReflectionException)3 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)2 Drawable (com.badlogic.gdx.scenes.scene2d.utils.Drawable)2 NinePatchDrawable (com.badlogic.gdx.scenes.scene2d.utils.NinePatchDrawable)2 SpriteDrawable (com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable)2 TextureRegionDrawable (com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable)2 TiledDrawable (com.badlogic.gdx.scenes.scene2d.utils.TiledDrawable)2 ReadOnlySerializer (com.badlogic.gdx.utils.Json.ReadOnlySerializer)2 JsonValue (com.badlogic.gdx.utils.JsonValue)2 ObjectMap (com.badlogic.gdx.utils.ObjectMap)2 Field (com.badlogic.gdx.utils.reflect.Field)2 AssetDescriptor (com.badlogic.gdx.assets.AssetDescriptor)1 BitmapFontData (com.badlogic.gdx.graphics.g2d.BitmapFont.BitmapFontData)1