Home | History | Annotate | Download | only in input
      1 package com.jme3.input;
      2 
      3 import com.jme3.input.controls.JoyAxisTrigger;
      4 import com.jme3.input.controls.JoyButtonTrigger;
      5 
      6 /**
      7  * A joystick represents a single joystick that is installed in the system.
      8  *
      9  * @author Kirill Vainer
     10  */
     11 public final class Joystick {
     12 
     13     private InputManager inputManager;
     14     private JoyInput joyInput;
     15     private int joyId;
     16     private int buttonCount;
     17     private int axisCount;
     18     private int axisXIndex, axisYIndex;
     19     private String name;
     20 
     21     /**
     22      * Creates a new joystick instance. Only used internally.
     23      */
     24     public Joystick(InputManager inputManager, JoyInput joyInput,
     25                     int joyId, String name, int buttonCount, int axisCount,
     26                     int xAxis, int yAxis){
     27         this.inputManager = inputManager;
     28         this.joyInput = joyInput;
     29         this.joyId = joyId;
     30         this.name = name;
     31         this.buttonCount = buttonCount;
     32         this.axisCount = axisCount;
     33 
     34         this.axisXIndex = xAxis;
     35         this.axisYIndex = yAxis;
     36     }
     37 
     38     /**
     39      * Rumbles the joystick for the given amount/magnitude.
     40      *
     41      * @param amount The amount to rumble. Should be between 0 and 1.
     42      */
     43     public void rumble(float amount){
     44         joyInput.setJoyRumble(joyId, amount);
     45     }
     46 
     47     /**
     48      * Assign the mapping name to receive events from the given button index
     49      * on the joystick.
     50      *
     51      * @param mappingName The mapping to receive joystick button events.
     52      * @param buttonId The button index.
     53      *
     54      * @see Joystick#getButtonCount()
     55      */
     56     public void assignButton(String mappingName, int buttonId){
     57         if (buttonId < 0 || buttonId >= buttonCount)
     58             throw new IllegalArgumentException();
     59 
     60         inputManager.addMapping(mappingName, new JoyButtonTrigger(joyId, buttonId));
     61     }
     62 
     63     /**
     64      * Assign the mappings to receive events from the given joystick axis.
     65      *
     66      * @param positiveMapping The mapping to receive events when the axis is negative
     67      * @param negativeMapping The mapping to receive events when the axis is positive
     68      * @param axisId The axis index.
     69      *
     70      * @see Joystick#getAxisCount()
     71      */
     72     public void assignAxis(String positiveMapping, String negativeMapping, int axisId){
     73         inputManager.addMapping(positiveMapping, new JoyAxisTrigger(joyId, axisId, false));
     74         inputManager.addMapping(negativeMapping, new JoyAxisTrigger(joyId, axisId, true));
     75     }
     76 
     77     /**
     78      * Gets the index number for the X axis on the joystick.
     79      *
     80      * <p>E.g. for most gamepads, the left control stick X axis will be returned.
     81      *
     82      * @return The axis index for the X axis for this joystick.
     83      *
     84      * @see Joystick#assignAxis(java.lang.String, java.lang.String, int)
     85      */
     86     public int getXAxisIndex(){
     87         return axisXIndex;
     88     }
     89 
     90     /**
     91      * Gets the index number for the Y axis on the joystick.
     92      *
     93      * <p>E.g. for most gamepads, the left control stick Y axis will be returned.
     94      *
     95      * @return The axis index for the Y axis for this joystick.
     96      *
     97      * @see Joystick#assignAxis(java.lang.String, java.lang.String, int)
     98      */
     99     public int getYAxisIndex(){
    100         return axisYIndex;
    101     }
    102 
    103     /**
    104      * Returns the number of axes on this joystick.
    105      *
    106      * @return the number of axes on this joystick.
    107      */
    108     public int getAxisCount() {
    109         return axisCount;
    110     }
    111 
    112     /**
    113      * Returns the number of buttons on this joystick.
    114      *
    115      * @return the number of buttons on this joystick.
    116      */
    117     public int getButtonCount() {
    118         return buttonCount;
    119     }
    120 
    121     /**
    122      * Returns the name of this joystick.
    123      *
    124      * @return the name of this joystick.
    125      */
    126     public String getName() {
    127         return name;
    128     }
    129 
    130     @Override
    131     public String toString(){
    132         return "Joystick[name=" + name + ", id=" + joyId + ", buttons=" + buttonCount
    133                                 + ", axes=" + axisCount + "]";
    134     }
    135 
    136 }
    137