persona-3-dual
A DS demake of Persona 3
Loading...
Searching...
No Matches
SignContractScreen.cpp
Go to the documentation of this file.
2#include "core/globals.hpp"
3
4// TODO: remove this function
5// Maybe replace with a draw pixel function in the UI Manager or let the keyboard handle this
15static void drawPixel(uint16_t* videoBuffer, int x, int y, int paletteValue)
16{
17 if (!videoBuffer || x < 0 || x >= 256 || y < 0 || y >= 192)
18 {
19 return;
20 }
21
22 int wordIndex = (y * 256 + x) / 2;
23 u16 currentWord = videoBuffer[wordIndex];
24 if (x % 2 == 0)
25 {
26 //Clear the lower 8 bits, then inject our 8-bit color index
27 videoBuffer[wordIndex] = (currentWord & 0xFF00) | (paletteValue & 0xFF);
28 }
29 else
30 {
31 //Clear the upper 8 bits, then inject our 8-bit color index shifted up
32 videoBuffer[wordIndex] = (currentWord & 0x00FF) | ((paletteValue & 0xFF) << 8);
33 }
34}
35
37
39{
40 if (!instance)
41 {
43 }
44}
45
47{
48 if (instance)
49 {
50 delete instance;
51 instance = nullptr;
52 }
53}
54
63
65{
66 // Copy name into local variables
67 for (int i = 0; i < 10; ++i)
68 {
69 lastName[i] = saveData.lastName[i];
70 firstName[i] = saveData.firstName[i];
71 }
72
73 // Set cursor to the end of the name
74 // Check if first name first
75 bool endFound = false;
76 for (int i = 9; i >= 0; --i)
77 {
78 if (endFound)
79 {
80 break;
81 }
82
83 if (firstName[i] != ' ' && firstName[i] != '\0')
84 {
85 endFound = true;
86 index = i + 11; // +10 for first name offset
87 isLastName = false;
88 }
89 }
90 // If first name is empty, check last name
91 if (!endFound)
92 {
94 }
95
96 renderName();
98}
99
101{
102 // Load backgrounds into ram
103 bgUI[0] = graphics->loadGraphic(bgPath + "base/base");
104 bgUI[1] = graphics->loadGraphic(bgPath + "shift/shift");
105}
106
108{
110 if (!isLastName)
111 {
112 updateStatus("Enter your first name");
113 }
114 else
115 {
116 updateStatus("Enter your last name");
117 }
118}
119
121{
122 // load palettes
123 dmaCopy(bgUI[bgIndex].pal, BG_PALETTE_SUB, bgUI[bgIndex].palLen);
124 TextManager::getInstance().loadDefaultPalette(); // prevent this from being overwritten
125
126 // draw background (copy into vram)
127 dmaFillHalfWords(0, bgGetMapPtr(bgId), 2048);
128 dmaCopy(bgUI[bgIndex].tiles, bgGetGfxPtr(bgId), bgUI[bgIndex].tilesLen);
129 dmaCopy(bgUI[bgIndex].map, bgGetMapPtr(bgId), bgUI[bgIndex].mapLen);
130}
131
133{
134 int index = 0;
135 if (isShift || isCapsLock)
136 {
137 index = 1;
138 }
139 else
140 {
141 index = 0;
142 }
144}
145
147{
148 // clear vram
149 dmaFillHalfWords(0, bgGetMapPtr(bgId), 2048);
150 dmaFillHalfWords(0, BG_PALETTE_SUB, bgUI[0].palLen);
151
152 // unload backgrounds from ram
153 graphics->unloadGraphic(bgUI[0]);
154 graphics->unloadGraphic(bgUI[1]);
155 bgUI[0] = {};
156 bgUI[1] = {};
157}
158
160{
161 // Only update every 60 frames
162 if (frame % 60 == 30)
163 {
164 if (cursorRendered)
165 {
166 clearCursor();
167 cursorRendered = false;
168 }
169 else
170 {
171 renderCursor();
172 cursorRendered = true;
173 }
174 }
175
176 if (systemKeysDown & KEY_A)
177 {
178 return confirmAction();
179 }
180 if (systemKeysDown & KEY_B)
181 {
183 }
184 if (systemKeysDown & KEY_L)
185 {
187 }
188 if (systemKeysDown & KEY_R)
189 {
191 }
192
193 return 0;
194}
195
196int SignContractScreen::onTouch(touchPosition* touch)
197{
198 char c = keyboard.evaluateInput(touch);
199 const unsigned char input = static_cast<unsigned char>(c);
200
201 switch (input)
202 {
203 case static_cast<unsigned char>(KEYCODES::CAPSLOCK):
204 {
207 break;
208 }
209 case static_cast<unsigned char>(KEYCODES::SHIFT):
210 {
211 isShift = !isShift;
213 break;
214 }
215 case static_cast<unsigned char>(KEYCODES::BACKSPACE):
216 {
218 break;
219 }
220 case static_cast<unsigned char>(KEYCODES::LEFT):
221 {
223 break;
224 }
225 case static_cast<unsigned char>(KEYCODES::RIGHT):
226 {
228 break;
229 }
230 case static_cast<unsigned char>(KEYCODES::CONFIRM):
231 {
232 return confirmAction();
233 }
234 case 0:
235 {
236 break; // didnt hit any key
237 }
238 default:
239 {
240 if (index < 20)
241 {
243 }
244 renderCursor();
245 break;
246 }
247 }
248 return 0;
249}
250
252{
253 if (isShift)
254 {
255 isShift = false;
257 }
258 if (index < 10)
259 {
260 lastName[index] = c;
261 index++;
262 if (index == 10)
263 {
264 isLastName = false;
265 updateStatus("Enter your first name");
266 }
267 }
268 else
269 {
270 if (index < 20)
271 {
272 firstName[index - 10] = c;
273 index++;
274 }
275 }
276 renderName();
277}
278
279void SignContractScreen::updateStatus(std::string status)
280{
281 text->clearArea(10, 5, 105, FONT_SIZE + lineSpacing);
282 text->drawText(status, 10, 5);
283}
284
286{
287 text->clearArea(65, 31, 125, 30);
288 for (int i = 0; i < 10; i++)
289 {
290 if (lastName[i] == '\0' || lastName[i] == ' ')
291 {
292 continue;
293 }
294 char c = lastName[i];
295 Glyph* g = text->getGlyph(c, true);
296 text->drawGlyph(*g, 65 + (13 * i), 31 - lineSpacing, TextColor::White, true);
297 }
298 for (int i = 0; i < 10; i++)
299 {
300 if (firstName[i] == '\0' || firstName[i] == ' ')
301 {
302 continue;
303 }
304 char c = firstName[i];
305 Glyph* g = text->getGlyph(c, true);
306 text->drawGlyph(*g, 65 + (13 * i), 48 - lineSpacing, TextColor::White, true);
307 }
308}
309
311{
312 // Clear old cursor first
313 clearCursor();
314
315 // If end was written clear it too
316 if (index == 19)
317 {
318 text->clearArea(197, 48 - lineSpacing, 30, 10);
319 }
320
321 // Draw new cursor
322 if (index >= 20)
323 {
324 text->drawText("End", 197, 48 - lineSpacing);
325 return;
326 }
327 int y = isLastName ? 42 : 59;
328 int x = 65 + (13 * (isLastName ? index : index - 10));
329 for (int i = 0; i < 9; i++)
330 {
332 }
333}
334
336{
337 // Clear both lines to be safe (then we don't need to store the old cursor position)
338 text->clearArea(65, 42, 126, 1);
339 text->clearArea(65, 59, 126, 1);
340}
341
343{
344 if (isLastName)
345 {
346 isLastName = false;
347 index = 10;
348 updateStatus("Enter your first name");
349 renderCursor();
350 return 0;
351 }
352 else
353 {
354 // TODO: implement confirm prompt
355 return 1;
356 }
357}
358
360{
361 if (index == 0)
362 {
363 return;
364 }
365 if (isLastName)
366 {
367 lastName[--index] = ' ';
368 }
369 else
370 {
371 if (index != 10) // index is at start of the second line
372 {
373 firstName[--index - 10] = ' ';
374 }
375 else
376 {
377 index--;
378 isLastName = true;
379 updateStatus("Enter your last name");
380 lastName[9] = ' ';
382 }
383 }
384 renderName();
385 renderCursor();
386}
387
389{
390 if (index > 0)
391 {
392 index--;
393 if (index == 9)
394 {
395 isLastName = true;
396 updateStatus("Enter your last name");
398 }
399 }
400 renderCursor();
401}
402
404{
405 if (index < 19)
406 {
407 index++;
408 if (index == 10)
409 {
410 isLastName = false;
411 updateStatus("Enter your first name");
412 }
413 }
414 renderCursor();
415}
416
418{
419 bool endFound = false;
420 for (int i = 9; i >= 0; --i)
421 {
422 if (endFound)
423 {
424 break;
425 }
426
427 if (lastName[i] != ' ' && lastName[i] != '\0')
428 {
429 endFound = true;
430 index = i + 1; // put cursor after the last character in the last name
431 }
432 }
433}
434
436{
437 if (signContractUI == nullptr)
438 {
439 signContractUI = engine.createEntity();
440 graphics = engine.createComponent<GraphicsComponent>();
441 signContractUI->addComponent(graphics);
442 }
443
444 setBackdropColor(ARGB16(1, 0, 0, 4));
445
446 text = engine.createComponent<TextComponent>();
447 signContractUI->addComponent(text);
448 int bgTextSub = bgInitSub(3, BgType_Bmp8, BgSize_B8_256x256, 4, 0);
449 bgTextBufferSub = bgGetGfxPtr(bgTextSub);
450 bgSetPriority(bgTextSub, 0);
451 bgSetPriority(bgId, 1);
453 lineSpacing = text->getLineSpacing(); // get it now so we don't waste time at runtime
454
455 // Reset variables
456 index = 0;
457 keyboard.reset();
458 isLastName = true;
459 isShift = false;
460 isCapsLock = false;
461
464}
465
467{
468 // update save data (names)
469 for (int i = 0; i < 10; ++i)
470 {
471 saveData.lastName[i] = lastName[i];
472 saveData.firstName[i] = firstName[i];
473 }
474 ae::broadcastEvent(Event::WriteSave{});
475
477
478 if (signContractUI != nullptr)
479 {
480 engine.destroyEntity(signContractUI);
481 signContractUI = nullptr;
482 graphics = nullptr;
483 }
484}
static void drawPixel(uint16_t *videoBuffer, int x, int y, int paletteValue)
Draws a pixel to the specified videobuffer.
Handles the Rendering & Logic for the Sign Contract UI.
@ White
Definition TextTypes.hpp:14
@ RichBlue
Definition TextTypes.hpp:21
void loadBackgrounds()
Loads background assets into RAM.
void renderSprites() override
Renders the background and status text to the screen.
void load()
Loads graphics data into memory & pushes to VRAM.
void loadSaveDataName()
Loads the name from the save data file into a local variable.
void moveCursorLeft()
Moves the cursor to the left in the name input area.
void renderBackground(int bgIndex=0)
Pushes background data into VRAM so that it is rendered on screen.
static SignContractScreen * instance
void switchBackground()
Switches the rendered background between the default and shift/capslock backgrounds.
int confirmAction()
Handles the logic for when the user presses the confirm button.
void writeCharacter(char c)
Writes a character to the name buffer at the current index and increments the index.
void backSpaceAction()
Handles logic for when user presses the backspace button.
void renderName()
Clears any old name text and writes the current name buffer to the name input area.
void unload()
Unloads graphics data from memory.
void moveCursorRight()
Moves the cursor to the right in the name input area.
GraphicsComponent * graphics
int update()
Frame update function for the SignContractScreen. Handles cursor blinking and button presses.
void unloadBackgrounds()
Clears the current background from VRAM and unloads the background data from RAM.
const std::string bgPath
static SignContractScreen * getInstance()
void clearCursor()
Clears the previously rendered cursor from the screen.
OnScreenKeyboard keyboard
void sendCursorToEndOfLastName()
Helper function to set the cursor index to the end of the last name, if any characters have been writ...
void updateStatus(std::string status)
Clears any old status text and writes a new status to the status text box.
void renderCursor()
Renders a small cursor at the current slot where the next character will be written in the name buffe...
int onTouch(touchPosition *touch) override
Handles touch input. Will check if any of the keyboard keys were pressed and execute any necessary ac...
Holds global variables used game-wide.
Save saveData
Definition main.cpp:39
volatile u32 systemKeysDown
Definition main.cpp:36
GameEngine engine
Definition main.cpp:31
volatile int32_t frame
Definition main.cpp:35
@ CAPSLOCK
Zero is reserved for not touching the keyboard.
Definition keyboard.hpp:18
Event payload to trigger a save write.
Stores data for a single glyph (character) in a font.
Definition TextTypes.hpp:55
A struct that holds initial config values for the TextComponent.