• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.jme3.input.lwjgl;
2 
3 import com.jme3.input.InputManager;
4 import com.jme3.input.JoyInput;
5 import com.jme3.input.Joystick;
6 import com.jme3.input.RawInputListener;
7 import com.jme3.input.event.JoyAxisEvent;
8 import com.jme3.input.event.JoyButtonEvent;
9 import com.jme3.util.IntMap;
10 import java.util.HashMap;
11 import net.java.games.input.Component.Identifier;
12 import net.java.games.input.Component.Identifier.Axis;
13 import net.java.games.input.Component.Identifier.Button;
14 import net.java.games.input.Component.POV;
15 import net.java.games.input.*;
16 
17 public class JInputJoyInput implements JoyInput {
18 
19     private boolean inited = false;
20     private Joystick[] joysticks;
21     private RawInputListener listener;
22 
23     private HashMap<Button, Integer>[] buttonIdsToIndices;
24     private HashMap<Axis, Integer>[] axisIdsToIndices;
25     private HashMap<Controller, Integer> controllerToIndices;
26     private IntMap<Controller> indicesToController;
27 
28     private int xAxis, yAxis;
29 
loadIdentifiers(int controllerIdx, Controller c)30     private void loadIdentifiers(int controllerIdx, Controller c){
31         Component[] ces = c.getComponents();
32         int numButtons = 0;
33         int numAxes = 0;
34         xAxis = -1;
35         yAxis = -1;
36         for (Component comp : ces){
37             Identifier id = comp.getIdentifier();
38             if (id instanceof Button){
39                 buttonIdsToIndices[controllerIdx].put((Button)id, numButtons);
40                 numButtons ++;
41             }else if (id instanceof Axis){
42                 Axis axis = (Axis) id;
43                 if (axis == Axis.X){
44                     xAxis = numAxes;
45                 }else if (axis == Axis.Y){
46                     yAxis = numAxes;
47                 }
48 
49                 axisIdsToIndices[controllerIdx].put((Axis)id, numAxes);
50                 numAxes ++;
51             }
52         }
53     }
54 
setJoyRumble(int joyId, float amount)55     public void setJoyRumble(int joyId, float amount){
56         Controller c = indicesToController.get(joyId);
57         if (c == null)
58             throw new IllegalArgumentException();
59 
60         for (Rumbler r : c.getRumblers()){
61             r.rumble(amount);
62         }
63     }
64 
loadJoysticks(InputManager inputManager)65     public Joystick[] loadJoysticks(InputManager inputManager){
66         ControllerEnvironment ce =
67             ControllerEnvironment.getDefaultEnvironment();
68 
69         int joyIndex = 0;
70         controllerToIndices = new HashMap<Controller, Integer>();
71         indicesToController = new IntMap<Controller>();
72         Controller[] cs = ce.getControllers();
73         for (int i = 0; i < cs.length; i++){
74             Controller c = cs[i];
75             if (c.getType() == Controller.Type.KEYBOARD
76              || c.getType() == Controller.Type.MOUSE)
77                 continue;
78 
79             controllerToIndices.put(c, joyIndex);
80             indicesToController.put(joyIndex, c);
81             joyIndex ++;
82         }
83 
84         buttonIdsToIndices = new HashMap[joyIndex];
85         axisIdsToIndices = new HashMap[joyIndex];
86         joysticks = new Joystick[joyIndex];
87 
88         joyIndex = 0;
89 
90         for (int i = 0; i < cs.length; i++){
91             Controller c = cs[i];
92             if (c.getType() == Controller.Type.KEYBOARD
93              || c.getType() == Controller.Type.MOUSE)
94                 continue;
95 
96             buttonIdsToIndices[joyIndex] = new HashMap<Button, Integer>();
97             axisIdsToIndices[joyIndex] = new HashMap<Axis, Integer>();
98             loadIdentifiers(joyIndex, c);
99             Joystick joy = new Joystick(inputManager,
100                                         this,
101                                         joyIndex, c.getName(),
102                                         buttonIdsToIndices[joyIndex].size(),
103                                         axisIdsToIndices[joyIndex].size(),
104                                         xAxis, yAxis);
105             joysticks[joyIndex] = joy;
106             joyIndex++;
107         }
108 
109         return joysticks;
110     }
111 
initialize()112     public void initialize() {
113         inited = true;
114     }
115 
update()116     public void update() {
117         ControllerEnvironment ce =
118             ControllerEnvironment.getDefaultEnvironment();
119 
120         Controller[] cs = ce.getControllers();
121         Event e = new Event();
122         for (int i = 0; i < cs.length; i++){
123             Controller c = cs[i];
124             if (c.getType() == Controller.Type.UNKNOWN
125              || c.getType() == Controller.Type.KEYBOARD
126              || c.getType() == Controller.Type.MOUSE)
127                 continue;
128 
129             if (!c.poll())
130                 continue;
131 
132             int joyId = controllerToIndices.get(c);
133             EventQueue q = c.getEventQueue();
134             while (q.getNextEvent(e)){
135                 Identifier id = e.getComponent().getIdentifier();
136                 if (id == Identifier.Axis.POV){
137                     float x = 0, y = 0;
138                     float v = e.getValue();
139 
140                     if (v == POV.CENTER){
141                         x = 0; y = 0;
142                     }else if (v == POV.DOWN){
143                         x = 0; y = -1f;
144                     }else if (v == POV.DOWN_LEFT){
145                         x = -1f; y = -1f;
146                     }else if (v == POV.DOWN_RIGHT){
147                         x = -1f; y = 1f;
148                     }else if (v == POV.LEFT){
149                         x = -1f; y = 0;
150                     }else if (v == POV.RIGHT){
151                         x = 1f; y = 0;
152                     }else if (v == POV.UP){
153                         x = 0; y = 1f;
154                     }else if (v == POV.UP_LEFT){
155                         x = -1f; y = 1f;
156                     }else if (v == POV.UP_RIGHT){
157                         x = 1f; y = 1f;
158                     }
159 
160                     JoyAxisEvent evt1 = new JoyAxisEvent(joyId, JoyInput.AXIS_POV_X, x);
161                     JoyAxisEvent evt2 = new JoyAxisEvent(joyId, JoyInput.AXIS_POV_Y, y);
162                     listener.onJoyAxisEvent(evt1);
163                     listener.onJoyAxisEvent(evt2);
164                 }else if (id instanceof Axis){
165                     float value = e.getValue();
166                     Axis axis = (Axis) id;
167                     JoyAxisEvent evt = new JoyAxisEvent(joyId, axisIdsToIndices[joyId].get(axis), value);
168                     listener.onJoyAxisEvent(evt);
169                 }else if (id instanceof Button){
170                     Button button = (Button) id;
171                     JoyButtonEvent evt = new JoyButtonEvent(joyId, buttonIdsToIndices[joyId].get(button), e.getValue() == 1f);
172                     listener.onJoyButtonEvent(evt);
173                 }
174             }
175         }
176     }
177 
destroy()178     public void destroy() {
179         inited = false;
180     }
181 
isInitialized()182     public boolean isInitialized() {
183         return inited;
184     }
185 
setInputListener(RawInputListener listener)186     public void setInputListener(RawInputListener listener) {
187         this.listener = listener;
188     }
189 
getInputTimeNanos()190     public long getInputTimeNanos() {
191         return 0;
192     }
193 
194 }
195