Search in sources :

Example 1 with ControllerListener

use of com.badlogic.gdx.controllers.ControllerListener in project libgdx by libgdx.

the class Lwjgl3Controller method pollState.

void pollState() {
    if (!GLFW.glfwJoystickPresent(index)) {
        manager.disconnected(this);
        return;
    }
    FloatBuffer axes = GLFW.glfwGetJoystickAxes(index);
    if (axes == null) {
        manager.disconnected(this);
        return;
    }
    ByteBuffer buttons = GLFW.glfwGetJoystickButtons(index);
    if (buttons == null) {
        manager.disconnected(this);
        return;
    }
    for (int i = 0; i < axes.limit(); i++) {
        if (axisState[i] != axes.get(i)) {
            for (ControllerListener listener : listeners) {
                listener.axisMoved(this, i, axes.get(i));
            }
            manager.axisChanged(this, i, axes.get(i));
        }
        axisState[i] = axes.get(i);
    }
    for (int i = 0; i < buttons.limit(); i++) {
        if (buttonState[i] != (buttons.get(i) == GLFW.GLFW_PRESS)) {
            for (ControllerListener listener : listeners) {
                if (buttons.get(i) == GLFW.GLFW_PRESS) {
                    listener.buttonDown(this, i);
                } else {
                    listener.buttonUp(this, i);
                }
            }
            manager.buttonChanged(this, i, buttons.get(i) == GLFW.GLFW_PRESS);
        }
        buttonState[i] = buttons.get(i) == GLFW.GLFW_PRESS;
    }
}
Also used : ControllerListener(com.badlogic.gdx.controllers.ControllerListener) FloatBuffer(java.nio.FloatBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 2 with ControllerListener

use of com.badlogic.gdx.controllers.ControllerListener in project libgdx by libgdx.

the class ControllersTest method initialize.

private void initialize() {
    if (initialized)
        return;
    // print the currently connected controllers to the console
    print("Controllers: " + Controllers.getControllers().size);
    int i = 0;
    for (Controller controller : Controllers.getControllers()) {
        print("#" + i++ + ": " + controller.getName());
    }
    if (Controllers.getControllers().size == 0)
        print("No controllers attached");
    // setup the listener that prints events to the console
    Controllers.addListener(new ControllerListener() {

        public int indexOf(Controller controller) {
            return Controllers.getControllers().indexOf(controller, true);
        }

        @Override
        public void connected(Controller controller) {
            print("connected " + controller.getName());
            int i = 0;
            for (Controller c : Controllers.getControllers()) {
                print("#" + i++ + ": " + c.getName());
            }
        }

        @Override
        public void disconnected(Controller controller) {
            print("disconnected " + controller.getName());
            int i = 0;
            for (Controller c : Controllers.getControllers()) {
                print("#" + i++ + ": " + c.getName());
            }
            if (Controllers.getControllers().size == 0)
                print("No controllers attached");
        }

        @Override
        public boolean buttonDown(Controller controller, int buttonIndex) {
            print("#" + indexOf(controller) + ", button " + buttonIndex + " down");
            return false;
        }

        @Override
        public boolean buttonUp(Controller controller, int buttonIndex) {
            print("#" + indexOf(controller) + ", button " + buttonIndex + " up");
            return false;
        }

        @Override
        public boolean axisMoved(Controller controller, int axisIndex, float value) {
            print("#" + indexOf(controller) + ", axis " + axisIndex + ": " + value);
            return false;
        }

        @Override
        public boolean povMoved(Controller controller, int povIndex, PovDirection value) {
            print("#" + indexOf(controller) + ", pov " + povIndex + ": " + value);
            return false;
        }

        @Override
        public boolean xSliderMoved(Controller controller, int sliderIndex, boolean value) {
            print("#" + indexOf(controller) + ", x slider " + sliderIndex + ": " + value);
            return false;
        }

        @Override
        public boolean ySliderMoved(Controller controller, int sliderIndex, boolean value) {
            print("#" + indexOf(controller) + ", y slider " + sliderIndex + ": " + value);
            return false;
        }

        @Override
        public boolean accelerometerMoved(Controller controller, int accelerometerIndex, Vector3 value) {
            // not printing this as we get to many values
            return false;
        }
    });
    initialized = true;
}
Also used : PovDirection(com.badlogic.gdx.controllers.PovDirection) ControllerListener(com.badlogic.gdx.controllers.ControllerListener) Vector3(com.badlogic.gdx.math.Vector3) Controller(com.badlogic.gdx.controllers.Controller)

Aggregations

ControllerListener (com.badlogic.gdx.controllers.ControllerListener)2 Controller (com.badlogic.gdx.controllers.Controller)1 PovDirection (com.badlogic.gdx.controllers.PovDirection)1 Vector3 (com.badlogic.gdx.math.Vector3)1 ByteBuffer (java.nio.ByteBuffer)1 FloatBuffer (java.nio.FloatBuffer)1