-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathXboxGamepadInput.java
171 lines (134 loc) · 5.91 KB
/
XboxGamepadInput.java
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import net.java.games.input.*;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class XboxGamepadInput {
public static void main(String[] args) throws ReflectiveOperationException {
List<Controller> gamepads = Arrays.stream(ControllerEnvironment.getDefaultEnvironment().getControllers()).filter(controller ->
controller.getType().equals(Controller.Type.GAMEPAD)).collect(Collectors.toList());
Controller gamepad = gamepads.get(0); // only working with one gamepad
Event event;
Component component;
float value;
String tempPosition = "";
while (true) {
gamepad.poll();
EventQueue eq = gamepad.getEventQueue();
event = new Event();
while (eq.getNextEvent(event)) {
component = event.getComponent();
value = event.getValue();
// clear temporarily stored position if analog stick is in neutral position
if ((value < 0.3) && (value > -0.3) && (tempPosition.equals(component.getIdentifier().getName()))) {
tempPosition = "";
}
if (component.isAnalog()) {
// input from analog-sticks and back triggers
if ((value > 0.8) && !(tempPosition.equals(component.getIdentifier().getName()))) {
// positive direction
switch (component.getIdentifier().getName()) {
case "x":
// left stick - RIGHT
tempPosition = "x";
break;
case "y":
// left stick - DOWN
tempPosition = "y";
break;
case "rx":
// right stick - RIGHT
tempPosition = "rx";
break;
case "ry":
// right stick - DOWN
tempPosition = "ry";
break;
case "z":
// left trigger (z-axis)
tempPosition = "z";
break;
}
}
if (value < -0.8 && !(tempPosition.equals(component.getIdentifier().getName()))) {
// negative direction
switch (component.getIdentifier().getName()) {
case "x":
// left stick - LEFT
tempPosition = "x";
break;
case "y":
// left stick - UP
tempPosition = "y";
break;
case "rx":
// right stick - LEFT
tempPosition = "rx";
break;
case "ry":
// right stick - UP
tempPosition = "ry";
break;
case "z":
// right trigger (z-axis)
tempPosition = "z";
break;
}
}
} else {
// input from buttons, dpad analog-stick-pushes
if (value == 1.0) {
switch (component.getIdentifier().getName()) {
case "0":
// A-Button
break;
case "1":
// B-Button
break;
case "2":
// X-Button
break;
case "3":
// Y-Button
break;
case "4":
// LB-Button
break;
case "5":
// RB-Button
break;
case "6":
// Back-Button
break;
case "7":
// Start-Button
break;
case "8":
// Left Stick Push
break;
case "9":
// Right Stick Push
break;
case "pov":
// DPad Left
break;
default:
break;
}
} else if (value == 0.25) {
// DPad Up
} else if (value == 0.5) {
// DPad Right
} else if (value == 0.75) {
// DPad Down
}
// if you want to use UpLeft, UpRight, DownLeft and DownRight on DPad, you have to ad cases for value = 0.125, 0.375, 0.625 and 0.875
}
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}