/
KeybindingController.java
195 lines (165 loc) · 6.58 KB
/
KeybindingController.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package thePackage;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.KeyStroke;
public class KeybindingController {
popUpGUI window = null;
private int leftThrottle = 0;
private int rightThrottle = 0;
private static final int SPEED_INCREMENT = 5;
private static char leftAccel = 'q';
private static char leftDecel = 'a';
private static char rightAccel = 'e';
private static char rightDecel = 'd';
private static char bothAccel = 'w';
private static char bothDecel = 's';
public KeybindingController(popUpGUI window)
{
this.window = window;
}
public void bindKeys()
{
//set input maps so that the program can read key bindings
//putting something in the input map means to assign a key to an action name
//action name is associated with a method in the action map
window.btnLeftAccel.getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(leftAccel), "accelerateLeft");
window.btnLeftAccel.getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(Character.toUpperCase(leftAccel)), "accelerateLeft");
window.btnLeftAccel.getActionMap().put("accelerateLeft", accelerateLeft);
window.btnLeftDecel.getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(leftDecel), "decelerateLeft");
window.btnLeftDecel.getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(Character.toUpperCase(leftDecel)), "decelerateLeft");
window.btnLeftDecel.getActionMap().put("decelerateLeft", decelerateLeft);
window.btnRightAccel.getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(rightAccel), "accelerateRight");
window.btnRightAccel.getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(Character.toUpperCase(rightAccel)), "accelerateRight");
window.btnRightAccel.getActionMap().put("accelerateRight", accelerateRight);
window.btnRightDecel.getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(rightDecel), "decelerateRight");
window.btnRightDecel.getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(Character.toUpperCase(rightDecel)), "decelerateRight");
window.btnRightDecel.getActionMap().put("decelerateRight", decelerateRight);
//just attach to the left button, since theres no button for both
window.btnLeftAccel.getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(bothAccel), "accelerateBoth");
window.btnLeftAccel.getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(Character.toUpperCase(bothAccel)), "accelerateBoth");
window.btnLeftAccel.getActionMap().put("accelerateBoth", accelerateBoth);
//just attach to the left button, since theres no button for both
window.btnLeftDecel.getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(bothDecel), "decelerateBoth");
window.btnLeftDecel.getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(Character.toUpperCase(bothDecel)), "decelerateBoth");
window.btnLeftDecel.getActionMap().put("decelerateBoth", decelerateBoth);
}
public void toggleControls()
{
if (window.communicator.getConnected() == true) {
window.btnLeftAccel.setEnabled(true);
window.btnLeftDecel.setEnabled(true);
window.btnRightAccel.setEnabled(true);
window.btnRightDecel.setEnabled(true);
window.btnDisconnect.setEnabled(true);
window.btnConnect.setEnabled(false);
window.cboxPorts.setEnabled(false);
}
else {
window.btnLeftAccel.setEnabled(false);
window.btnLeftDecel.setEnabled(false);
window.btnRightAccel.setEnabled(false);
window.btnRightDecel.setEnabled(false);
window.btnDisconnect.setEnabled(false);
window.btnConnect.setEnabled(true);
window.cboxPorts.setEnabled(true);
}
}
//defining the action
@SuppressWarnings("serial")
Action accelerateLeft = new AbstractAction()
{
public void actionPerformed(ActionEvent evt)
{
leftThrottle = accelerate(leftThrottle);
updateLabels();
}
};
@SuppressWarnings("serial")
Action decelerateLeft = new AbstractAction()
{
public void actionPerformed(ActionEvent evt)
{
leftThrottle = decelerate(leftThrottle);
updateLabels();
}
};
@SuppressWarnings("serial")
Action accelerateRight = new AbstractAction()
{
public void actionPerformed(ActionEvent evt)
{
rightThrottle = accelerate(rightThrottle);
updateLabels();
}
};
@SuppressWarnings("serial")
Action decelerateRight = new AbstractAction()
{
public void actionPerformed(ActionEvent evt)
{
rightThrottle = decelerate(rightThrottle);
updateLabels();
}
};
@SuppressWarnings("serial")
Action accelerateBoth = new AbstractAction()
{
public void actionPerformed(ActionEvent evt)
{
leftThrottle = accelerate(leftThrottle);
rightThrottle = accelerate(rightThrottle);
updateLabels();
}
};
@SuppressWarnings("serial")
Action decelerateBoth = new AbstractAction()
{
public void actionPerformed(ActionEvent evt)
{
leftThrottle = decelerate(leftThrottle);
rightThrottle = decelerate(rightThrottle);
updateLabels();
}
};
@SuppressWarnings("static-access")
public void updateLabels()
{
window.lblLeft.setText(String.valueOf(leftThrottle));
window.lblRight.setText(String.valueOf(rightThrottle));
// window.communicator.writeData(leftThrottle, rightThrottle);
}
public int accelerate(int throttle)
{
if (throttle < 100)
{
throttle += SPEED_INCREMENT;
}
return throttle;
}
public int decelerate(int throttle)
{
if (throttle > 0)
{
throttle -= SPEED_INCREMENT;
}
return throttle;
}
final public int getLeftThrottle()
{
return leftThrottle;
}
public void setLeftThrottle(int value)
{
leftThrottle = value;
}
final public int getRightThrottle()
{
return rightThrottle;
}
public void setRightThrottle(int value)
{
rightThrottle = value;
}
}