-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenuScene.cpp
More file actions
383 lines (319 loc) · 16.3 KB
/
MenuScene.cpp
File metadata and controls
383 lines (319 loc) · 16.3 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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#include "MenuScene.h"
#include "MenuConstants.h"
#include "core/Engine.h"
#include "platforms/EngineConfig.h"
#include "graphics/Renderer.h"
#include "examples/Games/Pong/PongScene.h"
#include "examples/Games/BrickBreaker/BrickBreakerScene.h"
#include "examples/Games/TicTacToe/TicTacToeScene.h"
#include "examples/Games/Snake/SnakeScene.h"
#include "examples/Games/SpaceInvaders/SpaceInvadersScene.h"
#ifdef PIXELROOT32_ENABLE_4BPP_SPRITES
#include "examples/Games/Metroidvania/MetroidvaniaScene.h"
#endif
#include "examples/PhysicsDemo/PhysicsDemoScene.h"
#include "examples/CameraDemo/CameraDemoScene.h"
#include "examples/DualPaletteTest/DualPaletteTestScene.h"
#include "examples/FontTest/FontTestScene.h"
#if defined(PIXELROOT32_ENABLE_2BPP_SPRITES) || defined(PIXELROOT32_ENABLE_4BPP_SPRITES)
#include "examples/SpritesDemo/SpritesDemoScene.h"
#endif
#ifdef PIXELROOT32_ENABLE_4BPP_SPRITES
#include "examples/TileMapDemo/TileMapDemoScene.h"
#endif
#include "examples/UIElementDemo/CheckBoxDemo/CheckBoxScene.h"
#include "examples/UIElementDemo/ButtonDemo/ButtonScene.h"
#include "examples/UIElementDemo/LabelDemo/LabelScene.h"
#include "examples/UIElementDemo/Layouts/VerticalLayoutDemo/VerticalLayoutDemoScene.h"
#include "examples/UIElementDemo/Layouts/HorizontalLayoutDemo/HorizontalLayoutDemoScene.h"
#include "examples/UIElementDemo/Layouts/GridLayoutDemo/GridLayoutDemoScene.h"
#include "examples/UIElementDemo/Layouts/AnchorLayoutDemo/AnchorLayoutDemoScene.h"
namespace pr32 = pixelroot32;
extern pr32::core::Engine engine;
// Define instances here
pong::PongScene pongScene;
brickbreaker::BrickBreakerScene brickScene;
tictactoe::TicTacToeScene tttScene;
snake::SnakeScene snakeScene;
spaceinvaders::SpaceInvadersScene spaceInvadersScene;
#ifdef PIXELROOT32_ENABLE_4BPP_SPRITES
metroidvania::MetroidvaniaScene metroidvaniaScene;
#endif
physicsdemo::PhysicsDemoScene physicsDemoScene;
camerademo::CameraDemoScene cameraDemoScene;
dualpalettetest::DualPaletteTestScene dualPaletteTestScene;
fonttest::FontTestScene fontTestScene;
checkboxdemo::CheckBoxScene checkBoxScene;
buttondemo::ButtonScene buttonScene;
labeldemo::LabelScene labelScene;
verticallayoutdemo::VerticalLayoutDemoScene verticalLayoutDemoScene;
horizontallayoutdemo::HorizontalLayoutDemoScene horizontalLayoutDemoScene;
gridlayoutdemo::GridLayoutDemoScene gridLayoutDemoScene;
anchorlayoutdemo::AnchorLayoutDemoScene anchorLayoutDemoScene;
#if defined(PIXELROOT32_ENABLE_2BPP_SPRITES) || defined(PIXELROOT32_ENABLE_4BPP_SPRITES)
spritesdemo::SpritesDemoScene spritesDemoScene;
#endif
#ifdef PIXELROOT32_ENABLE_4BPP_SPRITES
tilemapdemo::TileMapDemoScene tileMapDemoScene;
#endif
using Color = pr32::graphics::Color;
using Vector2 = pr32::math::Vector2;
void MenuScene::init() {
// Clear any existing entities to prevent duplicates if init() is called multiple times
clearEntities();
pr32::graphics::setPalette(pr32::graphics::PaletteType::PR32);
int screenWidth = engine.getRenderer().getLogicalWidth();
int screenHeight = engine.getRenderer().getLogicalHeight();
// Create title label
titleLabel = std::make_unique<pr32::graphics::ui::UILabel>("Examples", Vector2(0, menu::TITLE_Y), Color::White, menu::TITLE_FONT_SIZE);
titleLabel->centerX(screenWidth);
titleLabel->setRenderLayer(2);
addEntity(titleLabel.get());
// Create vertical layout for buttons
float btnW = menu::BTN_WIDTH;
float cx = screenWidth / 2.0f;
float btnX = cx - (btnW / 2.0f);
float startY = menu::BTN_START_Y;
float layoutHeight = screenHeight - startY - menu::NAV_INSTR_Y_OFFSET - 10;
buttonLayout = std::make_unique<pr32::graphics::ui::UIVerticalLayout>(btnX, startY, btnW, layoutHeight);
buttonLayout->setPadding(0);
buttonLayout->setSpacing(menu::BTN_GAP);
buttonLayout->setScrollEnabled(true);
buttonLayout->setNavigationButtons(menu::BTN_NAV_UP, menu::BTN_NAV_DOWN);
buttonLayout->setButtonStyle(Color::White, Color::Cyan, Color::White, Color::Black);
buttonLayout->setRenderLayer(2);
addEntity(buttonLayout.get());
// Create all buttons (will be shown/hidden based on menu state)
setupMainMenu();
setupGamesMenu();
setupUIElementsMenu();
setupLayoutsMenu();
// Navigation labels
lblNavigate = std::make_unique<pr32::graphics::ui::UILabel>("UP/DOWN: Navigate", Vector2(0, screenHeight - menu::NAV_INSTR_Y_OFFSET), Color::Cyan, menu::INSTRUCTION_FONT_SIZE);
lblNavigate->centerX(screenWidth);
lblNavigate->setRenderLayer(2);
addEntity(lblNavigate.get());
lblSelect = std::make_unique<pr32::graphics::ui::UILabel>("A: Select", Vector2(0, screenHeight - menu::SEL_INSTR_Y_OFFSET), Color::Cyan, menu::INSTRUCTION_FONT_SIZE);
lblSelect->centerX(screenWidth);
lblSelect->setRenderLayer(2);
addEntity(lblSelect.get());
lblBack = std::make_unique<pr32::graphics::ui::UILabel>("B: Back", Vector2(0, screenHeight - 15), Color::Cyan, menu::INSTRUCTION_FONT_SIZE);
lblBack->centerX(screenWidth);
lblBack->setRenderLayer(2);
addEntity(lblBack.get());
// Show main menu initially
currentState = MenuState::MAIN;
showMenu(MenuState::MAIN);
}
void MenuScene::update(unsigned long deltaTime) {
Scene::update(deltaTime);
// Input Handling
auto& input = engine.getInputManager();
// Handle back button (B button, typically button 5)
static bool wasBackPressed = false;
bool isBackPressed = input.isButtonPressed(5); // B button
if (isBackPressed && !wasBackPressed) {
goBack();
// Play back sound
pr32::audio::AudioEvent ev;
ev.type = pr32::audio::WaveType::TRIANGLE;
ev.frequency = menu::SOUND_NAV_FREQ;
ev.duration = menu::SOUND_NAV_DUR;
ev.volume = menu::SOUND_VOL_NAV;
engine.getAudioEngine().playEvent(ev);
}
wasBackPressed = isBackPressed;
// Handle layout navigation (UP/DOWN handled by layout)
// Track previous selection for sound feedback
static int lastSelectedIndex = -1;
buttonLayout->handleInput(input);
// Check if selection changed (for sound feedback)
int newSelectedIndex = buttonLayout->getSelectedIndex();
if (newSelectedIndex != lastSelectedIndex && newSelectedIndex >= 0) {
// Play navigation sound
pr32::audio::AudioEvent ev;
ev.type = pr32::audio::WaveType::TRIANGLE;
ev.frequency = menu::SOUND_NAV_FREQ;
ev.duration = menu::SOUND_NAV_DUR;
ev.volume = menu::SOUND_VOL_NAV;
engine.getAudioEngine().playEvent(ev);
lastSelectedIndex = newSelectedIndex;
}
// Sound test for SELECT button
if (input.isButtonPressed(menu::BTN_SELECT)) {
pr32::audio::AudioEvent ev;
ev.type = pr32::audio::WaveType::PULSE;
ev.frequency = menu::SOUND_BLIP_FREQ;
ev.duration = menu::SOUND_BLIP_DUR;
ev.volume = menu::SOUND_VOL_BLIP;
ev.duty = 0.5f;
engine.getAudioEngine().playEvent(ev);
}
}
void MenuScene::draw(pixelroot32::graphics::Renderer& renderer) {
Scene::draw(renderer);
}
void MenuScene::setupMainMenu() {
int btnW = menu::BTN_WIDTH;
int btnH = menu::BTN_HEIGHT;
gamesButton = std::make_unique<pr32::graphics::ui::UIButton>("GAMES", menu::BTN_SELECT, Vector2::ZERO(), Vector2{btnW, btnH}, [this]() {
showMenu(MenuState::GAMES);
}, pr32::graphics::ui::TextAlignment::CENTER, menu::BTN_FONT_SIZE);
physicsDemoButton = std::make_unique<pr32::graphics::ui::UIButton>("PHYSICS DEMO", menu::BTN_SELECT, Vector2::ZERO(), Vector2{btnW, btnH}, []() {
engine.setScene(&physicsDemoScene);
}, pr32::graphics::ui::TextAlignment::CENTER, menu::BTN_FONT_SIZE);
cameraDemoButton = std::make_unique<pr32::graphics::ui::UIButton>("CAMERA DEMO", menu::BTN_SELECT, Vector2::ZERO(), Vector2{btnW, btnH}, []() {
engine.setScene(&cameraDemoScene);
}, pr32::graphics::ui::TextAlignment::CENTER, menu::BTN_FONT_SIZE);
#if defined(PIXELROOT32_ENABLE_2BPP_SPRITES) || defined(PIXELROOT32_ENABLE_4BPP_SPRITES)
spritesDemoButton = std::make_unique<pr32::graphics::ui::UIButton>("SPRITES DEMO", menu::BTN_SELECT, Vector2::ZERO(), Vector2{btnW, btnH}, []() {
engine.setScene(&spritesDemoScene);
}, pr32::graphics::ui::TextAlignment::CENTER, menu::BTN_FONT_SIZE);
#endif
#ifdef PIXELROOT32_ENABLE_4BPP_SPRITES
tileMapDemoButton = std::make_unique<pr32::graphics::ui::UIButton>("TILEMAP DEMO", menu::BTN_SELECT, Vector2::ZERO(), Vector2{btnW, btnH}, []() {
engine.setScene(&tileMapDemoScene);
}, pr32::graphics::ui::TextAlignment::CENTER, menu::BTN_FONT_SIZE);
#endif
fontTestButton = std::make_unique<pr32::graphics::ui::UIButton>("FONT TEST", menu::BTN_SELECT, Vector2::ZERO(), Vector2{btnW, btnH}, []() {
engine.setScene(&fontTestScene);
}, pr32::graphics::ui::TextAlignment::CENTER, menu::BTN_FONT_SIZE);
dualPaletteTestButton = std::make_unique<pr32::graphics::ui::UIButton>("DUAL PALETTE TEST", menu::BTN_SELECT, Vector2::ZERO(), Vector2{btnW, btnH}, []() {
engine.setScene(&dualPaletteTestScene);
}, pr32::graphics::ui::TextAlignment::CENTER, menu::BTN_FONT_SIZE);
uiElementsButton = std::make_unique<pr32::graphics::ui::UIButton>("UIELEMENTS", menu::BTN_SELECT, Vector2::ZERO(), Vector2{btnW, btnH}, [this]() {
showMenu(MenuState::UIELEMENTS);
}, pr32::graphics::ui::TextAlignment::CENTER, menu::BTN_FONT_SIZE);
}
void MenuScene::setupGamesMenu() {
int btnW = menu::BTN_WIDTH;
int btnH = menu::BTN_HEIGHT;
pongButton = std::make_unique<pr32::graphics::ui::UIButton>("PONG", menu::BTN_SELECT, Vector2::ZERO(), Vector2{btnW, btnH}, []() {
engine.setScene(&pongScene);
}, pr32::graphics::ui::TextAlignment::CENTER, menu::BTN_FONT_SIZE);
brickBreakerButton = std::make_unique<pr32::graphics::ui::UIButton>("BRICK BREAKER", menu::BTN_SELECT, Vector2::ZERO(), Vector2{btnW, btnH}, []() {
engine.setScene(&brickScene);
}, pr32::graphics::ui::TextAlignment::CENTER, menu::BTN_FONT_SIZE);
snakeButton = std::make_unique<pr32::graphics::ui::UIButton>("SNAKE", menu::BTN_SELECT, Vector2::ZERO(), Vector2{btnW, btnH}, []() {
engine.setScene(&snakeScene);
}, pr32::graphics::ui::TextAlignment::CENTER, menu::BTN_FONT_SIZE);
spaceInvadersButton = std::make_unique<pr32::graphics::ui::UIButton>("SPACE INVADERS", menu::BTN_SELECT, Vector2::ZERO(), Vector2{btnW, btnH}, []() {
engine.setScene(&spaceInvadersScene);
}, pr32::graphics::ui::TextAlignment::CENTER, menu::BTN_FONT_SIZE);
tttButton = std::make_unique<pr32::graphics::ui::UIButton>("TIC TAC TOE", menu::BTN_SELECT, Vector2::ZERO(), Vector2{btnW, btnH}, []() {
engine.setScene(&tttScene);
}, pr32::graphics::ui::TextAlignment::CENTER, menu::BTN_FONT_SIZE);
#ifdef PIXELROOT32_ENABLE_4BPP_SPRITES
metroidvaniaButton = std::make_unique<pr32::graphics::ui::UIButton>("METROIDVANIA", menu::BTN_SELECT, Vector2::ZERO(), Vector2{btnW, btnH}, []() {
engine.setScene(&metroidvaniaScene);
}, pr32::graphics::ui::TextAlignment::CENTER, menu::BTN_FONT_SIZE);
#endif
}
void MenuScene::setupUIElementsMenu() {
int btnW = menu::BTN_WIDTH;
int btnH = menu::BTN_HEIGHT;
buttonsButton = std::make_unique<pr32::graphics::ui::UIButton>("BUTTONS", menu::BTN_SELECT, Vector2::ZERO(), Vector2{btnW, btnH}, [this]() {
showMenu(MenuState::BUTTONS);
engine.setScene(&buttonScene);
}, pr32::graphics::ui::TextAlignment::CENTER, menu::BTN_FONT_SIZE);
labelsButton = std::make_unique<pr32::graphics::ui::UIButton>("LABELS", menu::BTN_SELECT, Vector2::ZERO(), Vector2{btnW, btnH}, [this]() {
showMenu(MenuState::LABELS);
engine.setScene(&labelScene);
}, pr32::graphics::ui::TextAlignment::CENTER, menu::BTN_FONT_SIZE);
checkboxesButton = std::make_unique<pr32::graphics::ui::UIButton>("CHECKBOXES", menu::BTN_SELECT, Vector2::ZERO(), Vector2{btnW, btnH}, [this]() {
showMenu(MenuState::CHECKBOXES);
engine.setScene(&checkBoxScene);
}, pr32::graphics::ui::TextAlignment::CENTER, menu::BTN_FONT_SIZE);
layoutsButton = std::make_unique<pr32::graphics::ui::UIButton>("LAYOUTS", menu::BTN_SELECT, Vector2::ZERO(), Vector2{btnW, btnH}, [this]() {
showMenu(MenuState::LAYOUTS);
}, pr32::graphics::ui::TextAlignment::CENTER, menu::BTN_FONT_SIZE);
}
void MenuScene::setupLayoutsMenu() {
int btnW = menu::BTN_WIDTH;
int btnH = menu::BTN_HEIGHT;
verticalLayoutButton = std::make_unique<pr32::graphics::ui::UIButton>("VERTICAL LAYOUT", menu::BTN_SELECT, Vector2::ZERO(), Vector2{btnW, btnH}, []() {
engine.setScene(&verticalLayoutDemoScene);
}, pr32::graphics::ui::TextAlignment::CENTER, menu::BTN_FONT_SIZE);
horizontalLayoutButton = std::make_unique<pr32::graphics::ui::UIButton>("HORIZONTAL LAYOUT", menu::BTN_SELECT, Vector2::ZERO(), Vector2{btnW, btnH}, []() {
engine.setScene(&horizontalLayoutDemoScene);
}, pr32::graphics::ui::TextAlignment::CENTER, menu::BTN_FONT_SIZE);
gridLayoutButton = std::make_unique<pr32::graphics::ui::UIButton>("GRID LAYOUT", menu::BTN_SELECT, Vector2::ZERO(), Vector2{btnW, btnH}, []() {
engine.setScene(&gridLayoutDemoScene);
}, pr32::graphics::ui::TextAlignment::CENTER, menu::BTN_FONT_SIZE);
anchorLayoutButton = std::make_unique<pr32::graphics::ui::UIButton>("ANCHOR LAYOUT", menu::BTN_SELECT, Vector2::ZERO(), Vector2{btnW, btnH}, []() {
engine.setScene(&anchorLayoutDemoScene);
}, pr32::graphics::ui::TextAlignment::CENTER, menu::BTN_FONT_SIZE);
}
void MenuScene::showMenu(MenuState state) {
currentState = state;
// Clear all elements from layout
buttonLayout->clearElements();
// Update title
int screenWidth = engine.getRenderer().getLogicalWidth();
switch (state) {
case MenuState::MAIN:
titleLabel->setText("Main");
buttonLayout->addElement(gamesButton.get());
buttonLayout->addElement(physicsDemoButton.get());
buttonLayout->addElement(cameraDemoButton.get());
#if defined(PIXELROOT32_ENABLE_2BPP_SPRITES) || defined(PIXELROOT32_ENABLE_4BPP_SPRITES)
buttonLayout->addElement(spritesDemoButton.get());
#endif
#ifdef PIXELROOT32_ENABLE_4BPP_SPRITES
buttonLayout->addElement(tileMapDemoButton.get());
#endif
buttonLayout->addElement(fontTestButton.get());
buttonLayout->addElement(dualPaletteTestButton.get());
buttonLayout->addElement(uiElementsButton.get());
break;
case MenuState::GAMES:
titleLabel->setText("Games");
#ifdef PIXELROOT32_ENABLE_4BPP_SPRITES
buttonLayout->addElement(metroidvaniaButton.get());
#endif
buttonLayout->addElement(pongButton.get());
buttonLayout->addElement(brickBreakerButton.get());
buttonLayout->addElement(snakeButton.get());
buttonLayout->addElement(spaceInvadersButton.get());
buttonLayout->addElement(tttButton.get());
break;
case MenuState::UIELEMENTS:
titleLabel->setText("UIElements");
buttonLayout->addElement(buttonsButton.get());
buttonLayout->addElement(labelsButton.get());
buttonLayout->addElement(checkboxesButton.get());
buttonLayout->addElement(layoutsButton.get());
break;
case MenuState::LAYOUTS:
titleLabel->setText("Layouts");
buttonLayout->addElement(verticalLayoutButton.get());
buttonLayout->addElement(horizontalLayoutButton.get());
buttonLayout->addElement(gridLayoutButton.get());
buttonLayout->addElement(anchorLayoutButton.get());
break;
case MenuState::BUTTONS:
case MenuState::LABELS:
case MenuState::CHECKBOXES:
break;
}
titleLabel->centerX(screenWidth);
}
void MenuScene::goBack() {
switch (currentState) {
case MenuState::MAIN:
// Already at main menu, nothing to do
break;
case MenuState::GAMES:
showMenu(MenuState::MAIN);
break;
case MenuState::UIELEMENTS:
showMenu(MenuState::MAIN);
break;
case MenuState::BUTTONS:
case MenuState::LABELS:
case MenuState::CHECKBOXES:
case MenuState::LAYOUTS:
showMenu(MenuState::UIELEMENTS);
break;
}
}