persona-3-dual
A DS demake of Persona 3
Loading...
Searching...
No Matches
DialogueComponent.cpp
Go to the documentation of this file.
2#include "core/globals.hpp"
3#include "core/logger.hpp"
4#include "types/UITypes.hpp"
5
6namespace
7{
8// text area inside the dialogue box; text beyond TEXT_MAX_X wraps and beyond TEXT_MAX_Y moves to a new page
9constexpr int TEXT_X = 8;
10constexpr int TEXT_Y = 154;
11constexpr int TEXT_MAX_X = 248;
12constexpr int TEXT_MAX_Y = 186;
13
14constexpr int OPTIONS_X = 128;
15constexpr int OPTIONS_Y = 50;
16} // namespace
17
19{
20 active = false;
21}
22
23void DialogueComponent::update(ae::q20_12_t)
24{
25 if (closing)
26 {
27 if (screen == nullptr || !screen->isAnimating())
28 {
29 closing = false;
30 active = false;
31 }
32 return;
33 }
34
35 if (!active || current == nullptr)
36 {
37 active = false;
38 return;
39 }
40
41 if (opening)
42 {
43 if (screen != nullptr && screen->isAnimating())
44 {
45 return;
46 }
47 opening = false;
49 return;
50 }
51
52 // animation
53 if (!text->appearTextDone())
54 {
57 if (renderBust)
58 {
59 if (screen != nullptr)
60 {
61 screen->renderBust(current->spritePayload);
62 }
63
64 renderBust = false;
65 }
66 else
67 {
68 optionCount = (int)current->selections.size();
69
70 if (optionCount > 0)
71 {
72 // render the full text + option list now that animation ended
73 doRenderOptions = true;
74 }
75 }
76 }
77 else if (doRenderOptions)
78 {
80 doRenderOptions = false;
81 }
82
83 // input
84 u32 pressed = systemKeysHeld & ~prevKeys;
86
87 if (pressed & KEY_START)
88 {
89 end();
90 return;
91 }
92
93 if (optionCount > 0)
94 {
95 if (text->appearTextDone())
96 {
97 // switch palette to green
98 if (screen != nullptr)
99 {
100 screen->triggerAction(UIAction::SwitchToPalette1);
101 }
102
103 // selection dialogue
104 if (pressed & KEY_DOWN)
105 {
108 }
109 else if (pressed & KEY_UP)
110 {
113 }
114 }
115 if (pressed & KEY_A)
116 {
117 if (skipOrContinueText())
118 {
119 return;
120 }
121 Dialogue* next = current->selections[selectedOption].next;
122 if (next == nullptr)
123 {
124 end();
125 return;
126 }
127
128 // switch palette to blue
129 if (screen != nullptr)
130 {
131 screen->triggerAction(UIAction::SwitchToPalette0);
132 }
133
134 advanceTo(next);
135 }
136 }
137 else
138 {
139 // linear dialogue
140 if (pressed & KEY_A)
141 {
142 if (skipOrContinueText())
143 {
144 return;
145 }
146 Dialogue* next = current->next;
147 if (next == nullptr)
148 {
149 end();
150 return;
151 }
152 advanceTo(next);
153 }
154 else if (pressed & KEY_B)
155 {
156 if (skipOrContinueText())
157 {
158 return;
159 }
160 Dialogue* next = current->prev;
161 if (next == nullptr)
162 {
163 end();
164 return;
165 }
166 advanceTo(next);
167 }
168 }
169}
170
172{
173 active = false;
174}
175
177{
178 if (config.text == nullptr || config.textAlt == nullptr)
179 {
180 logMessage(LogSeverity::WARNING, "Called with null text component");
181 }
182
183 text = config.text;
184 textAlt = config.textAlt;
185 screen = config.screen;
186 prevKeys = 0;
187
188 // load busts into memory
189 if (screen != nullptr)
190 {
191 screen->loadBusts(config.spritePayloads);
192 }
193}
194
196{
197 if (firstLine == nullptr)
198 {
199 logMessage(LogSeverity::WARNING, "Called with null firstLine");
200 return;
201 }
202
203 logMessage(LogSeverity::DEBUG, "Dialogue started: " + firstLine->name);
204
205 current = firstLine;
206 optionCount = 0;
207 selectedOption = 0;
208 doRenderOptions = false;
209 text->clearScreen();
210 opening = true;
211 closing = false;
212 if (screen != nullptr)
213 {
214 screen->showAnimated();
215 }
216
218 active = true;
219 renderBust = true;
220}
221
223{
224 logMessage(LogSeverity::DEBUG, "Dialogue ended");
225
226 if (text != nullptr)
227 {
228 text->clearScreen();
229 }
230
231 if (screen != nullptr)
232 {
233 // Keep the component active until hideAnimated() finishes; update()
234 // watches `closing` and clears active when !screen->isAnimating().
235 screen->hideAnimated();
236 closing = true;
237 return;
238 }
239 active = false;
240}
241
243{
244 if (next == nullptr)
245 {
246 logMessage(LogSeverity::WARNING, "Called with null Dialogue");
247 end();
248 return;
249 }
250
251 logMessage(LogSeverity::DEBUG, "Dialogue advanced to: " + next->name);
252
253 current = next;
254 doRenderOptions = false;
255 optionCount = 0;
256 selectedOption = 0;
257 renderBust = true;
258 if (text != nullptr)
259 {
260 text->clearScreen();
261 }
263}
264
266{
267 if (!text || !textAlt || !current)
268 {
269 logMessage(LogSeverity::WARNING, "text, textAlt, or current is null");
270 return;
271 }
272
273 textAlt->drawText("\xFF\x02\x01" + current->name, 8, 128, TextColor::RichBlue);
274 text->appearText(current->text, TEXT_X, TEXT_Y, TextColor::Black, TEXT_MAX_X, TEXT_MAX_Y, true);
275}
276
278{
279 if (text->appearTextDone())
280 {
281 return false;
282 }
283
284 // a full page waits for input, otherwise finish the current page instantly
285 if (text->appearTextPaused())
286 {
287 text->appearTextNextPage();
288 }
289 else
290 {
291 text->appearTextSkip();
292 }
293 return true;
294}
295
297{
298 if (!text || !textAlt || !current)
299 {
300 logMessage(LogSeverity::WARNING, "text, textAlt, or current is null");
301 return;
302 }
303
304 // the line itself stays on screen (it may span several pages), only the name and options are redrawn
305 textAlt->drawText("\xFF\x02\x01" + current->name, 8, 128, TextColor::DualGreen);
306 //TODO: The options are currently drawn outside the textbox, do we want to add an extra overlay for them similar to the actual game?
307 int textSize = text->getFontSize();
308 text->clearArea(OPTIONS_X, OPTIONS_Y, 256 - OPTIONS_X, textSize * optionCount + 4);
309 for (int i = 0; i < optionCount; i++)
310 {
311 if (i == selectedOption)
312 {
313 text->drawText(current->selections[i].text, OPTIONS_X, OPTIONS_Y + textSize * i, TextColor::Blue);
314 }
315 else
316 {
317 text->drawText(current->selections[i].text, OPTIONS_X, OPTIONS_Y + textSize * i, TextColor::White);
318 }
319 }
320}
Orchestrates dialogue display & branching logic.
@ DualGreen
Definition TextTypes.hpp:15
@ White
Definition TextTypes.hpp:14
@ Black
Definition TextTypes.hpp:13
@ RichBlue
Definition TextTypes.hpp:21
@ Blue
Definition TextTypes.hpp:29
@ SwitchToPalette1
Definition UITypes.hpp:25
@ SwitchToPalette0
Definition UITypes.hpp:24
void start(Dialogue *firstLine)
Start the currently loaded dialogue.
bool skipOrContinueText()
Handle input while the line is still being displayed.
bool renderBust
track the currently loaded bust
void update(ae::q20_12_t) override
Core update loop that renders & advances through dialogue.
TextComponent * textAlt
DialogueScreen * screen
void end()
End the dialogue display.
void configureDialogue(const DialogueConfig &config)
Configure the dialogue system.
void renderAnimFrame()
Animate in the dialogue text on appear.
void renderOptions()
Display the DialogueSelection options.
void destroy() override
void advanceTo(Dialogue *next)
Transition to a new Dialogue node and reset animation state.
Holds global variables used game-wide.
volatile u32 systemKeysHeld
Definition main.cpp:37
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.
A struct that holds initial config values for the DialogueComponent.
TextComponent * textAlt
etl::array< etl::span< SpritePayload >, 10 > * spritePayloads
TextComponent * text
DialogueScreen * screen
Holds dialogue content and branching data.
std::string name