-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.c
More file actions
212 lines (175 loc) · 6.64 KB
/
Copy pathmenu.c
File metadata and controls
212 lines (175 loc) · 6.64 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
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
// menu.c — see menu.h for the module overview.
#include "menu.h"
#include "context.h"
#include "display.h"
#include "input.h"
#include "audio.h"
// ---- Main menu -----------------------------------------------------------
typedef enum {
MENU_ITEM_SNAKE = 0,
MENU_ITEM_TETRIS,
MENU_ITEM_BRICKS,
MENU_ITEM_SETTINGS,
MENU_ITEM_COUNT
} MenuItem;
// TODO: swap for real labels once displayDrawText has a font.
static const char *const s_menuLabels[MENU_ITEM_COUNT] = {
"Snake",
"Tetris",
"Breakout",
"Settings",
};
static uint8_t s_selectedIndex;
static bool s_dirty;
static bool s_firstDraw;
void menuEnter(void) {
s_selectedIndex = 0;
s_dirty = true;
s_firstDraw = true;
}
void menuUpdate(uint32_t nowMs, uint16_t deltaMs) {
(void)nowMs; (void)deltaMs;
bool up = inputIsRepeated(BUTTON_UP);
bool down = inputIsRepeated(BUTTON_DOWN);
if (up) {
s_selectedIndex = (s_selectedIndex == 0) ? (MENU_ITEM_COUNT - 1) : (uint8_t)(s_selectedIndex - 1);
audioPlaySfxMenuMove();
s_dirty = true;
} else if (down) {
s_selectedIndex = (uint8_t)((s_selectedIndex + 1) % MENU_ITEM_COUNT);
audioPlaySfxMenuMove();
s_dirty = true;
}
if (inputWasPressed(BUTTON_SELECT) || inputWasPressed(BUTTON_START)) {
audioPlaySfxMenuSelect();
switch (s_selectedIndex) {
case MENU_ITEM_SNAKE:
g_context.selectedGameId = GAME_ID_SNAKE;
contextRequestScreen(SCREEN_GAME_LAUNCHER); break;
case MENU_ITEM_TETRIS:
g_context.selectedGameId = GAME_ID_TETRIS;
contextRequestScreen(SCREEN_GAME_LAUNCHER); break;
case MENU_ITEM_BRICKS:
g_context.selectedGameId = GAME_ID_BRICKS;
contextRequestScreen(SCREEN_GAME_LAUNCHER); break;
case MENU_ITEM_SETTINGS:
contextRequestScreen(SCREEN_SETTINGS); break;
}
}
}
void menuDraw(void) {
if (!s_dirty) return;
s_dirty = false;
if (s_firstDraw) {
displayFillScreen(COLOR_BLACK);
displayDrawTextScaled(10, 4, "MAIN MENU", COLOR_RED, COLOR_BLACK, 2);
s_firstDraw = false;
}
uint8_t rowHeight = 20;
for (uint8_t i = 0; i < MENU_ITEM_COUNT; i++) {
uint8_t y = 32 + i * rowHeight;
if (i == s_selectedIndex) {
displayDrawText(8, y, ">", COLOR_BLUE, COLOR_BLACK);
displayDrawText(20, y, s_menuLabels[i], COLOR_BLUE, COLOR_BLACK);
} else {
displayDrawText(8, y, " ", COLOR_WHITE, COLOR_BLACK); // clear cursor
displayDrawText(20, y, s_menuLabels[i], COLOR_WHITE, COLOR_BLACK);
}
}
}
// ---- Settings screen -------------------------------------------------
typedef enum {
SETTING_BRIGHTNESS = 0,
SETTING_VOLUME,
SETTING_COUNT
} SettingItem;
static uint8_t s_settingIndex;
static bool s_settingsDirty;
static bool s_settingsFirstDraw;
void settingsEnter(void) {
s_settingIndex = 0;
s_settingsDirty = true;
s_settingsFirstDraw = true;
}
void settingsUpdate(uint32_t nowMs, uint16_t deltaMs) {
(void)nowMs; (void)deltaMs;
bool up = inputIsRepeated(BUTTON_UP);
bool down = inputIsRepeated(BUTTON_DOWN);
bool left = inputIsRepeated(BUTTON_LEFT);
bool right = inputIsRepeated(BUTTON_RIGHT);
if (up) {
s_settingIndex = (s_settingIndex == 0) ? (SETTING_COUNT - 1) : (uint8_t)(s_settingIndex - 1);
s_settingsDirty = true;
} else if (down) {
s_settingIndex = (uint8_t)((s_settingIndex + 1) % SETTING_COUNT);
s_settingsDirty = true;
}
int8_t delta = 0;
if (right) delta = 5;
if (left) delta = -5;
if (delta != 0) {
uint8_t *value = (s_settingIndex == SETTING_BRIGHTNESS)
? &g_context.settings.brightness
: &g_context.settings.volume;
int16_t updated = (int16_t)(*value) + delta;
// Prevent backlight from going fully dark (min 10)
if (s_settingIndex == SETTING_BRIGHTNESS) {
if (updated < 10) updated = 10;
} else {
if (updated < 0) updated = 0;
}
if (updated > 100) updated = 100;
*value = (uint8_t)updated;
s_settingsDirty = true;
if (s_settingIndex == SETTING_BRIGHTNESS) {
audioPlaySfxMenuMove();
} else {
audioPlayTone(880, 50); // Beep to test the new volume
}
}
if (inputWasPressed(BUTTON_HOME)) {
contextRequestScreen(SCREEN_MENU);
}
}
void settingsDraw(void) {
if (!s_settingsDirty) return;
s_settingsDirty = false;
if (s_settingsFirstDraw) {
displayFillScreen(COLOR_BLACK);
displayDrawText(4, 4, "SETTINGS", COLOR_RED, COLOR_BLACK);
s_settingsFirstDraw = false;
}
// Brightness row
uint8_t yBright = 24;
if (s_settingIndex == SETTING_BRIGHTNESS) {
displayFillRect(4, yBright - 2, DISPLAY_WIDTH - 8, 28, COLOR_BLACK);
displayDrawText(8, yBright + 2, "> Brightness", COLOR_BLUE, COLOR_BLACK);
} else {
displayFillRect(4, yBright - 2, DISPLAY_WIDTH - 8, 28, COLOR_BLACK);
displayDrawText(8, yBright + 2, " Brightness", COLOR_WHITE, COLOR_BLACK);
}
// Draw Brightness bar
uint8_t barWidth = (uint8_t)((uint16_t)g_context.settings.brightness * (DISPLAY_WIDTH - 16) / 100);
displayFillRect(8, yBright + 12, barWidth, 10, COLOR_WHITE);
if (barWidth < DISPLAY_WIDTH - 16) {
displayFillRect(8 + barWidth, yBright + 12, (DISPLAY_WIDTH - 16) - barWidth, 10,
RGB565(50, 50, 50));
}
// Volume row
uint8_t yVol = 60;
if (s_settingIndex == SETTING_VOLUME) {
displayFillRect(4, yVol - 2, DISPLAY_WIDTH - 8, 28, COLOR_BLACK);
displayDrawText(8, yVol + 2, "> Volume", COLOR_BLUE, COLOR_BLACK);
} else {
displayFillRect(4, yVol - 2, DISPLAY_WIDTH - 8, 28, COLOR_BLACK);
displayDrawText(8, yVol + 2, " Volume", COLOR_WHITE, COLOR_BLACK);
}
uint8_t volVal = g_context.settings.volume;
uint8_t volDisplay = (volVal > 50) ? (volVal - 50) * 2 : 0;
barWidth = (uint8_t)((uint16_t)volDisplay * (DISPLAY_WIDTH - 16) / 100);
displayFillRect(8, yVol + 12, barWidth, 10, COLOR_WHITE);
if (barWidth < DISPLAY_WIDTH - 16) {
displayFillRect(8 + barWidth, yVol + 12, (DISPLAY_WIDTH - 16) - barWidth, 10,
RGB565(50, 50, 50));
}
}