Search in sources :

Example 1 with Button

use of net.java.games.input.Component.Identifier.Button 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 Button

use of net.java.games.input.Component.Identifier.Button in project jmonkeyengine by jMonkeyEngine.

the class JInputJoyInput method update.

public void update() {
    ControllerEnvironment ce = ControllerEnvironment.getDefaultEnvironment();
    Controller[] cs = ce.getControllers();
    Event e = new Event();
    for (int i = 0; i < cs.length; i++) {
        Controller c = cs[i];
        JInputJoystick stick = joystickIndex.get(c);
        if (stick == null)
            continue;
        if (!c.poll())
            continue;
        int joyId = stick.getJoyId();
        EventQueue q = c.getEventQueue();
        while (q.getNextEvent(e)) {
            Identifier id = e.getComponent().getIdentifier();
            if (id == Identifier.Axis.POV) {
                float x = 0, y = 0;
                float v = e.getValue();
                if (v == POV.CENTER) {
                    x = 0;
                    y = 0;
                } else if (v == POV.DOWN) {
                    x = 0;
                    y = -1f;
                } else if (v == POV.DOWN_LEFT) {
                    x = -1f;
                    y = -1f;
                } else if (v == POV.DOWN_RIGHT) {
                    x = 1f;
                    y = -1f;
                } else if (v == POV.LEFT) {
                    x = -1f;
                    y = 0;
                } else if (v == POV.RIGHT) {
                    x = 1f;
                    y = 0;
                } else if (v == POV.UP) {
                    x = 0;
                    y = 1f;
                } else if (v == POV.UP_LEFT) {
                    x = -1f;
                    y = 1f;
                } else if (v == POV.UP_RIGHT) {
                    x = 1f;
                    y = 1f;
                }
                JoyAxisEvent evt1 = new JoyAxisEvent(stick.povX, x);
                JoyAxisEvent evt2 = new JoyAxisEvent(stick.povY, y);
                listener.onJoyAxisEvent(evt1);
                listener.onJoyAxisEvent(evt2);
            } else if (id instanceof Axis) {
                float value = e.getValue();
                JoystickAxis axis = stick.axisIndex.get(e.getComponent());
                JoyAxisEvent evt = new JoyAxisEvent(axis, value);
                listener.onJoyAxisEvent(evt);
            } else if (id instanceof Button) {
                JoystickButton button = stick.buttonIndex.get(e.getComponent());
                JoyButtonEvent evt = new JoyButtonEvent(button, e.getValue() == 1f);
                listener.onJoyButtonEvent(evt);
            }
        }
    }
}
Also used : DefaultJoystickButton(com.jme3.input.DefaultJoystickButton) JoystickButton(com.jme3.input.JoystickButton) Identifier(net.java.games.input.Component.Identifier) JoyAxisEvent(com.jme3.input.event.JoyAxisEvent) JoyButtonEvent(com.jme3.input.event.JoyButtonEvent) DefaultJoystickButton(com.jme3.input.DefaultJoystickButton) JoystickButton(com.jme3.input.JoystickButton) Button(net.java.games.input.Component.Identifier.Button) JoyAxisEvent(com.jme3.input.event.JoyAxisEvent) JoyButtonEvent(com.jme3.input.event.JoyButtonEvent) JoystickAxis(com.jme3.input.JoystickAxis) DefaultJoystickAxis(com.jme3.input.DefaultJoystickAxis) JoystickAxis(com.jme3.input.JoystickAxis) DefaultJoystickAxis(com.jme3.input.DefaultJoystickAxis) Axis(net.java.games.input.Component.Identifier.Axis)

Aggregations

Identifier (net.java.games.input.Component.Identifier)2 Button (net.java.games.input.Component.Identifier.Button)2 DefaultJoystickAxis (com.jme3.input.DefaultJoystickAxis)1 DefaultJoystickButton (com.jme3.input.DefaultJoystickButton)1 JoystickAxis (com.jme3.input.JoystickAxis)1 JoystickButton (com.jme3.input.JoystickButton)1 JoyAxisEvent (com.jme3.input.event.JoyAxisEvent)1 JoyButtonEvent (com.jme3.input.event.JoyButtonEvent)1 Component (net.java.games.input.Component)1 Axis (net.java.games.input.Component.Identifier.Axis)1 ControllerInfo (org.terasology.config.ControllerConfig.ControllerInfo)1 ButtonState (org.terasology.input.ButtonState)1 Input (org.terasology.input.Input)1 ControllerAction (org.terasology.input.device.ControllerAction)1