diff --git a/Libraries/include/GamesPlayedPage.hpp b/Libraries/include/GamesPlayedPage.hpp index c23c3c3..70ea3bc 100644 --- a/Libraries/include/GamesPlayedPage.hpp +++ b/Libraries/include/GamesPlayedPage.hpp @@ -1,3 +1,10 @@ +/****************************************************************************** + * 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 "Stuckfish.hpp" diff --git a/Libraries/include/Logic.hpp b/Libraries/include/Logic.hpp index dd03415..d07af11 100644 --- a/Libraries/include/Logic.hpp +++ b/Libraries/include/Logic.hpp @@ -1,3 +1,10 @@ +/****************************************************************************** + * File: Logic.hpp + * Authors: see AUTHORS file + * Date: May 3, 2024 + * Description: This file contains the API requests methods + *****************************************************************************/ + #pragma once #include diff --git a/Libraries/include/Page.hpp b/Libraries/include/Page.hpp index a80c6e8..49fd717 100644 --- a/Libraries/include/Page.hpp +++ b/Libraries/include/Page.hpp @@ -1,3 +1,10 @@ +/****************************************************************************** + * File: Page.hpp + * Authors: see AUTHORS file + * Date: May 3, 2024 + * Description: Page was meant to be an interface for other pages... + *****************************************************************************/ + #pragma once #include "Logic.hpp" diff --git a/Libraries/include/Stuckfish.hpp b/Libraries/include/Stuckfish.hpp index 39b7e91..83be76b 100644 --- a/Libraries/include/Stuckfish.hpp +++ b/Libraries/include/Stuckfish.hpp @@ -1,3 +1,10 @@ +/****************************************************************************** + * File: Stuckfish.hpp + * Authors: see AUTHORS file + * Date: May 3, 2024 + * Description: This file contains the Core class and setup methods + *****************************************************************************/ + #pragma once #include diff --git a/Libraries/include/UserInfosPage.hpp b/Libraries/include/UserInfosPage.hpp index e8cd1c8..c618fc3 100644 --- a/Libraries/include/UserInfosPage.hpp +++ b/Libraries/include/UserInfosPage.hpp @@ -1,3 +1,10 @@ +/****************************************************************************** + * File: UserInfosPage.hpp + * Authors: see AUTHORS file + * Date: May 3, 2024 + * Description: This file contains the required methods to render the 1st page + *****************************************************************************/ + #include "Page.hpp" #include "Exceptions.hpp" diff --git a/Libraries/include/fonts.hpp b/Libraries/include/fonts.hpp index 8a0e871..b3068b9 100644 --- a/Libraries/include/fonts.hpp +++ b/Libraries/include/fonts.hpp @@ -1,3 +1,10 @@ +/****************************************************************************** + * File: fonts.hpp + * Authors: see AUTHORS file + * Date: May 3, 2024 + * Description: This file contains all the data related to the fonts we are using + *****************************************************************************/ + #pragma once extern unsigned char roboto_regular[168260]; diff --git a/Sources/App/Logic.cpp b/Sources/App/Logic.cpp index 17b28fe..25ebdbc 100644 --- a/Sources/App/Logic.cpp +++ b/Sources/App/Logic.cpp @@ -1,3 +1,8 @@ +/****************************************************************************** + * File: Logic.cpp + * Authors: see AUTHORS file + *****************************************************************************/ + #include "../../Libraries/include/Logic.hpp" static std::string @@ -26,97 +31,108 @@ extractInt(const Value& jsonObject, const std::string& fieldName) namespace Stuckfish { - bool - Logic::IsChessDotComUser(const std::string& username) - { - std::string url = "https://api.chess.com/pub/player/" + to_lower(username); - cpr::Response res = cpr::Get(cpr::Url{ url }); - - if (res.status_code == cpr::status::HTTP_OK) - return true; - return false; - } - void - Logic::GetInfosFromListOfGamesPlayed(const std::string& username, const Document& doc) - { - // Check if parsing succeeded - if (!doc.HasParseError()) { - if (doc.HasMember("games") && doc["games"].IsArray()) { - const Value& gamesArray = doc["games"]; - - if (gamesArray.Empty()) { - // display on the window "No games found" - // Log in the local console the same thing - std::cout << "No games found !\n"; - return; - } - // Iterate over the array and do whatever you need with each game object - for (SizeType i = 0; i < gamesArray.Size(); ++i) { - const Value& game = gamesArray[i]; - GamesData gameData; - - // get white username and rating - if ((gameData.whiteUsername = extractString(game["white"], "username")) == "") - std::cerr << "Unable to get white username\n"; - if ((gameData.whiteRating = extractInt(game["white"], "rating")) == "") - std::cerr << "Unable to get white rating\n"; - - // get black username and rating - if ((gameData.blackUsername = extractString(game["black"], "username")) == "") - std::cerr << "Unable to get black username\n"; - if ((gameData.blackRating = extractInt(game["black"], "rating")) == "") - std::cerr << "Unable to get black rating\n"; - - // get time_class - if ((gameData.timeClass = extractString(game, "time_class")) == "") - std::cerr << "Unable to get time class\n"; - - //get pgn - if ((gameData.pgn = extractString(game, "pgn")) == "") - std::cerr << "Unable to get png\n"; - - // find if player has played as white or black and display if he won or lost the game. - if (gameData.whiteUsername == username) - gameData.gameResult = game["white"]["result"] != "win" ? "Lost" : "Won"; - else - gameData.gameResult = game["black"]["result"] != "win" ? "Lost" : "Won"; - - // insert this game data in the vector - _gamesData.push_back(gameData); - } - } - else { - std::cerr << "JSON does not contain a valid 'games' array field." << std::endl; +//------------------------------------------------------------------------------ +/** +*/ +bool +Logic::IsChessDotComUser(const std::string& username) +{ + std::string url = "https://api.chess.com/pub/player/" + to_lower(username); + cpr::Response res = cpr::Get(cpr::Url{ url }); + + if (res.status_code == cpr::status::HTTP_OK) + return true; + return false; +} + +//------------------------------------------------------------------------------ +/** +*/ +void +Logic::GetInfosFromListOfGamesPlayed(const std::string& username, const Document& doc) +{ + // Check if parsing succeeded + if (!doc.HasParseError()) { + if (doc.HasMember("games") && doc["games"].IsArray()) { + const Value& gamesArray = doc["games"]; + + if (gamesArray.Empty()) { + // display on the window "No games found" + // Log in the local console the same thing + std::cout << "No games found !\n"; return; } + // Iterate over the array and do whatever you need with each game object + for (SizeType i = 0; i < gamesArray.Size(); ++i) { + const Value& game = gamesArray[i]; + GamesData gameData; + + // get white username and rating + if ((gameData.whiteUsername = extractString(game["white"], "username")) == "") + std::cerr << "Unable to get white username\n"; + if ((gameData.whiteRating = extractInt(game["white"], "rating")) == "") + std::cerr << "Unable to get white rating\n"; + + // get black username and rating + if ((gameData.blackUsername = extractString(game["black"], "username")) == "") + std::cerr << "Unable to get black username\n"; + if ((gameData.blackRating = extractInt(game["black"], "rating")) == "") + std::cerr << "Unable to get black rating\n"; + + // get time_class + if ((gameData.timeClass = extractString(game, "time_class")) == "") + std::cerr << "Unable to get time class\n"; + + //get pgn + if ((gameData.pgn = extractString(game, "pgn")) == "") + std::cerr << "Unable to get png\n"; + + // find if player has played as white or black and display if he won or lost the game. + if (gameData.whiteUsername == username) + gameData.gameResult = game["white"]["result"] != "win" ? "Lost" : "Won"; + else + gameData.gameResult = game["black"]["result"] != "win" ? "Lost" : "Won"; + + // insert this game data in the vector + _gamesData.push_back(gameData); + } } else { - std::cerr << "Parsing failed with error code " << GetParseError_En(doc.GetParseError()) << std::endl; + std::cerr << "JSON does not contain a valid 'games' array field." << std::endl; return; } } + else { + std::cerr << "Parsing failed with error code " << GetParseError_En(doc.GetParseError()) << std::endl; + return; + } +} - void - Logic::GamesPlayedWithinPeriod(const std::string& username, const std::string& year, const std::string& month) - { - std::string url = "https://api.chess.com/pub/player/" + to_lower(username) + "/games/" + year + '/' + month; - cpr::Response res = cpr::Get(cpr::Url{ url }); - - if (res.status_code == cpr::status::HTTP_OK) { - Document doc; - doc.Parse(res.text.c_str()); - GetInfosFromListOfGamesPlayed(username, doc); - } else { - std::cerr << "Request failed with status code: " << res.status_code << std::endl; - return; - } - - for (auto g : _gamesData) { - std::cout << "White username: " << g.whiteUsername << '(' << g.whiteRating << ")\n"; - std::cout << "Black username: " << g.blackUsername << '(' << g.blackRating << ")\n"; - std::cout << "Game Result: " << g.gameResult << "\n\n"; - } +//------------------------------------------------------------------------------ +/** +*/ +void +Logic::GamesPlayedWithinPeriod(const std::string& username, const std::string& year, const std::string& month) +{ + std::string url = "https://api.chess.com/pub/player/" + to_lower(username) + "/games/" + year + '/' + month; + cpr::Response res = cpr::Get(cpr::Url{ url }); + + if (res.status_code == cpr::status::HTTP_OK) { + Document doc; + doc.Parse(res.text.c_str()); + GetInfosFromListOfGamesPlayed(username, doc); + } else { + std::cerr << "Request failed with status code: " << res.status_code << std::endl; return; } + + for (auto g : _gamesData) { + std::cout << "White username: " << g.whiteUsername << '(' << g.whiteRating << ")\n"; + std::cout << "Black username: " << g.blackUsername << '(' << g.blackRating << ")\n"; + std::cout << "Game Result: " << g.gameResult << "\n\n"; + } + return; +} + } diff --git a/Sources/App/Stuckfish.cpp b/Sources/App/Stuckfish.cpp index f42c549..229e4d2 100644 --- a/Sources/App/Stuckfish.cpp +++ b/Sources/App/Stuckfish.cpp @@ -1,3 +1,8 @@ +/****************************************************************************** + * File: Stuckfish.cpp + * Authors: see AUTHORS file + *****************************************************************************/ + #include "../../Libraries/include/Stuckfish.hpp" #include "../../Libraries/include/UserInfosPage.hpp" #include "../../Libraries/include/GamesPlayedPage.hpp" @@ -21,159 +26,187 @@ T* check_return(T* ptr, const char* error_msg) { namespace Stuckfish { - Core::Core(const WindowSpecs& win_specs) - { - Init(); +//------------------------------------------------------------------------------ +/** +*/ +Core::Core(const WindowSpecs& win_specs) +{ + Init(); - current_instance = this; - } + current_instance = this; +} - Core::~Core() - { - Quit(); +//------------------------------------------------------------------------------ +/** +*/ +Core::~Core() +{ + Quit(); - current_instance = nullptr; - } + current_instance = nullptr; +} - void Core::Init(void) +//------------------------------------------------------------------------------ +/** +*/ +void Core::Init(void) +{ + glfwSetErrorCallback(glfw_error_callback); + if (!glfwInit()) { - glfwSetErrorCallback(glfw_error_callback); - if (!glfwInit()) - { - std::cerr << "Could not initalize GLFW!\n"; - return; - } - 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."); - glfwMakeContextCurrent(_window); - glfwSwapInterval(1); // Enable vsync - - // Setup Dear ImGui context - IMGUI_CHECKVERSION(); - ImGui::CreateContext(); - ImGuiIO& io = ImGui::GetIO(); (void)io; - io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls - io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls - ImGui::StyleColorsDark(); // Setup Dear ImGui style - - // Load Fonts - 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; - - // Setup Platform/Renderer backends - ImGui_ImplGlfw_InitForOpenGL(_window, true); - ImGui_ImplOpenGL3_Init(GLSL_VERSION); + std::cerr << "Could not initalize GLFW!\n"; + return; } + 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."); + glfwMakeContextCurrent(_window); + glfwSwapInterval(1); // Enable vsync + + // Setup Dear ImGui context + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + ImGuiIO& io = ImGui::GetIO(); (void)io; + io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls + io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls + ImGui::StyleColorsDark(); // Setup Dear ImGui style + + // Load Fonts + 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; + + // Setup Platform/Renderer backends + ImGui_ImplGlfw_InitForOpenGL(_window, true); + ImGui_ImplOpenGL3_Init(GLSL_VERSION); +} - void Core::Run(void) - { - ImGuiIO& io = ImGui::GetIO(); (void)io; - ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); +//------------------------------------------------------------------------------ +/** +*/ +void Core::Run(void) +{ + ImGuiIO& io = ImGui::GetIO(); (void)io; + ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); - while (!glfwWindowShouldClose(_window)) - { - glfwPollEvents(); + while (!glfwWindowShouldClose(_window)) + { + glfwPollEvents(); - // Start the Dear ImGui frame - ImGui_ImplOpenGL3_NewFrame(); - ImGui_ImplGlfw_NewFrame(); - ImGui::NewFrame(); + // Start the Dear ImGui frame + ImGui_ImplOpenGL3_NewFrame(); + ImGui_ImplGlfw_NewFrame(); + ImGui::NewFrame(); - { + { - _pageStack.front()->OnUIRender(); - if (_pageStack.front()->_errorOccured) - DisplayErrorPopup(_pageStack.front()->_errorMessage.c_str()); - } - - ImGui::Render(); - - 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()); - glfwSwapBuffers(_window); + _pageStack.front()->OnUIRender(); + if (_pageStack.front()->_errorOccured) + DisplayErrorPopup(_pageStack.front()->_errorMessage.c_str()); } + + ImGui::Render(); + + 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()); + glfwSwapBuffers(_window); } +} - void Core::Quit(void) - { - //for (auto& page : _pageStack) - //page.reset(); - ImGui_ImplOpenGL3_Shutdown(); - ImGui_ImplGlfw_Shutdown(); - ImGui::DestroyContext(); +//------------------------------------------------------------------------------ +/** +*/ +void Core::Quit(void) +{ + //for (auto& page : _pageStack) + //page.reset(); + ImGui_ImplOpenGL3_Shutdown(); + ImGui_ImplGlfw_Shutdown(); + ImGui::DestroyContext(); - // Clear page stack - _pageStack.clear(); + // Clear page stack + _pageStack.clear(); - glfwDestroyWindow(_window); - glfwTerminate(); - } + glfwDestroyWindow(_window); + glfwTerminate(); +} - Core& Core::Get(void) - { - return *current_instance; - } +//------------------------------------------------------------------------------ +/** +*/ +Core& Core::Get(void) +{ + return *current_instance; +} - std::unique_ptr CreateApplication(void) - { - WindowSpecs specs; +//------------------------------------------------------------------------------ +/** +*/ +std::unique_ptr CreateApplication(void) +{ + WindowSpecs specs; - std::unique_ptr app = std::make_unique(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); + app->PushLayer(Core::Get(), app->_appLogic, app-> _userData); + app->PushLayer(Core::Get(), app->_appLogic, app->_userData); - return app; - } + return app; +} - void Core::DisplayErrorPopup(const char *error_message) +//------------------------------------------------------------------------------ +/** +*/ +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); + ImVec4 redColor = ImVec4(1.0f, 0.0f, 0.0f, 1.0f); + + ImGui::OpenPopup(WindowTitlesToString(WindowTitle::ERROR_POPUP)); + ImGui::SetNextWindowPos(popupPos, ImGuiCond_Always); // Set position relative to top right corner + if (ImGui::BeginPopup(WindowTitlesToString(WindowTitle::ERROR_POPUP), + ImGuiWindowFlags_AlwaysAutoResize | + ImGuiWindowFlags_NoMove)) { - // Calculate the position of the popup in the upper right corner - 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)); - ImGui::SetNextWindowPos(popupPos, ImGuiCond_Always); // Set position relative to top right corner - if (ImGui::BeginPopup(WindowTitlesToString(WindowTitle::ERROR_POPUP), - ImGuiWindowFlags_AlwaysAutoResize | - ImGuiWindowFlags_NoMove)) - { - ImGui::PushFont(_robotoFontBodyMedium); - ImGui::TextColored(redColor, "An error occurred"); - ImGui::PopFont(); - ImGui::Separator(); - ImGui::Spacing(); - - ImGui::TextWrapped(error_message); - - ImGui::SetCursorPosX(ImGui::GetWindowSize().x / 2 - 25); - if (ImGui::Button("OK", ImVec2(popupConfirmButtonSizeX, popupConfirmButtonSizeY))) - RemoveErrorPopup(); - ImGui::EndPopup(); - } + ImGui::PushFont(_robotoFontBodyMedium); + ImGui::TextColored(redColor, "An error occurred"); + ImGui::PopFont(); + ImGui::Separator(); + ImGui::Spacing(); - // Close popup when clicking outside of it - if (ImGui::IsMouseClicked(0) && !ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow)) + ImGui::TextWrapped(error_message); + + ImGui::SetCursorPosX(ImGui::GetWindowSize().x / 2 - 25); + if (ImGui::Button("OK", ImVec2(popupConfirmButtonSizeX, popupConfirmButtonSizeY))) RemoveErrorPopup(); + ImGui::EndPopup(); } - void Core::RemoveErrorPopup(void) + // Close popup when clicking outside of it + if (ImGui::IsMouseClicked(0) && !ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow)) + RemoveErrorPopup(); +} + +//------------------------------------------------------------------------------ +/** +*/ +void Core::RemoveErrorPopup(void) +{ + for (auto& page : _pageStack) { - for (auto& page : _pageStack) - { - page->_errorOccured = false; - page->_errorMessage.clear(); - } - ImGui::CloseCurrentPopup(); + page->_errorOccured = false; + page->_errorMessage.clear(); } + ImGui::CloseCurrentPopup(); +} + } diff --git a/Sources/Main.cpp b/Sources/Main.cpp index 31bd741..1452206 100644 --- a/Sources/Main.cpp +++ b/Sources/Main.cpp @@ -1,3 +1,8 @@ +/****************************************************************************** + * File: Main.cpp + * Authors: see AUTHORS file + *****************************************************************************/ + #include "../Libraries/include/Stuckfish.hpp" int main(int argc, char* argv[]) diff --git a/Sources/UI/GamesPlayedPage.cpp b/Sources/UI/GamesPlayedPage.cpp index c8c15b3..ceb91b6 100644 --- a/Sources/UI/GamesPlayedPage.cpp +++ b/Sources/UI/GamesPlayedPage.cpp @@ -1,69 +1,90 @@ +/****************************************************************************** + * File: GamesPlayedPage.cpp + * Authors: see AUTHORS file + *****************************************************************************/ + #include "GamesPlayedPage.hpp" namespace Stuckfish { - void GamesPlayedPage::OnUIRender() - { - float windowPosX = 20; - float windowPosY = 20; +//------------------------------------------------------------------------------ +/** +*/ +void GamesPlayedPage::OnUIRender() +{ + float windowPosX = 20; + float windowPosY = 20; - 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); + 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); + 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)); - ImGui::Text("Games played: April 2024"); // TODO: get the current month using a C++ library - ImGui::PopFont(); + // page title + ImGui::PushFont(_app._robotoFontHeader); + ImGui::SetCursorPos(ImVec2(windowMiddlePos.x - 170, windowMiddlePos.y - 340)); + ImGui::Text("Games played: April 2024"); // TODO: get the current month using a C++ library + ImGui::PopFont(); - ImVec2 buttonSize(confirmButtonSizeX*5, confirmButtonSizeY); - ImGui::SetCursorPos(ImVec2(windowMiddlePos.x - (buttonSize.x / 2.0f), windowMiddlePos.y - 300)); - ImGui::PushStyleVar(ImGuiStyleVar_ButtonTextAlign, ImVec2(0.0f, 0.5f)); - - // list of all games played in the specified month from which we can extract: - // the player and his ELO and same for the opponent, the game status for the player (win/lose/draw) - // the game time control (rapid, blitz, bullet) - //std::cout << "\nUsername: " << _username << std;; - if (!_hasRetrievedGames) - { - _logic.GamesPlayedWithinPeriod(_userdata.username, "2024", "05"); - _hasRetrievedGames = true; - } - // games list - if (ImGui::Button("Button 1", buttonSize)) - { - std::cout << "1"; - } + ImVec2 buttonSize(confirmButtonSizeX*5, confirmButtonSizeY); + ImGui::SetCursorPos(ImVec2(windowMiddlePos.x - (buttonSize.x / 2.0f), windowMiddlePos.y - 300)); + ImGui::PushStyleVar(ImGuiStyleVar_ButtonTextAlign, ImVec2(0.0f, 0.5f)); - ImGui::SetCursorPos(ImVec2(windowMiddlePos.x - (buttonSize.x / 2.0f), windowMiddlePos.y - 260)); - if (ImGui::Button("Button 2", buttonSize)) - { - // Handle button click - std::cout << "2"; - } + // list of all games played in the specified month from which we can extract: + // the player and his ELO and same for the opponent, the game status for the player (win/lose/draw) + // the game time control (rapid, blitz, bullet) + if (!_hasRetrievedGames) + { + // Check if the retrieval has been ended with success or with an error code + // and handle the error appropriately if it occurs. When there are no errors, + // find a way to get the Games data list. + _logic.GamesPlayedWithinPeriod(_userdata.username, "2024", "05"); + _hasRetrievedGames = true; + } - ImGui::SetCursorPos(ImVec2(windowMiddlePos.x - (buttonSize.x / 2.0f), windowMiddlePos.y - 220)); - if (ImGui::Button("Button 3", buttonSize)) - { - // Handle button click - std::cout << "3"; - } - ImGui::PopStyleVar(); + // games list + if (ImGui::Button("Button 1", buttonSize)) + { + std::cout << "1"; + } + + ImGui::SetCursorPos(ImVec2(windowMiddlePos.x - (buttonSize.x / 2.0f), windowMiddlePos.y - 260)); + if (ImGui::Button("Button 2", buttonSize)) + { + // Handle button click + std::cout << "2"; + } - ImGui::End(); + ImGui::SetCursorPos(ImVec2(windowMiddlePos.x - (buttonSize.x / 2.0f), windowMiddlePos.y - 220)); + if (ImGui::Button("Button 3", buttonSize)) + { + // Handle button click + std::cout << "3"; } + ImGui::PopStyleVar(); + + ImGui::End(); +} + +//------------------------------------------------------------------------------ +/** +*/ +void GamesPlayedPage::OnUpdate() +{} + +//------------------------------------------------------------------------------ +/** +*/ +void GamesPlayedPage::OnAttach() +{} - void GamesPlayedPage::OnUpdate() - {} - - void GamesPlayedPage::OnAttach() - {} +//------------------------------------------------------------------------------ +/** +*/ +void GamesPlayedPage::OnDetach() +{} - void GamesPlayedPage::OnDetach() - {} } \ No newline at end of file diff --git a/Sources/UI/UserInfosPage.cpp b/Sources/UI/UserInfosPage.cpp index 8ed0a5f..d64b7ca 100644 --- a/Sources/UI/UserInfosPage.cpp +++ b/Sources/UI/UserInfosPage.cpp @@ -1,84 +1,102 @@ +/****************************************************************************** + * File: UserInfosPage.cpp + * Authors: see AUTHORS file + *****************************************************************************/ + #include "../../Libraries/include/UserInfosPage.hpp" #include "../../Libraries/include/GamesPlayedPage.hpp" namespace Stuckfish { - void UserInfosPage::OnUIRender() - { - 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::Begin(WindowTitlesToString(WindowTitle::USERINFO_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); - - ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, roundingValue); - - // page title - ImGui::PushFont(_app._robotoFontHeader); - ImGui::SetCursorPos(ImVec2(windowMiddlePos.x - 225, windowMiddlePos.y - 140)); - ImGui::Text("Please provide your Chess.com username"); - ImGui::PopFont(); - - // input field - 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::PopStyleVar(); - - ImGui::SetCursorPos(ImVec2(windowMiddlePos.x - (buttonSize.x / 2.0f), windowMiddlePos.y - 40)); - if (ImGui::Button(ButtonsToString(Buttons::CONFIRM), buttonSize)) { - OnUpdate(); - } - ImGui::PopStyleVar(); - ImGui::End(); +//------------------------------------------------------------------------------ +/** +*/ +void UserInfosPage::OnUIRender() +{ + 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::Begin(WindowTitlesToString(WindowTitle::USERINFO_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); + + ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, roundingValue); + + // page title + ImGui::PushFont(_app._robotoFontHeader); + ImGui::SetCursorPos(ImVec2(windowMiddlePos.x - 225, windowMiddlePos.y - 140)); + ImGui::Text("Please provide your Chess.com username"); + ImGui::PopFont(); + + // input field + 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::PopStyleVar(); + + ImGui::SetCursorPos(ImVec2(windowMiddlePos.x - (buttonSize.x / 2.0f), windowMiddlePos.y - 40)); + if (ImGui::Button(ButtonsToString(Buttons::CONFIRM), buttonSize)) { + OnUpdate(); } - - void UserInfosPage::OnUpdate() + ImGui::PopStyleVar(); + ImGui::End(); +} + +//------------------------------------------------------------------------------ +/** +*/ +void UserInfosPage::OnUpdate() +{ + //std::string& username = _activeSession.GetUsername(); + if (_userdata.username.empty()) { - //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) - { - _errorOccured = true; - _errorMessage = ErrorsToString(Errors::USERNAME_NOT_FOUND); - return; - } - - OnDetach(); + _errorOccured = true; + _errorMessage = ErrorsToString(Errors::EMPTY_USERNAME); return; } - void UserInfosPage::OnAttach() + bool user_exists = _logic.IsChessDotComUser(_userdata.username); + + if (!user_exists) { - _app.PushLayer(_app, _logic, _userdata); + _errorOccured = true; + _errorMessage = ErrorsToString(Errors::USERNAME_NOT_FOUND); + return; } - void UserInfosPage::OnDetach() - { - std::vector>& pageStack = _app.GetPageStack(); + OnDetach(); + return; +} - if (!pageStack.empty()) - pageStack.erase(pageStack.begin()); +//------------------------------------------------------------------------------ +/** +*/ +void UserInfosPage::OnAttach() +{ + _app.PushLayer(_app, _logic, _userdata); +} + +//------------------------------------------------------------------------------ +/** +*/ +void UserInfosPage::OnDetach() +{ + std::vector>& pageStack = _app.GetPageStack(); + + if (!pageStack.empty()) + pageStack.erase(pageStack.begin()); + + return; +} - return; - } } \ No newline at end of file diff --git a/Sources/UI/fonts.cpp b/Sources/UI/fonts.cpp index f839362..e3f746d 100644 --- a/Sources/UI/fonts.cpp +++ b/Sources/UI/fonts.cpp @@ -1,5 +1,13 @@ +/****************************************************************************** + * File: Fonts.cpp + * Authors: see AUTHORS file + *****************************************************************************/ + #include "../../Libraries/include/fonts.hpp" +//------------------------------------------------------------------------------ +/** +*/ unsigned char roboto_regular[168260] = { 0x00, 0x01, 0x00, 0x00, 0x00, 0x12, 0x01, 0x00, 0x00, 0x04, 0x00, 0x20, 0x47, 0x44, 0x45, 0x46, 0xB4, 0x42, 0xB0, 0x82, 0x00, 0x02, 0x1B, 0x84, @@ -14025,6 +14033,9 @@ unsigned char roboto_regular[168260] = { 0x00, 0x03, 0x00, 0x4A, 0x00, 0x57, 0x00, 0x95 }; +//------------------------------------------------------------------------------ +/** +*/ const float FontSizes::LargeHeaderFontSize = 32.0f; const float FontSizes::MediumHeaderFontSize = 28.0f; const float FontSizes::SmallHeaderFontSize = 24.0f; @@ -14033,6 +14044,9 @@ const float FontSizes::LargeBodyFontSize = 24.0f; const float FontSizes::MediumBodyFontSize = 20.0f; const float FontSizes::SmallBodyFontSize = 16.0f; +//------------------------------------------------------------------------------ +/** +*/ unsigned char roboto_medium[168644] = { 0x00, 0x01, 0x00, 0x00, 0x00, 0x12, 0x01, 0x00, 0x00, 0x04, 0x00, 0x20, 0x47, 0x44, 0x45, 0x46, 0xB4, 0x42, 0xB0, 0x82, 0x00, 0x02, 0x15, 0xA0, diff --git a/Stuckfish.vcxproj b/Stuckfish.vcxproj index 7ad5a17..24ea28c 100644 --- a/Stuckfish.vcxproj +++ b/Stuckfish.vcxproj @@ -32,7 +32,7 @@ - +