Search in sources :

Example 1 with ButtonState

use of org.terasology.input.ButtonState in project Terasology by MovingBlocks.

the class JInputControllerDevice method convertEvent.

private ControllerAction convertEvent(Controller c, Event event) {
    Component comp = event.getComponent();
    Identifier id = comp.getIdentifier();
    float axisValue = comp.getPollData();
    Input input;
    ButtonState state = ButtonState.UP;
    if (id instanceof Identifier.Button) {
        state = event.getValue() != 0 ? ButtonState.DOWN : ButtonState.UP;
        Integer buttonId = buttonMap.get(id);
        if (buttonId == null) {
            // button not registered
            return null;
        }
        input = InputType.CONTROLLER_BUTTON.getInput(buttonId);
    } else if (id instanceof Identifier.Axis) {
        ControllerInfo info = config.getController(c.getName());
        if (id.equals(Identifier.Axis.X)) {
            if (Math.abs(axisValue) < info.getMovementDeadZone()) {
                axisValue = 0;
            }
            input = InputType.CONTROLLER_AXIS.getInput(ControllerId.X_AXIS);
        } else if (id.equals(Identifier.Axis.Y)) {
            if (Math.abs(axisValue) < info.getMovementDeadZone()) {
                axisValue = 0;
            }
            input = InputType.CONTROLLER_AXIS.getInput(ControllerId.Y_AXIS);
        } else if (id.equals(Identifier.Axis.Z)) {
            if (Math.abs(axisValue) < info.getMovementDeadZone()) {
                axisValue = 0;
            }
            input = InputType.CONTROLLER_AXIS.getInput(ControllerId.Z_AXIS);
        } else if (id.equals(Identifier.Axis.RX)) {
            if (Math.abs(axisValue) < info.getRotationDeadZone()) {
                axisValue = 0;
            }
            input = InputType.CONTROLLER_AXIS.getInput(ControllerId.RX_AXIS);
        } else if (id.equals(Identifier.Axis.RY)) {
            if (Math.abs(axisValue) < info.getRotationDeadZone()) {
                axisValue = 0;
            }
            input = InputType.CONTROLLER_AXIS.getInput(ControllerId.RY_AXIS);
        } else if (id.equals(Identifier.Axis.POV)) {
            // the poll data float value is actually an ID in this case
            boolean isX = (axisValue == Component.POV.LEFT) || (axisValue == Component.POV.RIGHT);
            boolean isY = (axisValue == Component.POV.UP) || (axisValue == Component.POV.DOWN);
            if (isX || isY) {
                input = InputType.CONTROLLER_AXIS.getInput(isX ? ControllerId.POVX_AXIS : ControllerId.POVY_AXIS);
                if ((axisValue == Component.POV.UP) || (axisValue == Component.POV.LEFT)) {
                    axisValue = -1;
                }
                if ((axisValue == Component.POV.DOWN) || (axisValue == Component.POV.RIGHT)) {
                    axisValue = 1;
                }
            } else {
                // TODO: handle 8-button POVs
                return null;
            }
        } else {
            // unrecognized axis
            return null;
        }
    } else {
        // unrecognized id (e.g. Identifier.Key)
        return null;
    }
    return new ControllerAction(input, c.getName(), state, axisValue);
}
Also used : Input(org.terasology.input.Input) Identifier(net.java.games.input.Component.Identifier) Button(net.java.games.input.Component.Identifier.Button) Component(net.java.games.input.Component) ButtonState(org.terasology.input.ButtonState) ControllerInfo(org.terasology.config.ControllerConfig.ControllerInfo) ControllerAction(org.terasology.input.device.ControllerAction)

Example 2 with ButtonState

use of org.terasology.input.ButtonState in project Terasology by MovingBlocks.

the class LwjglKeyboardDevice method getInputQueue.

@Override
public Queue<KeyboardAction> getInputQueue() {
    Queue<KeyboardAction> result = Queues.newArrayDeque();
    while (Keyboard.next()) {
        ButtonState state;
        if (Keyboard.isRepeatEvent()) {
            state = ButtonState.REPEAT;
        } else {
            state = (Keyboard.getEventKeyState()) ? ButtonState.DOWN : ButtonState.UP;
        }
        Input input = InputType.KEY.getInput(Keyboard.getEventKey());
        result.add(new KeyboardAction(input, state, Keyboard.getEventCharacter()));
    }
    return result;
}
Also used : Input(org.terasology.input.Input) KeyboardAction(org.terasology.input.device.KeyboardAction) ButtonState(org.terasology.input.ButtonState)

Example 3 with ButtonState

use of org.terasology.input.ButtonState in project Terasology by MovingBlocks.

the class LwjglMouseDevice method getInputQueue.

@Override
public Queue<MouseAction> getInputQueue() {
    Queue<MouseAction> result = Queues.newArrayDeque();
    while (Mouse.next()) {
        if (Mouse.getEventButton() != -1) {
            ButtonState state = (Mouse.getEventButtonState()) ? ButtonState.DOWN : ButtonState.UP;
            result.add(new MouseAction(InputType.MOUSE_BUTTON.getInput(Mouse.getEventButton()), state, getPosition()));
        }
        if (Mouse.getEventDWheel() != 0) {
            int id = (Mouse.getEventDWheel() > 0) ? 1 : -1;
            result.add(new MouseAction(InputType.MOUSE_WHEEL.getInput(id), id * Mouse.getEventDWheel() / 120, getPosition()));
        }
    }
    return result;
}
Also used : MouseAction(org.terasology.input.device.MouseAction) ButtonState(org.terasology.input.ButtonState)

Example 4 with ButtonState

use of org.terasology.input.ButtonState in project Terasology by MovingBlocks.

the class LwjglControllerDevice method getInputQueue.

@Override
public Queue<ControllerAction> getInputQueue() {
    Queue<ControllerAction> controllerActions = Lists.newLinkedList();
    for (int jid : gamepadIds.toArray()) {
        if (GLFW.glfwJoystickPresent(jid)) {
            // callback remove it later, if you are here
            continue;
        }
        String controllerName = GLFW.glfwGetGamepadName(jid);
        GLFWGamepadState gamepadState = GLFWGamepadState.create();
        if (GLFW.glfwGetGamepadState(jid, gamepadState)) {
            FloatBuffer axes = gamepadState.axes();
            int axesIndex = 0;
            while (axes.hasRemaining()) {
                int teraId = axisMapping.get(axesIndex);
                Input input = InputType.CONTROLLER_AXIS.getInput(teraId);
                float axisValue = axes.get();
                if (Math.abs(axisValue) < getDeadzoneForInput(controllerName, input)) {
                    axisValue = 0;
                }
                controllerActions.add(new ControllerAction(input, controllerName, ButtonState.UP, axisValue));
                axesIndex++;
            }
            ByteBuffer buttonsStates = gamepadState.buttons();
            int buttonIndex = 0;
            while (buttonsStates.hasRemaining()) {
                int teraButtonId = buttonMap.get(buttonIndex);
                if (teraButtonId == -1) {
                    if (GLFW.GLFW_GAMEPAD_BUTTON_DPAD_UP <= buttonIndex && buttonIndex <= GLFW.GLFW_GAMEPAD_BUTTON_DPAD_LEFT) {
                        boolean isX = (buttonIndex == GLFW.GLFW_GAMEPAD_BUTTON_DPAD_LEFT) || (buttonIndex == GLFW.GLFW_GAMEPAD_BUTTON_DPAD_RIGHT);
                        Input input = InputType.CONTROLLER_AXIS.getInput(isX ? ControllerId.POVX_AXIS : ControllerId.POVY_AXIS);
                        float axisValue;
                        if (buttonIndex == GLFW.GLFW_GAMEPAD_BUTTON_DPAD_RIGHT || buttonIndex == GLFW.GLFW_GAMEPAD_BUTTON_DPAD_DOWN) {
                            axisValue = -1;
                        } else {
                            axisValue = 1;
                        }
                        controllerActions.add(new ControllerAction(input, controllerName, ButtonState.UP, axisValue));
                    }
                    logger.error("Received unknown/unhandled buttonId for GLFW: {}", buttonIndex);
                } else {
                    Input input = InputType.CONTROLLER_BUTTON.getInput(teraButtonId);
                    short btnState = buttonsStates.get();
                    ButtonState buttonState = btnState == GLFW.GLFW_RELEASE ? ButtonState.UP : ButtonState.DOWN;
                    controllerActions.add(new ControllerAction(input, controllerName, buttonState, 0.0F));
                }
                buttonIndex++;
            }
        } else {
            logger.error("Cannot get states for {}", GLFW.glfwGetGamepadName(jid));
        }
    }
    return controllerActions;
}
Also used : Input(org.terasology.input.Input) GLFWGamepadState(org.lwjgl.glfw.GLFWGamepadState) FloatBuffer(java.nio.FloatBuffer) ByteBuffer(java.nio.ByteBuffer) ButtonState(org.terasology.input.ButtonState) ControllerAction(org.terasology.input.device.ControllerAction)

Example 5 with ButtonState

use of org.terasology.input.ButtonState in project Terasology by MovingBlocks.

the class LwjglKeyboardDevice method glfwKeyCallback.

/**
 * Callback receive key input events. All keys in {@link GLFW}
 *
 * @param window window's pointer
 * @param key one of key listed in {@link GLFW} GLFW_KEY*, also you can see {@link
 *         LwjglKeyboardDevice#GLFW_TO_TERA_MAPPING}
 * @param scancode
 * @param action button state now: {@link GLFW#GLFW_PRESS},{@link GLFW#GLFW_RELEASE} or {@link
 *         GLFW#GLFW_REPEAT}
 * @param mods - modification keys: {@link GLFW#GLFW_MOD_SHIFT} and etc.
 */
private void glfwKeyCallback(long window, int key, int scancode, int action, int mods) {
    int teraKey = GLFW_TO_TERA_MAPPING.get(key);
    Input input = InputType.KEY.getInput(teraKey);
    ButtonState state;
    if (action == GLFW.GLFW_PRESS) {
        buttonStates.add(teraKey);
        state = ButtonState.DOWN;
    } else if (action == GLFW.GLFW_RELEASE) {
        state = ButtonState.UP;
        buttonStates.remove(teraKey);
    } else /*if (action == GLFW.GLFW_REPEAT)*/
    {
        state = ButtonState.REPEAT;
    }
    rawKeyQueue.offer(new RawKeyboardAction(input, state));
}
Also used : Input(org.terasology.input.Input) ButtonState(org.terasology.input.ButtonState) RawKeyboardAction(org.terasology.input.device.RawKeyboardAction)

Aggregations

ButtonState (org.terasology.input.ButtonState)6 Input (org.terasology.input.Input)4 ControllerAction (org.terasology.input.device.ControllerAction)2 MouseAction (org.terasology.input.device.MouseAction)2 ByteBuffer (java.nio.ByteBuffer)1 FloatBuffer (java.nio.FloatBuffer)1 Component (net.java.games.input.Component)1 Identifier (net.java.games.input.Component.Identifier)1 Button (net.java.games.input.Component.Identifier.Button)1 GLFWGamepadState (org.lwjgl.glfw.GLFWGamepadState)1 ControllerInfo (org.terasology.config.ControllerConfig.ControllerInfo)1 MouseInput (org.terasology.input.MouseInput)1 KeyboardAction (org.terasology.input.device.KeyboardAction)1 RawKeyboardAction (org.terasology.input.device.RawKeyboardAction)1