mirror of
https://github.com/DarkStore-3DS/Universal-Core.git
synced 2026-07-02 16:59:05 +00:00
Some more checks to avoid possible crashes.
This commit is contained in:
@@ -28,6 +28,7 @@
|
||||
#include "screenCommon.hpp"
|
||||
|
||||
#include <3ds.h>
|
||||
#include <unistd.h>
|
||||
|
||||
C3D_RenderTarget* Top;
|
||||
C3D_RenderTarget* TopRight;
|
||||
@@ -39,20 +40,19 @@ std::stack<std::unique_ptr<Screen>> 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> screen)
|
||||
{
|
||||
screens.push(std::move(screen));
|
||||
}
|
||||
void Gui::setScreen(std::unique_ptr<Screen> 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
|
||||
}
|
||||
Reference in New Issue
Block a user