persona-3-dual
A DS demake of Persona 3
Loading...
Searching...
No Matches
TextComponent.cpp
Go to the documentation of this file.
1#include "TextComponent.hpp"
2#include "core/logger.hpp"
3#include <aegis/ndsTypes.hpp>
4#include <aegis/types.hpp>
5#include <algorithm>
6
7void TextComponent::update(ae::q20_12_t)
8{
9 if (appearingText != nullptr)
10 {
11 if (appearingText->pageBreak)
12 {
13 return; // waiting for appearTextNextPage()
14 }
15
16 if (appearingText->cursorPos < (int)appearingText->content.size())
17 {
18 if (appearingText->counter <= 0)
19 {
20 ts.drawNextFromText(appearingText.get());
21 if (!appearingText->pageBreak)
22 {
23 appearingText->cursorPos++;
24 }
25 appearingText->counter = ts.APPEAR_DELAY; // Reset the counter for the next character
26 }
27 else
28 appearingText->counter--;
29 }
30 else //this text has finished appearing, so we can clear the storage
31 {
32 appearingText.reset();
33 }
34 }
35}
36
38{
39 active = false;
41 font.reset();
42}
43
44void TextComponent::configureText(const TextConfig& config, bool loadDefaultPalette)
45{
46 active = true;
47
48 font.reset();
49
50 // set video buffer
51 if (config.videoBuffer != nullptr)
52 {
53 videoBuffer = config.videoBuffer;
54 }
55
57 // load font
58 if (config.fontNamePath != nullptr)
59 {
60 font = tm.loadFont(config.fontNamePath, config.fontSize);
61 fontSize = config.fontSize;
62
66 letterSpacing = std::max(1, fontSize / 12);
67 lineSpacing = std::max(1, fontSize / 6);
68 spaceWidth = std::max(1, fontSize / 6);
69 }
70 // load font palette
71 else if (config.fontPalettePath != nullptr)
72 {
73 tm.loadPalette(config.fontPalettePath, config.isSub);
74 }
75
76 // load default font palette
77 if (loadDefaultPalette)
78 {
79 if (font == nullptr)
80 {
81 logMessage(LogSeverity::WARNING, "Cannot load a font palette if a font has not been loaded first!");
82 }
83 else
84 {
85 tm.loadDefaultPalette();
86 }
87 }
88}
89
90void TextComponent::drawText(const std::string& text, int x, int y, int color, int xBoundary)
91{
92 if (!font || !videoBuffer)
93 {
94 logMessage(LogSeverity::WARNING, "Called without font or videoBuffer");
95 return;
96 }
97 ts.drawText(text, font.get(), videoBuffer, x, y, color, letterSpacing, lineSpacing, spaceWidth, xBoundary);
98}
99
100void TextComponent::appearText(const std::string& text, int x, int y, int color, int maxX, int maxY, bool hyphenate)
101{
102 if (!font || !videoBuffer)
103 {
104 logMessage(LogSeverity::WARNING, "Called without font or videoBuffer");
105 return;
106 }
107 ts.appearText(appearingText,
108 text,
109 font.get(),
111 x,
112 y,
113 color,
117 maxX,
118 maxY,
119 hyphenate);
120}
121
123{
124 ts.appearTextSkip(appearingText.get());
125}
126
128{
129 return ts.appearTextPaused(appearingText.get());
130}
131
133{
134 ts.appearTextNextPage(appearingText.get());
135}
136
138{
139 appearingText.reset();
140}
141
143{
144 return ts.appearTextDone(appearingText.get());
145}
146
147void TextComponent::drawGlyph(const Glyph& glyph, int x, int y, int color, bool bold, bool italic, bool underline)
148{
149 ts.drawGlyph(glyph, font.get(), videoBuffer, x, y, color, bold, italic, underline);
150}
151
152Glyph* TextComponent::getGlyph(const char& c, bool bold)
153{
154 if (font == nullptr)
155 {
156 return nullptr;
157 }
158 if (bold)
159 {
160 return &font->boldGlyphs[static_cast<unsigned char>(c)];
161 }
162 return &font->glyphs[static_cast<unsigned char>(c)];
163}
164
165void TextComponent::clearArea(int x, int y, int width, int height)
166{
167 ts.clearArea(videoBuffer, x, y, width, height);
168}
169
171{
172 tm.clearScreen(videoBuffer);
174}
175
177{
178 return fontSize;
179}
180
182{
183 return lineSpacing;
184}
185
190
192{
193 return spaceWidth;
194}
195
197{
198 letterSpacing = value;
199}
200
202{
203 lineSpacing = value;
204}
205
207{
208 spaceWidth = value;
209}
210
212{
213 ts.testBitmap(font.get(), videoBuffer);
214}
215
217{
218 ts.testPalette(videoBuffer);
219}
Orchestrates rendering text from a bitmap.
void configureText(const TextConfig &config, bool loadDefaultPalette=true)
Configure the text system.
int getLetterSpacing()
A getter to return the letter spacing.
void drawGlyph(const Glyph &glyph, int x, int y, int color, bool bold=false, bool italic=false, bool underline=false)
A wrapper for drawGlyph in TextSystem. Draw a single glyph to the screen.
void appearTextSkip()
A wrapper for appearTextSkip in TextSystem. If a text is currently being rendered with appearText,...
std::unique_ptr< Font > font
void testPalette()
A wrapper for testPalette in TextSystem.
void clearScreen()
A wrapper for clearScreen in TextManager. Clear the text layer by filling the video buffer with black...
int getFontSize()
A getter to return the font size.
void clearArea(int x, int y, int width, int height)
A wrapper for clearArea in TextSystem. Clear a rectangular area of the text video buffer.
void update(ae::q20_12_t) override
void testBitmap()
A wrapper for testBitmap in TextSystem.
int getSpaceWidth()
A getter to return the space width (width of a ' ' character).
std::unique_ptr< Text > appearingText
void appearTextStop()
Stops the appearText animation immediately and clears the state.
TextSystem & ts
int letterSpacing
Auto-derived from fontSize in configureText(); can be overridden manually via the setters above.
void setSpaceWidth(int value)
Manually override the auto-calculated space width.
int getLineSpacing()
A getter to return the line spacing.
uint16_t * videoBuffer
bool appearTextDone()
A wrapper for appearTextDone in TextSystem. Check if the text being rendered with appearText has fini...
void setLetterSpacing(int value)
Manually override the auto-calculated letter spacing.
bool appearTextPaused()
Check if the paginated text is waiting for the next page.
void destroy() override
Sets active to false on component destruction.
Glyph * getGlyph(const char &c, bool bold=false)
Returns the Glyph struct for a given character. Useful for when drawing single characters from outsid...
void appearTextNextPage()
Clear the text area and continue the paused text from the top of it.
void drawText(const std::string &text, int x, int y, int color=TextColor::White, int xBoundary=256)
A wrapper for drawText in TextSystem. Draws text to the screen.
void setLineSpacing(int value)
Manually override the auto-calculated line spacing.
void appearText(const std::string &text, int x, int y, int color=TextColor::White, int maxX=256, int maxY=0, bool hyphenate=false)
A wrapper for appearText in TextSystem. Simulates text typing effect.
TextManager & tm
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.
Stores data for a single glyph (character) in a font.
Definition TextTypes.hpp:55
A struct that holds initial config values for the TextComponent.
std::string * fontPalettePath
for loadPalette
uint16_t * videoBuffer
std::string * fontNamePath
for loadFont