From a5f7134d1cdf1d8d38d0a41e3172120777fb6df5 Mon Sep 17 00:00:00 2001 From: StackZ <47382115+SuperSaiyajinStackZ@users.noreply.github.com> Date: Tue, 26 May 2020 05:25:33 +0200 Subject: [PATCH] Major Screen changes. (If using the screen class, read the description!!) Changes which are needed when using the screen class: - Add `void callConstructor() override { }` to your screen's public header. - Call `Gui::DrawScreen();` to draw the screen. - Call `Gui::ScreenLogic(u32 hDown, u32 hHeld, touchPosition touch);` to call the Logic. - Call `Gui::CallConstructor();` to call the used screen's constructor. - call `Gui::setScreen(std::make_unique(), );` to set the screen. - Call `Gui::transferScreen();` in case you like to set the tempScreen to the used one. - Get rid of the `Gui::screenBack();` calls and use `Gui::setScreen(...)` directly for it. --- .vscode/settings.json | 5 +++++ gui.cpp | 39 +++++++++++++++++++++++++++------------ gui.hpp | 29 +++++++++++++++++++---------- hid.hpp | 3 +-- screen.hpp | 5 ++--- structs.hpp | 5 +---- 6 files changed, 55 insertions(+), 31 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..a7bd7a0 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "string": "cpp" + } +} \ No newline at end of file diff --git a/gui.cpp b/gui.cpp index 69164e3..c8b426b 100644 --- a/gui.cpp +++ b/gui.cpp @@ -36,7 +36,7 @@ C3D_RenderTarget* Bottom; C2D_TextBuf TextBuf; C2D_Font Font; -std::stack> screens; +std::unique_ptr usedScreen, tempScreen; // tempScreen used for "fade" effects. bool currentScreen = false; // Clear Text. @@ -97,6 +97,7 @@ void Gui::exit(void) { C2D_TextBufDelete(TextBuf); C2D_Fini(); C3D_Fini(); + usedScreen = nullptr; } // Draw a Centered String. @@ -238,24 +239,38 @@ bool Gui::Draw_Rect(float x, float y, float w, float h, u32 color) { return C2D_DrawRectSolid(x, y, 0.5f, w, h, color); } -// Mainloop the GUI. -void Gui::mainLoop(u32 hDown, u32 hHeld, touchPosition touch) { - if (!screens.empty()) { - screens.top()->Draw(); - screens.top()->Logic(hDown, hHeld, touch); - } +// Draw's the current screen's draw. +void Gui::DrawScreen() { + if (usedScreen != nullptr) usedScreen->Draw(); +} + +// Do the current screen's logic. +void Gui::ScreenLogic(u32 hDown, u32 hHeld, touchPosition touch) { + if (usedScreen != nullptr) usedScreen->Logic(hDown, hHeld, touch); +} + +// Calls the current screen's constructor. +void Gui::CallConstructor() { + if (usedScreen != nullptr) usedScreen->callConstructor(); +} + +// Move's the tempScreen to the used one. +void Gui::transferScreen() { + if (tempScreen != nullptr) usedScreen = std::move(tempScreen); } // Set the current Screen. -void Gui::setScreen(std::unique_ptr screen) { screens.push(std::move(screen)); } - -// Go a Screen back. -void Gui::screenBack() { if (screens.size() > 0) screens.pop(); } +void Gui::setScreen(std::unique_ptr screen, bool screenSwitch) { + tempScreen = std::move(screen); + if (screenSwitch) { + Gui::transferScreen(); + } +} // Select, on which Screen should be drawn. void Gui::ScreenDraw(C3D_RenderTarget * screen) { C2D_SceneBegin(screen); - currentScreen = (screen==Top || screen==TopRight) ? 1 : 0; + currentScreen = (screen == Top || screen == TopRight) ? 1 : 0; } void Gui::drawGrid(float xPos, float yPos, float Width, float Height, u32 color) { diff --git a/gui.hpp b/gui.hpp index ebeddc0..f9f6569 100644 --- a/gui.hpp +++ b/gui.hpp @@ -32,10 +32,8 @@ #include <3ds.h> #include #include -#include -namespace Gui -{ +namespace Gui { // Clear the Text Buffer. void clearTextBufs(void); @@ -134,20 +132,31 @@ namespace Gui */ bool Draw_Rect(float x, float y, float w, float h, u32 color); - /* Used for the mainLoop to display the screens. (Optional!) + // Used for the current Screen's Draw. (Optional!) + void DrawScreen(); + + /* Used for the current Screen's Logic. (Optional!) * hDown: the hidKeysDown() variable. * hHeld: the HidKeysHeld() variable. * touch: The TouchPosition variable. */ - void mainLoop(u32 hDown, u32 hHeld, touchPosition touch); + void ScreenLogic(u32 hDown, u32 hHeld, touchPosition touch); - /* Set a specific Screen. - * screen: unique_ptr of the screen. (Optional by using the screen class.) + /* Used for the current Screen's Constructor call. (Optional!) + * This is useful if you need to call the screen's constructor. */ - void setScreen(std::unique_ptr screen); + void CallConstructor(); + + /* Transfer the Temp Screen to the used one. (Optional!) + * It will check, if the tempScreen variable is not nullptr, so don't worry. + */ + void transferScreen(); - // Go a Screen back. (Optional by using the screen class.) - void screenBack(); + /* 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. + */ + void setScreen(std::unique_ptr screen, bool screenSwitch = true); /* Set on which screen to draw. * screen: The render target. (Targets are inside the screenCommon.hpp file.) diff --git a/hid.hpp b/hid.hpp index f17eac6..0683f2b 100644 --- a/hid.hpp +++ b/hid.hpp @@ -29,8 +29,7 @@ #include -class HID -{ +class HID { public: /* The parameters to initialize this class. diff --git a/screen.hpp b/screen.hpp index 38315a9..c3d1dc8 100644 --- a/screen.hpp +++ b/screen.hpp @@ -30,13 +30,12 @@ #include <3ds.h> #include -class Screen -{ +class Screen { public: virtual ~Screen() {} virtual void Logic(u32 hDown, u32 hHeld, touchPosition touch) = 0; virtual void Draw() const = 0; -private: + virtual void callConstructor() = 0; }; #endif \ No newline at end of file diff --git a/structs.hpp b/structs.hpp index 7a6d222..60a85e4 100644 --- a/structs.hpp +++ b/structs.hpp @@ -29,8 +29,7 @@ #include -class Structs -{ +class Structs { public: struct ButtonPos { int x; @@ -45,8 +44,6 @@ public: int y; int w; }; - -private: }; #endif \ No newline at end of file