Search in sources :

Example 21 with Array

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

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

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

the class ParticleEmitterTest method create.

@Override
public void create() {
    spriteBatch = new SpriteBatch();
    effect = new ParticleEffect();
    effect.load(Gdx.files.internal("data/test.p"), Gdx.files.internal("data"));
    effect.setPosition(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2);
    // Of course, a ParticleEffect is normally just used, without messing around with its emitters.
    emitters = new Array(effect.getEmitters());
    effect.getEmitters().clear();
    effect.getEmitters().add(emitters.get(0));
    inputProcessor = new InputProcessor() {

        public boolean touchUp(int x, int y, int pointer, int button) {
            return false;
        }

        public boolean touchDragged(int x, int y, int pointer) {
            effect.setPosition(x, Gdx.graphics.getHeight() - y);
            return false;
        }

        public boolean touchDown(int x, int y, int pointer, int newParam) {
            // effect.setPosition(x, Gdx.graphics.getHeight() - y);
            ParticleEmitter emitter = emitters.get(emitterIndex);
            particleCount += 100;
            System.out.println(particleCount);
            particleCount = Math.max(0, particleCount);
            if (particleCount > emitter.getMaxParticleCount())
                emitter.setMaxParticleCount(particleCount * 2);
            emitter.getEmission().setHigh(particleCount / emitter.getLife().getHighMax() * 1000);
            effect.getEmitters().clear();
            effect.getEmitters().add(emitter);
            return false;
        }

        public boolean keyUp(int keycode) {
            return false;
        }

        public boolean keyTyped(char character) {
            return false;
        }

        public boolean keyDown(int keycode) {
            ParticleEmitter emitter = emitters.get(emitterIndex);
            if (keycode == Input.Keys.DPAD_UP)
                particleCount += 5;
            else if (keycode == Input.Keys.DPAD_DOWN)
                particleCount -= 5;
            else if (keycode == Input.Keys.SPACE) {
                emitterIndex = (emitterIndex + 1) % emitters.size;
                emitter = emitters.get(emitterIndex);
                // if we've previously stopped the emitter reset it
                if (emitter.isComplete())
                    emitter.reset();
                particleCount = (int) (emitter.getEmission().getHighMax() * emitter.getLife().getHighMax() / 1000f);
            } else if (keycode == Input.Keys.ENTER) {
                emitter = emitters.get(emitterIndex);
                if (emitter.isComplete())
                    emitter.reset();
                else
                    emitter.allowCompletion();
            } else
                return false;
            particleCount = Math.max(0, particleCount);
            if (particleCount > emitter.getMaxParticleCount())
                emitter.setMaxParticleCount(particleCount * 2);
            emitter.getEmission().setHigh(particleCount / emitter.getLife().getHighMax() * 1000);
            effect.getEmitters().clear();
            effect.getEmitters().add(emitter);
            return false;
        }

        @Override
        public boolean mouseMoved(int x, int y) {
            return false;
        }

        @Override
        public boolean scrolled(int amount) {
            return false;
        }
    };
    Gdx.input.setInputProcessor(inputProcessor);
}
Also used : ParticleEffect(com.badlogic.gdx.graphics.g2d.ParticleEffect) Array(com.badlogic.gdx.utils.Array) ParticleEmitter(com.badlogic.gdx.graphics.g2d.ParticleEmitter) InputProcessor(com.badlogic.gdx.InputProcessor) SpriteBatch(com.badlogic.gdx.graphics.g2d.SpriteBatch)

Example 24 with Array

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

the class SelectTest method createDummies.

public static Array<Dummy> createDummies(int n) {
    float variance = 20;
    Array<Dummy> dummies = new Array<Dummy>();
    for (int i = 0; i < n; i++) {
        Dummy d = new Dummy();
        dummies.add(d);
        d.pos = new Vector2();
        d.id = nextID++;
    }
    return dummies;
}
Also used : Array(com.badlogic.gdx.utils.Array) Vector2(com.badlogic.gdx.math.Vector2)

Example 25 with Array

use of com.badlogic.gdx.utils.Array in project RubeLoader by tescott.

the class WorldSerializer method read.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public World read(Json json, JsonValue jsonData, Class type) {
    World world = scene.getWorld();
    if (world == null) {
        boolean allowSleep = json.readValue("allowSleep", boolean.class, RubeDefaults.World.allowSleep, jsonData);
        boolean autoClearForces = json.readValue("autoClearForces", boolean.class, RubeDefaults.World.autoClearForces, jsonData);
        boolean continuousPhysics = json.readValue("continuousPhysics", boolean.class, RubeDefaults.World.continuousPhysics, jsonData);
        boolean warmStarting = json.readValue("warmStarting", boolean.class, RubeDefaults.World.warmStarting, jsonData);
        Vector2 gravity = json.readValue("gravity", Vector2.class, RubeDefaults.World.gravity, jsonData);
        world = new World(gravity, allowSleep);
        world.setAutoClearForces(autoClearForces);
        world.setContinuousPhysics(continuousPhysics);
        world.setWarmStarting(warmStarting);
    }
    // else ignore world settings and use the ones that were previously loaded
    scene.parseCustomProperties(json, world, jsonData);
    // Bodies
    bodySerializer.setWorld(world);
    Array<Body> bodies = json.readValue("body", Array.class, Body.class, jsonData);
    if (bodies != null) {
        if (scene.getBodies() == null) {
            scene.setBodies(bodies);
        } else {
            scene.addBodies(bodies);
        }
    }
    // Joints
    // joints are done in two passes because gear joints reference other joints
    // First joint pass
    jointSerializer.init(world, bodies, null);
    Array<Joint> joints = json.readValue("joint", Array.class, Joint.class, jsonData);
    if (joints != null) {
        if (scene.getJoints() == null) {
            scene.setJoints(joints);
        } else {
            scene.getJoints().addAll(joints);
        }
    }
    // Second joint pass
    jointSerializer.init(world, bodies, joints);
    joints = json.readValue("joint", Array.class, Joint.class, jsonData);
    // Images
    Array<RubeImage> images = json.readValue("image", Array.class, RubeImage.class, jsonData);
    if (images != null) {
        if (scene.getImages() == null) {
            scene.setImages(images);
        } else {
            scene.getImages().addAll(images);
        }
        for (int i = 0; i < images.size; i++) {
            RubeImage image = images.get(i);
            scene.setMappedImage(image.body, image);
        }
    }
    return world;
}
Also used : Array(com.badlogic.gdx.utils.Array) Vector2(com.badlogic.gdx.math.Vector2) World(com.badlogic.gdx.physics.box2d.World) Joint(com.badlogic.gdx.physics.box2d.Joint) Body(com.badlogic.gdx.physics.box2d.Body) RubeImage(com.gushikustudios.rube.loader.serializers.utils.RubeImage) Joint(com.badlogic.gdx.physics.box2d.Joint)

Aggregations

Array (com.badlogic.gdx.utils.Array)67 FileHandle (com.badlogic.gdx.files.FileHandle)18 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)16 AssetDescriptor (com.badlogic.gdx.assets.AssetDescriptor)9 Texture (com.badlogic.gdx.graphics.Texture)8 TextureAtlas (com.badlogic.gdx.graphics.g2d.TextureAtlas)7 IntArray (com.badlogic.gdx.utils.IntArray)7 Element (com.badlogic.gdx.utils.XmlReader.Element)7 IOException (java.io.IOException)7 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)5 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)5 JsonValue (com.badlogic.gdx.utils.JsonValue)5 ObjectMap (com.badlogic.gdx.utils.ObjectMap)5 Color (com.badlogic.gdx.graphics.Color)4 ModelMaterial (com.badlogic.gdx.graphics.g3d.model.data.ModelMaterial)4 Page (com.badlogic.gdx.tools.texturepacker.TexturePacker.Page)4 Rect (com.badlogic.gdx.tools.texturepacker.TexturePacker.Rect)4 Json (com.badlogic.gdx.utils.Json)4 TextureParameter (com.badlogic.gdx.assets.loaders.TextureLoader.TextureParameter)3 GwtFileHandle (com.badlogic.gdx.backends.gwt.GwtFileHandle)3