Font & Text Changes.

`Gui::loadFont()` now needs a C2D_Font parameter & path. If you're using this, I suggest to update the function to match this. If you're using `Gui::loadFont()` to load the SystemFont, I suggest to remove that, since `Gui::init()` loads it now.

All the Text Functions can now optional use the C2D_Font parameter to use a specific font. (Uses SystemFont if nullptr.)

`Gui::unloadFont()` now needs a C2D_Font parameter to unload it.
This commit is contained in:
StackZ
2020-05-21 18:17:07 +02:00
parent 4dc2426fa5
commit f0dca7c784
2 changed files with 108 additions and 37 deletions
+93 -27
View File
@@ -62,23 +62,20 @@ Result Gui::init(void) {
Bottom = C2D_CreateScreenTarget(GFX_BOTTOM, GFX_LEFT); Bottom = C2D_CreateScreenTarget(GFX_BOTTOM, GFX_LEFT);
// Load Textbuffer. // Load Textbuffer.
TextBuf = C2D_TextBufNew(4096); TextBuf = C2D_TextBufNew(4096);
Font = C2D_FontLoadSystem(CFG_REGION_USA); // Load System font.
return 0; return 0;
} }
// Load a Font. // Load a Font.
Result Gui::loadFont(bool sysFont, const char* Path) { Result Gui::loadFont(C2D_Font &fnt, const char* Path) {
if (sysFont) { if (access(Path, F_OK) == 0) fnt = C2D_FontLoad(Path); // Only load if found.
Font = C2D_FontLoadSystem(CFG_REGION_USA);
} else {
if (access(Path, F_OK) == 0) Font = C2D_FontLoad(Path); // Only load if found.
}
return 0; return 0;
} }
// Unload a Font. // Unload a Font.
Result Gui::unloadFont() { Result Gui::unloadFont(C2D_Font &fnt) {
if (Font != nullptr) { if (fnt != nullptr) {
C2D_FontFree(Font); // Make sure to only unload if not nullptr. C2D_FontFree(fnt); // Make sure to only unload if not nullptr.
} }
return 0; return 0;
} }
@@ -103,67 +100,136 @@ void Gui::exit(void) {
} }
// Draw a Centered String. // Draw a Centered String.
void Gui::DrawStringCentered(float x, float y, float size, u32 color, std::string Text, int maxWidth, int maxHeight) { void Gui::DrawStringCentered(float x, float y, float size, u32 color, std::string Text, int maxWidth, int maxHeight, C2D_Font fnt) {
float lineHeight, widthScale; float lineHeight, widthScale;
lineHeight = Gui::GetStringHeight(size, " ");
// Check for the lineHeight.
if (fnt != nullptr) {
lineHeight = Gui::GetStringHeight(size, " ", fnt);
} else {
lineHeight = Gui::GetStringHeight(size, " ");
}
int line = 0; int line = 0;
while(Text.find('\n') != Text.npos) { while(Text.find('\n') != Text.npos) {
if (maxWidth == 0) { if (maxWidth == 0) {
widthScale = Gui::GetStringWidth(size, Text.substr(0, Text.find('\n'))); // Do the widthScale.
if (fnt != nullptr) {
widthScale = Gui::GetStringWidth(size, Text.substr(0, Text.find('\n')), fnt);
} else {
widthScale = Gui::GetStringWidth(size, Text.substr(0, Text.find('\n')));
}
} else { } else {
widthScale = std::min((float)maxWidth, Gui::GetStringWidth(size, Text.substr(0, Text.find('\n')))); // Do the widthScale 2.
if (fnt != nullptr) {
widthScale = std::min((float)maxWidth, Gui::GetStringWidth(size, Text.substr(0, Text.find('\n')), fnt));
} else {
widthScale = std::min((float)maxWidth, Gui::GetStringWidth(size, Text.substr(0, Text.find('\n'))));
}
} }
Gui::DrawString((currentScreen ? 200 : 160)+x-(widthScale/2), y+(lineHeight*line), size, color, Text.substr(0, Text.find('\n')), maxWidth, maxHeight);
if (fnt != nullptr) {
Gui::DrawString((currentScreen ? 200 : 160)+x-(widthScale/2), y+(lineHeight*line), size, color, Text.substr(0, Text.find('\n')), maxWidth, maxHeight, fnt);
} else {
Gui::DrawString((currentScreen ? 200 : 160)+x-(widthScale/2), y+(lineHeight*line), size, color, Text.substr(0, Text.find('\n')), maxWidth, maxHeight);
}
Text = Text.substr(Text.find('\n')+1); Text = Text.substr(Text.find('\n')+1);
line++; line++;
} }
if (maxWidth == 0) { if (maxWidth == 0) {
widthScale = Gui::GetStringWidth(size, Text.substr(0, Text.find('\n'))); // Do the next WidthScale.
if (fnt != nullptr) {
widthScale = Gui::GetStringWidth(size, Text.substr(0, Text.find('\n')), fnt);
} else {
widthScale = Gui::GetStringWidth(size, Text.substr(0, Text.find('\n')));
}
} else { } else {
widthScale = std::min((float)maxWidth, Gui::GetStringWidth(size, Text.substr(0, Text.find('\n')))); // And again.
if (fnt != nullptr) {
widthScale = std::min((float)maxWidth, Gui::GetStringWidth(size, Text.substr(0, Text.find('\n')), fnt));
} else {
widthScale = std::min((float)maxWidth, Gui::GetStringWidth(size, Text.substr(0, Text.find('\n'))));
}
}
if (fnt != nullptr) {
Gui::DrawString((currentScreen ? 200 : 160)+x-(widthScale/2), y+(lineHeight*line), size, color, Text.substr(0, Text.find('\n')), maxWidth, maxHeight, fnt);
} else {
Gui::DrawString((currentScreen ? 200 : 160)+x-(widthScale/2), y+(lineHeight*line), size, color, Text.substr(0, Text.find('\n')), maxWidth, maxHeight);
} }
Gui::DrawString((currentScreen ? 200 : 160)+x-(widthScale/2), y+(lineHeight*line), size, color, Text.substr(0, Text.find('\n')), maxWidth, maxHeight);
} }
// Draw String or Text. // Draw String or Text.
void Gui::DrawString(float x, float y, float size, u32 color, std::string Text, int maxWidth, int maxHeight) { void Gui::DrawString(float x, float y, float size, u32 color, std::string Text, int maxWidth, int maxHeight, C2D_Font fnt) {
C2D_Text c2d_text; C2D_Text c2d_text;
C2D_TextFontParse(&c2d_text, Font, TextBuf, Text.c_str());
if (fnt != nullptr) {
C2D_TextFontParse(&c2d_text, fnt, TextBuf, Text.c_str());
} else {
C2D_TextFontParse(&c2d_text, Font, TextBuf, Text.c_str());
}
C2D_TextOptimize(&c2d_text); C2D_TextOptimize(&c2d_text);
float heightScale; float heightScale;
if (maxHeight == 0) { if (maxHeight == 0) {
heightScale = size; heightScale = size;
} else { } else {
heightScale = std::min(size, size*(maxHeight/Gui::GetStringHeight(size, Text)));
if (fnt != nullptr) {
heightScale = std::min(size, size*(maxHeight/Gui::GetStringHeight(size, Text, fnt)));
} else {
heightScale = std::min(size, size*(maxHeight/Gui::GetStringHeight(size, Text)));
}
} }
if (maxWidth == 0) { if (maxWidth == 0) {
C2D_DrawText(&c2d_text, C2D_WithColor, x, y, 0.5f, size, heightScale, color); C2D_DrawText(&c2d_text, C2D_WithColor, x, y, 0.5f, size, heightScale, color);
} else { } else {
C2D_DrawText(&c2d_text, C2D_WithColor, x, y, 0.5f, std::min(size, size*(maxWidth/Gui::GetStringWidth(size, Text))), heightScale, color); if (fnt != nullptr) {
C2D_DrawText(&c2d_text, C2D_WithColor, x, y, 0.5f, std::min(size, size*(maxWidth/Gui::GetStringWidth(size, Text, fnt))), heightScale, color);
} else {
C2D_DrawText(&c2d_text, C2D_WithColor, x, y, 0.5f, std::min(size, size*(maxWidth/Gui::GetStringWidth(size, Text))), heightScale, color);
}
} }
} }
// Get String or Text Width. // Get String or Text Width.
float Gui::GetStringWidth(float size, std::string Text) { float Gui::GetStringWidth(float size, std::string Text, C2D_Font fnt) {
float width = 0; float width = 0;
GetStringSize(size, &width, NULL, Text); if (fnt != nullptr) {
GetStringSize(size, &width, NULL, Text, fnt);
} else {
GetStringSize(size, &width, NULL, Text);
}
return width; return width;
} }
// Get String or Text Size. // Get String or Text Size.
void Gui::GetStringSize(float size, float *width, float *height, std::string Text) { void Gui::GetStringSize(float size, float *width, float *height, std::string Text, C2D_Font fnt) {
C2D_Text c2d_text; C2D_Text c2d_text;
C2D_TextFontParse(&c2d_text, Font, TextBuf, Text.c_str()); if (fnt != nullptr) {
C2D_TextFontParse(&c2d_text, fnt, TextBuf, Text.c_str());
} else {
C2D_TextFontParse(&c2d_text, Font, TextBuf, Text.c_str());
}
C2D_TextGetDimensions(&c2d_text, size, size, width, height); C2D_TextGetDimensions(&c2d_text, size, size, width, height);
} }
// Get String or Text Height. // Get String or Text Height.
float Gui::GetStringHeight(float size, std::string Text) { float Gui::GetStringHeight(float size, std::string Text, C2D_Font fnt) {
float height = 0; float height = 0;
GetStringSize(size, NULL, &height, Text.c_str()); if (fnt != nullptr) {
GetStringSize(size, NULL, &height, Text.c_str(), fnt);
} else {
GetStringSize(size, NULL, &height, Text.c_str());
}
return height; return height;
} }
+15 -10
View File
@@ -53,16 +53,16 @@ namespace Gui
Result init(void); Result init(void);
/* Load a Font. (BCFNT) /* Load a Font. (BCFNT)
* sysFont: Whether the system font should be loaded instead of custom Font. * fnt: The C2D_Font variable which should be initialized.
* Path: Path to the BCFNT file. * Path: Path to the BCFNT file.
* if you're unsure, just call 'Gui::loadFont();' and it will load the system font. * if you're unsure, just call 'Gui::init();' and it will load the system font.
*/ */
Result loadFont(bool sysFont = true, const char * Path = ""); Result loadFont(C2D_Font &fnt, const char * Path = "");
/* Unload a Font. (BCFNT) /* Unload a Font. (BCFNT)
* Only use this if a custom Font has been loaded. * fnt: The C2D_Font variable which should be unloaded.
*/ */
Result unloadFont(); Result unloadFont(C2D_Font &fnt);
/* Load a spritesheet. /* Load a spritesheet.
* Path: Path to the SpriteSheet file. (T3X) * Path: Path to the SpriteSheet file. (T3X)
@@ -86,8 +86,9 @@ namespace Gui
* Text: The Text which should be displayed. * Text: The Text which should be displayed.
* maxWidth: The maxWidth for the Text. (Optional!) * maxWidth: The maxWidth for the Text. (Optional!)
* maxHeight: The maxHeight of the Text. (Optional!) * maxHeight: The maxHeight of the Text. (Optional!)
* fnt: The Font which should be used. Uses SystemFont by default. (Optional!)
*/ */
void DrawStringCentered(float x, float y, float size, u32 color, std::string Text, int maxWidth = 0, int maxHeight = 0); void DrawStringCentered(float x, float y, float size, u32 color, std::string Text, int maxWidth = 0, int maxHeight = 0, C2D_Font fnt = nullptr);
/* Draws a String. /* Draws a String.
* x: The X Position where the Text should be drawn. * x: The X Position where the Text should be drawn.
@@ -97,28 +98,32 @@ namespace Gui
* Text: The Text which should be displayed. * Text: The Text which should be displayed.
* maxWidth: The maxWidth for the Text. (Optional!) * maxWidth: The maxWidth for the Text. (Optional!)
* maxHeight: The maxHeight of the Text. (Optional!) * maxHeight: The maxHeight of the Text. (Optional!)
* fnt: The Font which should be used. Uses SystemFont by default. (Optional!)
*/ */
void DrawString(float x, float y, float size, u32 color, std::string Text, int maxWidth = 0, int maxHeight = 0); void DrawString(float x, float y, float size, u32 color, std::string Text, int maxWidth = 0, int maxHeight = 0, C2D_Font fnt = nullptr);
/* Get the width of a String. /* Get the width of a String.
* size: The size of the Text. * size: The size of the Text.
* Text: The Text where the width should be getted from. * Text: The Text where the width should be getted from.
* fnt: The Font which should be used. Uses SystemFont by default. (Optional!)
*/ */
float GetStringWidth(float size, std::string Text); float GetStringWidth(float size, std::string Text, C2D_Font fnt = nullptr);
/* Get the size of a String. /* Get the size of a String.
* size: The size of the Text. * size: The size of the Text.
* width: The width of the Text. * width: The width of the Text.
* height: The height of the Text. * height: The height of the Text.
* Text: The Text where the size should be getted from. * Text: The Text where the size should be getted from.
* fnt: The Font which should be used. Uses SystemFont by default. (Optional!)
*/ */
void GetStringSize(float size, float *width, float *height, std::string Text); void GetStringSize(float size, float *width, float *height, std::string Text, C2D_Font fnt = nullptr);
/* Get the height of a String. /* Get the height of a String.
* size: The size of the Text. * size: The size of the Text.
* Text: The Text where the height should be getted from. * Text: The Text where the height should be getted from.
* fnt: The Font which should be used. Uses SystemFont by default. (Optional!)
*/ */
float GetStringHeight(float size, std::string Text); float GetStringHeight(float size, std::string Text, C2D_Font fnt = nullptr);
/* Draw a Rectangle. /* Draw a Rectangle.
* x: X Position of the Rectangle. * x: X Position of the Rectangle.