-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathControlsPanel.java
254 lines (228 loc) · 10.1 KB
/
ControlsPanel.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
//a panel which allows the user to edit their controls. This also writes the user's
//prefferences to a text file.
public class ControlsPanel extends JPanel implements FocusListener
{
//the button which allows the user to go back to the previous panel
private BackButton backButton;
//each airplane control has a control setting object
private ControlSetting throttleUpControl;
private ControlSetting throttleDownControl;
private ControlSetting pitchUpControl;
private ControlSetting pitchDownControl;
private ControlSetting rollLeftControl;
private ControlSetting rollRightControl;
private ControlSetting yawLeftControl;
private ControlSetting yawRightControl;
private ControlSetting brakesControl;
//set up the panel
public ControlsPanel()
{
setOpaque(false);
setLayout(new BorderLayout());
addFocusListener(this);
add(topPanel(), BorderLayout.NORTH);
add(ControlsContents(), BorderLayout.CENTER);
}
//returns a jpanel which is the panel at the top of the ControlsPanel which
//contains the back button and the title.
private JPanel topPanel()
{
JPanel panel = new JPanel();
panel.setOpaque(false);
panel.setLayout(new FlowLayout(FlowLayout.LEFT, 30, 30));
backButton = new BackButton(FlightSimulator.flightSim.getCardLayout(), FlightSimulator.flightSim.getCardPanelHolder());
panel.add(backButton);
JLabel label = new JLabel("Controls");
label.setFont(new Font(FlightSimulator.FONTSTYLE, Font.BOLD, 40));
label.setForeground(Color.WHITE);
panel.add(label);
return panel;
}
//the central panel in the borderLayout which contains all the individual control settings
private JPanel ControlsContents()
{
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.LEFT, 100, 30));
panel.setOpaque(false);
throttleUpControl = new ControlSetting("Throttle Up");
panel.add(throttleUpControl);
throttleDownControl = new ControlSetting("Throttle Down");
panel.add(throttleDownControl);
pitchUpControl = new ControlSetting("Pitch Up");
panel.add(pitchUpControl);
pitchDownControl = new ControlSetting("Pitch Down");
panel.add(pitchDownControl);
rollLeftControl = new ControlSetting("Roll Left");
panel.add(rollLeftControl);
rollRightControl = new ControlSetting("Roll Right");
panel.add(rollRightControl);
yawLeftControl = new ControlSetting("Yaw Left");
panel.add(yawLeftControl);
yawRightControl = new ControlSetting("Yaw Right");
panel.add(yawRightControl);
brakesControl = new ControlSetting("Brakes");
panel.add(brakesControl);
Button resetButton = new Button("Restore Defaults", 30);
resetButton.addActionListener(new restoreDefaultsListener());
panel.add(resetButton);
Button confirmButton = new Button("Apply Changes", 30);
confirmButton.addActionListener(new confirmChangesListener());
panel.add(confirmButton);
return panel;
}
protected void paintComponent(Graphics g)
{
Utils.paintBackground(this, g);
requestFocusInWindow();
}
//when this panel gains focus (rather than using paintComponent) I load all the
//user preferences into the ControlSetting objects.
public void focusGained(FocusEvent e)
{
throttleUpControl.setKey(FlightSimulator.user.getSettings().throttleUp);
throttleDownControl.setKey(FlightSimulator.user.getSettings().throttleDown);
pitchUpControl.setKey(FlightSimulator.user.getSettings().pitchUp);
pitchDownControl.setKey(FlightSimulator.user.getSettings().pitchDown);
rollLeftControl.setKey(FlightSimulator.user.getSettings().rollLeft);
rollRightControl.setKey(FlightSimulator.user.getSettings().rollRight);
yawLeftControl.setKey(FlightSimulator.user.getSettings().yawLeft);
yawRightControl.setKey(FlightSimulator.user.getSettings().yawRight);
brakesControl.setKey(FlightSimulator.user.getSettings().brakes);
}
public void focusLost(FocusEvent e) {}
//This class is the object for each each keybind which contains a text field and a
//label for the function of the keybind.
class ControlSetting extends JPanel
{
private JTextField keyDisplay; //the textfield that displays the current setting
public ControlSetting(String settingName)
{
setOpaque(false);
setLayout(new FlowLayout(FlowLayout.LEFT, 20, 20));
JLabel settingLabel = new JLabel(settingName);
settingLabel.setFont(new Font(FlightSimulator.FONTSTYLE, Font.BOLD, 30));
settingLabel.setForeground(Color.WHITE);
keyDisplay = new JTextField(4);
keyDisplay.setFont(new Font(FlightSimulator.FONTSTYLE, Font.BOLD, 30));
add(keyDisplay);
add(settingLabel);
}
//sets the key based on the value param. accounts for special keys like
//the arrows.
public void setKey(int value)
{
switch (value) {
case KeyEvent.VK_UP:
keyDisplay.setText("up");
return;
case KeyEvent.VK_DOWN:
keyDisplay.setText("down");
return;
case KeyEvent.VK_LEFT:
keyDisplay.setText("left");
return;
case KeyEvent.VK_RIGHT:
keyDisplay.setText("right");
return;
case KeyEvent.VK_SPACE:
keyDisplay.setText("space");
return;
default:
break;
}
keyDisplay.setText("" + (char)(value+32));
}
//returns the value of the key based on the KeyEvent VK finals
public int getKeyValue()
{
switch (keyDisplay.getText().toLowerCase()) {
case "up":
return KeyEvent.VK_UP;
case "down":
return KeyEvent.VK_DOWN;
case "left":
return KeyEvent.VK_LEFT;
case "right":
return KeyEvent.VK_RIGHT;
case "space":
return KeyEvent.VK_SPACE;
default:
return keyDisplay.getText().toLowerCase().charAt(0) - 32;
}
}
}
//a listener class for the restore defaults button. This restores all the controls to their
//corresponding default values then saves it into the text file.
class restoreDefaultsListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
FlightSimulator.user.getSettings().restoreDefaults();
throttleUpControl.setKey(FlightSimulator.user.getSettings().throttleUp);
throttleDownControl.setKey(FlightSimulator.user.getSettings().throttleDown);
pitchUpControl.setKey(FlightSimulator.user.getSettings().pitchUp);
pitchDownControl.setKey(FlightSimulator.user.getSettings().pitchDown);
rollLeftControl.setKey(FlightSimulator.user.getSettings().rollLeft);
rollRightControl.setKey(FlightSimulator.user.getSettings().rollRight);
yawLeftControl.setKey(FlightSimulator.user.getSettings().yawLeft);
yawRightControl.setKey(FlightSimulator.user.getSettings().yawRight);
brakesControl.setKey(FlightSimulator.user.getSettings().brakes);
}
}
//applies any changes from the ControlSettings objects into the text file
class confirmChangesListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
FlightSimulator.user.getSettings().throttleUp = throttleUpControl.getKeyValue();
FlightSimulator.user.getSettings().throttleDown = throttleDownControl.getKeyValue();
FlightSimulator.user.getSettings().pitchUp = pitchUpControl.getKeyValue();
FlightSimulator.user.getSettings().pitchDown = pitchDownControl.getKeyValue();
FlightSimulator.user.getSettings().rollLeft = rollLeftControl.getKeyValue();
FlightSimulator.user.getSettings().rollRight = rollRightControl.getKeyValue();
FlightSimulator.user.getSettings().yawLeft = yawLeftControl.getKeyValue();
FlightSimulator.user.getSettings().yawRight = yawRightControl.getKeyValue();
FlightSimulator.user.getSettings().brakes = brakesControl.getKeyValue();
FlightSimulator.user.saveData();
}
}
//returns a listener for switching to this panel.
public static SwitchToControlsListener getControlsSwitcher(String switcherName)
{
return new SwitchToControlsListener(switcherName);
}
//the name used for card layout switching
public static String name()
{
return "ControlsPanel";
}
//a static listener class which can handle any button who's function is to
//switch to the ControlsPanel. This is a polymorphic apporach that avoids having
//to make multiple listeners in many difference classes which all have the same purpose.
static class SwitchToControlsListener implements ActionListener
{
private String prevName;
public SwitchToControlsListener(String previousName)
{
prevName = previousName;
}
public void actionPerformed(ActionEvent e)
{
FlightSimulator.flightSim.showPanel(name());
FlightSimulator.flightSim.getControlsPanel().backButton.setDestination(prevName);
}
}
}