Skip to content

Commit

Permalink
refactor: userInfosPage -> homePage
Browse files Browse the repository at this point in the history
  • Loading branch information
AmanMenda committed May 6, 2024
1 parent 901647a commit 0bb9227
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 180 deletions.
9 changes: 5 additions & 4 deletions Stuckfish.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@
<ClCompile Include="src\glad.cpp" />
<ClCompile Include="src\Main.cpp" />
<ClCompile Include="src\UI\Fonts.cpp" />
<ClCompile Include="src\UI\GamesPlayedPage.cpp" />
<ClCompile Include="src\UI\UserInfosPage.cpp" />
<!-- <ClCompile Include="src\UI\GamesPlayedPage.cpp" /> -->
<ClCompile Include="src\UI\HomePage.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\Imgui.hpp" />
<ClInclude Include="thirdparty\imgui\imgui.h" />
<ClInclude Include="thirdparty\imgui\imgui_impl_glfw.h" />
<ClInclude Include="thirdparty\imgui\imgui_impl_opengl3.h" />
Expand All @@ -46,11 +47,11 @@
<ClInclude Include="thirdparty\imgui\imstb_textedit.h" />
<ClInclude Include="thirdparty\imgui\imstb_truetype.h" />
<ClInclude Include="include\UI\fonts.hpp" />
<ClInclude Include="include\UI\GamesPlayedPage.hpp" />
<!-- <ClInclude Include="include\UI\GamesPlayedPage.hpp" /> -->
<ClInclude Include="include\App\Logic.hpp" />
<ClInclude Include="include\UI\Page.hpp" />
<ClInclude Include="include\App\Stuckfish.hpp" />
<ClInclude Include="include\UI\UserInfosPage.hpp" />
<ClInclude Include="include\UI\HomePage.hpp" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
8 changes: 8 additions & 0 deletions include/App/Logic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ namespace Stuckfish
NETWORK_ERROR
};

struct UserData
{
UserData(const std::string& name = "") : username(name)
{};

std::string username;
};

class Logic
{
public:
Expand Down
45 changes: 11 additions & 34 deletions include/App/Stuckfish.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@
#include <stdio.h>
#include <iostream>

#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 <vector>

Expand Down Expand Up @@ -52,45 +50,24 @@ namespace Stuckfish
~Core();

void Run(void);

static Core& Get(void);

template<typename T, typename... Args>
void PushLayer(Args&&... args) {
static_assert(std::is_base_of<Page, T>::value, "Pushed type is not subclass of Page!");
_pageStack.emplace_back(std::make_shared<T>(std::forward<Args>(args)...));
}

/*template<typename T>
T& GetLayer() {
return dynamic_cast<T&>(*(_pageStack.front()));
}*/

std::vector<std::shared_ptr<Page>>& 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<std::shared_ptr<Page>> _pageStack;
HomePage _homePage;
};

std::unique_ptr<Core> CreateApplication(void);
};
5 changes: 5 additions & 0 deletions include/Imgui.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once
#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
#include "imgui_stdlib.h"
10 changes: 5 additions & 5 deletions include/UI/GamesPlayedPage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
31 changes: 22 additions & 9 deletions include/UI/UserInfosPage.hpp → include/UI/HomePage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<UserData>())
{
_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> _userData;
HomePageEvent _event;
};
}

25 changes: 0 additions & 25 deletions include/UI/Page.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = "";
};
}
71 changes: 17 additions & 54 deletions src/App/Stuckfish.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -29,11 +25,10 @@ namespace Stuckfish
//------------------------------------------------------------------------------
/**
*/
Core::Core(const WindowSpecs& win_specs)
Core::Core(const WindowSpecs& win_specs) :
_homePage(*this)
{
Init();

current_instance = this;
}

//------------------------------------------------------------------------------
Expand All @@ -42,8 +37,6 @@ Core::Core(const WindowSpecs& win_specs)
Core::~Core()
{
Quit();

current_instance = nullptr;
}

//------------------------------------------------------------------------------
Expand All @@ -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

Expand All @@ -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);
Expand All @@ -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());
Expand All @@ -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<Core> CreateApplication(void)
{
WindowSpecs specs;

std::unique_ptr<Core> app = std::make_unique<Core>(specs);

app->PushLayer<UserInfosPage>(Core::Get(), app->_appLogic, app-> _userData);
app->PushLayer<GamesPlayedPage>(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<float>(_specs.width - ImGui::GetWindowSize().x / 2.8), 0);
ImVec2 popupPos(static_cast<float>(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));
Expand All @@ -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();
Expand All @@ -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();
}

Expand Down
5 changes: 3 additions & 2 deletions src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

int main(int argc, char* argv[])
{
std::unique_ptr<Stuckfish::Core> app = Stuckfish::CreateApplication();

Stuckfish::WindowSpecs specs;
std::unique_ptr<Stuckfish::Core> app = std::make_unique<Stuckfish::Core>(specs);

app->Run();
app.release();

Expand Down
Loading

0 comments on commit 0bb9227

Please sign in to comment.