aboutsummaryrefslogtreecommitdiff
path: root/engine/src/core/com/jme3/input/Joystick.java
blob: 658781fedf0ea9788ce2551be1d621b23de70052 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package com.jme3.input;

import com.jme3.input.controls.JoyAxisTrigger;
import com.jme3.input.controls.JoyButtonTrigger;

/**
 * A joystick represents a single joystick that is installed in the system.
 * 
 * @author Kirill Vainer
 */
public final class Joystick {

    private InputManager inputManager;
    private JoyInput joyInput;
    private int joyId;
    private int buttonCount;
    private int axisCount;
    private int axisXIndex, axisYIndex;
    private String name;

    /**
     * Creates a new joystick instance. Only used internally.
     */
    public Joystick(InputManager inputManager, JoyInput joyInput,
                    int joyId, String name, int buttonCount, int axisCount,
                    int xAxis, int yAxis){
        this.inputManager = inputManager;
        this.joyInput = joyInput;
        this.joyId = joyId;
        this.name = name;
        this.buttonCount = buttonCount;
        this.axisCount = axisCount;

        this.axisXIndex = xAxis;
        this.axisYIndex = yAxis;
    }

    /**
     * Rumbles the joystick for the given amount/magnitude.
     * 
     * @param amount The amount to rumble. Should be between 0 and 1.
     */
    public void rumble(float amount){
        joyInput.setJoyRumble(joyId, amount);
    }

    /**
     * Assign the mapping name to receive events from the given button index
     * on the joystick.
     * 
     * @param mappingName The mapping to receive joystick button events.
     * @param buttonId The button index.
     * 
     * @see Joystick#getButtonCount() 
     */
    public void assignButton(String mappingName, int buttonId){
        if (buttonId < 0 || buttonId >= buttonCount)
            throw new IllegalArgumentException();

        inputManager.addMapping(mappingName, new JoyButtonTrigger(joyId, buttonId));
    }

    /**
     * Assign the mappings to receive events from the given joystick axis.
     * 
     * @param positiveMapping The mapping to receive events when the axis is negative
     * @param negativeMapping The mapping to receive events when the axis is positive
     * @param axisId The axis index.
     * 
     * @see Joystick#getAxisCount() 
     */
    public void assignAxis(String positiveMapping, String negativeMapping, int axisId){
        inputManager.addMapping(positiveMapping, new JoyAxisTrigger(joyId, axisId, false));
        inputManager.addMapping(negativeMapping, new JoyAxisTrigger(joyId, axisId, true));
    }

    /**
     * Gets the index number for the X axis on the joystick.
     * 
     * <p>E.g. for most gamepads, the left control stick X axis will be returned.
     * 
     * @return The axis index for the X axis for this joystick.
     * 
     * @see Joystick#assignAxis(java.lang.String, java.lang.String, int) 
     */
    public int getXAxisIndex(){
        return axisXIndex;
    }

    /**
     * Gets the index number for the Y axis on the joystick.
     * 
     * <p>E.g. for most gamepads, the left control stick Y axis will be returned.
     * 
     * @return The axis index for the Y axis for this joystick.
     * 
     * @see Joystick#assignAxis(java.lang.String, java.lang.String, int) 
     */
    public int getYAxisIndex(){
        return axisYIndex;
    }

    /**
     * Returns the number of axes on this joystick.
     * 
     * @return the number of axes on this joystick.
     */
    public int getAxisCount() {
        return axisCount;
    }

    /**
     * Returns the number of buttons on this joystick.
     * 
     * @return the number of buttons on this joystick.
     */
    public int getButtonCount() {
        return buttonCount;
    }

    /**
     * Returns the name of this joystick.
     * 
     * @return the name of this joystick.
     */
    public String getName() {
        return name;
    }

    @Override
    public String toString(){
        return "Joystick[name=" + name + ", id=" + joyId + ", buttons=" + buttonCount
                                + ", axes=" + axisCount + "]";
    }

}