Search in sources :

Example 6 with JsonValue

use of com.badlogic.gdx.utils.JsonValue in project nhglib by VoidZombie.

the class InputConfigurationsJson method parse.

@Override
public void parse(JsonValue jsonValue) {
    inputConfigurations = new InputConfigurations();
    JsonValue configurationsJson = jsonValue.get("configurations");
    JsonValue keysJson = configurationsJson.get("keys");
    JsonValue sticksJson = configurationsJson.get("sticks");
    JsonValue pointersJson = configurationsJson.get("pointers");
    JsonValue controllersJson = configurationsJson.get("controllers");
    JsonValue mouseJson = configurationsJson.get("mouse");
    // Keys
    for (JsonValue keyJson : keysJson) {
        String name = keyJson.getString("name");
        Integer keyCode = keyJson.getInt("keyCode");
        InputMode inputMode = InputMode.fromString(keyJson.getString("inputMode"));
        KeyInputConfiguration keyInputConfiguration = new KeyInputConfiguration();
        keyInputConfiguration.setName(name);
        keyInputConfiguration.setKeyCode(keyCode);
        keyInputConfiguration.setInputMode(inputMode);
        inputConfigurations.keyInputConfigurations.add(keyInputConfiguration);
    }
    // Sticks
    for (JsonValue stickJson : sticksJson) {
        String name = stickJson.getString("name");
        Integer controllerId = stickJson.getInt("controllerId");
        StickType stickType = StickType.fromString(stickJson.getString("stickType"));
        StickInputConfiguration stickInputConfiguration = new StickInputConfiguration();
        stickInputConfiguration.setName(name);
        stickInputConfiguration.setControllerId(controllerId);
        stickInputConfiguration.setStickType(stickType);
        inputConfigurations.stickInputConfigurations.add(stickInputConfiguration);
    }
    // Pointers
    for (JsonValue pointerJson : pointersJson) {
        Integer pointerId = pointerJson.getInt("id");
        String name = pointerJson.getString("name");
        Float horizontalSensitivity = pointerJson.getFloat("horizontalSensitivity");
        Float verticalSensitivity = pointerJson.getFloat("verticalSensitivity");
        PointerSourceType pointerSourceType = PointerSourceType.fromString(pointerJson.getString("sourceType"));
        PointerInputConfiguration pointerInputConfiguration = new PointerInputConfiguration();
        pointerInputConfiguration.setId(pointerId);
        pointerInputConfiguration.setName(name);
        pointerInputConfiguration.setHorizontalSensitivity(horizontalSensitivity);
        pointerInputConfiguration.setVerticalSensitivity(verticalSensitivity);
        pointerInputConfiguration.setPointerSourceType(pointerSourceType);
        inputConfigurations.pointerInputConfigurations.add(pointerInputConfiguration);
    }
    // Controllers
    for (JsonValue controllerJson : controllersJson) {
        Integer id = controllerJson.getInt("id");
        JsonValue leftStickConfigurationJson = controllerJson.get("leftStick");
        JsonValue rightStickConfigurationJson = controllerJson.get("rightStick");
        StickConfiguration leftStick = stickConfigurationFromJson(leftStickConfigurationJson);
        StickConfiguration rightStick = stickConfigurationFromJson(rightStickConfigurationJson);
        ControllerConfiguration controllerConfiguration = new ControllerConfiguration();
        controllerConfiguration.setId(id);
        controllerConfiguration.setLeftStick(leftStick);
        controllerConfiguration.setRightStick(rightStick);
        inputConfigurations.controllerConfigurations.add(controllerConfiguration);
    }
    // Mouse
    for (JsonValue mouse : mouseJson) {
        Float mouseHorizontalSensitivity = mouse.getFloat("horizontalSensitivity", 0f);
        Float mouseVerticalSensitivity = mouse.getFloat("verticalSensitivity", 0f);
        String name = mouse.getString("name");
        MouseSourceType mouseSourceType = MouseSourceType.fromString(mouse.getString("sourceType"));
        MouseInputConfiguration mouseInputConfiguration = new MouseInputConfiguration();
        mouseInputConfiguration.setName(name);
        mouseInputConfiguration.setHorizontalSensitivity(mouseHorizontalSensitivity);
        mouseInputConfiguration.setVerticalSensitivity(mouseVerticalSensitivity);
        mouseInputConfiguration.setMouseSourceType(mouseSourceType);
        inputConfigurations.mouseInputConfigurations.add(mouseInputConfiguration);
    }
}
Also used : InputMode(io.github.voidzombie.nhglib.input.models.InputMode) StickInputConfiguration(io.github.voidzombie.nhglib.input.configuration.impls.StickInputConfiguration) JsonValue(com.badlogic.gdx.utils.JsonValue) StickType(io.github.voidzombie.nhglib.input.models.StickType) InputConfigurations(io.github.voidzombie.nhglib.input.configuration.InputConfigurations) PointerSourceType(io.github.voidzombie.nhglib.input.models.PointerSourceType) MouseInputConfiguration(io.github.voidzombie.nhglib.input.configuration.impls.MouseInputConfiguration) PointerInputConfiguration(io.github.voidzombie.nhglib.input.configuration.impls.PointerInputConfiguration) KeyInputConfiguration(io.github.voidzombie.nhglib.input.configuration.impls.KeyInputConfiguration) ControllerConfiguration(io.github.voidzombie.nhglib.input.controllers.ControllerConfiguration) StickConfiguration(io.github.voidzombie.nhglib.input.controllers.StickConfiguration) MouseSourceType(io.github.voidzombie.nhglib.input.models.MouseSourceType)

Example 7 with JsonValue

use of com.badlogic.gdx.utils.JsonValue in project nhglib by VoidZombie.

the class TransformJson method parse.

@Override
public void parse(JsonValue jsonValue) {
    JsonValue positionJson = jsonValue.get("position");
    JsonValue rotationJson = jsonValue.get("rotation");
    JsonValue scaleJson = jsonValue.get("scale");
    float xPosition = positionJson.getFloat("x");
    float yPosition = positionJson.getFloat("y");
    float zPosition = positionJson.getFloat("z");
    float xRotation = rotationJson.getFloat("x");
    float yRotation = rotationJson.getFloat("y");
    float zRotation = rotationJson.getFloat("z");
    float xScale = scaleJson.getFloat("x");
    float yScale = scaleJson.getFloat("y");
    float zScale = scaleJson.getFloat("z");
    position.set(xPosition, yPosition, zPosition);
    rotation.set(xRotation, yRotation, zRotation);
    scale.set(xScale, yScale, zScale);
}
Also used : JsonValue(com.badlogic.gdx.utils.JsonValue)

Example 8 with JsonValue

use of com.badlogic.gdx.utils.JsonValue in project nhglib by VoidZombie.

the class RigidBodyComponentJson method parse.

@Override
public void parse(JsonValue jsonValue) {
    RigidBodyComponent rigidBodyComponent = entities.createComponent(entity, RigidBodyComponent.class);
    JsonValue shapeJson = jsonValue.get("shape");
    String activationStateString = jsonValue.getString("activationState", "wantsDeactivation");
    ActivationState activationStateJson = ActivationState.fromString(activationStateString);
    btCollisionShape collisionShape = getCollisionShape(shapeJson);
    int activationState = activationStateJson.state;
    float mass = jsonValue.getFloat("mass", 1.0f);
    float friction = jsonValue.getFloat("friction", 0.5f);
    float restitution = jsonValue.getFloat("restitution", 0f);
    rigidBodyComponent.build(collisionShape, activationState, mass, friction, restitution);
    output = rigidBodyComponent;
}
Also used : RigidBodyComponent(io.github.voidzombie.nhglib.runtime.ecs.components.physics.RigidBodyComponent) JsonValue(com.badlogic.gdx.utils.JsonValue) ActivationState(io.github.voidzombie.nhglib.data.models.serialization.physics.ActivationState) com.badlogic.gdx.physics.bullet.collision.btCollisionShape(com.badlogic.gdx.physics.bullet.collision.btCollisionShape)

Example 9 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)

Example 10 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)

Aggregations

JsonValue (com.badlogic.gdx.utils.JsonValue)25 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)7 Array (com.badlogic.gdx.utils.Array)5 Color (com.badlogic.gdx.graphics.Color)3 Environment (com.badlogic.gdx.graphics.g3d.Environment)3 ModelBuilder (com.badlogic.gdx.graphics.g3d.utils.ModelBuilder)3 Matrix4 (com.badlogic.gdx.math.Matrix4)3 Vector3 (com.badlogic.gdx.math.Vector3)3 FileHandle (com.badlogic.gdx.files.FileHandle)2 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)2 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)2 Quaternion (com.badlogic.gdx.math.Quaternion)2 Json (com.badlogic.gdx.utils.Json)2 ReadOnlySerializer (com.badlogic.gdx.utils.Json.ReadOnlySerializer)2 SerializationException (com.badlogic.gdx.utils.SerializationException)2 ReflectionException (com.badlogic.gdx.utils.reflect.ReflectionException)2 ArrayListGameObject (com.nilunder.bdx.GameObject.ArrayListGameObject)2 ModelComponent (io.github.voidzombie.nhglib.runtime.ecs.components.graphics.ModelComponent)2 NodeComponent (io.github.voidzombie.nhglib.runtime.ecs.components.scenes.NodeComponent)2 GraphicsSystem (io.github.voidzombie.nhglib.runtime.ecs.systems.impl.GraphicsSystem)2