-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDebugMenuScreen.java
More file actions
140 lines (124 loc) · 5.06 KB
/
Copy pathDebugMenuScreen.java
File metadata and controls
140 lines (124 loc) · 5.06 KB
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
/*
* Authors: Jerry Li & Victor Jiang
* Date: June 13, 2025
* Description: This class manages the debug menu for the different debug optionas (inifinite health, max upgrades)
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/** Screen that exposes developer debug options. */
public class DebugMenuScreen extends JPanel {
public GamePanel parent;
public Font uiFont;
public ToggleButton infiniteHpBtn;
public ToggleButton maxUpgradesBtn;
public ToggleButton retryWaveBtn;
public JComboBox<Integer> waveSelect;
public GameButton backBtn;
public DebugMenuScreen(GamePanel parent) {
this.parent = parent;
this.uiFont = parent.getGameFont();
setLayout(null);
setOpaque(false);
infiniteHpBtn = new ToggleButton("Infinite HP", DebugConfig.infiniteHp, v -> {
DebugConfig.infiniteHp = v;
});
infiniteHpBtn.setFont(uiFont.deriveFont(24f));
maxUpgradesBtn = new ToggleButton("Max Upgrades", DebugConfig.maxUpgrades, v -> {
DebugConfig.maxUpgrades = v;
});
maxUpgradesBtn.setFont(uiFont.deriveFont(24f));
retryWaveBtn = new ToggleButton("Retry-Wave Mode", DebugConfig.retryWave, v -> {
DebugConfig.retryWave = v;
});
retryWaveBtn.setFont(uiFont.deriveFont(24f));
// Build the wave list dynamically so it always matches the current
// number of waves loaded in the GamePanel
Integer[] waveOptions = buildWaveList();
waveSelect = new JComboBox<>(waveOptions);
waveSelect.setSelectedItem(DebugConfig.startWave);
waveSelect.setFont(uiFont.deriveFont(22f));
waveSelect.addActionListener(e -> {
DebugConfig.startWave = (Integer) waveSelect.getSelectedItem();
});
backBtn = new GameButton("Back");
backBtn.setFont(uiFont.deriveFont(Font.BOLD, 32f));
backBtn.addActionListener(e -> parent.showScreen("MAIN_MENU"));
add(infiniteHpBtn);
add(maxUpgradesBtn);
add(waveSelect);
add(retryWaveBtn);
add(backBtn);
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) { positionComponents(); }
@Override
public void componentShown(ComponentEvent e) {
refreshWaveSelect();
}
});
setFocusTraversalKeysEnabled(false);
setupNavigation();
}
/**
* Build a list of available waves based on the parent's loaded wave data.
*/
private Integer[] buildWaveList() {
int count = (parent.waves != null && !parent.waves.isEmpty())
? parent.waves.size() : 1;
int i = 0;
if (DebugConfig.startWave < 1 || DebugConfig.startWave > count) {
DebugConfig.startWave = 1;
}
Integer[] arr = new Integer[count];
for (i = 0; i < count; i++) arr[i] = i + 1;
return arr;
}
/**
* Refresh the wave dropdown whenever the menu is shown, ensuring that the
* available waves and selected item stay in sync with the current game
* state.
*/
private void refreshWaveSelect() {
Integer[] options = buildWaveList();
DefaultComboBoxModel<Integer> model = new DefaultComboBoxModel<>(options);
waveSelect.setModel(model);
waveSelect.setSelectedItem(DebugConfig.startWave);
waveSelect.repaint();
}
private void setupNavigation() {
Component[] order = {infiniteHpBtn, maxUpgradesBtn, waveSelect, retryWaveBtn, backBtn};
InputMap im = getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
ActionMap am = getActionMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "next");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "prev");
am.put("next", new AbstractAction() {
@Override public void actionPerformed(ActionEvent e) { moveFocus(order, 1); }
});
am.put("prev", new AbstractAction() {
@Override public void actionPerformed(ActionEvent e) { moveFocus(order, -1); }
});
}
private void moveFocus(Component[] order, int delta) {
Component c = null;
int idx = -1;
int i = 0;
c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
for (i = 0; i < order.length; i++) if (order[i] == c) { idx = i; break; }
if (idx == -1) idx = 0; else idx = (idx + delta + order.length) % order.length;
order[idx].requestFocusInWindow();
}
public void positionComponents() {
int w = getWidth() > 0 ? getWidth() : GamePanel.GAME_WIDTH;
int btnW = 320;
int btnH = 60;
int x = (w - btnW)/2;
int y = 120;
int gap = 20;
infiniteHpBtn.setBounds(x, y, btnW, btnH); y += btnH + gap;
maxUpgradesBtn.setBounds(x, y, btnW, btnH); y += btnH + gap;
waveSelect.setBounds(x, y, btnW, btnH); y += btnH + gap;
retryWaveBtn.setBounds(x, y, btnW, btnH); y += btnH + gap;
backBtn.setBounds(x, y + 20, btnW, btnH);
}
}