From ec56603e690d4f452d77bc0a46682e4392fe8892 Mon Sep 17 00:00:00 2001 From: Aman Menda Date: Mon, 6 May 2024 21:12:00 +0100 Subject: [PATCH] feat: list of games played as a popup --- include/App/Stuckfish.hpp | 4 +- include/UI/GamesPlayedPage.hpp | 36 -------- include/UI/HomePage.hpp | 11 ++- src/UI/GamesPlayedPage.cpp | 153 --------------------------------- src/UI/HomePage.cpp | 131 +++++++++++++++++++++++++--- 5 files changed, 131 insertions(+), 204 deletions(-) delete mode 100644 include/UI/GamesPlayedPage.hpp delete mode 100644 src/UI/GamesPlayedPage.cpp diff --git a/include/App/Stuckfish.hpp b/include/App/Stuckfish.hpp index 0845a6d..c3390d3 100644 --- a/include/App/Stuckfish.hpp +++ b/include/App/Stuckfish.hpp @@ -39,8 +39,8 @@ namespace Stuckfish struct WindowSpecs { std::string name = "Stuckfish"; - uint32_t width = 1500; - uint32_t height = 800; + float width = 1500; + float height = 800; }; class Core diff --git a/include/UI/GamesPlayedPage.hpp b/include/UI/GamesPlayedPage.hpp deleted file mode 100644 index 8644f63..0000000 --- a/include/UI/GamesPlayedPage.hpp +++ /dev/null @@ -1,36 +0,0 @@ -/****************************************************************************** - * File: GamesPlayedPage.hpp - * Authors: see AUTHORS file - * Date: May 3, 2024 - * Description: This file contains the required method to render the 2nd Page - *****************************************************************************/ - -#pragma once - -#include "../App/Stuckfish.hpp" -#include - -namespace Stuckfish -{ - class GamesPlayedPage - { - public: - GamesPlayedPage(Core& app, Logic& logic, UserData& userData) : _app(app), _logic(logic), _userdata(userData) - {}; - - void OnUpdate(); - void OnUIRender(); - void OnAttach(); - void OnDetach(); - - private: - Core& _app; - Logic& _logic; - UserData& _userdata; - Response _res; - GamesData _selectedGameData; - - bool _hasRetrievedGames = false; - bool _isFirstElement = true; - }; -} diff --git a/include/UI/HomePage.hpp b/include/UI/HomePage.hpp index b6acb12..f0acce4 100644 --- a/include/UI/HomePage.hpp +++ b/include/UI/HomePage.hpp @@ -19,7 +19,8 @@ namespace Stuckfish enum HomePageEvent { NONE = 0, - ON_USERNAME_SUBMITTED + ON_USERNAME_SUBMITTED, + ON_GAME_SELECTION, }; class HomePage @@ -28,15 +29,21 @@ namespace Stuckfish HomePage(Core& app) : _app(app), _userData(std::make_shared()) { _event = HomePageEvent::NONE; + _selectedGameData = nullptr; } void Render(void); void Update(void); + void RenderPopup(void); private: Core& _app; - std::shared_ptr _userData; HomePageEvent _event; + std::shared_ptr _selectedGameData; + std::vector _gamesData; + std::shared_ptr _userData; + + bool _hasRetrievedGames = false; }; } diff --git a/src/UI/GamesPlayedPage.cpp b/src/UI/GamesPlayedPage.cpp deleted file mode 100644 index b187b9c..0000000 --- a/src/UI/GamesPlayedPage.cpp +++ /dev/null @@ -1,153 +0,0 @@ -/****************************************************************************** - * File: GamesPlayedPage.cpp - * Authors: see AUTHORS file - *****************************************************************************/ - -#include "../../include/UI/GamesPlayedPage.hpp" - - -static -const std::string getCurrentMonth(void) -{ - std::time_t currentTime; - std::tm currentTm; - std::time(¤tTime); - - if (localtime_s(¤tTm, ¤tTime) != 0) { - std::cerr << "Error: Unable to get the current time." << std::endl; - return ""; - } - - int currentMonth = currentTm.tm_mon + 1; - - if (currentMonth >= 1 && currentMonth <= 9) - { - return "0" + std::to_string(currentMonth); - } - return std::to_string(currentMonth); -} - -static -const std::string getCurrentYear(void) -{ - std::time_t currentTime; - std::tm currentTm; - std::time(¤tTime); - - if (localtime_s(¤tTm, ¤tTime) != 0) { - std::cerr << "Error: Unable to get the current time." << std::endl; - return ""; - } - return std::to_string(currentTm.tm_year + 1900); -} - -namespace Stuckfish -{ -//------------------------------------------------------------------------------ -/** -*/ -void GamesPlayedPage::OnUIRender() -{ - float windowPosX = 20; - float windowPosY = 20; - std::string month = getCurrentMonth(); - std::string year = getCurrentYear(); - - // raise an error if year/month is empty. - - - ImVec2 windowPos(windowPosX, windowPosY); - ImGui::SetNextWindowPos(windowPos, ImGuiCond_Always); - ImGui::SetNextWindowSize(ImVec2(static_cast(_app._specs.width - windowPosX*2), static_cast(_app._specs.height - windowPosY*2)), ImGuiCond_Always); - - ImGui::Begin(WindowTitlesToString(WindowTitle::GAMESPLAYED_PAGE), NULL, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoBringToFrontOnFocus); - ImVec2 contentRegion = ImGui::GetContentRegionAvail(); - ImVec2 windowMiddlePos = ImVec2(contentRegion.x / 2.0f, contentRegion.y / 2.0f); - - // page title - ImGui::PushFont(_app._robotoFontHeader); - ImGui::SetCursorPos(ImVec2(windowMiddlePos.x - 170, windowMiddlePos.y - 340)); - std::string pageTitleLabel = "Games played: " + month + '/' + year; - ImGui::Text(pageTitleLabel.c_str()); // TODO: get the current year using a C++ library - ImGui::PopFont(); - - ImVec2 buttonSize(confirmButtonSizeX * 5, confirmButtonSizeY); - ImGui::BeginChild("ScrollingRegion", ImVec2(0, 0), true, ImGuiWindowFlags_HorizontalScrollbar); - if (!_hasRetrievedGames) - { - // just use the current month automatically for now; - _res = _logic.GamesPlayedWithinPeriod(_userdata.username, year, month); - _hasRetrievedGames = true; - } - - // only when switch between months will be supported. - if (_res.statusCode == ResponseCode::NO_GAMES_FOUND) - { - ImGui::PushFont(_app._robotoFontHeader); - ImGui::SetCursorPos(ImVec2(windowMiddlePos.x - 170, windowMiddlePos.y)); - ImGui::Text("No game found for the specified period !"); - ImGui::PopFont(); - ImGui::EndChild(); - ImGui::End(); - return; - } - - // every other errors. - if (_res.statusCode != ResponseCode::OK) - { - ImGui::PushFont(_app._robotoFontHeader); - ImGui::SetCursorPos(ImVec2(windowMiddlePos.x - 170, windowMiddlePos.y)); - ImGui::Text("Unexpected error occured while fetching your games !"); - ImGui::PopFont(); - ImGui::EndChild(); - ImGui::End(); - return; - } - - // reverse the list of games - for (const auto& game : _res.gamesData) { - std::string buttonLabel = game.whiteUsername + "(" + game.whiteRating + ") vs " + - game.blackUsername + "(" + game.blackRating + ") - " + - game.result + " - " + game.timeClass; - - ImVec2 buttonPos = ImVec2((ImGui::GetWindowSize().x - buttonSize.x) / 2.0f, ImGui::GetCursorPosY()); - ImGui::SetCursorPos(buttonPos); - if (ImGui::Button(buttonLabel.c_str(), buttonSize)) { - _selectedGameData = game; - std::cout << game.pgn; - //OnUpdate(); - } - } - ImGui::EndChild(); - ImGui::End(); -} - -//------------------------------------------------------------------------------ -/** -*/ -void GamesPlayedPage::OnUpdate() -{ - OnDetach(); -} - -//------------------------------------------------------------------------------ -/** -*/ -void GamesPlayedPage::OnAttach() -{} - -//------------------------------------------------------------------------------ -/** -* Consider writing OnDetach() directly in Page.hpp -*/ -void GamesPlayedPage::OnDetach() -{ - std::vector>& pageStack = _app.GetPageStack(); - - if (!pageStack.empty()) - pageStack.erase(pageStack.begin()); - - return; -} - -} \ No newline at end of file diff --git a/src/UI/HomePage.cpp b/src/UI/HomePage.cpp index f3f22ca..ce1984c 100644 --- a/src/UI/HomePage.cpp +++ b/src/UI/HomePage.cpp @@ -5,6 +5,41 @@ #include "../../include/App/Stuckfish.hpp" +static +const std::string getCurrentMonth(void) +{ + std::time_t currentTime; + std::tm currentTm; + std::time(¤tTime); + + if (localtime_s(¤tTm, ¤tTime) != 0) { + std::cerr << "Error: Unable to get the current time." << std::endl; + return ""; + } + + int currentMonth = currentTm.tm_mon + 1; + + if (currentMonth >= 1 && currentMonth <= 9) + { + return "0" + std::to_string(currentMonth); + } + return std::to_string(currentMonth); +} + +static +const std::string getCurrentYear(void) +{ + std::time_t currentTime; + std::tm currentTm; + std::time(¤tTime); + + if (localtime_s(¤tTm, ¤tTime) != 0) { + std::cerr << "Error: Unable to get the current time." << std::endl; + return ""; + } + return std::to_string(currentTm.tm_year + 1900); +} + namespace Stuckfish { //------------------------------------------------------------------------------ @@ -12,20 +47,18 @@ namespace Stuckfish */ void HomePage::Render(void) { - _event = HomePageEvent::NONE; + bool isDisabled = (_event == HomePageEvent::ON_GAME_SELECTION); - float centeredWindowPosX = static_cast(_app.specs.width / 2 - 390); - float centeredWindowPosY = static_cast(_app.specs.height / 2 - 120); + _event = (_event != HomePageEvent::ON_GAME_SELECTION) ? HomePageEvent::NONE : _event; 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::SetNextWindowPos(ImVec2(0, 0)); + ImGui::SetNextWindowSize(ImVec2(_app.specs.width, _app.specs.height)); ImGui::Begin(WindowTitlesToString(WindowTitle::USERINFO_PAGE), NULL, ImGuiWindowFlags_NoTitleBar | - ImGuiWindowFlags_NoDecoration | - ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | - ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoBringToFrontOnFocus); + ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoResize | + ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoBringToFrontOnFocus + | ImGuiWindowFlags_NoSavedSettings); ImVec2 contentRegion = ImGui::GetContentRegionAvail(); ImVec2 windowMiddlePos = ImVec2(contentRegion.x / 2.0f, contentRegion.y / 2.0f); @@ -46,9 +79,13 @@ void HomePage::Render(void) ImGui::PopStyleVar(); ImGui::SetCursorPos(ImVec2(windowMiddlePos.x - (buttonSize.x / 2.0f), windowMiddlePos.y - 40)); - if (ImGui::Button(ButtonsToString(Buttons::CONFIRM), buttonSize)) { + if (ImGui::Button(ButtonsToString(Buttons::CONFIRM), buttonSize) && !isDisabled) { _event = HomePageEvent::ON_USERNAME_SUBMITTED; } + + if (_event == HomePageEvent::ON_GAME_SELECTION) + RenderPopup(); + ImGui::PopStyleVar(); ImGui::End(); } @@ -75,9 +112,81 @@ void HomePage::Update() _app.errorMessage = ErrorsToString(Errors::USERNAME_NOT_FOUND); return; } + _event = HomePageEvent::ON_GAME_SELECTION; } return; } +void HomePage::RenderPopup(void) +{ + Response res; + + ImVec2 screenSize = ImGui::GetIO().DisplaySize; + ImVec2 popupSize(screenSize.x/2, screenSize.y/2); + ImVec2 popupPos(screenSize.x * 0.5f - popupSize.x * 0.5f, screenSize.y * 0.5f - popupSize.y * 0.5f); + std::string month = getCurrentMonth(); + std::string year = getCurrentYear(); + std::string pageTitleLabel = "Games played: " + month + '/' + year; + + ImGui::SetNextWindowPos(popupPos); + ImGui::SetNextWindowSize(popupSize); + ImGui::Begin(pageTitleLabel.c_str(), nullptr, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse); + ImVec2 contentRegion = ImGui::GetContentRegionAvail(); + ImVec2 windowMiddlePos = ImVec2(contentRegion.x / 2.0f, contentRegion.y / 2.0f); + + ImVec2 buttonSize(confirmButtonSizeX * 3, confirmButtonSizeY); + ImGui::BeginChild("ScrollingRegion", ImVec2(0, 0), true, ImGuiWindowFlags_HorizontalScrollbar); + if (!_hasRetrievedGames) + { + // just use the current month automatically for now; + res = _app.logic.GamesPlayedWithinPeriod(_userData->username, year, month); + + if (res.statusCode == ResponseCode::NO_GAMES_FOUND) + { + ImGui::PushFont(_app.robotoFontHeader); + ImGui::SetCursorPos(ImVec2(windowMiddlePos.x - 170, windowMiddlePos.y)); + ImGui::Text("No game found for the specified period !"); + ImGui::PopFont(); + ImGui::EndChild(); + ImGui::End(); + return; + } + + // every other errors. + if (res.statusCode != ResponseCode::OK) + { + ImGui::PushFont(_app.robotoFontHeader); + ImGui::SetCursorPos(ImVec2(windowMiddlePos.x - 170, windowMiddlePos.y)); + ImGui::Text("Unexpected error occured while fetching your games !"); + ImGui::PopFont(); + ImGui::EndChild(); + ImGui::End(); + return; + } + + _gamesData = res.gamesData; + _hasRetrievedGames = true; + } + + + // reverse the list of games + for (const auto& game : _gamesData) { + std::string buttonLabel = game.whiteUsername + "(" + game.whiteRating + ") vs " + + game.blackUsername + "(" + game.blackRating + ") - " + + game.result + " - " + game.timeClass; + + ImVec2 buttonPos = ImVec2((ImGui::GetWindowSize().x - buttonSize.x) / 2.0f, ImGui::GetCursorPosY()); + ImGui::SetCursorPos(buttonPos); + if (ImGui::Button(buttonLabel.c_str(), buttonSize)) { + _selectedGameData = std::make_shared(game); + _event = HomePageEvent::NONE; + _hasRetrievedGames = false; + } + } + + ImGui::EndChild(); + ImGui::End(); +} + } \ No newline at end of file