Search in sources :

Example 1 with Event

use of net.java.games.input.Event 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 Event

use of net.java.games.input.Event in project litiengine by gurkenlabs.

the class Gamepad method update.

@Override
public void update() {
    final boolean couldPoll = this.controller.poll();
    if (!couldPoll) {
        this.dispose();
    }
    final Event event = new Event();
    while (this.controller.getEventQueue().getNextEvent(event)) {
        this.handlePollEvents(event);
    }
    for (Component comp : this.controller.getComponents()) {
        if (Math.abs(comp.getPollData()) > getDeadZone(comp.getIdentifier())) {
            this.handlePressed(comp);
        } else {
            this.handleRelease(comp);
        }
    }
}
Also used : Event(net.java.games.input.Event) Component(net.java.games.input.Component)

Example 3 with Event

use of net.java.games.input.Event in project j6dof-flight-sim by chris-ali.

the class JoystickVisitor method handleDeviceInput.

@Override
public void handleDeviceInput(Controller device) {
    if (!canHandleDevice(device))
        return;
    JoystickAssignments assignments = joystickAssignments.get(device.getName());
    Map<String, JoystickAxis> axisAssignments = assignments.getAxisAssignments();
    Map<String, KeyCommand> buttonAssignments = assignments.getButtonAssignments();
    Map<Float, KeyCommand> hatAssignments = assignments.getHatAssignments();
    if (device.poll()) {
        Event event = new Event();
        while (device.getEventQueue().getNextEvent(event)) {
            Component component = event.getComponent();
            Identifier componentIdentifier = component.getIdentifier();
            String componentName = componentIdentifier.getName();
            float pollValue = component.getPollData();
            // Buttons
            if (buttonAssignments != null && componentIdentifier.getName().matches("^[0-9]*$")) {
                // If the component name contains only numbers, it is a button
                KeyCommand command = buttonAssignments.get(componentName);
                if (command != null)
                    actuator.handleParameterChange(command, pollValue);
                continue;
            }
            // Hat Switch
            if (hatAssignments != null && componentIdentifier == Axis.POV) {
                KeyCommand command = hatAssignments.get(pollValue);
                if (command != null)
                    actuator.handleParameterChange(command, pollValue);
                continue;
            }
            // Joystick Axes
            if (axisAssignments != null) {
                JoystickAxis axis = axisAssignments.get(componentName);
                if (axis != null)
                    actuator.handleParameterChange(axis.getAxisAssignment(), pollValue);
                continue;
            }
        }
    }
}
Also used : Identifier(net.java.games.input.Component.Identifier) Event(net.java.games.input.Event) JoystickAssignments(com.chrisali.javaflightsim.simulation.setup.ControlsConfiguration.JoystickAssignments) JoystickAxis(com.chrisali.javaflightsim.simulation.setup.JoystickAxis) KeyCommand(com.chrisali.javaflightsim.simulation.setup.KeyCommand) Component(net.java.games.input.Component)

Example 4 with Event

use of net.java.games.input.Event 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)4 Component (net.java.games.input.Component)3 EventQueue (net.java.games.input.EventQueue)2 JoystickAssignments (com.chrisali.javaflightsim.simulation.setup.ControlsConfiguration.JoystickAssignments)1 JoystickAxis (com.chrisali.javaflightsim.simulation.setup.JoystickAxis)1 KeyCommand (com.chrisali.javaflightsim.simulation.setup.KeyCommand)1 ArrayDeque (java.util.ArrayDeque)1 Identifier (net.java.games.input.Component.Identifier)1 Controller (net.java.games.input.Controller)1 ControllerEvent (net.java.games.input.ControllerEvent)1 ControllerAction (org.terasology.input.device.ControllerAction)1