com.jme3.input.Joystick Java Examples

The following examples show how to use com.jme3.input.Joystick. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestJoystick.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void simpleInitApp() {
    Joystick[] joysticks = inputManager.getJoysticks();
    if (joysticks == null)
        throw new IllegalStateException("Cannot find any joysticks!");
        
    for (Joystick joy : joysticks){
        System.out.println(joy.toString());
    }

    inputManager.addMapping("DPAD Left", new JoyAxisTrigger(0, JoyInput.AXIS_POV_X, true));
    inputManager.addMapping("DPAD Right", new JoyAxisTrigger(0, JoyInput.AXIS_POV_X, false));
    inputManager.addMapping("DPAD Down", new JoyAxisTrigger(0, JoyInput.AXIS_POV_Y, true));
    inputManager.addMapping("DPAD Up", new JoyAxisTrigger(0, JoyInput.AXIS_POV_Y, false));
    inputManager.addListener(this, "DPAD Left", "DPAD Right", "DPAD Down", "DPAD Up");

    inputManager.addMapping("Joy Left", new JoyAxisTrigger(0, 0, true));
    inputManager.addMapping("Joy Right", new JoyAxisTrigger(0, 0, false));
    inputManager.addMapping("Joy Down", new JoyAxisTrigger(0, 1, true));
    inputManager.addMapping("Joy Up", new JoyAxisTrigger(0, 1, false));
    inputManager.addListener(this, "Joy Left", "Joy Right", "Joy Down", "Joy Up");
}
 
Example #2
Source File: JInputJoyInput.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Joystick[] loadJoysticks(InputManager inputManager){
    ControllerEnvironment ce =
        ControllerEnvironment.getDefaultEnvironment();

    Controller[] cs = ce.getControllers();
    
    List<Joystick> list = new ArrayList<Joystick>();
    for( Controller c : ce.getControllers() ) {
        if (c.getType() == Controller.Type.KEYBOARD
         || c.getType() == Controller.Type.MOUSE)
            continue;

        logger.log(Level.FINE, "Attempting to create joystick for: \"{0}\"", c);        
 
        // Try to create it like a joystick
        JInputJoystick stick = new JInputJoystick(inputManager, this, c, list.size(), c.getName()); 
        for( Component comp : c.getComponents() ) {
            stick.addComponent(comp);                   
        }
 
        // If it has no axes then we'll assume it's not
        // a joystick
        if( stick.getAxisCount() == 0 ) {
            logger.log(Level.FINE, "Not a joystick: {0}", c);
            continue;
        }
 
        joystickIndex.put(c, stick);
        list.add(stick);                      
    }

    joysticks = list.toArray( new JInputJoystick[list.size()] );
    
    return joysticks;
}
 
Example #3
Source File: TestJoystick.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected void dumpJoysticks( Joystick[] joysticks, PrintWriter out ) {
    for( Joystick j : joysticks ) {
        out.println( "Joystick[" + j.getJoyId() + "]:" + j.getName() );
        out.println( "  buttons:" + j.getButtonCount() );
        for( JoystickButton b : j.getButtons() ) {
            out.println( "   " + b );
        }
        
        out.println( "  axes:" + j.getAxisCount() );
        for( JoystickAxis axis : j.getAxes() ) {
            out.println( "   " + axis );
        }
    }
}
 
Example #4
Source File: TestJoystick.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected void setViewedJoystick( Joystick stick ) {
    if( this.viewedJoystick == stick )
        return;
 
    if( this.viewedJoystick != null ) {
        joystickInfo.detachAllChildren();
    }
               
    this.viewedJoystick = stick;
 
    if( this.viewedJoystick != null ) {       
        // Draw the hud
        yInfo = 0;
 
        addInfo(  "Joystick:\"" + stick.getName() + "\"  id:" + stick.getJoyId(), 0 );
 
        yInfo -= 5;
                   
        float ySave = yInfo;
        
        // Column one for the buttons
        addInfo( "Buttons:", 0 );
        for( JoystickButton b : stick.getButtons() ) {
            addInfo( " '" + b.getName() + "' id:'" + b.getLogicalId() + "'", 0 );
        }
        yInfo = ySave;
        
        // Column two for the axes
        addInfo( "Axes:", 1 );
        for( JoystickAxis a : stick.getAxes() ) {
            addInfo( " '" + a.getName() + "' id:'" + a.getLogicalId() + "' analog:" + a.isAnalog(), 1 );
        }
        
    } 
}
 
Example #5
Source File: AndroidJoyInput14.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Joystick[] loadJoysticks(InputManager inputManager) {
    // load the simulated joystick for device orientation
    super.loadJoysticks(inputManager);
    // load physical gamepads/joysticks
    joystickList.addAll(joystickJoyInput.loadJoysticks(joystickList.size(), inputManager));
    // return the list of joysticks back to InputManager
    return joystickList.toArray( new Joystick[joystickList.size()] );
}
 
Example #6
Source File: AndroidJoyInput.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Joystick[] loadJoysticks(InputManager inputManager) {
    logger.log(Level.INFO, "loading joysticks for {0}", this.getClass().getName());
    if (!disableSensors) {
        joystickList.add(sensorJoyInput.loadJoystick(joystickList.size(), inputManager));
    }
    return joystickList.toArray( new Joystick[joystickList.size()] );
}
 
Example #7
Source File: AndroidSensorJoyInput.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public AndroidSensorJoystickAxis(InputManager inputManager, Joystick parent,
                   int axisIndex, String name, String logicalId,
                   boolean isAnalog, boolean isRelative, float deadZone,
                   float maxRawValue) {
    super(inputManager, parent, axisIndex, name, logicalId, isAnalog, isRelative, deadZone);

    this.maxRawValue = maxRawValue;
}
 
Example #8
Source File: TestAndroidSensors.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void simpleUpdate(float tpf) {
    if (!initialCalibrationComplete) {
        // Calibrate the axis (set new zero position) if the axis
        // is a sensor joystick axis
        for (IntMap.Entry<Joystick> entry : joystickMap) {
            for (JoystickAxis axis : entry.getValue().getAxes()) {
                if (axis instanceof SensorJoystickAxis) {
                    logger.log(Level.INFO, "Calibrating Axis: {0}", axis.toString());
                    ((SensorJoystickAxis) axis).calibrateCenter();
                }
            }
        }
        initialCalibrationComplete = true;
    }

    if (enableGeometryRotation) {
        rotationQuat.fromAngles(anglesCurrent);
        rotationQuat.normalizeLocal();

        if (useAbsolute) {
            geomZero.setLocalRotation(rotationQuat);
        } else {
            geomZero.rotate(rotationQuat);
        }

        anglesCurrent[0] = anglesCurrent[1] = anglesCurrent[2] = 0f;
    }
}
 
Example #9
Source File: TestJoystick.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void simpleInitApp() {
    getFlyByCamera().setEnabled(false);

    Joystick[] joysticks = inputManager.getJoysticks();
    if (joysticks == null)
        throw new IllegalStateException("Cannot find any joysticks!");

    try {
        PrintWriter out = new PrintWriter( new FileWriter( "joysticks-" + System.currentTimeMillis() + ".txt" ) );
        dumpJoysticks( joysticks, out );
        out.close();
    } catch( IOException e ) {
        throw new RuntimeException( "Error writing joystick dump", e );
    }   


    int gamepadSize = cam.getHeight() / 2;
    float scale = gamepadSize / 512.0f;        
    gamepad = new GamepadView();       
    gamepad.setLocalTranslation( cam.getWidth() - gamepadSize - (scale * 20), 0, 0 );
    gamepad.setLocalScale( scale, scale, scale ); 
    guiNode.attachChild(gamepad); 

    joystickInfo = new Node( "joystickInfo" );
    joystickInfo.setLocalTranslation( 0, cam.getHeight(), 0 );
    guiNode.attachChild( joystickInfo );

    // Add a raw listener because it's easier to get all joystick events
    // this way.
    inputManager.addRawInputListener( new JoystickEventListener() );
    
    // add action listener for mouse click 
    // to all easier custom mapping
    inputManager.addMapping("mouseClick", new MouseButtonTrigger(mouseInput.BUTTON_LEFT));
    inputManager.addListener(new ActionListener() {
        @Override
        public void onAction(String name, boolean isPressed, float tpf) {
            if(isPressed){
                pickGamePad(getInputManager().getCursorPosition());
            }
        }
    }, "mouseClick");
}
 
Example #10
Source File: AndroidJoystickJoyInput14.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public List<Joystick> loadJoysticks(int joyId, InputManager inputManager) {
    logger.log(Level.INFO, "loading Joystick devices");
    ArrayList<Joystick> joysticks = new ArrayList<Joystick>();
    joysticks.clear();
    joystickIndex.clear();

    ArrayList<Integer> gameControllerDeviceIds = new ArrayList<>();
    int[] deviceIds = InputDevice.getDeviceIds();
    for (int deviceId : deviceIds) {
        InputDevice dev = InputDevice.getDevice(deviceId);
        int sources = dev.getSources();
        logger.log(Level.FINE, "deviceId[{0}] sources: {1}", new Object[]{deviceId, sources});

        // Verify that the device has gamepad buttons, control sticks, or both.
        if (((sources & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD) ||
                ((sources & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK)) {
            // This device is a game controller. Store its device ID.
            if (!gameControllerDeviceIds.contains(deviceId)) {
                gameControllerDeviceIds.add(deviceId);
                logger.log(Level.FINE, "Attempting to create joystick for device: {0}", dev);
                // Create an AndroidJoystick and store the InputDevice so we
                // can later correspond the input from the InputDevice to the
                // appropriate jME Joystick event
                AndroidJoystick joystick = new AndroidJoystick(inputManager,
                                                            joyInput,
                                                            dev,
                                                            joyId+joysticks.size(),
                                                            dev.getName());
                joystickIndex.put(deviceId, joystick);
                joysticks.add(joystick);

                // Each analog input is reported as a MotionRange
                // The axis number corresponds to the type of axis
                // The AndroidJoystick.addAxis(MotionRange) converts the axis
                // type reported by Android into the jME Joystick axis
                List<MotionRange> motionRanges = dev.getMotionRanges();
                for (MotionRange motionRange: motionRanges) {
                    logger.log(Level.INFO, "motion range: {0}", motionRange.toString());
                    logger.log(Level.INFO, "axis: {0}", motionRange.getAxis());
                    JoystickAxis axis = joystick.addAxis(motionRange);
                    logger.log(Level.INFO, "added axis: {0}", axis);
                }

                // InputDevice has a method for determining if a keyCode is
                // supported (InputDevice  public boolean[] hasKeys (int... keys)).
                // But this method wasn't added until rev 19 (Android 4.4)
                // Therefore, we only can query the entire device and see if
                // any InputDevice supports the keyCode.  This may result in
                // buttons being configured that don't exist on the specific
                // device, but I haven't found a better way yet.
                for (int keyCode: AndroidGamepadButtons) {
                    logger.log(Level.INFO, "button[{0}]: {1}",
                            new Object[]{keyCode, KeyCharacterMap.deviceHasKey(keyCode)});
                    if (KeyCharacterMap.deviceHasKey(keyCode)) {
                        // add button even though we aren't sure if the button
                        // actually exists on this InputDevice
                        logger.log(Level.INFO, "button[{0}] exists somewhere", keyCode);
                        JoystickButton button = joystick.addButton(keyCode);
                        logger.log(Level.INFO, "added button: {0}", button);
                    }
                }

            }
        }
    }


    loaded = true;
    return joysticks;
}
 
Example #11
Source File: AndroidSensorJoyInput.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public Joystick loadJoystick(int joyId, InputManager inputManager) {
        SensorData sensorData;
        AndroidSensorJoystickAxis axis;

        AndroidSensorJoystick joystick = new AndroidSensorJoystick(inputManager,
                                    joyInput,
                                    joyId,
                                    "AndroidSensorsJoystick");

        List<Sensor> availSensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
        for (Sensor sensor: availSensors) {
            logger.log(Level.FINE, "{0} Sensor is available, Type: {1}, Vendor: {2}, Version: {3}",
                    new Object[]{sensor.getName(), sensor.getType(), sensor.getVendor(), sensor.getVersion()});
        }

        // manually create orientation sensor data since orientation is not a physical sensor
        sensorData = new SensorData(Sensor.TYPE_ORIENTATION, null);
        sensorData.lastValues = new float[3];
        sensors.put(Sensor.TYPE_ORIENTATION, sensorData);
        axis = joystick.addAxis(SensorJoystickAxis.ORIENTATION_X, SensorJoystickAxis.ORIENTATION_X, joystick.getAxisCount(), FastMath.HALF_PI);
        joystick.setYAxis(axis); // joystick y axis = rotation around device x axis
        sensorData.axes.add(axis);
        axis = joystick.addAxis(SensorJoystickAxis.ORIENTATION_Y, SensorJoystickAxis.ORIENTATION_Y, joystick.getAxisCount(), FastMath.HALF_PI);
        joystick.setXAxis(axis); // joystick x axis = rotation around device y axis
        sensorData.axes.add(axis);
        axis = joystick.addAxis(SensorJoystickAxis.ORIENTATION_Z, SensorJoystickAxis.ORIENTATION_Z, joystick.getAxisCount(), FastMath.HALF_PI);
        sensorData.axes.add(axis);

        // add axes for physical sensors
        sensorData = initSensor(Sensor.TYPE_MAGNETIC_FIELD);
        if (sensorData != null) {
            sensorData.lastValues = new float[3];
            sensors.put(Sensor.TYPE_MAGNETIC_FIELD, sensorData);
//            axis = joystick.addAxis(SensorJoystickAxis.MAGNETIC_X, "MagneticField_X", joystick.getAxisCount(), 1f);
//            sensorData.axes.add(axis);
//            axis = joystick.addAxis(SensorJoystickAxis.MAGNETIC_Y, "MagneticField_Y", joystick.getAxisCount(), 1f);
//            sensorData.axes.add(axis);
//            axis = joystick.addAxis(SensorJoystickAxis.MAGNETIC_Z, "MagneticField_Z", joystick.getAxisCount(), 1f);
//            sensorData.axes.add(axis);
        }

        sensorData = initSensor(Sensor.TYPE_ACCELEROMETER);
        if (sensorData != null) {
            sensorData.lastValues = new float[3];
            sensors.put(Sensor.TYPE_ACCELEROMETER, sensorData);
//            axis = joystick.addAxis(SensorJoystickAxis.ACCELEROMETER_X, "Accelerometer_X", joystick.getAxisCount(), 1f);
//            sensorData.axes.add(axis);
//            axis = joystick.addAxis(SensorJoystickAxis.ACCELEROMETER_Y, "Accelerometer_Y", joystick.getAxisCount(), 1f);
//            sensorData.axes.add(axis);
//            axis = joystick.addAxis(SensorJoystickAxis.ACCELEROMETER_Z, "Accelerometer_Z", joystick.getAxisCount(), 1f);
//            sensorData.axes.add(axis);
        }

//        sensorData = initSensor(Sensor.TYPE_GYROSCOPE);
//        if (sensorData != null) {
//            sensorData.lastValues = new float[3];
//        }
//
//        sensorData = initSensor(Sensor.TYPE_GRAVITY);
//        if (sensorData != null) {
//            sensorData.lastValues = new float[3];
//        }
//
//        sensorData = initSensor(Sensor.TYPE_LINEAR_ACCELERATION);
//        if (sensorData != null) {
//            sensorData.lastValues = new float[3];
//        }
//
//        sensorData = initSensor(Sensor.TYPE_ROTATION_VECTOR);
//        if (sensorData != null) {
//            sensorData.lastValues = new float[4];
//        }
//
//        sensorData = initSensor(Sensor.TYPE_PROXIMITY);
//        if (sensorData != null) {
//            sensorData.lastValues = new float[1];
//        }
//
//        sensorData = initSensor(Sensor.TYPE_LIGHT);
//        if (sensorData != null) {
//            sensorData.lastValues = new float[1];
//        }
//
//        sensorData = initSensor(Sensor.TYPE_PRESSURE);
//        if (sensorData != null) {
//            sensorData.lastValues = new float[1];
//        }
//
//        sensorData = initSensor(Sensor.TYPE_TEMPERATURE);
//        if (sensorData != null) {
//            sensorData.lastValues = new float[1];
//        }


        loaded = true;
        return joystick;
    }
 
Example #12
Source File: StaticCamera.java    From OpenRTS with MIT License 4 votes vote down vote up
/**
     * Registers the FlyByCamera to receive input events from the provided
     * Dispatcher.
     * @param dispacher
     */
    public void registerWithInput(InputManager inputManager){
        this.inputManager = inputManager;
        
        String[] mappings = new String[]{
            "FLYCAM_Left",
            "FLYCAM_Right",
            "FLYCAM_Up",
            "FLYCAM_Down",

//            "FLYCAM_ZoomIn",
//            "FLYCAM_ZoomOut",
            "FLYCAM_RotateDrag",

        };

        // both mouse and button - rotation of cam
        inputManager.addMapping("FLYCAM_Left", new MouseAxisTrigger(MouseInput.AXIS_X, true),
                                               new KeyTrigger(KeyInput.KEY_LEFT));

        inputManager.addMapping("FLYCAM_Right", new MouseAxisTrigger(MouseInput.AXIS_X, false),
                                                new KeyTrigger(KeyInput.KEY_RIGHT));

        inputManager.addMapping("FLYCAM_Up", new MouseAxisTrigger(MouseInput.AXIS_Y, false),
                                             new KeyTrigger(KeyInput.KEY_UP));

        inputManager.addMapping("FLYCAM_Down", new MouseAxisTrigger(MouseInput.AXIS_Y, true),
                                               new KeyTrigger(KeyInput.KEY_DOWN));

        // mouse only - zoom in/out with wheel, and rotate drag
//        inputManager.addMapping("FLYCAM_ZoomIn", new MouseAxisTrigger(MouseInput.AXIS_WHEEL, false));
//        inputManager.addMapping("FLYCAM_ZoomOut", new MouseAxisTrigger(MouseInput.AXIS_WHEEL, true));
        inputManager.addMapping("FLYCAM_RotateDrag", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));

        inputManager.addListener(this, mappings);
        inputManager.setCursorVisible(dragToRotate);

        Joystick[] joysticks = inputManager.getJoysticks();
        if (joysticks != null && joysticks.length > 0){
            Joystick joystick = joysticks[0];
            joystick.assignAxis("FLYCAM_Right", "FLYCAM_Left", joystick.getXAxisIndex());
            joystick.assignAxis("FLYCAM_Down", "FLYCAM_Up", joystick.getYAxisIndex());
        }
    }
 
Example #13
Source File: AzertyFlyByCamera.java    From OpenRTS with MIT License 4 votes vote down vote up
@Override
public void registerWithInput(InputManager inputManager){
       this.inputManager = inputManager;
       
       String[] mappings = new String[]{
           "FLYCAM_Left",
           "FLYCAM_Right",
           "FLYCAM_Up",
           "FLYCAM_Down",

           "FLYCAM_StrafeLeft",
           "FLYCAM_StrafeRight",
           "FLYCAM_Forward",
           "FLYCAM_Backward",

           "FLYCAM_ZoomIn",
           "FLYCAM_ZoomOut",
           "FLYCAM_RotateDrag",

           "FLYCAM_Rise",
           "FLYCAM_Lower"
       };

       // both mouse and button - rotation of cam
       inputManager.addMapping("FLYCAM_Left", new MouseAxisTrigger(MouseInput.AXIS_X, true),
                                              new KeyTrigger(KeyInput.KEY_LEFT));

       inputManager.addMapping("FLYCAM_Right", new MouseAxisTrigger(MouseInput.AXIS_X, false),
                                               new KeyTrigger(KeyInput.KEY_RIGHT));

       inputManager.addMapping("FLYCAM_Up", new MouseAxisTrigger(MouseInput.AXIS_Y, false),
                                            new KeyTrigger(KeyInput.KEY_UP));

       inputManager.addMapping("FLYCAM_Down", new MouseAxisTrigger(MouseInput.AXIS_Y, true),
                                              new KeyTrigger(KeyInput.KEY_DOWN));

       // mouse only - zoom in/out with wheel, and rotate drag
       inputManager.addMapping("FLYCAM_ZoomIn", new MouseAxisTrigger(MouseInput.AXIS_WHEEL, false));
       inputManager.addMapping("FLYCAM_ZoomOut", new MouseAxisTrigger(MouseInput.AXIS_WHEEL, true));
       inputManager.addMapping("FLYCAM_RotateDrag", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));

       // keyboard only WASD for movement and WZ for rise/lower height
       inputManager.addMapping("FLYCAM_StrafeLeft", new KeyTrigger(KeyInput.KEY_Q));
       inputManager.addMapping("FLYCAM_StrafeRight", new KeyTrigger(KeyInput.KEY_D));
       inputManager.addMapping("FLYCAM_Forward", new KeyTrigger(KeyInput.KEY_Z));
       inputManager.addMapping("FLYCAM_Backward", new KeyTrigger(KeyInput.KEY_S));
       inputManager.addMapping("FLYCAM_Rise", new KeyTrigger(KeyInput.KEY_R));
       inputManager.addMapping("FLYCAM_Lower", new KeyTrigger(KeyInput.KEY_F));

       inputManager.addListener(this, mappings);
       inputManager.setCursorVisible(dragToRotate);

       Joystick[] joysticks = inputManager.getJoysticks();
       if (joysticks != null && joysticks.length > 0){
           Joystick joystick = joysticks[0];
           joystick.assignAxis("FLYCAM_StrafeRight", "FLYCAM_StrafeLeft", JoyInput.AXIS_POV_X);
           joystick.assignAxis("FLYCAM_Forward", "FLYCAM_Backward", JoyInput.AXIS_POV_Y);
           joystick.assignAxis("FLYCAM_Right", "FLYCAM_Left", joystick.getXAxisIndex());
           joystick.assignAxis("FLYCAM_Down", "FLYCAM_Up", joystick.getYAxisIndex());
       }
   }