diff --git a/README.md b/README.md index 5e634d9..5842a7d 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ This contains the GUI & Core part for our DS(i) Applications. #define the following in `UNIVCORE_CONFIG.h` to alter specific things. - `UNIVCORE_TEXT_BUFFERED` to have the text be first rendered to a buffer before drawing to the screen with Font::update() - `UNIVCORE_3DS_SIZE` to make all X and Y positions use the 3DS resolution +- `UNIVCORE_FONT_COUNT` the number of font sizes to load, default 3 (Large, Medium, Small) Logo created by: [TotallyNotGuy](https://github.com/TotallyNotGuy). diff --git a/include/gui.hpp b/include/gui.hpp index 14e5dbc..ac11f41 100644 --- a/include/gui.hpp +++ b/include/gui.hpp @@ -37,9 +37,13 @@ #include "UNIVCORE_CONFIG.h" #ifdef UNIVCORE_3DS_SIZE -#define SCALE_3DS(Pos) Pos = (Pos * 4 / 5) + #define SCALE_3DS(Pos) Pos = (Pos * 4 / 5) #else -#define SCALE_3DS(Pos) + #define SCALE_3DS(Pos) +#endif + +#ifndef UNIVCORE_FONT_COUNT + #define UNIVCORE_FONT_COUNT 3 #endif // TODO: Maybe change these, they need to be C2D's flags atm to be compatible with 3DS Universal-Core @@ -93,9 +97,9 @@ namespace Gui { Initialize the GUI. Call this when initializing. - FontPaths: The locations to check for the font. + FontPaths: The locations to check for the fonts. */ - bool init(const std::vector &FontPaths); + bool init(const std::array, UNIVCORE_FONT_COUNT> &FontPaths); /* Load a Font. (NFTR) @@ -109,15 +113,15 @@ namespace Gui { /* Reinit the GUI. - FontPaths: The locations to check for the font. + FontPaths: The locations to check for the fonts. */ - bool reinit(const std::vector &FontPaths); + bool reinit(const std::array, UNIVCORE_FONT_COUNT> &FontPaths); /* Draws a centered String. x: The X Offset from center. (Center: 200 px on top, 160 px on Bottom.) y: The Y Position of the Text. - size: The size of the Text. + size: The size of the Text, unused when custom font used. color: The color of the Text. Text: The Text which should be displayed. maxWidth: The maxWidth for the Text. (Optional!) @@ -125,7 +129,7 @@ namespace Gui { fnt: The Font which should be used. Uses default font by default. (Optional!) int flags: C2D text flags to use. (Optional!) */ - void DrawStringCentered(int x, int y, float size, u8 color, const std::string &Text, int maxWidth = 0, int maxHeight = 0, Font *fnt = nullptr, int flags = 0); + void DrawStringCentered(int x, int y, u8 size, u8 color, const std::string &Text, float maxWidth = 0.0f, float maxHeight = 0.0f, Font *fnt = nullptr, int flags = 0); /* Draws a String. @@ -140,7 +144,7 @@ namespace Gui { fnt: The Font which should be used. Uses default font by default. (Optional!) flags: C2D text flags to use. */ - void DrawString(int x, int y, float size, u8 color, const std::string &Text, int maxWidth = 0, int maxHeight = 0, Font *fnt = nullptr, int flags = 0); + void DrawString(int x, int y, u8 size, u8 color, const std::string &Text, float maxWidth = 0.0f, float maxHeight = 0.0f, Font *fnt = nullptr, int flags = 0); /* Get the width of a String. @@ -149,7 +153,7 @@ namespace Gui { Text: The Text where the width should be getted from. fnt: The Font which should be used. Uses default font by default. (Optional!) */ - int GetStringWidth(float size, const std::string &Text, Font *fnt = nullptr); + int GetStringWidth(u8 size, const std::string &Text, Font *fnt = nullptr); /* Get the height of a String. @@ -158,7 +162,7 @@ namespace Gui { Text: The Text where the height should be getted from. fnt: The Font which should be used. Uses default font by default. (Optional!) */ - int GetStringHeight(float size, const std::string &Text, Font *fnt = nullptr); + int GetStringHeight(u8 size, const std::string &Text, Font *fnt = nullptr); /* @@ -170,7 +174,7 @@ namespace Gui { Text: The Text where the size should be getted from. fnt: The Font which should be used. Uses default font by default. (Optional!) */ - void GetStringSize(float size, int *width, int *height, const std::string &Text, Font *fnt = nullptr); + void GetStringSize(u8 size, int *width, int *height, const std::string &Text, Font *fnt = nullptr); /* Draw a Rectangle. diff --git a/source/gui.cpp b/source/gui.cpp index e7c18a4..3c1fa41 100644 --- a/source/gui.cpp +++ b/source/gui.cpp @@ -34,7 +34,7 @@ #define ANIMATED_SELECTOR_COLOR 0xFF -std::unique_ptr DefaultFont; +std::array, UNIVCORE_FONT_COUNT> DefaultFonts; std::unique_ptr usedScreen, tempScreen; // tempScreen used for "fade" effects. std::stack> screens; bool currentScreen = false; @@ -44,7 +44,7 @@ int fadecolor = 0; bool widescreen = false; int selectorTimer = 0; -bool Gui::init(const std::vector &FontPaths) { +bool Gui::init(const std::array, UNIVCORE_FONT_COUNT> &FontPaths) { // Initialize video mode videoSetMode(MODE_5_2D); videoSetModeSub(MODE_5_2D); @@ -74,7 +74,9 @@ bool Gui::init(const std::vector &FontPaths) { REG_BLDCNT_SUB = 1 << 11; // Load the default font - DefaultFont = std::make_unique(FontPaths); + for(size_t i = 0; i < DefaultFonts.size(); i++) { + DefaultFonts[i] = std::make_unique(FontPaths[i]); + } return true; } @@ -84,11 +86,11 @@ void Gui::clearScreen(bool top) { } void Gui::clearTextBufs(void) { - DefaultFont->clear(); + Font::clear(); } void Gui::updateTextBufs(bool top) { - DefaultFont->update(top); + Font::update(top); } void Gui::DrawSprite(Spritesheet &sheet, size_t imgindex, int x, int y, float ScaleX, float ScaleY) { @@ -108,42 +110,45 @@ bool Gui::loadSheet(const char *Path, Spritesheet &sheet) { return true; } -bool Gui::reinit(const std::vector &FontPaths) { +bool Gui::reinit(const std::array, UNIVCORE_FONT_COUNT> &FontPaths) { return Gui::init(FontPaths); } -void Gui::DrawStringCentered(int x, int y, float size, u8 color, const std::string &Text, int maxWidth, int maxHeight, Font *fnt, int flags) { +void Gui::DrawStringCentered(int x, int y, u8 size, u8 color, const std::string &Text, float maxWidth, float maxHeight, Font *fnt, int flags) { Gui::DrawString(x, y, size, color, Text, maxWidth, maxHeight, fnt, flags | C2D_AlignCenter); } -void Gui::DrawString(int x, int y, float size, u8 color, const std::string &Text, int maxWidth, int maxHeight, Font *fnt, int flags) { - float heightScale = maxHeight == 0 ? size : std::min(size, size * (maxHeight / Gui::GetStringHeight(size, Text, fnt))); - float widthScale = maxWidth == 0 ? size : std::min(size, size * (maxWidth / Gui::GetStringWidth(size, Text, fnt))); +void Gui::DrawString(int x, int y, u8 size, u8 color, const std::string &Text, float maxWidth, float maxHeight, Font *fnt, int flags) { + if (!fnt && size >= DefaultFonts.size()) return; + + float heightScale = maxHeight == 0 ? 1.0f : std::min(1.0f, maxHeight / Gui::GetStringHeight(size, Text, fnt)); + float widthScale = maxWidth == 0 ? 1.0f : std::min(1.0f, maxWidth / Gui::GetStringWidth(size, Text, fnt)); // TODO: Wrapping and such - if(fnt) - fnt->print(x, y, Text, flags & C2D_AlignCenter ? Alignment::center : (flags & C2D_AlignRight ? Alignment::right : Alignment::left), color, maxWidth, widthScale, heightScale); - else - DefaultFont->print(x, y, Text, flags & C2D_AlignCenter ? Alignment::center : (flags & C2D_AlignRight ? Alignment::right : Alignment::left), color, maxWidth, widthScale, heightScale); + (fnt ? *fnt : *DefaultFonts[size]).print(x, y, Text, flags & C2D_AlignCenter ? Alignment::center : (flags & C2D_AlignRight ? Alignment::right : Alignment::left), color, maxWidth, widthScale, heightScale); } -int Gui::GetStringWidth(float size, const std::string &Text, Font *fnt) { +int Gui::GetStringWidth(u8 size, const std::string &Text, Font *fnt) { if(fnt) - return fnt->calcWidth(Text) * size; - else - return DefaultFont->calcWidth(Text) * size; + return fnt->calcWidth(Text); + else if(size < DefaultFonts.size()) + return DefaultFonts[size]->calcWidth(Text); + + return 0; } -int Gui::GetStringHeight(float size, const std::string &Text, Font *fnt) { +int Gui::GetStringHeight(u8 size, const std::string &Text, Font *fnt) { const int lines = 1 + std::count(Text.begin(), Text.end(), '\n'); if(fnt) - return fnt->height() * lines * size; - else - return DefaultFont->height() * lines * size; + return fnt->height() * lines; + else if(size < DefaultFonts.size()) + return DefaultFonts[size]->height() * lines; + + return 0; } -void Gui::GetStringSize(float size, int *width, int *height, const std::string &Text, Font *fnt) { +void Gui::GetStringSize(u8 size, int *width, int *height, const std::string &Text, Font *fnt) { if(width) *width = GetStringWidth(size, Text, fnt);