From 30b04fa488e0d5da7f6c75131d0b743de545e3d9 Mon Sep 17 00:00:00 2001 From: StackZ <47382115+SuperSaiyajinStackZ@users.noreply.github.com> Date: Mon, 19 Oct 2020 11:12:20 +0200 Subject: [PATCH] Some changes, see description for more. - [GUI]: Add `std::string Gui::WrapText(...);` - [GUI]: Some cleanup i guess. - Remove HID part, cause it wasn't used anyways. --- gui.cpp | 496 +++++++++++++++++++++++++++++++++-------------- gui.hpp | 263 +++++++++++++++---------- hid.cpp | 61 ------ hid.hpp | 75 ------- screenCommon.hpp | 4 +- 5 files changed, 508 insertions(+), 391 deletions(-) delete mode 100644 hid.cpp delete mode 100644 hid.hpp diff --git a/gui.cpp b/gui.cpp index 68bd441..180163f 100644 --- a/gui.cpp +++ b/gui.cpp @@ -30,10 +30,9 @@ #include <3ds.h> #include #include +#include -C3D_RenderTarget* Top; -C3D_RenderTarget* TopRight; -C3D_RenderTarget* Bottom; +C3D_RenderTarget *Top, *TopRight, *Bottom; C2D_TextBuf TextBuf; C2D_Font Font; @@ -44,269 +43,430 @@ bool fadeout = false, fadein = false, fadeout2 = false, fadein2 = false; int fadealpha = 0; int fadecolor = 0; -// Clear Text. -void Gui::clearTextBufs(void) { C2D_TextBufClear(TextBuf); } +/* + Clear the Text Buffer. +*/ +void Gui::clearTextBufs(void) { C2D_TextBufClear(TextBuf); }; -// Draw a sprite from the sheet. +/* + Draw a sprite from the sheet. + + C2D_SpriteSheet sheet: The SpriteSheet. + size_t imgindex: The image index. + int x: The X-Position where to draw the sprite. + int y: The Y-Position where to draw the sprite. + float ScaleX: The X-Scale of the sprite. + float ScaleY: The Y-Scale of the sprite. + + If the spritesheet is nullptr or image index goes out of scope, this doesn't do anything. +*/ void Gui::DrawSprite(C2D_SpriteSheet sheet, size_t imgindex, int x, int y, float ScaleX, float ScaleY) { - if (sheet != nullptr) { + if (sheet) { if (C2D_SpriteSheetCount(sheet) >= imgindex) { C2D_DrawImageAt(C2D_SpriteSheetGetImage(sheet, imgindex), x, y, 0.5f, nullptr, ScaleX, ScaleY); } } } -// Initialize GUI. +/* + Initialize the GUI. + + Contains initializing Citro2D, Citro3D and the screen targets. + Call this when initing the app. +*/ Result Gui::init(void) { C3D_Init(C3D_DEFAULT_CMDBUF_SIZE); C2D_Init(C2D_DEFAULT_MAX_OBJECTS); C2D_Prepare(); - // Create Screen Targets. + + /* Create Screen Targets. */ Top = C2D_CreateScreenTarget(GFX_TOP, GFX_LEFT); TopRight = C2D_CreateScreenTarget(GFX_TOP, GFX_RIGHT); Bottom = C2D_CreateScreenTarget(GFX_BOTTOM, GFX_LEFT); - // Load Textbuffer. + + /* Load Textbuffer. */ TextBuf = C2D_TextBufNew(4096); Font = C2D_FontLoadSystem(CFG_REGION_USA); // Load System font. return 0; } -// Load a Font. -Result Gui::loadFont(C2D_Font &fnt, const char* Path) { - if (access(Path, F_OK) == 0) fnt = C2D_FontLoad(Path); // Only load if found. +/* + Load a bcfnt font. + + C2D_Font &fnt: The reference to the C2D_Font variable. + const char *Path: The path to the file. +*/ +Result Gui::loadFont(C2D_Font &fnt, const char *Path) { + if (access(Path, F_OK) == 0) fnt = C2D_FontLoad(Path); // Only load if found. + return 0; } -// Unload a Font. +/* + Unload a Font. + + C2D_Font &fnt: The reference to the C2D_Font variable. +*/ Result Gui::unloadFont(C2D_Font &fnt) { - if (fnt != nullptr) { - C2D_FontFree(fnt); // Make sure to only unload if not nullptr. - } + if (fnt) C2D_FontFree(fnt); // Make sure to only unload if not nullptr. + return 0; } -// Load a Sheet. -Result Gui::loadSheet(const char* Path, C2D_SpriteSheet &sheet) { - if (access(Path, F_OK) == 0) sheet = C2D_SpriteSheetLoad(Path); // Only load if found. +/* + Load a t3x SpriteSheet. + + const char *Path: The path to the file. + C2D_SpriteSheet &sheet: The reference to the C2D_SpriteSheet variable. +*/ +Result Gui::loadSheet(const char *Path, C2D_SpriteSheet &sheet) { + if (access(Path, F_OK) == 0) sheet = C2D_SpriteSheetLoad(Path); // Only load if found. + return 0; } -// Unload a Sheet. +/* + Unload a SpriteSheet. + + C2D_SpriteSheet &sheet: The reference to the C2D_SpriteSheet variable. +*/ Result Gui::unloadSheet(C2D_SpriteSheet &sheet) { - if (sheet != nullptr) C2D_SpriteSheetFree(sheet); // Make sure to only unload if not nullptr. + if (sheet) C2D_SpriteSheetFree(sheet); // Make sure to only unload if not nullptr. + return 0; } -// Exit the GUI. +/* + Exit the GUI. + + Contains deinitializing Citro2D, Citro3D and clearing the textbuffer. + Call this when exiting the app. +*/ void Gui::exit(void) { C2D_TextBufDelete(TextBuf); C2D_Fini(); C3D_Fini(); - if (usedScreen != nullptr) usedScreen = nullptr; + if (usedScreen) usedScreen = nullptr; } -// Reinitialize GUI. +/* + Reinitialize the GUI. +*/ Result Gui::reinit(void) { C2D_TextBufDelete(TextBuf); C2D_Fini(); C3D_Fini(); - return init(); + return Gui::init(); } -// Draw a Centered String. +/* + Draw a Centered String. + + float x: The X-Addition offset for the position from 200 (top) or 160 (bottom). + float y: The Y-Position where to draw. + float size: The size for the Font. + u32 color: The Text Color. + std::string Text: The Text which should be drawn. + int maxWidth: (Optional) The max width of the Text. + int maxHeight: (Optional) The max height of the Text. + C2D_Font fnt: (Optional) The wanted C2D_Font. Is nullptr by default. +*/ void Gui::DrawStringCentered(float x, float y, float size, u32 color, std::string Text, int maxWidth, int maxHeight, C2D_Font fnt) { float lineHeight, widthScale; - // Check for the lineHeight. - if (fnt != nullptr) { - lineHeight = Gui::GetStringHeight(size, " ", fnt); - } else { - lineHeight = Gui::GetStringHeight(size, " "); - } + /* Check for the lineHeight. */ + if (fnt) lineHeight = Gui::GetStringHeight(size, " ", fnt); + else lineHeight = Gui::GetStringHeight(size, " "); int line = 0; + while(Text.find('\n') != Text.npos) { if (maxWidth == 0) { - // 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'))); - } + /* Do the widthScale. */ + if (fnt) widthScale = Gui::GetStringWidth(size, Text.substr(0, Text.find('\n')), fnt); + else widthScale = Gui::GetStringWidth(size, Text.substr(0, Text.find('\n'))); } else { - // 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')))); - } - + /* Do the widthScale 2. */ + if (fnt) 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); - } + if (fnt) 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); line++; } if (maxWidth == 0) { - // 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'))); - } + /* Do the next WidthScale. */ + if (fnt) widthScale = Gui::GetStringWidth(size, Text.substr(0, Text.find('\n')), fnt); + else widthScale = Gui::GetStringWidth(size, Text.substr(0, Text.find('\n'))); } else { - // 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')))); - } + /* And again. */ + if (fnt) 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); - } + if (fnt) 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); } -// Draw String or Text. +/* + Draw a String. + + float x: The X-Position where to draw. + float y: The Y-Position where to draw. + float size: The size for the Font. + u32 color: The Text Color. + std::string Text: The Text which should be drawn. + int maxWidth: (Optional) The max width of the Text. + int maxHeight: (Optional) The max height of the Text. + C2D_Font fnt: (Optional) The wanted C2D_Font. Is nullptr by default. +*/ 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; - if (fnt != nullptr) { - C2D_TextFontParse(&c2d_text, fnt, TextBuf, Text.c_str()); - } else { - C2D_TextFontParse(&c2d_text, Font, TextBuf, Text.c_str()); - } + if (fnt) C2D_TextFontParse(&c2d_text, fnt, TextBuf, Text.c_str()); + else C2D_TextFontParse(&c2d_text, Font, TextBuf, Text.c_str()); C2D_TextOptimize(&c2d_text); float heightScale; + if (maxHeight == 0) { heightScale = size; + } else { - - 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 (fnt) 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) { C2D_DrawText(&c2d_text, C2D_WithColor, x, y, 0.5f, size, heightScale, color); + } else { - 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); - } + if (fnt) 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 size: The Textsize. + std::string Text: The Text. + C2D_Font fnt: (Optional) The wanted C2D_Font. Is nullptr by default. +*/ float Gui::GetStringWidth(float size, std::string Text, C2D_Font fnt) { float width = 0; - if (fnt != nullptr) { - GetStringSize(size, &width, NULL, Text, fnt); - } else { - GetStringSize(size, &width, NULL, Text); - } + + if (fnt) GetStringSize(size, &width, NULL, Text, fnt); + else GetStringSize(size, &width, NULL, Text); + return width; } -// Get String or Text Size. +/* + Get String or Text Size. + + float size: The Textsize. + float *width: Pointer where to store the width. + float *height: Pointer where to store the height. + std::string Text: The Text. + C2D_Font fnt: (Optional) The wanted C2D_Font. Is nullptr by default. +*/ void Gui::GetStringSize(float size, float *width, float *height, std::string Text, C2D_Font fnt) { C2D_Text c2d_text; - if (fnt != nullptr) { - C2D_TextFontParse(&c2d_text, fnt, TextBuf, Text.c_str()); - } else { - C2D_TextFontParse(&c2d_text, Font, TextBuf, Text.c_str()); - } + + if (fnt) 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); } -// Get String or Text Height. +/* + Get String or Text Height. + + float size: The Textsize. + std::string Text: The Text. + C2D_Font fnt: (Optional) The wanted C2D_Font. Is nullptr by default. +*/ float Gui::GetStringHeight(float size, std::string Text, C2D_Font fnt) { float height = 0; - if (fnt != nullptr) { - GetStringSize(size, NULL, &height, Text.c_str(), fnt); - } else { - GetStringSize(size, NULL, &height, Text.c_str()); - } + + if (fnt) GetStringSize(size, NULL, &height, Text.c_str(), fnt); + else GetStringSize(size, NULL, &height, Text.c_str()); + return height; } -// Draw a Rectangle. +/* + Wrap long Text into a string. This code is based of: + https://github.com/DS-Homebrew/TWiLightMenu/blob/master/settings/arm9/source/settingsgui.cpp#L151. + + const std::string &text: The Text which should be wrapped. + float TextSize: The Textsize. + int maxWidth: The max width for wrapping. + + NOTE: Only call this once! This does do a lot of calls with while, for loops etc. +*/ +std::string Gui::WrapText(const std::string &text, float Textsize, int maxWidth) { + std::string result, temp, _resultStr = text; + std::vector words; + std::size_t pos; + + /* Process comment to stay within the maxWidth. */ + while((pos = _resultStr.find(' ')) != std::string::npos) { + words.push_back(_resultStr.substr(0, pos)); + _resultStr = _resultStr.substr(pos + 1); + } + + if (_resultStr.size()) words.push_back(_resultStr); + + for(auto word : words) { + /* Split word if the word is too long for a line. */ + const int width = Gui::GetStringWidth(Textsize, word); + + if (width > maxWidth) { + if (temp.length()) { + result += temp + "\n"; + temp = ""; + } + + for(int i = 0; i < width / maxWidth; i++) { + word.insert((float)((i + 1) * word.length()) / ((width / maxWidth) + 1), "\n"); + } + + result += word + "\n"; + continue; + } + + const int width2 = Gui::GetStringWidth(Textsize, temp + " " + word); + + if (width2 > 240) { + result += temp + "\n"; + temp = word; + + } else { + temp += " " + word; + } + } + + if (temp.size()) result += temp; + + /* Ensure there are no newlines at the beginning or end. */ + while(result[0] == '\n') result = result.substr(1); + while(result[result.length() - 1] == '\n') result = result.substr(0, result.length() - 2); + + return result; +} + +/* + Draw a Rectangle. + + float x: The X-Position where to draw. + float y: The Y-Position where to draw. + float w: The width of the rectangle. + float h: The height of the rectangle. + u32 color: The color. +*/ bool Gui::Draw_Rect(float x, float y, float w, float h, u32 color) { return C2D_DrawRectSolid(x, y, 0.5f, w, h, color); } -// Draw's the current screen's draw. +/* + Draw's the current screen's draw. + + bool stack: If using the stack-screens or not. +*/ void Gui::DrawScreen(bool stack) { if (!stack) { - if (usedScreen != nullptr) usedScreen->Draw(); + if (usedScreen) usedScreen->Draw(); + } else { - if (!screens.empty()) screens.top()->Draw(); + if (!screens.empty()) screens.top()->Draw(); } } -// Do the current screen's logic. +/* + Do the current screen's logic. + + u32 hDown: The hidKeysDown() variable. + u32 hHeld: The hidKeysHeld() variable. + touchPosition touch: The touchPosition variable. + bool waitFade: If waiting for the fade until control of the screen or not. + bool stack: If using the stack-screens. +*/ void Gui::ScreenLogic(u32 hDown, u32 hHeld, touchPosition touch, bool waitFade, bool stack) { if (waitFade) { if (!fadein && !fadeout && !fadein2 && !fadeout2) { - if (!stack) { - if (usedScreen != nullptr) usedScreen->Logic(hDown, hHeld, touch); - } else { - if (!screens.empty()) screens.top()->Logic(hDown, hHeld, touch); - } + if (!stack) if (usedScreen) usedScreen->Logic(hDown, hHeld, touch); + + } else { + if (!screens.empty()) screens.top()->Logic(hDown, hHeld, touch); } + } else { if (!stack) { - if (usedScreen != nullptr) usedScreen->Logic(hDown, hHeld, touch); + if (usedScreen) usedScreen->Logic(hDown, hHeld, touch); + } else { - if (!screens.empty()) screens.top()->Logic(hDown, hHeld, touch); + if (!screens.empty()) screens.top()->Logic(hDown, hHeld, touch); } } } -// Move's the tempScreen to the used one. +/* + Move's the tempScreen to the used one. + + bool stack: If using the stack-screens or not. +*/ void Gui::transferScreen(bool stack) { if (!stack) { - if (tempScreen != nullptr) usedScreen = std::move(tempScreen); + if (tempScreen) usedScreen = std::move(tempScreen); + } else { - if (tempScreen != nullptr) screens.push(std::move(tempScreen)); + if (tempScreen) screens.push(std::move(tempScreen)); } } -// Set the current Screen. -void Gui::setScreen(std::unique_ptr screen, bool fade, bool stack) { +/* + Set the current Screen. + + std::unique_ptr screen: The screen class. + bool fade: If doing a fade effect or not. + bool stack: If using the stack-screens or not. +*/ +void Gui::setScreen(std::unique_ptr screen, bool fade, bool stack) { tempScreen = std::move(screen); - // Switch screen without fade. + + /* Switch screen without fade. */ if (!fade) { Gui::transferScreen(stack); + } else { - // Fade, then switch. + /* Fade, then switch. */ fadeout = true; } } -// Fade's the screen in and out and transfer the screen. -// Credits goes to RocketRobz & SavvyManager. +/* + Fade's the screen in and out and transfer the screen. + Credits goes to RocketRobz & SavvyManager. + + int fadeoutFrames: The fadeout frames. + int fadeinFrames: The fadein frames. + bool stack: If using the stack-screens or not. (Used to properly transfer screens). +*/ void Gui::fadeEffects(int fadeoutFrames, int fadeinFrames, bool stack) { if (fadein) { fadealpha -= fadeinFrames; + if (fadealpha < 0) { fadealpha = 0; fadecolor = 255; @@ -317,6 +477,7 @@ void Gui::fadeEffects(int fadeoutFrames, int fadeinFrames, bool stack) { if (stack) { if (fadein2) { fadealpha -= fadeinFrames; + if (fadealpha < 0) { fadealpha = 0; fadecolor = 255; @@ -327,6 +488,7 @@ void Gui::fadeEffects(int fadeoutFrames, int fadeinFrames, bool stack) { if (fadeout) { fadealpha += fadeoutFrames; + if (fadealpha > 255) { fadealpha = 255; Gui::transferScreen(stack); // Transfer Temp screen to the stack / used one. @@ -338,6 +500,7 @@ void Gui::fadeEffects(int fadeoutFrames, int fadeinFrames, bool stack) { if (stack) { if (fadeout2) { fadealpha += fadeoutFrames; + if (fadealpha > 255) { fadealpha = 255; Gui::screenBack2(); // Go screen back. @@ -348,34 +511,68 @@ void Gui::fadeEffects(int fadeoutFrames, int fadeinFrames, bool stack) { } } -// Go a screen back. (Stack only!) +/* + Go a screen back. (Stack only!) + + bool fade: If doing a fade or not. +*/ void Gui::screenBack(bool fade) { if (!fade) { - if (screens.size() > 0) screens.pop(); + if (screens.size() > 0) screens.pop(); + } else { - if (screens.size() > 0) fadeout2 = true; + if (screens.size() > 0) fadeout2 = true; } } -void Gui::screenBack2() { if (screens.size() > 0) screens.pop(); } +void Gui::screenBack2() { if (screens.size() > 0) screens.pop(); }; -// Select, on which Screen should be drawn. -void Gui::ScreenDraw(C3D_RenderTarget * screen) { +/* + Select, on which Screen should be drawn. + + C3D_RenderTarget *screen: The render target. +*/ +void Gui::ScreenDraw(C3D_RenderTarget *screen) { C2D_SceneBegin(screen); currentScreen = (screen == Top || screen == TopRight) ? 1 : 0; } -void Gui::drawGrid(float xPos, float yPos, float Width, float Height, u32 color) { - static constexpr int w = 1; - // BG Color for the Grid. (Transparent.) - C2D_DrawRectSolid(xPos, yPos, 0.5, Width, Height, C2D_Color32(0, 0, 0, 0)); +/* + Draw a grid. - // Grid part. - C2D_DrawRectSolid(xPos, yPos, 0.5, Width, w, color); // top - C2D_DrawRectSolid(xPos, yPos + w, 0.5, w, Height - 2 * w, color); // left - C2D_DrawRectSolid(xPos + Width - w, yPos + w, 0.5, w, Height - 2 * w, color); // right - C2D_DrawRectSolid(xPos, yPos + Height - w, 0.5, Width, w, color); // bottom + float xPos: The X-Position of the grid. + float yPos: The Y-Position of the grid. + float Width: The width of the grid. + float Height: The height of the grid. + u32 color: The color of the grid. + u32 bgColor: The BG Color of the grid. +*/ +void Gui::drawGrid(float xPos, float yPos, float Width, float Height, u32 color, u32 bgColor) { + static constexpr int w = 1; + + /* BG Color for the Grid. (Transparent by default.) */ + Gui::Draw_Rect(xPos, yPos, Width, Height, bgColor); + + /* Grid part, Top. */ + Gui::Draw_Rect(xPos, yPos, Width, w, color); + /* Left. */ + Gui::Draw_Rect(xPos, yPos + w, w, Height - 2 * w, color); + /* Right. */ + Gui::Draw_Rect(xPos + Width - w, yPos + w, w, Height - 2 * w, color); + /* Bottom. */ + Gui::Draw_Rect(xPos, yPos + Height - w, Width, w, color); } +/* + Draw an animated selector. + + float xPos: The X-Position of the selector. + float yPos: The Y-Position of the selector. + float Width: The width of the selector. + float Height: The height of the selector. + float speed: The selector animation speed. Use .030f or so. + u32 SelectorColor: The selector fade color. + u32 bgColor: The selector BG color. +*/ void Gui::drawAnimatedSelector(float xPos, float yPos, float Width, float Height, float speed, u32 SelectorColor, u32 bgColor) { static constexpr int w = 2; static float timer = 0.0f; @@ -385,14 +582,17 @@ void Gui::drawAnimatedSelector(float xPos, float yPos, float Width, float Height u8 b = (SelectorColor >> 16) & 0xFF; u32 color = C2D_Color32(r + (255 - r) * highlight_multiplier, g + (255 - g) * highlight_multiplier, b + (255 - b) * highlight_multiplier, 255); - // BG Color for the Selector. - C2D_DrawRectSolid(xPos, yPos, 0.5, Width, Height, bgColor); + /* BG Color for the Selector. */ + Gui::Draw_Rect(xPos, yPos, Width, Height, bgColor); - // Animated Selector part. - C2D_DrawRectSolid(xPos, yPos, 0.5, Width, w, color); // top - C2D_DrawRectSolid(xPos, yPos + w, 0.5, w, Height - 2 * w, color); // left - C2D_DrawRectSolid(xPos + Width - w, yPos + w, 0.5, w, Height - 2 * w, color); // right - C2D_DrawRectSolid(xPos, yPos + Height - w, 0.5, Width, w, color); // bottom + /* Selector part, Top. */ + Gui::Draw_Rect(xPos, yPos, Width, w, color); + /* Left. */ + Gui::Draw_Rect(xPos, yPos + w, w, Height - 2 * w, color); + /* Right. */ + Gui::Draw_Rect(xPos + Width - w, yPos + w, w, Height - 2 * w, color); + /* Bottom. */ + Gui::Draw_Rect(xPos, yPos + Height - w, Width, w, color); - timer += speed; // Speed of the animation. Example : .030f / .030 + timer += speed; } \ No newline at end of file diff --git a/gui.hpp b/gui.hpp index c325fa5..efe4f27 100644 --- a/gui.hpp +++ b/gui.hpp @@ -34,167 +34,222 @@ #include namespace Gui { - // Clear the Text Buffer. + /* + Clear the Text Buffer. + */ void clearTextBufs(void); - /* Draw a sprite from a SpriteSheet. - * sheet: The SpriteSheet which should be used. - * imgIndex: The index of the sprite from the sheet which should be drawn. - * x: The X Position where the sprite should be drawn. - * y: The Y Position where the sprite should be drawn. - * ScaleX: The X-Scale for the sprite. (Optional!) - * ScaleY: The Y-Scale for the sprite. (Optional!) + /* + Draw a sprite from a SpriteSheet. + sheet: The SpriteSheet which should be used. + imgIndex: The index of the sprite from the sheet which should be drawn. + x: The X Position where the sprite should be drawn. + y: The Y Position where the sprite should be drawn. + ScaleX: The X-Scale for the sprite. (Optional!) + ScaleY: The Y-Scale for the sprite. (Optional!) */ void DrawSprite(C2D_SpriteSheet sheet, size_t imgindex, int x, int y, float ScaleX = 1, float ScaleY = 1); - // Initialize the GUI with Citro2D & Citro3D and initialize the Textbuffer. (call this when initializing.) + /* + Initialize the GUI with Citro2D & Citro3D and initialize the Textbuffer. + call this when initializing. + */ Result init(void); - /* Load a Font. (BCFNT) - * fnt: The C2D_Font variable which should be initialized. - * Path: Path to the BCFNT file. - * if you're unsure, just call 'Gui::init();' and it will load the system font. - */ - Result loadFont(C2D_Font &fnt, const char * Path = ""); + /* + Load a Font. (BCFNT) - /* Unload a Font. (BCFNT) - * fnt: The C2D_Font variable which should be unloaded. + fnt: The C2D_Font variable which should be initialized. + Path: Path to the BCFNT file. + if you're unsure, just call 'Gui::init();' and it will load the system font. + */ + Result loadFont(C2D_Font &fnt, const char *Path = ""); + + /* + Unload a Font. (BCFNT) + + fnt: The C2D_Font variable which should be unloaded. */ Result unloadFont(C2D_Font &fnt); - /* Load a spritesheet. - * Path: Path to the SpriteSheet file. (T3X) - * sheet: Reference to the C2D_SpriteSheet declaration. - */ - Result loadSheet(const char* Path, C2D_SpriteSheet &sheet); + /* + Load a spritesheet. - /* Unload a spritesheet. - * sheet: Reference to the C2D_SpriteSheet which should be free'd. + Path: Path to the SpriteSheet file. (T3X) + sheet: Reference to the C2D_SpriteSheet declaration. + */ + Result loadSheet(const char *Path, C2D_SpriteSheet &sheet); + + /* + Unload a spritesheet. + + sheet: Reference to the C2D_SpriteSheet which should be free'd. */ Result unloadSheet(C2D_SpriteSheet &sheet); - // Exit the GUI. (Call this at exit.) + /* + Exit the GUI. + Call this at exit. + */ void exit(void); - - // Reinit the GUI. + + /* + Reinit the GUI. + */ Result reinit(void); - /* 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. - * color: The Color of the Text. - * Text: The Text which should be displayed. - * maxWidth: The maxWidth for the Text. (Optional!) - * maxHeight: The maxHeight of the Text. (Optional!) - * fnt: The Font which should be used. Uses SystemFont by default. (Optional!) + /* + 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. + color: The Color of the Text. + Text: The Text which should be displayed. + maxWidth: The maxWidth for 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, C2D_Font fnt = nullptr); - /* Draws a String. - * x: The X Position where the Text should be drawn. - * y: The Y Position where the Text should be drawn. - * size: The size of the Text. - * color: The Color of the Text. - * Text: The Text which should be displayed. - * maxWidth: The maxWidth for the Text. (Optional!) - * maxHeight: The maxHeight of the Text. (Optional!) - * fnt: The Font which should be used. Uses SystemFont by default. (Optional!) + /* + Draws a String. + + x: The X Position where the Text should be drawn. + y: The Y Position where the Text should be drawn. + size: The size of the Text. + color: The Color of the Text. + Text: The Text which should be displayed. + maxWidth: The maxWidth for 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, C2D_Font fnt = nullptr); - /* Get the width of a String. - * size: The size of the Text. - * Text: The Text where the width should be getted from. - * fnt: The Font which should be used. Uses SystemFont by default. (Optional!) + /* + Get the width of a String. + + size: The size of the Text. + 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, C2D_Font fnt = nullptr); - /* Get the size of a String. - * size: The size of the Text. - * width: The width of the Text. - * height: The height of the Text. - * Text: The Text where the size should be getted from. - * fnt: The Font which should be used. Uses SystemFont by default. (Optional!) + /* + Get the size of a String. + + size: The size of the Text. + width: The width of the Text. + height: The height of the Text. + 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, C2D_Font fnt = nullptr); - /* Get the height of a String. - * size: The size of the Text. - * Text: The Text where the height should be getted from. - * fnt: The Font which should be used. Uses SystemFont by default. (Optional!) + /* + Get the height of a String. + + size: The size of the Text. + 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, C2D_Font fnt = nullptr); - /* Draw a Rectangle. - * x: X Position of the Rectangle. - * y: Y Position of the Rectangle. - * w: The width of the rectangle. - * h: The height of the rectangle. - * color: The color of the rectangle. + /* + Wrap a long string into a wrapped string. + + const std::string &text: The Text which should be wrapped. + float TextSize: The Textsize. + int maxWidth: The max width for wrapping. + + NOTE: Only call this once! This does do a lot of calls with while, for loops etc. + */ + std::string WrapText(const std::string &text, float Textsize, int maxWidth); + + /* + Draw a Rectangle. + + x: X Position of the Rectangle. + y: Y Position of the Rectangle. + w: The width of the rectangle. + h: The height of the rectangle. + color: The color of the rectangle. */ bool Draw_Rect(float x, float y, float w, float h, u32 color); - - // Used for the current Screen's Draw. (Optional!) - // stack: Is it the stack variant? + + /* + Used for the current Screen's Draw. (Optional!) + stack: Is it the stack variant? + */ void DrawScreen(bool stack = false); - /* Used for the current Screen's Logic. (Optional!) - * hDown: the hidKeysDown() variable. - * hHeld: the HidKeysHeld() variable. - * touch: The TouchPosition variable. - * waitFade: Wheter to wait until the fade ends. - * stack: Is it the stack variant? + /* + Used for the current Screen's Logic. (Optional!) + + hDown: the hidKeysDown() variable. + hHeld: the HidKeysHeld() variable. + touch: The TouchPosition variable. + waitFade: Wheter to wait until the fade ends. + stack: Is it the stack variant? */ void ScreenLogic(u32 hDown, u32 hHeld, touchPosition touch, bool waitFade = true, bool stack = false); - - /* Transfer the Temp Screen to the used one. (Optional!) - * stack: Is it the stack variant? - * It will check, if the tempScreen variable is not nullptr, so don't worry. + + /* + Transfer the Temp Screen to the used one. (Optional!) + + stack: Is it the stack variant? + It will check, if the tempScreen variable is not nullptr, so don't worry. */ void transferScreen(bool stack = false); - /* Set a specific Screen with switch function. (Optional!) - * screen: unique_ptr of the screen. (Optional by using the screen class.) - * screenSwitch: Wheter to switch to the current screen. - * stack: Is it the stack variant? + /* + Set a specific Screen with switch function. (Optional!) + + screen: unique_ptr of the screen. (Optional by using the screen class.) + screenSwitch: Wheter to switch to the current screen. + stack: Is it the stack variant? */ void setScreen(std::unique_ptr screen, bool fade = false, bool stack = false); - /* Fades into screens and calls the constructor after it. (Optional!) - * fadeoutFrames: Amount of frames for fadeout. - * fadeinFrames: Amount of frames for fadein. - * stack: Is it the stack variant? + /* + Fades into screens and calls the constructor after it. (Optional!) + fadeoutFrames: Amount of frames for fadeout. + fadeinFrames: Amount of frames for fadein. + stack: Is it the stack variant? */ void fadeEffects(int fadeoutFrames = 6, int fadeinFrames = 6, bool stack = false); void screenBack(bool fade = false); // Goes a screen back. (Set!) (Stack only!) void screenBack2(); // Goes a screen back.(Action!) (Stack only!) - /* Set on which screen to draw. - * screen: The render target. (Targets are inside the screenCommon.hpp file.) + /* + Set on which screen to draw. + + screen: The render target. (Targets are inside the screenCommon.hpp file.) */ void ScreenDraw(C3D_RenderTarget * screen); - /* Draws a grid. - * xPos: X Position of the grid. - * yPos: Y Position of the grid. - * Width: Width of the grid. - * Height: Height of the grid. - * color: Color of the grid. + /* + Draws a grid. + xPos: X Position of the grid. + yPos: Y Position of the grid. + Width: Width of the grid. + Height: Height of the grid. + color: Color of the grid. + bgColor: The BG Color from the grid. (Optional! It's transparent by default.) */ - void drawGrid(float xPos, float yPos, float Width, float Height, u32 color); + void drawGrid(float xPos, float yPos, float Width, float Height, u32 color, u32 bgColor = C2D_Color32(0, 0, 0, 0)); - /* Draws an animated selector. - * xPos: X Position of the selector. - * yPos: Y Position of the Selector. - * Width: Width of the Selector. - * Height: Height of the Selector. - * speed: The speed of the animation. (Use .030 or something by default.) - * SelectorColor: The Color of the Selector. - * bgColor: The Color from the middle of the Selector. (Optional! It's transparent by default.) + /* + Draws an animated selector. + xPos: X Position of the selector. + yPos: Y Position of the Selector. + Width: Width of the Selector. + Height: Height of the Selector. + speed: The speed of the animation. (Use .030f or something by default.) + SelectorColor: The Color of the Selector outline. + bgColor: The BG Color from the selector. (Optional! It's transparent by default.) */ void drawAnimatedSelector(float xPos, float yPos, float Width, float Height, float speed, u32 SelectorColor, u32 bgColor = C2D_Color32(0, 0, 0, 0)); -} +}; #endif \ No newline at end of file diff --git a/hid.cpp b/hid.cpp deleted file mode 100644 index 64011dd..0000000 --- a/hid.cpp +++ /dev/null @@ -1,61 +0,0 @@ -/* -* This file is part of Universal-Core -* Copyright (C) 2020 Universal-Team -* -* This program is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program. If not, see . -* -* Additional Terms 7.b and 7.c of GPLv3 apply to this file: -* * Requiring preservation of specified reasonable legal notices or -* author attributions in that material or in the Appropriate Legal -* Notices displayed by works containing it. -* * Prohibiting misrepresentation of the origin of that material, -* or requiring that modified versions of such material be marked in -* reasonable ways as different from the original version. -*/ - -#include "hid.hpp" - -HID::HID(size_t EntryPerPage, size_t EntryAmount) { - this->maxEntries = EntryAmount; - this->pageEntry = EntryPerPage; -} - -size_t HID::getIndex() { return this->currentEntry; } - -size_t HID::getMaxEntries() { return this->maxEntries; } - -void HID::nextEntry() { if (this->currentEntry < this->maxEntries-1) this->currentEntry++; } - -void HID::lastEntry() { if (this->currentEntry > 0) this->currentEntry--; } - -void HID::nextPage() { - // Only go to the next page, if the next Page doesn't reach the maxEntries. - if (this->currentEntry + this->pageEntry < this->maxEntries - 1) { - this->currentPage++; - this->currentEntry += this->pageEntry; - // If the first index of the page is smaller than maxEntries -> Go to the next page. - } else if (this->currentPage * this->pageEntry < this->maxEntries - 1) { - this->currentPage++; - this->currentEntry = this->currentPage * this->pageEntry; - } -} - -size_t HID::getPage() { return this->currentPage; } - -void HID::prevPage() { - if (this->currentPage > 0) { - this->currentPage--; - this->currentEntry -= this->pageEntry; - } -} \ No newline at end of file diff --git a/hid.hpp b/hid.hpp deleted file mode 100644 index 43d2cd0..0000000 --- a/hid.hpp +++ /dev/null @@ -1,75 +0,0 @@ -/* -* This file is part of Universal-Core -* Copyright (C) 2020 Universal-Team -* -* This program is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program. If not, see . -* -* Additional Terms 7.b and 7.c of GPLv3 apply to this file: -* * Requiring preservation of specified reasonable legal notices or -* author attributions in that material or in the Appropriate Legal -* Notices displayed by works containing it. -* * Prohibiting misrepresentation of the origin of that material, -* or requiring that modified versions of such material be marked in -* reasonable ways as different from the original version. -*/ - -#ifndef _UNIVERSAL_CORE_HID_HPP -#define _UNIVERSAL_CORE_HID_HPP - -#include - -class HID { -public: - - /* The parameters to initialize this class. - * EntryPage: The entries per page. - * EntryAmount: The amount of entries. - */ - HID(size_t EntryPerPage, size_t EntryAmount); - - /* Get the current index. - * returns the current index as a size_t. - */ - size_t getIndex(); - - /* Get the max amount of the entries per page. - * returns the max amount of entries per page as a size_t. - */ - size_t getMaxEntries(); - - // Go to the next Entry, if maxEntries is not reached. - void nextEntry(); - - // Go to the last Entry, if 0 is not reached. - void lastEntry(); - - // Go to the next Page. - void nextPage(); - - // Go to the previous Page. - void prevPage(); - - /* Get the current page. - * returns the current page as a size_t. - */ - size_t getPage(); - -private: - size_t maxEntries; - size_t currentEntry = 0; - size_t pageEntry; - size_t currentPage = 0; -}; - -#endif \ No newline at end of file diff --git a/screenCommon.hpp b/screenCommon.hpp index 09a873d..d77cb85 100644 --- a/screenCommon.hpp +++ b/screenCommon.hpp @@ -30,9 +30,7 @@ #include "gui.hpp" #include "structs.hpp" -extern C3D_RenderTarget* Top; -extern C3D_RenderTarget* TopRight; -extern C3D_RenderTarget* Bottom; +extern C3D_RenderTarget *Top, *TopRight, *Bottom; extern bool fadeout, fadein, fadeout2, fadein2; extern int fadealpha, fadecolor;