From 0bb9227e0cc090f265d3146482cbb77f2e3fc0a8 Mon Sep 17 00:00:00 2001 From: Aman Menda Date: Mon, 6 May 2024 19:57:08 +0100 Subject: [PATCH] refactor: userInfosPage -> homePage --- Stuckfish.vcxproj | 9 ++- include/App/Logic.hpp | 8 ++ include/App/Stuckfish.hpp | 45 +++-------- include/Imgui.hpp | 5 ++ include/UI/GamesPlayedPage.hpp | 10 +-- .../UI/{UserInfosPage.hpp => HomePage.hpp} | 31 +++++--- include/UI/Page.hpp | 25 ------- src/App/Stuckfish.cpp | 71 +++++------------- src/Main.cpp | 5 +- src/UI/GamesPlayedPage.cpp | 1 + src/UI/{UserInfosPage.cpp => HomePage.cpp} | 75 +++++++------------ 11 files changed, 105 insertions(+), 180 deletions(-) create mode 100644 include/Imgui.hpp rename include/UI/{UserInfosPage.hpp => HomePage.hpp} (53%) rename src/UI/{UserInfosPage.cpp => HomePage.cpp} (55%) diff --git a/Stuckfish.vcxproj b/Stuckfish.vcxproj index a7dfa19..afbdfad 100644 --- a/Stuckfish.vcxproj +++ b/Stuckfish.vcxproj @@ -33,10 +33,11 @@ - - + + + @@ -46,11 +47,11 @@ - + - + diff --git a/include/App/Logic.hpp b/include/App/Logic.hpp index 6f67b58..4126ba8 100644 --- a/include/App/Logic.hpp +++ b/include/App/Logic.hpp @@ -46,6 +46,14 @@ namespace Stuckfish NETWORK_ERROR }; + struct UserData + { + UserData(const std::string& name = "") : username(name) + {}; + + std::string username; + }; + class Logic { public: diff --git a/include/App/Stuckfish.hpp b/include/App/Stuckfish.hpp index b7b2192..0845a6d 100644 --- a/include/App/Stuckfish.hpp +++ b/include/App/Stuckfish.hpp @@ -12,13 +12,11 @@ #include #include -#include "imgui.h" -#include "imgui_impl_glfw.h" -#include "imgui_impl_opengl3.h" -#include "imgui_stdlib.h" +#include "../Imgui.hpp" #include "../UI/fonts.hpp" #include "../UI/Page.hpp" +#include "../UI/HomePage.hpp" #include @@ -52,45 +50,24 @@ namespace Stuckfish ~Core(); void Run(void); - - static Core& Get(void); - - template - void PushLayer(Args&&... args) { - static_assert(std::is_base_of::value, "Pushed type is not subclass of Page!"); - _pageStack.emplace_back(std::make_shared(std::forward(args)...)); - } - - /*template - T& GetLayer() { - return dynamic_cast(*(_pageStack.front())); - }*/ - - std::vector>& GetPageStack(void) - { - return _pageStack; - } - void DisplayErrorPopup(const char *error_message); void RemoveErrorPopup(void); public: - ImFont* _robotoFontHeader = nullptr; - ImFont* _robotoFontBody = nullptr; - ImFont* _robotoFontBodyMedium = nullptr; - - WindowSpecs _specs; - Logic _appLogic; - UserData _userData; + ImFont* robotoFontHeader = nullptr; + ImFont* robotoFontBody = nullptr; + ImFont* robotoFontBodyMedium = nullptr; + + WindowSpecs specs; + Logic logic; + bool errorOccured = false; + std::string errorMessage; private: void Init(void); void Quit(void); private: GLFWwindow* _window = nullptr; - - std::vector> _pageStack; + HomePage _homePage; }; - - std::unique_ptr CreateApplication(void); }; \ No newline at end of file diff --git a/include/Imgui.hpp b/include/Imgui.hpp new file mode 100644 index 0000000..0c6284c --- /dev/null +++ b/include/Imgui.hpp @@ -0,0 +1,5 @@ +#pragma once +#include "imgui.h" +#include "imgui_impl_glfw.h" +#include "imgui_impl_opengl3.h" +#include "imgui_stdlib.h" \ No newline at end of file diff --git a/include/UI/GamesPlayedPage.hpp b/include/UI/GamesPlayedPage.hpp index 866aa7d..8644f63 100644 --- a/include/UI/GamesPlayedPage.hpp +++ b/include/UI/GamesPlayedPage.hpp @@ -12,16 +12,16 @@ namespace Stuckfish { - class GamesPlayedPage : public Page + class GamesPlayedPage { public: GamesPlayedPage(Core& app, Logic& logic, UserData& userData) : _app(app), _logic(logic), _userdata(userData) {}; - void OnUpdate() override; - void OnUIRender() override; - void OnAttach() override; - void OnDetach() override; + void OnUpdate(); + void OnUIRender(); + void OnAttach(); + void OnDetach(); private: Core& _app; diff --git a/include/UI/UserInfosPage.hpp b/include/UI/HomePage.hpp similarity index 53% rename from include/UI/UserInfosPage.hpp rename to include/UI/HomePage.hpp index c585bf3..b6acb12 100644 --- a/include/UI/UserInfosPage.hpp +++ b/include/UI/HomePage.hpp @@ -6,24 +6,37 @@ *****************************************************************************/ #include "Page.hpp" +#include "../Imgui.hpp" namespace Stuckfish { - class UserInfosPage : public Page + class Core; +} + +namespace Stuckfish +{ + + enum HomePageEvent + { + NONE = 0, + ON_USERNAME_SUBMITTED + }; + + class HomePage { public: - UserInfosPage(Core& app, Logic& logic, UserData& userData) : _app(app), _logic(logic), _userdata(userData) - {} + HomePage(Core& app) : _app(app), _userData(std::make_shared()) + { + _event = HomePageEvent::NONE; + } - void OnUpdate() override; - void OnUIRender() override; - void OnAttach() override; - void OnDetach() override; + void Render(void); + void Update(void); private: Core& _app; - Logic& _logic; - UserData& _userdata; + std::shared_ptr _userData; + HomePageEvent _event; }; } diff --git a/include/UI/Page.hpp b/include/UI/Page.hpp index 1c8a558..58c60a0 100644 --- a/include/UI/Page.hpp +++ b/include/UI/Page.hpp @@ -21,9 +21,7 @@ namespace Stuckfish const float confirmButtonSizeY = 35.0f; const float popupConfirmButtonSizeX = 50.0f; const float popupConfirmButtonSizeY = 0.0f; - const float roundingValue = 5.0f; - const float inputFieldWidth = 400; enum class WindowTitle @@ -71,27 +69,4 @@ namespace Stuckfish default: return "[Unknown Error]"; } } - - struct UserData - { - UserData(const std::string& name = "") : username(name) - {}; - - std::string username; - }; - - class Page - { - public: - - virtual ~Page() = default; // virtual destructor as the class will be inherited. - virtual void OnUpdate() {}; - virtual void OnUIRender() {}; - virtual void OnAttach() {}; - virtual void OnDetach() {}; - - public: - bool _errorOccured = false; - std::string _errorMessage = ""; - }; } \ No newline at end of file diff --git a/src/App/Stuckfish.cpp b/src/App/Stuckfish.cpp index 35c14e5..271f1d0 100644 --- a/src/App/Stuckfish.cpp +++ b/src/App/Stuckfish.cpp @@ -4,10 +4,6 @@ *****************************************************************************/ #include "../../include/App/Stuckfish.hpp" -#include "../../include/UI/UserInfosPage.hpp" -#include "../../include/UI/GamesPlayedPage.hpp" - -static Stuckfish::Core* current_instance = nullptr; static void glfw_error_callback(int error, const char* description) { @@ -29,11 +25,10 @@ namespace Stuckfish //------------------------------------------------------------------------------ /** */ -Core::Core(const WindowSpecs& win_specs) +Core::Core(const WindowSpecs& win_specs) : + _homePage(*this) { Init(); - - current_instance = this; } //------------------------------------------------------------------------------ @@ -42,8 +37,6 @@ Core::Core(const WindowSpecs& win_specs) Core::~Core() { Quit(); - - current_instance = nullptr; } //------------------------------------------------------------------------------ @@ -60,7 +53,7 @@ void Core::Init(void) glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); - _window = check_return(glfwCreateWindow(_specs.width, _specs.height, _specs.name.c_str(), nullptr, nullptr), "Unable to create window. Exiting with code 1."); + _window = check_return(glfwCreateWindow(specs.width, specs.height, specs.name.c_str(), nullptr, nullptr), "Unable to create window. Exiting with code 1."); glfwMakeContextCurrent(_window); glfwSwapInterval(1); // Enable vsync @@ -76,10 +69,10 @@ void Core::Init(void) ImFontConfig fontConfig; fontConfig.FontDataOwnedByAtlas = true; ImFontAtlas* fontAtlas = io.Fonts; - _robotoFontHeader = check_return(fontAtlas->AddFontFromMemoryTTF((void*)roboto_regular, sizeof(roboto_regular), 28.0f), "Failed to load roboto header regular font."); - _robotoFontBody = check_return(fontAtlas->AddFontFromMemoryTTF((void*)roboto_regular, sizeof(roboto_regular), 20.0f), "Failed to load roboto body regular font!"); - _robotoFontBodyMedium = check_return(fontAtlas->AddFontFromMemoryTTF((void*)roboto_medium, sizeof(roboto_medium), 20.0f), "Failed to load roboto body medium font!"); - io.FontDefault = _robotoFontBody; + robotoFontHeader = check_return(fontAtlas->AddFontFromMemoryTTF((void*)roboto_regular, sizeof(roboto_regular), 28.0f), "Failed to load roboto header regular font."); + robotoFontBody = check_return(fontAtlas->AddFontFromMemoryTTF((void*)roboto_regular, sizeof(roboto_regular), 20.0f), "Failed to load roboto body regular font!"); + robotoFontBodyMedium = check_return(fontAtlas->AddFontFromMemoryTTF((void*)roboto_medium, sizeof(roboto_medium), 20.0f), "Failed to load roboto body medium font!"); + io.FontDefault = robotoFontBody; // Setup Platform/Renderer backends ImGui_ImplGlfw_InitForOpenGL(_window, true); @@ -104,15 +97,16 @@ void Core::Run(void) ImGui::NewFrame(); { - - _pageStack.front()->OnUIRender(); - if (_pageStack.front()->_errorOccured) - DisplayErrorPopup(_pageStack.front()->_errorMessage.c_str()); + _homePage.Render(); + _homePage.Update(); + + if (errorOccured) + DisplayErrorPopup(errorMessage.c_str()); } ImGui::Render(); - glViewport(0, 0, _specs.width, _specs.height); + glViewport(0, 0, specs.width, specs.height); glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w); glClear(GL_COLOR_BUFFER_BIT); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); @@ -125,49 +119,21 @@ void Core::Run(void) */ void Core::Quit(void) { - //for (auto& page : _pageStack) - //page.reset(); ImGui_ImplOpenGL3_Shutdown(); ImGui_ImplGlfw_Shutdown(); ImGui::DestroyContext(); - // Clear page stack - _pageStack.clear(); - glfwDestroyWindow(_window); glfwTerminate(); } -//------------------------------------------------------------------------------ -/** -*/ -Core& Core::Get(void) -{ - return *current_instance; -} - -//------------------------------------------------------------------------------ -/** -*/ -std::unique_ptr CreateApplication(void) -{ - WindowSpecs specs; - - std::unique_ptr app = std::make_unique(specs); - - app->PushLayer(Core::Get(), app->_appLogic, app-> _userData); - app->PushLayer(Core::Get(), app->_appLogic, app->_userData); - - return app; -} - //------------------------------------------------------------------------------ /** */ void Core::DisplayErrorPopup(const char *error_message) { // Calculate the position of the popup in the upper right corner - ImVec2 popupPos(static_cast(_specs.width - ImGui::GetWindowSize().x / 2.8), 0); + ImVec2 popupPos(static_cast(specs.width - ImGui::GetWindowSize().x / 2.8), 0); ImVec4 redColor = ImVec4(1.0f, 0.0f, 0.0f, 1.0f); ImGui::OpenPopup(WindowTitlesToString(WindowTitle::ERROR_POPUP)); @@ -177,7 +143,7 @@ void Core::DisplayErrorPopup(const char *error_message) ImGuiWindowFlags_NoMove)) { - ImGui::PushFont(_robotoFontBodyMedium); + ImGui::PushFont(robotoFontBodyMedium); ImGui::TextColored(redColor, "An error occurred"); ImGui::PopFont(); ImGui::Separator(); @@ -201,11 +167,8 @@ void Core::DisplayErrorPopup(const char *error_message) */ void Core::RemoveErrorPopup(void) { - for (auto& page : _pageStack) - { - page->_errorOccured = false; - page->_errorMessage.clear(); - } + errorOccured = false; + errorMessage.clear(); ImGui::CloseCurrentPopup(); } diff --git a/src/Main.cpp b/src/Main.cpp index a24d350..7a2af6e 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -7,8 +7,9 @@ int main(int argc, char* argv[]) { - std::unique_ptr app = Stuckfish::CreateApplication(); - + Stuckfish::WindowSpecs specs; + std::unique_ptr app = std::make_unique(specs); + app->Run(); app.release(); diff --git a/src/UI/GamesPlayedPage.cpp b/src/UI/GamesPlayedPage.cpp index 0d53e41..b187b9c 100644 --- a/src/UI/GamesPlayedPage.cpp +++ b/src/UI/GamesPlayedPage.cpp @@ -104,6 +104,7 @@ void GamesPlayedPage::OnUIRender() return; } + // reverse the list of games for (const auto& game : _res.gamesData) { std::string buttonLabel = game.whiteUsername + "(" + game.whiteRating + ") vs " + game.blackUsername + "(" + game.blackRating + ") - " + diff --git a/src/UI/UserInfosPage.cpp b/src/UI/HomePage.cpp similarity index 55% rename from src/UI/UserInfosPage.cpp rename to src/UI/HomePage.cpp index 8da1900..f3f22ca 100644 --- a/src/UI/UserInfosPage.cpp +++ b/src/UI/HomePage.cpp @@ -3,26 +3,27 @@ * Authors: see AUTHORS file *****************************************************************************/ -#include "../../include/UI/UserInfosPage.hpp" -#include "../../include/UI/GamesPlayedPage.hpp" +#include "../../include/App/Stuckfish.hpp" namespace Stuckfish { //------------------------------------------------------------------------------ /** */ -void UserInfosPage::OnUIRender() +void HomePage::Render(void) { - float centeredWindowPosX = static_cast(_app._specs.width / 2 - 390); - float centeredWindowPosY = static_cast(_app._specs.height / 2 - 120); + _event = HomePageEvent::NONE; + + float centeredWindowPosX = static_cast(_app.specs.width / 2 - 390); + float centeredWindowPosY = static_cast(_app.specs.height / 2 - 120); ImVec2 buttonSize(confirmButtonSizeX, confirmButtonSizeY); ImVec2 windowPos(centeredWindowPosX, centeredWindowPosY); ImGui::SetNextWindowPos(windowPos, ImGuiCond_Always); - ImGui::SetNextWindowSize(ImVec2(static_cast(_app._specs.width) / 2, static_cast(_app._specs.height) / 2), ImGuiCond_Always); + ImGui::SetNextWindowSize(ImVec2(static_cast(_app.specs.width) / 2, static_cast(_app.specs.height) / 2), ImGuiCond_Always); ImGui::Begin(WindowTitlesToString(WindowTitle::USERINFO_PAGE), NULL, ImGuiWindowFlags_NoTitleBar | - ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoBackground | + ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoBringToFrontOnFocus); @@ -32,7 +33,7 @@ void UserInfosPage::OnUIRender() ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, roundingValue); // page title - ImGui::PushFont(_app._robotoFontHeader); + ImGui::PushFont(_app.robotoFontHeader); ImGui::SetCursorPos(ImVec2(windowMiddlePos.x - 225, windowMiddlePos.y - 140)); ImGui::Text("Please provide your Chess.com username"); ImGui::PopFont(); @@ -41,12 +42,12 @@ void UserInfosPage::OnUIRender() ImGui::SetCursorPos(ImVec2(windowMiddlePos.x - 200, windowMiddlePos.y - 90)); ImGui::SetNextItemWidth(inputFieldWidth); ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(10.0f, 10.0f)); - ImGui::InputTextWithHint("##input", "Chess.com username...", &_userdata.username); + ImGui::InputTextWithHint("##input", "Chess.com username...", &_userData->username); ImGui::PopStyleVar(); ImGui::SetCursorPos(ImVec2(windowMiddlePos.x - (buttonSize.x / 2.0f), windowMiddlePos.y - 40)); if (ImGui::Button(ButtonsToString(Buttons::CONFIRM), buttonSize)) { - OnUpdate(); + _event = HomePageEvent::ON_USERNAME_SUBMITTED; } ImGui::PopStyleVar(); ImGui::End(); @@ -55,47 +56,27 @@ void UserInfosPage::OnUIRender() //------------------------------------------------------------------------------ /** */ -void UserInfosPage::OnUpdate() +void HomePage::Update() { - //std::string& username = _activeSession.GetUsername(); - if (_userdata.username.empty()) - { - _errorOccured = true; - _errorMessage = ErrorsToString(Errors::EMPTY_USERNAME); - return; - } - - bool user_exists = _logic.IsChessDotComUser(_userdata.username); - - if (!user_exists) + if (_event == HomePageEvent::ON_USERNAME_SUBMITTED) { - _errorOccured = true; - _errorMessage = ErrorsToString(Errors::USERNAME_NOT_FOUND); - return; + if (_userData->username.empty()) + { + _app.errorOccured = true; + _app.errorMessage = ErrorsToString(Errors::EMPTY_USERNAME); + return; + } + + bool user_exists = _app.logic.IsChessDotComUser(_userData->username); + + if (!user_exists) + { + _app.errorOccured = true; + _app.errorMessage = ErrorsToString(Errors::USERNAME_NOT_FOUND); + return; + } } - OnDetach(); - return; -} - -//------------------------------------------------------------------------------ -/** -*/ -void UserInfosPage::OnAttach() -{ - _app.PushLayer(_app, _logic, _userdata); -} - -//------------------------------------------------------------------------------ -/** -*/ -void UserInfosPage::OnDetach() -{ - std::vector>& pageStack = _app.GetPageStack(); - - if (!pageStack.empty()) - pageStack.erase(pageStack.begin()); - return; }