Search in sources :

Example 1 with EventQueue

use of net.java.games.input.EventQueue 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 2 with EventQueue

use of net.java.games.input.EventQueue in project lwjgl by LWJGL.

the class JInputController method poll.

/*
	 * @see org.lwjgl.input.Controller#poll()
	 */
public void poll() {
    target.poll();
    Event event = new Event();
    EventQueue queue = target.getEventQueue();
    while (queue.getNextEvent(event)) {
        // handle button event
        if (buttons.contains(event.getComponent())) {
            Component button = event.getComponent();
            int buttonIndex = buttons.indexOf(button);
            buttonState[buttonIndex] = event.getValue() != 0;
            // fire button pressed event
            Controllers.addEvent(new ControllerEvent(this, event.getNanos(), ControllerEvent.BUTTON, buttonIndex, buttonState[buttonIndex], false, false, 0, 0));
        }
        // handle pov events
        if (pov.contains(event.getComponent())) {
            Component povComponent = event.getComponent();
            int povIndex = pov.indexOf(povComponent);
            float prevX = getPovX();
            float prevY = getPovY();
            povValues[povIndex] = event.getValue();
            if (prevX != getPovX()) {
                Controllers.addEvent(new ControllerEvent(this, event.getNanos(), ControllerEvent.POVX, 0, false, false));
            }
            if (prevY != getPovY()) {
                Controllers.addEvent(new ControllerEvent(this, event.getNanos(), ControllerEvent.POVY, 0, false, false));
            }
        }
        // handle axis updates
        if (axes.contains(event.getComponent())) {
            Component axis = event.getComponent();
            int axisIndex = axes.indexOf(axis);
            float value = axis.getPollData();
            float xaxisValue = 0;
            float yaxisValue = 0;
            // fixed dead zone since most axis don't report it :(
            if (Math.abs(value) < deadZones[axisIndex]) {
                value = 0;
            }
            if (Math.abs(value) < axis.getDeadZone()) {
                value = 0;
            }
            if (Math.abs(value) > axesMax[axisIndex]) {
                axesMax[axisIndex] = Math.abs(value);
            }
            // normalize the value based on maximum value read in the past
            value /= axesMax[axisIndex];
            if (axisIndex == xaxis) {
                xaxisValue = value;
            }
            if (axisIndex == yaxis) {
                yaxisValue = value;
            }
            // fire event
            Controllers.addEvent(new ControllerEvent(this, event.getNanos(), ControllerEvent.AXIS, axisIndex, false, axisIndex == xaxis, axisIndex == yaxis, xaxisValue, yaxisValue));
            axesValue[axisIndex] = value;
        }
    }
}
Also used : Event(net.java.games.input.Event) Component(net.java.games.input.Component) EventQueue(net.java.games.input.EventQueue)

Aggregations

Event (net.java.games.input.Event)2 EventQueue (net.java.games.input.EventQueue)2 ArrayDeque (java.util.ArrayDeque)1 Component (net.java.games.input.Component)1 Controller (net.java.games.input.Controller)1 ControllerEvent (net.java.games.input.ControllerEvent)1 ControllerAction (org.terasology.input.device.ControllerAction)1