persona-3-dual
A DS demake of Persona 3
Loading...
Searching...
No Matches
UISystem.cpp
Go to the documentation of this file.
1#include "UISystem.hpp"
7#include "core/globals.hpp"
8#include "core/logger.hpp"
10#include <aegis/ndsTypes.hpp>
11#include <aegis/types.hpp>
12
14{
15 active = false;
16 renderUIText = false;
17}
18
19void UISystem::update(ae::q20_12_t dt)
20{
21 // apply screen switches at the start of the frame, while still in VBlank
22 if (pendingScreen != nullptr)
23 {
25 }
26
27 // skip if nullptr or not active
28 if ((activeMenu == nullptr) || !activeMenu->isActive)
29 {
30 return;
31 }
32
33 bool selectionWasMoving = activeMenu->selectionOffset() != 0;
34 activeMenu->updateAnimation();
35 if (selectionWasMoving || activeMenu->selectionOffset() != 0)
36 {
37 renderUIText = true;
38 }
39
40 // run the hook
41 ViewState updateHookState = activeMenu->updateHook();
42 if (updateHookState != ViewState::DEFAULT)
43 {
44 ae::broadcastEvent(Event::SwitchView{updateHookState});
45 return;
46 }
47
48 // navigate options
49 if (systemKeysDown & KEY_DOWN)
50 {
51 if (sfxCmpt != nullptr)
52 {
53 sfxCmpt->playSFX(SFX::SFX_1, 255, 128);
54 }
55 activeMenu->selectedOption = (activeMenu->selectedOption + 1) % activeMenu->options.size();
56 activeMenu->animateSelection();
57 renderUIText = true;
58 }
59 else if (systemKeysDown & KEY_UP)
60 {
61 if (sfxCmpt != nullptr)
62 {
63 sfxCmpt->playSFX(SFX::SFX_1, 255, 128);
64 }
65 activeMenu->selectedOption =
66 (activeMenu->selectedOption + activeMenu->options.size() - 1) % activeMenu->options.size();
67 activeMenu->animateSelection();
68 renderUIText = true;
69 }
70
71 // Adjust scroll position
72 if (activeMenu->selectedOption < activeMenu->startIndex)
73 {
74 activeMenu->startIndex = activeMenu->selectedOption;
75 text->clearScreen();
76 renderUIText = true;
77 }
78 else if (activeMenu->selectedOption >= activeMenu->startIndex + activeMenu->visibleOptions)
79 {
80 activeMenu->startIndex = activeMenu->selectedOption - activeMenu->visibleOptions + 1;
81 text->clearScreen();
82 renderUIText = true;
83 }
84
85 if (systemKeysDown & KEY_A)
86 {
87 cancelSFX();
88 if (sfxCmpt != nullptr)
89 {
90 sfxCmpt->playSFX(SFX::SFX_2, 255, 128);
91 }
92 text->clearScreen();
93 renderUIText = true;
94
95 if (!activeMenu->options.empty() && activeMenu->selectedOption >= 0 &&
96 activeMenu->selectedOption < static_cast<int>(activeMenu->options.size()))
97 {
98 if (activeMenu->options[activeMenu->selectedOption].onSelect != nullptr)
99 {
100 ViewState result = (activeMenu->*(activeMenu->options[activeMenu->selectedOption].onSelect))();
101 if (result != ViewState::KEEP_CURRENT)
102 {
103 activeMenu->nextViewState = result;
104 activeMenu->isActive = false;
105 }
106 }
107 }
108 }
109 else if (systemKeysDown & KEY_B)
110 {
111 cancelSFX();
112 if (sfxCmpt != nullptr)
113 {
114 sfxCmpt->playSFX(SFX::SFX_0, 255, 128);
115 }
116 activeMenu->selectedOption = 0;
117 activeMenu->startIndex = 0;
118 text->clearScreen();
119 renderUIText = true;
120 activeMenu->prevOption();
121 }
122
123 // blink the "Pause" text
124 if (!activeMenu->customRender && activeMenu->pauseMessage.length() != 0)
125 {
126 if (frame % 60 < 30)
127 {
128 text->drawText(activeMenu->pauseMessage, 0, 0, TextColor::White);
129 }
130 else
131 {
132 text->clearArea(0, 0, 256, text->getFontSize() + text->getLineSpacing());
133 }
134 }
135
136 // display options
137 int textSize = text->getFontSize();
138 if (renderUIText)
139 {
140 renderUIText = false;
141 if (!activeMenu->customRender)
142 {
143 // Clear only the option list region so selection slide animation does not ghost.
144 const int optionsTop = textSize;
145 const int optionsHeight = textSize * activeMenu->visibleOptions + text->getLineSpacing();
146 text->clearArea(0, optionsTop, 256, optionsHeight);
147 for (int i = 0;
148 i < activeMenu->visibleOptions && activeMenu->startIndex + i < int(activeMenu->options.size());
149 i++)
150 {
151 int option = activeMenu->startIndex + i;
152 TextColor color = option == activeMenu->selectedOption ? TextColor::Blue : TextColor::White;
153 int x = option == activeMenu->selectedOption ? 10 + activeMenu->selectionOffset() : 10;
154 text->drawText(activeMenu->options[option].name, x, textSize + textSize * i, color);
155 }
156 }
157 }
158
159 if (activeMenu->nextViewState != ViewState::KEEP_CURRENT)
160 {
161 ae::broadcastEvent(Event::SwitchView{activeMenu->nextViewState});
162 }
163}
164
166{
168}
169
171{
172 sfxCmpt = sfx;
173}
174
176{
177 logMessage(LogSeverity::DEBUG, "View switch event received");
178 nextView = msg.view;
179}
180
185
187{
188 // reset previous config
190
191 // update() must run to apply pending screens, even in views without menus
192 active = true;
193
194 // set background
195 oamSub = config.oamSub;
196 oamMain = config.oamMain;
197
198 lruBgSub = config.bgSub;
199 hwBgSub = config.bgSub;
200
201 lruBgMain = config.bgMain;
202 hwBgMain = config.bgMain;
203
205
206 // register screens
207 for (UIScreen* screen : config.screens)
208 {
209 if (screen == nullptr)
210 {
211 continue;
212 }
213
214 registerScreen(screen);
215 }
216}
217
219{
220 // reset previous config
221 cleanupMenus();
222
223 active = true;
224 menus = config.menus;
225 text = config.text;
226
227 int textSize = text->getFontSize();
228
229 for (UIMenu*& menu : menus)
230 {
231 // skip if nullptr
232 if (menu == nullptr)
233 {
234 continue;
235 }
236
237 menu->isActive = false;
238 menu->text = text;
240 if (!menu->customRender)
241 {
242 menu->visibleOptions = (192 / textSize) - 1;
243 }
244 }
245}
246
248{
249 if (msg.screen == nullptr)
250 {
251 logMessage(LogSeverity::ERROR, "ShowScreen event received with nullptr screen");
252 return;
253 }
254
255 pendingScreen = msg.screen;
256}
257
259{
260 UIScreen* screen = pendingScreen;
261 pendingScreen = nullptr;
262
263 // check if it is already loaded. If not, load it in
264 if (!screen->isLoaded)
265 {
266 // add screen if space
267 if (screen->isMain ? (screenMainCount < 2) : (screenSubCount < 3))
268 {
269 registerScreen(screen);
270 }
271 // swap out screens if no space
272 else
273 {
274 UIScreen* oldScreen = nullptr;
275
276 // remove the least recently displayed screen
277 if (screen->isMain)
278 {
279 int targetBgId = lruBgMain[0];
280 for (int i = 0; i < 2; i++)
281 {
282 if ((loadedMain[i] != nullptr) && (loadedMain[i]->bgId == targetBgId))
283 {
284 oldScreen = loadedMain[i];
285 loadedMain[i] = screen;
286 break;
287 }
288 }
289 }
290 else
291 {
292 int targetBgId = lruBgSub[0];
293 for (int i = 0; i < 3; i++)
294 {
295 if ((loadedSub[i] != nullptr) && (loadedSub[i]->bgId == targetBgId))
296 {
297 oldScreen = loadedSub[i];
298 loadedSub[i] = screen;
299 break;
300 }
301 }
302 }
303
304 if (oldScreen != nullptr)
305 {
306 oldScreen->unload();
307 oldScreen->isLoaded = false;
308
309 screen->bgId = oldScreen->bgId;
310 screen->oam = oldScreen->oam;
311 oldScreen->bgId = -1;
312 oldScreen->oam = nullptr;
313 }
314 else
315 {
316 logMessage(LogSeverity::ERROR, "Failed to find LRU screen to evict");
317 }
318
319 screen->load();
320 screen->isLoaded = true;
321 }
322 }
323
324 // load screen
326
328 screen->renderSprites();
329
331 ui.showBg(screen->bgId);
332
334 screen->renderText();
335
336 // update the "least recently updated" index
337 lruUpdate(screen->bgId, screen->isMain);
338
339 logMessage(LogSeverity::DEBUG, "ShowScreen event processed");
340}
341
343{
345}
346
348{
349 if (msg.menu == nullptr)
350 {
351 logMessage(LogSeverity::WARNING, "ShowMenu event received with nullptr menu");
352 return;
353 }
354
355 logMessage(LogSeverity::DEBUG, "ShowMenu event processed");
356
357 if (sfxCmpt != nullptr)
358 {
359 sfxCmpt->registerSFX(SFX::SFX_1);
360 sfxCmpt->registerSFX(SFX::SFX_2);
361 sfxCmpt->registerSFX(SFX::SFX_0);
362 }
363
364 if (activeMenu != nullptr)
365 {
366 activeMenu->isActive = false;
367 }
368
369 renderUIText = true;
370 activeMenu = msg.menu;
371 activeMenu->resetMenu();
372 activeMenu->isActive = true;
373}
374
376{
377 renderUIText = false;
378 if (activeMenu != nullptr)
379 {
380 activeMenu->resetMenu();
381 activeMenu = nullptr;
382 }
383}
384
386{
387 renderUIText = true;
388}
389
390void UISystem::lruUpdate(int id, bool isMain)
391{
392 if (isMain)
393 {
394 int pos = 0;
395
396 // find where the existing id currently is
397 for (int i = 0; i < 2; i++)
398 {
399 if (lruBgMain[i] == id)
400 {
401 pos = i;
402 break;
403 }
404 }
405
406 // shift everything after that position to the left to close the gap
407 for (int i = pos; i < 1; i++)
408 {
409 lruBgMain[i] = lruBgMain[i + 1];
410 }
411
412 // place the updated id at the very end (most recently used)
413 lruBgMain[1] = id;
414 }
415 else
416 {
417 int pos = 0;
418
419 // find where the existing id currently is
420 for (int i = 0; i < 3; i++)
421 {
422 if (lruBgSub[i] == id)
423 {
424 pos = i;
425 break;
426 }
427 }
428
429 // shift everything after that position to the left to close the gap
430 for (int i = pos; i < 2; i++)
431 {
432 lruBgSub[i] = lruBgSub[i + 1];
433 }
434
435 // place the updated id at the very end (most recently used)
436 lruBgSub[2] = id;
437 }
438}
439
441{
442 if (screen == nullptr)
443 {
444 logMessage(LogSeverity::ERROR, "Called with nullptr screen");
445 return;
446 }
447
448 logMessage(LogSeverity::DEBUG, "Screen registered successfully");
449
450 // load screen
451 if (screen->isMain && screenMainCount < 2)
452 {
453 loadedMain[screenMainCount] = screen;
454 screen->bgId = hwBgMain[screenMainCount];
456 screen->oam = oamMain;
457 screen->load();
458 screen->isLoaded = true;
459 return;
460 }
461 else if (!screen->isMain && screenSubCount < 3)
462 {
463 loadedSub[screenSubCount] = screen;
464 screen->bgId = hwBgSub[screenSubCount];
465
467 screen->oam = oamSub;
468 screen->load();
469 screen->isLoaded = true;
470 return;
471 }
472
473 // throw error (too many screens registered)
474 if (screen->isMain)
475 {
477 "Too many main screens registered. A maximum of 2 main screens can be registered.");
478 return;
479 }
480 else
481 {
483 "Too many sub screens registered. A maximum of 3 sub screens can be registered.");
484 return;
485 }
486}
487
489{
490 // hide sub screens
491 for (UIScreen* val : loadedSub)
492 {
493 if (val == nullptr)
494 {
495 continue;
496 }
497 ui.hideBg(val->bgId);
498 val->removeSprites();
499 }
500
501 // NOTE: do not hide the 3D layer
502 // hide main screens
503 for (UIScreen* val : loadedMain)
504 {
505 if (val == nullptr)
506 {
507 continue;
508 }
509 ui.hideBg(val->bgId);
510 val->removeSprites();
511 }
512}
513
515{
516 // the pending screen may belong to the view being cleaned up
517 pendingScreen = nullptr;
518
519 // reset all bg ids, UIScreens
520 // sub
521 for (int i = 0; i < 3; i++)
522 {
523 lruBgSub[i] = hwBgSub[i];
524 if (loadedSub[i] == nullptr)
525 {
526 continue;
527 }
528 loadedSub[i]->unload();
529 loadedSub[i]->isLoaded = false;
530 loadedSub[i]->oam = nullptr;
531 loadedSub[i]->bgId = -1;
532 loadedSub[i] = nullptr;
533 }
534
535 // main
536 for (int j = 0; j < 2; j++)
537 {
538 lruBgMain[j] = hwBgMain[j];
539 if (loadedMain[j] == nullptr)
540 {
541 continue;
542 }
543 loadedMain[j]->unload();
544 loadedMain[j]->isLoaded = false;
545 loadedMain[j]->oam = nullptr;
546 loadedMain[j]->bgId = -1;
547 loadedMain[j] = nullptr;
548 }
549
550 screenMainCount = 0;
551 screenSubCount = 0;
552}
553
555{
556 if (sfxCmpt != nullptr)
557 {
558 sfxCmpt->stopSFX();
559 }
560}
561
563{
564 cancelSFX();
566 cleanupMenus();
567
568 active = false;
569 renderUIText = false;
570 sfxCmpt = nullptr;
571}
572
574{
575 if (activeMenu != nullptr)
576 {
577 activeMenu->resetMenu();
578 activeMenu = nullptr;
579 }
580
581 for (UIMenu* menu : menus)
582 {
583 if (menu != nullptr)
584 {
585 menu->text = nullptr;
586 }
587 }
588
589 menus = {};
590 text = nullptr;
591}
@ SFX_2
@ SFX_0
@ SFX_1
Generic events used across multiple systems.
Handles the rendering & logic for the menu HUD UI.
ViewState
Definition StateTypes.hpp:4
TextColor
Human Readable enum for text colors.
Definition TextTypes.hpp:11
@ White
Definition TextTypes.hpp:14
@ Blue
Definition TextTypes.hpp:29
Manages the mapping of UIScreens to these hardware layers.
bool isMain
Definition UIScreen.hpp:11
virtual void renderText()
Draws the screen's text. Called after the background is shown.
Definition UIScreen.hpp:38
OamState * oam
Definition UIScreen.hpp:12
virtual void load()=0
Loads graphics data into memory & pushes to VRAM.
bool isLoaded
Definition UIScreen.hpp:13
virtual void unload()=0
Unloads graphics data from memory.
virtual void renderSprites()
Applys sprite colour palette & draws sprites to screen.
Definition UIScreen.cpp:7
UIScreen * pendingScreen
screen requested by ShowScreen, applied at the start of the next update
Definition UISystem.hpp:231
void cancelSFX()
Stops playing the current sound effect, if any.
Definition UISystem.cpp:554
void shutdown() override
Unloads and cleans up all registered screens. Wrapper for cleanup.
Definition UISystem.cpp:165
std::array< UIScreen *, 2 > loadedMain
Definition UISystem.hpp:220
void applyPendingScreen()
Loads (if needed) and displays the pending screen, then clears it.
Definition UISystem.cpp:258
void resetUIResources()
Cleans up UISystem related resources.
Definition UISystem.cpp:562
int screenSubCount
Definition UISystem.hpp:218
std::array< int, 3 > hwBgSub
original background ids (order doesn't change)
Definition UISystem.hpp:213
std::array< int, 3 > lruBgSub
background ids. The order of the arrays matter. Front = least recently updated, back = last updated
Definition UISystem.hpp:209
int screenMainCount
currently loaded screens (max 3 sub, 2 main)
Definition UISystem.hpp:217
std::array< UIMenu *, 10 > menus
Definition UISystem.hpp:223
std::array< int, 2 > lruBgMain
Definition UISystem.hpp:210
OamState * oamSub
Definition UISystem.hpp:205
std::array< UIScreen *, 3 > loadedSub
Definition UISystem.hpp:219
void hideAllScreens()
hides all screens.
Definition UISystem.cpp:488
TextComponent * text
Definition UISystem.hpp:225
void lruUpdate(int id, bool isMain)
Updates the order of lruBgSub/lruBgMain for the "least recently updated" id.
Definition UISystem.cpp:390
void update(ae::q20_12_t) override
Performs menu navigation logic.
Definition UISystem.cpp:19
void setSFXComponent(SFXComponent *sfx)
Definition UISystem.cpp:170
UIManager & ui
Definition UISystem.hpp:203
void on_receive(const Event::SwitchView &msg)
Definition UISystem.cpp:175
SFXComponent * sfxCmpt
Definition UISystem.hpp:226
bool renderUIText
Definition UISystem.hpp:228
void cleanupMenus()
Unloads and cleans up all registered menus. Used to reset previous configs.
Definition UISystem.cpp:573
void cleanupScreens()
Unloads and cleans up all registered screens. Used to reset previous configs.
Definition UISystem.cpp:514
OamState * oamMain
Definition UISystem.hpp:206
void registerScreen(UIScreen *screen)
Registers screens to UISystem to have them pre-loaded before calling show.
Definition UISystem.cpp:440
UIMenu * activeMenu
Definition UISystem.hpp:224
void init() override
Definition UISystem.cpp:13
std::array< int, 2 > hwBgMain
Definition UISystem.hpp:214
Holds global variables used game-wide.
volatile u32 systemKeysDown
Definition main.cpp:36
ViewState nextView
Definition main.cpp:40
volatile int32_t frame
Definition main.cpp:35
void logMessage(LogSeverity severity, std::string_view message, std::source_location location)
Logs a diagnostic message and halts only for fatal messages.
Definition logger.cpp:110
Util to handle error logging.
All parameters needed to configure UIMenus.
Definition UIEvents.hpp:52
std::array< UIMenu *, 10 > menus
Definition UIEvents.hpp:54
TextComponent * text
Definition UIEvents.hpp:53
All parameters needed to configure UIScreens.
Definition UIEvents.hpp:22
std::array< UIScreen *, 5 > screens
The screens to register.
Definition UIEvents.hpp:36
OamState * oamSub
Pointer to sub OAM.
Definition UIEvents.hpp:30
std::array< int, 3 > bgSub
Array of sub screen background ids.
Definition UIEvents.hpp:24
std::array< int, 2 > bgMain
Array of main screen background ids.
Definition UIEvents.hpp:27
OamState * oamMain
Pointer to main OAM.
Definition UIEvents.hpp:33
Event to hide all menus.
Definition UIEvents.hpp:76
Event to trigger hiding all UIScreens.
Definition UIEvents.hpp:96
Event to trigger rendering UI text.
Definition UIEvents.hpp:103
Event payload to release resources owned by the current view.
Event to show specified menu.
Definition UIEvents.hpp:65
UIMenu * menu
Definition UIEvents.hpp:66
Event to show specified screen.
Definition UIEvents.hpp:83
UIScreen * screen
The screen to show.
Definition UIEvents.hpp:85
Event payload to switch the ViewState.