Search in sources :

Example 1 with StickInputConfiguration

use of io.github.voidzombie.nhglib.input.configuration.impls.StickInputConfiguration 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 2 with StickInputConfiguration

use of io.github.voidzombie.nhglib.input.configuration.impls.StickInputConfiguration in project nhglib by VoidZombie.

the class InputHandler method processStickInput.

private void processStickInput(Integer controllerId, Controller controller, NhgInput input) {
    if (config != null) {
        Vector2 axis = VectorPool.getVector2();
        axis.set(0, 0);
        boolean invertHorizontalAxis = false;
        boolean invertVerticalAxis = false;
        float deadZone = 0f;
        float horizontalSensitivity = 1f;
        float verticalSensitivity = 1f;
        StickInputConfiguration conf = config.getStickConfiguration(input.getName());
        StickType stickType = conf.getStickType();
        ControllerConfiguration controllerConf = config.getControllerConfiguration(controllerId);
        switch(stickType) {
            case LEFT:
                if (Xbox.isXboxController(controller)) {
                    axis.x = controller.getAxis(ControllerCodes.Xbox360.STICK_LEFT_HORIZONTAL);
                    axis.y = controller.getAxis(ControllerCodes.Xbox360.STICK_LEFT_VERTICAL);
                } else if (Ouya.isRunningOnOuya()) {
                    axis.x = controller.getAxis(Ouya.AXIS_LEFT_X);
                    axis.y = controller.getAxis(Ouya.AXIS_LEFT_Y);
                }
                StickConfiguration stickConfiguration = controllerConf.getLeftStick();
                invertHorizontalAxis = stickConfiguration.getInvertHorizontalAxis();
                invertVerticalAxis = stickConfiguration.getInvertVerticalAxis();
                deadZone = stickConfiguration.getDeadZoneRadius();
                horizontalSensitivity = stickConfiguration.getHorizontalSensitivity();
                verticalSensitivity = stickConfiguration.getVerticalSensitivity();
                break;
            case RIGHT:
                if (Xbox.isXboxController(controller)) {
                    axis.x = controller.getAxis(ControllerCodes.Xbox360.STICK_RIGHT_HORIZONTAL);
                    axis.y = controller.getAxis(ControllerCodes.Xbox360.STICK_RIGHT_VERTICAL);
                } else if (Ouya.isRunningOnOuya()) {
                    axis.x = controller.getAxis(Ouya.AXIS_RIGHT_X);
                    axis.y = controller.getAxis(Ouya.AXIS_RIGHT_Y);
                }
                stickConfiguration = controllerConf.getRightStick();
                invertHorizontalAxis = stickConfiguration.getInvertHorizontalAxis();
                invertVerticalAxis = stickConfiguration.getInvertVerticalAxis();
                deadZone = stickConfiguration.getDeadZoneRadius();
                horizontalSensitivity = stickConfiguration.getHorizontalSensitivity();
                verticalSensitivity = stickConfiguration.getVerticalSensitivity();
                break;
            case VIRTUAL:
                break;
        }
        if (invertHorizontalAxis) {
            axis.x *= -1;
        }
        if (invertVerticalAxis) {
            axis.y *= -1;
        }
        if (Math.abs(axis.x) < deadZone) {
            axis.x = 0;
        }
        if (Math.abs(axis.y) < deadZone) {
            axis.y = 0;
        }
        axis.scl(horizontalSensitivity, verticalSensitivity);
        InputSource inputSource = input.getInputSource();
        inputSource.setName(controller.getName());
        inputSource.setValue(axis);
    }
}
Also used : StickInputConfiguration(io.github.voidzombie.nhglib.input.configuration.impls.StickInputConfiguration) Vector2(com.badlogic.gdx.math.Vector2) ControllerConfiguration(io.github.voidzombie.nhglib.input.controllers.ControllerConfiguration) StickConfiguration(io.github.voidzombie.nhglib.input.controllers.StickConfiguration)

Aggregations

StickInputConfiguration (io.github.voidzombie.nhglib.input.configuration.impls.StickInputConfiguration)2 ControllerConfiguration (io.github.voidzombie.nhglib.input.controllers.ControllerConfiguration)2 StickConfiguration (io.github.voidzombie.nhglib.input.controllers.StickConfiguration)2 Vector2 (com.badlogic.gdx.math.Vector2)1 JsonValue (com.badlogic.gdx.utils.JsonValue)1 InputConfigurations (io.github.voidzombie.nhglib.input.configuration.InputConfigurations)1 KeyInputConfiguration (io.github.voidzombie.nhglib.input.configuration.impls.KeyInputConfiguration)1 MouseInputConfiguration (io.github.voidzombie.nhglib.input.configuration.impls.MouseInputConfiguration)1 PointerInputConfiguration (io.github.voidzombie.nhglib.input.configuration.impls.PointerInputConfiguration)1 InputMode (io.github.voidzombie.nhglib.input.models.InputMode)1 MouseSourceType (io.github.voidzombie.nhglib.input.models.MouseSourceType)1 PointerSourceType (io.github.voidzombie.nhglib.input.models.PointerSourceType)1 StickType (io.github.voidzombie.nhglib.input.models.StickType)1