Search in sources :

Example 1 with ControllerAction

use of org.terasology.input.device.ControllerAction in project Terasology by MovingBlocks.

the class InputSystem method processControllerInput.

private void processControllerInput(float delta) {
    if (!isCapturingMouse()) {
        return;
    }
    for (ControllerAction action : controllers.getInputQueue()) {
        // TODO: send event to entity system
        boolean consumed = false;
        Input input = action.getInput();
        if (input.getType() == InputType.CONTROLLER_BUTTON) {
            processControllerButtonInput(delta, action, consumed, input);
        } else if (input.getType() == InputType.CONTROLLER_AXIS) {
            processControllerAxisInput(action, input);
        }
    }
}
Also used : ControllerAction(org.terasology.input.device.ControllerAction)

Example 2 with ControllerAction

use of org.terasology.input.device.ControllerAction in project Terasology by MovingBlocks.

the class JInputControllerDevice method getInputQueue.

@Override
public Queue<ControllerAction> getInputQueue() {
    Queue<ControllerAction> result = new ArrayDeque<>();
    Event event = new Event();
    Iterator<Controller> it = controllers.iterator();
    while (it.hasNext()) {
        Controller c = it.next();
        if (c.poll()) {
            EventQueue queue = c.getEventQueue();
            while (queue.getNextEvent(event)) {
                ControllerAction action = convertEvent(c, event);
                if (action != null) {
                    result.add(action);
                }
            }
        } else {
            removeController(c);
        }
    }
    return result;
}
Also used : Event(net.java.games.input.Event) ControllerEvent(net.java.games.input.ControllerEvent) Controller(net.java.games.input.Controller) ControllerAction(org.terasology.input.device.ControllerAction) ArrayDeque(java.util.ArrayDeque) EventQueue(net.java.games.input.EventQueue)

Example 3 with ControllerAction

use of org.terasology.input.device.ControllerAction 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 4 with ControllerAction

use of org.terasology.input.device.ControllerAction in project Terasology by MovingBlocks.

the class InputSystem method processControllerInput.

/**
 * Processes the current input state of any connected controllers, and updates bind buttons.
 * <p>
 * Controller button and axis events are both handled in {@link #processControllerButtonInput(float,
 * ControllerAction, boolean, Input)} and {@link #processControllerAxisInput(ControllerAction, Input)} accordingly.
 *
 * @param delta The length of the current frame.
 */
private void processControllerInput(float delta) {
    if (!isCapturingMouse()) {
        return;
    }
    for (ControllerAction action : controllers.getInputQueue()) {
        // TODO: send event to entity system
        boolean consumed = false;
        Input input = action.getInput();
        if (input.getType() == InputType.CONTROLLER_BUTTON) {
            processControllerButtonInput(delta, action, consumed, input);
        } else if (input.getType() == InputType.CONTROLLER_AXIS) {
            processControllerAxisInput(action, input);
        }
    }
}
Also used : Input(org.terasology.input.Input) MouseInput(org.terasology.input.MouseInput) ControllerAction(org.terasology.input.device.ControllerAction)

Example 5 with ControllerAction

use of org.terasology.input.device.ControllerAction 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)

Aggregations

ControllerAction (org.terasology.input.device.ControllerAction)5 Input (org.terasology.input.Input)3 ButtonState (org.terasology.input.ButtonState)2 ByteBuffer (java.nio.ByteBuffer)1 FloatBuffer (java.nio.FloatBuffer)1 ArrayDeque (java.util.ArrayDeque)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 Controller (net.java.games.input.Controller)1 ControllerEvent (net.java.games.input.ControllerEvent)1 Event (net.java.games.input.Event)1 EventQueue (net.java.games.input.EventQueue)1 GLFWGamepadState (org.lwjgl.glfw.GLFWGamepadState)1 ControllerInfo (org.terasology.config.ControllerConfig.ControllerInfo)1 MouseInput (org.terasology.input.MouseInput)1