diff --git a/gui.cpp b/gui.cpp index 526f203..f2e7828 100644 --- a/gui.cpp +++ b/gui.cpp @@ -28,6 +28,7 @@ #include "screenCommon.hpp" #include <3ds.h> +#include C3D_RenderTarget* Top; C3D_RenderTarget* TopRight; @@ -39,20 +40,19 @@ std::stack> screens; bool currentScreen = false; // Clear Text. -void Gui::clearTextBufs(void) -{ - C2D_TextBufClear(TextBuf); -} +void Gui::clearTextBufs(void) { C2D_TextBufClear(TextBuf); } // Draw a sprite from the sheet. -void Gui::DrawSprite(C2D_SpriteSheet sheet, size_t imgindex, int x, int y, float ScaleX, float ScaleY) -{ - C2D_DrawImageAt(C2D_SpriteSheetGetImage(sheet, imgindex), x, y, 0.5f, nullptr, ScaleX, ScaleY); +void Gui::DrawSprite(C2D_SpriteSheet sheet, size_t imgindex, int x, int y, float ScaleX, float ScaleY) { + if (sheet != nullptr) { + if (C2D_SpriteSheetCount(sheet) >= imgindex) { + C2D_DrawImageAt(C2D_SpriteSheetGetImage(sheet, imgindex), x, y, 0.5f, nullptr, ScaleX, ScaleY); + } + } } // Initialize GUI. -Result Gui::init(void) -{ +Result Gui::init(void) { C3D_Init(C3D_DEFAULT_CMDBUF_SIZE); C2D_Init(C2D_DEFAULT_MAX_OBJECTS); C2D_Prepare(); @@ -67,43 +67,48 @@ Result Gui::init(void) // Load a Font. Result Gui::loadFont(bool sysFont, const char* Path) { - if (sysFont) Font = C2D_FontLoadSystem(CFG_REGION_USA); - else Font = C2D_FontLoad(Path); + if (sysFont) { + Font = C2D_FontLoadSystem(CFG_REGION_USA); + } else { + if (access(Path, F_OK) == 0) Font = C2D_FontLoad(Path); // Only load if found. + } return 0; } // Unload a Font. Result Gui::unloadFont() { - C2D_FontFree(Font); + if (Font != nullptr) { + C2D_FontFree(Font); // Make sure to only unload if not nullptr. + } return 0; } // Load a Sheet. Result Gui::loadSheet(const char* Path, C2D_SpriteSheet &sheet) { - sheet = C2D_SpriteSheetLoad(Path); + if (access(Path, F_OK) == 0) sheet = C2D_SpriteSheetLoad(Path); // Only load if found. return 0; } // Unload a Sheet. Result Gui::unloadSheet(C2D_SpriteSheet &sheet) { - C2D_SpriteSheetFree(sheet); + if (sheet != nullptr) C2D_SpriteSheetFree(sheet); // Make sure to only unload if not nullptr. return 0; } // Exit the GUI. -void Gui::exit(void) -{ +void Gui::exit(void) { C2D_TextBufDelete(TextBuf); C2D_Fini(); C3D_Fini(); } +// Draw a Centered String. void Gui::DrawStringCentered(float x, float y, float size, u32 color, std::string Text, int maxWidth, int maxHeight) { float lineHeight, widthScale; lineHeight = Gui::GetStringHeight(size, " "); int line = 0; while(Text.find('\n') != Text.npos) { - if(maxWidth == 0) { + if (maxWidth == 0) { widthScale = Gui::GetStringWidth(size, Text.substr(0, Text.find('\n'))); } else { widthScale = std::min((float)maxWidth, Gui::GetStringWidth(size, Text.substr(0, Text.find('\n')))); @@ -112,7 +117,7 @@ void Gui::DrawStringCentered(float x, float y, float size, u32 color, std::strin Text = Text.substr(Text.find('\n')+1); line++; } - if(maxWidth == 0) { + if (maxWidth == 0) { widthScale = Gui::GetStringWidth(size, Text.substr(0, Text.find('\n'))); } else { widthScale = std::min((float)maxWidth, Gui::GetStringWidth(size, Text.substr(0, Text.find('\n')))); @@ -127,20 +132,19 @@ void Gui::DrawString(float x, float y, float size, u32 color, std::string Text, C2D_TextOptimize(&c2d_text); float heightScale; - if(maxHeight == 0) { + if (maxHeight == 0) { heightScale = size; } 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); } 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. float Gui::GetStringWidth(float size, std::string Text) { float width = 0; @@ -170,31 +174,25 @@ bool Gui::Draw_Rect(float x, float y, float w, float h, u32 color) { // Mainloop the GUI. void Gui::mainLoop(u32 hDown, u32 hHeld, touchPosition touch) { - screens.top()->Draw(); - screens.top()->Logic(hDown, hHeld, touch); + if (!screens.empty()) { + screens.top()->Draw(); + screens.top()->Logic(hDown, hHeld, touch); + } } // Set the current Screen. -void Gui::setScreen(std::unique_ptr screen) -{ - screens.push(std::move(screen)); -} +void Gui::setScreen(std::unique_ptr screen) { screens.push(std::move(screen)); } // Go a Screen back. -void Gui::screenBack() -{ - screens.pop(); -} +void Gui::screenBack() { if (screens.size() > 0) screens.pop(); } // Select, on which Screen should be drawn. -void Gui::ScreenDraw(C3D_RenderTarget * screen) -{ +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) -{ +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)); @@ -206,24 +204,23 @@ void Gui::drawGrid(float xPos, float yPos, float Width, float Height, u32 color) C2D_DrawRectSolid(xPos, yPos + Height - w, 0.5, Width, w, color); // bottom } -void Gui::drawAnimatedSelector(float xPos, float yPos, float Width, float Height, float speed, u32 SelectorColor, u32 colour) -{ - static constexpr int w = 2; - static float timer = 0.0f; - float highlight_multiplier = fmax(0.0, fabs(fmod(timer, 1.0) - 0.5) / 0.5); - u8 r = SelectorColor & 0xFF; - u8 g = (SelectorColor >> 8) & 0xFF; - 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); +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; + float highlight_multiplier = fmax(0.0, fabs(fmod(timer, 1.0) - 0.5) / 0.5); + u8 r = SelectorColor & 0xFF; + u8 g = (SelectorColor >> 8) & 0xFF; + 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, colour); + C2D_DrawRectSolid(xPos, yPos, 0.5, 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, 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 + C2D_DrawRectSolid(xPos, yPos + Height - w, 0.5, Width, w, color); // bottom timer += speed; // Speed of the animation. Example : .030f / .030 } \ No newline at end of file diff --git a/gui.hpp b/gui.hpp index 866b381..1ee6aa0 100644 --- a/gui.hpp +++ b/gui.hpp @@ -24,8 +24,8 @@ * reasonable ways as different from the original version. */ -#ifndef GUI_HPP -#define GUI_HPP +#ifndef _UNIVERSAL_CORE_GUI_HPP +#define _UNIVERSAL_CORE_GUI_HPP #include "screen.hpp" @@ -36,65 +36,138 @@ namespace Gui { - // Clear Text Buffer. + // Clear the Text Buffer. void clearTextBufs(void); - // Draw a sprite from a sheet. + /* 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. + // Initialize the GUI with Citro2D & Citro3D and initialize the Textbuffer. (call this when initializing.) Result init(void); - // Load a Font. (bcfnt) + /* Load a Font. (BCFNT) + * sysFont: Whether the system font should be loaded instead of custom Font. + * Path: Path to the BCFNT file. + * if you're unsure, just call 'Gui::loadFont();' and it will load the system font. + */ Result loadFont(bool sysFont = true, const char * Path = ""); - // Unload a Font. (bcfnt) + /* Unload a Font. (BCFNT) + * Only use this if a custom Font has been loaded. + */ Result unloadFont(); - // Load a spritesheet. + /* 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); - // Unload a spritesheet. + /* Unload a spritesheet. + * sheet: Reference to the C2D_SpriteSheet which should be free'd. + */ Result unloadSheet(C2D_SpriteSheet &sheet); - // Exit the GUI. + // Exit the GUI. (Call this at exit.) void exit(void); - // Draw a centered String. + /* 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!) + */ void DrawStringCentered(float x, float y, float size, u32 color, std::string Text, int maxWidth = 0, int maxHeight = 0); - // Draw a String. + /* 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!) + */ void DrawString(float x, float y, float size, u32 color, std::string Text, int maxWidth = 0, int maxHeight = 0); - // Get the width of a String. + /* Get the width of a String. + * size: The size of the Text. + * Text: The Text where the width should be getted from. + */ float GetStringWidth(float size, std::string Text); - // Get the size of a String. + /* 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. + */ void GetStringSize(float size, float *width, float *height, std::string Text); - // Get the height of a String. + /* Get the height of a String. + * size: The size of the Text. + * Text: The Text where the height should be getted from. + */ float GetStringHeight(float size, std::string Text); - // Draw a Rectangle. + /* 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); - // Mainloop the GUI / Screen part. + /* Used for the mainLoop to display the screens. (Optional!) + * hDown: the hidKeysDown() variable. + * hHeld: the HidKeysHeld() variable. + * touch: The TouchPosition variable. + */ void mainLoop(u32 hDown, u32 hHeld, touchPosition touch); - // Set a specific Screen. + /* Set a specific Screen. + * screen: unique_ptr of the screen. (Optional by using the screen class.) + */ void setScreen(std::unique_ptr screen); - // Go a Screen back. + // Go a Screen back. (Optional by using the screen class.) void screenBack(); - // Set on which screen to draw. + /* Set on which screen to draw. + * screen: The render target. (Targets are inside the screenCommon.hpp file.) + */ void ScreenDraw(C3D_RenderTarget * screen); - // Draw a 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. + */ void drawGrid(float xPos, float yPos, float Width, float Height, u32 color); - // Draw an animated selector. - void drawAnimatedSelector(float xPos, float yPos, float Width, float Height, float speed, u32 SelectorColor, u32 colour); + /* 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.) + */ + 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 index f29df70..494b5f2 100644 --- a/hid.cpp +++ b/hid.cpp @@ -27,49 +27,35 @@ #include "hid.hpp" HID::HID(size_t EntryPerPage, size_t EntryAmount) { - maxEntries = EntryAmount; - pageEntry = EntryPerPage; + this->maxEntries = EntryAmount; + this->pageEntry = EntryPerPage; } -size_t HID::getIndex() { - return currentEntry; -} +size_t HID::getIndex() { return this->currentEntry; } -size_t HID::getMaxEntries() { - return maxEntries; -} +size_t HID::getMaxEntries() { return this->maxEntries; } -void HID::nextEntry() { - if (currentEntry < maxEntries-1) { - currentEntry++; - } -} +void HID::nextEntry() { if (this->currentEntry < this->maxEntries-1) this->currentEntry++; } -void HID::lastEntry() { - if (currentEntry > 0) { - 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 (currentEntry + pageEntry < maxEntries - 1) { - currentPage++; - currentEntry += pageEntry; + 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 (currentPage * pageEntry < maxEntries - 1) { - currentPage++; - currentEntry = currentPage * pageEntry; + } else if (this->currentPage * this->pageEntry < this->maxEntries - 1) { + this->currentPage++; + this->currentEntry = this->currentPage * this->pageEntry; } } -size_t HID::getPage() { - return currentPage; -} +size_t HID::getPage() { return this->currentPage; } void HID::prevPage() { - if (currentPage > 0) { - currentPage--; - currentEntry -= pageEntry; + if (this->currentPage > 0) { + this->currentPage--; + this->currentEntry -= this->pageEntry; } } \ No newline at end of file diff --git a/hid.hpp b/hid.hpp index 51c1c3e..f17eac6 100644 --- a/hid.hpp +++ b/hid.hpp @@ -24,29 +24,46 @@ * reasonable ways as different from the original version. */ -#ifndef HID_HPP -#define HID_HPP +#ifndef _UNIVERSAL_CORE_HID_HPP +#define _UNIVERSAL_CORE_HID_HPP #include class HID { public: - // The parameters to initialize this class. + + /* 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. + + /* Get the current index. + * returns the current index as a size_t. + */ size_t getIndex(); - // get the max amount of the index. + + /* 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. + + /* Get the current page. + * returns the current page as a size_t. + */ size_t getPage(); private: diff --git a/screen.hpp b/screen.hpp index f035c83..38315a9 100644 --- a/screen.hpp +++ b/screen.hpp @@ -24,8 +24,8 @@ * reasonable ways as different from the original version. */ -#ifndef SCREEN_HPP -#define SCREEN_HPP +#ifndef _UNIVERSAL_CORE_SCREEN_HPP +#define _UNIVERSAL_CORE_SCREEN_HPP #include <3ds.h> #include diff --git a/screenCommon.hpp b/screenCommon.hpp index ecadf46..c95940d 100644 --- a/screenCommon.hpp +++ b/screenCommon.hpp @@ -24,8 +24,8 @@ * reasonable ways as different from the original version. */ -#ifndef SCREENCOMMON_HPP -#define SCREENCOMMON_HPP +#ifndef _UNIVERSAL_CORE_SCREENCOMMON_HPP +#define _UNIVERSAL_CORE_SCREENCOMMON_HPP #include "gui.hpp" #include "structs.hpp" diff --git a/structs.hpp b/structs.hpp index 180fbd7..7a6d222 100644 --- a/structs.hpp +++ b/structs.hpp @@ -24,8 +24,8 @@ * reasonable ways as different from the original version. */ -#ifndef STRUCTS_HPP -#define STRUCTS_HPP +#ifndef _UNIVERSAL_CORE_STRUCTS_HPP +#define _UNIVERSAL_CORE_STRUCTS_HPP #include