diff --git a/include/client/ClientApp.h b/include/client/ClientApp.h index 6139675..c1a1ec8 100644 --- a/include/client/ClientApp.h +++ b/include/client/ClientApp.h @@ -22,6 +22,10 @@ class ClientApp : public Nz::ApplicationComponent { void Update(Nz::Time elapsedTime) override; + Nz::Window* GetWindow() { + return m_Window; + } + private: Nz::Window* m_Window; std::unique_ptr m_StateMachine; diff --git a/include/client/ImGuiAppComponent.h b/include/client/ImGuiAppComponent.h new file mode 100644 index 0000000..03e913f --- /dev/null +++ b/include/client/ImGuiAppComponent.h @@ -0,0 +1,33 @@ +#pragma once + +#include + +namespace Nz { + +class Window; +class Texture; + +} // namespace Nz + +namespace blitz { +namespace client { + +class StateData; + +class ImGuiAppComponent : public Nz::ApplicationComponent { + public: + ImGuiAppComponent(Nz::ApplicationBase& app, Nz::Window& a_Window); + ~ImGuiAppComponent(); + + void Update(Nz::Time elapsedTime) override; + + private: + Nz::Window& m_Window; + std::shared_ptr m_FontTexture; + + void SetStyle(); + void UpdateFontTexture(); +}; + +} // namespace client +} // namespace blitz diff --git a/include/client/states/ImGuiDrawer.h b/include/client/states/ImGuiDrawer.h new file mode 100644 index 0000000..4de4e90 --- /dev/null +++ b/include/client/states/ImGuiDrawer.h @@ -0,0 +1,17 @@ +#pragma once + +#include + +namespace blitz { +namespace client { + +class ImGuiDrawer : private Nz::ImguiHandler { + protected: + ImGuiDrawer(); + ~ImGuiDrawer(); + + virtual void OnRenderImgui() = 0; +}; + +} // namespace client +} // namespace blitz diff --git a/include/client/states/MainMenuState.h b/include/client/states/MainMenuState.h index 5568aa0..076df6c 100644 --- a/include/client/states/MainMenuState.h +++ b/include/client/states/MainMenuState.h @@ -2,11 +2,12 @@ #include #include +#include namespace blitz { namespace client { -class MainMenuState : public AbstractState { +class MainMenuState : public AbstractState, private ImGuiDrawer { public: MainMenuState(std::shared_ptr); ~MainMenuState(); @@ -21,6 +22,8 @@ class MainMenuState : public AbstractState { void LayoutWidgets() override; bool Update(Nz::StateMachine& fsm, Nz::Time elapsedTime) override; + void OnRenderImgui() override; + void OnJoinServerPressed(); void OnCreateServerPressed(); void OnOptionPressed(); diff --git a/src/ClientMain.cpp b/src/ClientMain.cpp index bb2f789..2ee0465 100644 --- a/src/ClientMain.cpp +++ b/src/ClientMain.cpp @@ -4,14 +4,17 @@ #include #include #include +#include #include +#include int main(int argc, char** argv) { - Nz::Application app(argc, argv); + Nz::Application app(argc, argv); app.AddComponent(); app.AddComponent(); - app.AddComponent(); + auto& client = app.AddComponent(); + app.AddComponent(*client.GetWindow()); return app.Run(); } diff --git a/src/client/ClientApp.cpp b/src/client/ClientApp.cpp index e053787..957f86b 100644 --- a/src/client/ClientApp.cpp +++ b/src/client/ClientApp.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -36,11 +37,15 @@ ClientApp::ClientApp(Nz::ApplicationBase& app) : Nz::ApplicationComponent(app), auto renderTarget = std::make_shared(windowSwapchain); + Nz::Imgui::Instance()->Init(*m_Window, false); + + auto passList = Nz::PipelinePassList::LoadFromFile("assets/example.passlist"); + entt::handle cameraEntity = world.CreateEntity(); { cameraEntity.emplace(); - auto& cameraComponent = cameraEntity.emplace(renderTarget, Nz::ProjectionType::Orthographic); + auto& cameraComponent = cameraEntity.emplace(renderTarget, passList, Nz::ProjectionType::Orthographic); cameraComponent.UpdateClearColor(Nz::Color(0.0f, 0.f, .0f, 0.0f)); cameraComponent.UpdateRenderMask(RenderMaskUI); @@ -52,6 +57,7 @@ ClientApp::ClientApp(Nz::ApplicationBase& app) : Nz::ApplicationComponent(app), m_StateData->m_AppComponent = this; m_StateData->m_RenderTarget = renderTarget; m_StateData->m_Window = m_Window; + m_StateData->m_Swapchain = &windowSwapchain; m_StateData->m_World = &world; m_StateData->m_Canvas.emplace( world.GetRegistry(), m_Window->GetEventHandler(), m_Window->GetCursorController().CreateHandle(), RenderMaskUI); diff --git a/src/client/ImGuiAppComponent.cpp b/src/client/ImGuiAppComponent.cpp new file mode 100644 index 0000000..cc0e674 --- /dev/null +++ b/src/client/ImGuiAppComponent.cpp @@ -0,0 +1,91 @@ +#include + +#include +#include +#include + +#include + +namespace blitz { +namespace client { + +ImGuiAppComponent::ImGuiAppComponent(Nz::ApplicationBase& a_App, Nz::Window& a_Window) : + Nz::ApplicationComponent(a_App), m_Window(a_Window) { + + ImGui::EnsureContextOnThisThread(); + + SetStyle(); + + a_App.AddUpdaterFunc(Nz::ApplicationBase::Interval{Nz::Time::Milliseconds(16)}, [&](Nz::Time elapsed) { + if (!m_Window.IsOpen()) + return; + + m_Window.ProcessEvents(); + + Nz::Imgui::Instance()->Update(Nz::Time::Milliseconds(16).AsSeconds()); + + Nz::Imgui::Instance()->Render(); + }); +} + +ImGuiAppComponent::~ImGuiAppComponent() {} + +void ImGuiAppComponent::SetStyle() { + const static ImVec4 colorButton = {1.0f, 0.0f, 0.0f, 0.73f}; + const static ImVec4 colorButtonHover = {0.56f, 0.02f, 0.02f, 1.0f}; + const static ImVec4 colorButtonActive = {0.36f, 0.03f, 0.03f, 1.0f}; + const static ImVec4 colorCheckMark = {1.0f, 1.0f, 1.0f, 1.0f}; + const static ImVec4 colorTab = {0.38f, 0.02f, 0.02f, 1.0f}; + const static ImVec4 colorTabHover = {0.74f, 0.0f, 0.0f, 0.73f}; + const static ImVec4 colorTabActive = {1.0f, 0.0f, 0.0f, 0.73f}; + + + ImGui::GetStyle().Colors[ImGuiCol_Button] = colorButton; + ImGui::GetStyle().Colors[ImGuiCol_ButtonActive] = colorButtonActive; + ImGui::GetStyle().Colors[ImGuiCol_ButtonHovered] = colorButtonHover; + ImGui::GetStyle().Colors[ImGuiCol_CheckMark] = colorCheckMark; + ImGui::GetStyle().Colors[ImGuiCol_FrameBg] = colorButton; + ImGui::GetStyle().Colors[ImGuiCol_FrameBgActive] = colorButtonActive; + ImGui::GetStyle().Colors[ImGuiCol_FrameBgHovered] = colorButtonHover; + ImGui::GetStyle().Colors[ImGuiCol_Tab] = colorTab; + ImGui::GetStyle().Colors[ImGuiCol_TabHovered] = colorTabHover; + ImGui::GetStyle().Colors[ImGuiCol_TabActive] = colorTabActive; + ImGui::GetStyle().Colors[ImGuiCol_TitleBgActive] = colorTabActive; + ImGui::GetStyle().Colors[ImGuiCol_SliderGrab] = colorButton; + ImGui::GetStyle().Colors[ImGuiCol_SliderGrabActive] = colorButton; + ImGui::GetStyle().Colors[ImGuiCol_HeaderActive] = colorTab; + ImGui::GetStyle().Colors[ImGuiCol_HeaderHovered] = colorTabActive; + ImGui::GetStyle().Colors[ImGuiCol_Header] = colorTabActive; + + /*ImFontConfig c; + c.SizePixels = 25.0f; + ImGui::GetIO().Fonts->AddFontDefault(&c); + auto* doomFont = ImGui::GetIO().Fonts->AddFontFromFileTTF("assets/fonts/doom.ttf", 25.0f, &c); + ImGui::GetIO().FontDefault = doomFont;*/ + + UpdateFontTexture(); +} + +void ImGuiAppComponent::UpdateFontTexture() { + auto& io = ImGui::GetIO(); + unsigned char* pixels; + int width, height; + + io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); + + auto renderDevice = Nz::Graphics::Instance()->GetRenderDevice(); + Nz::TextureInfo texParams; + texParams.width = width; + texParams.height = height; + texParams.pixelFormat = Nz::PixelFormat::RGBA8; + texParams.type = Nz::ImageType::E2D; + m_FontTexture = renderDevice->InstantiateTexture(texParams, pixels, true); + m_FontTexture->UpdateDebugName("FontTexture"); + + ImTextureID textureID = m_FontTexture.get(); + io.Fonts->TexID = textureID; +} + +void ImGuiAppComponent::Update(Nz::Time elapsedTime) {} +} // namespace client +} // namespace blitz diff --git a/src/client/states/ImGuiDrawer.cpp b/src/client/states/ImGuiDrawer.cpp new file mode 100644 index 0000000..e1275a5 --- /dev/null +++ b/src/client/states/ImGuiDrawer.cpp @@ -0,0 +1,17 @@ +#include + +#include + +namespace blitz { +namespace client { + +ImGuiDrawer::ImGuiDrawer() { + Nz::Imgui::Instance()->AddHandler(this); +} + +ImGuiDrawer::~ImGuiDrawer() { + Nz::Imgui::Instance()->RemoveHandler(this); +} + +} // namespace client +} // namespace blitz diff --git a/src/client/states/MainMenuState.cpp b/src/client/states/MainMenuState.cpp index 05cb285..800629e 100644 --- a/src/client/states/MainMenuState.cpp +++ b/src/client/states/MainMenuState.cpp @@ -3,7 +3,8 @@ #include #include #include - +#include +#include #include #include #include @@ -81,6 +82,12 @@ void MainMenuState::LayoutWidgets() { } } +void MainMenuState::OnRenderImgui() { +#ifndef NDEBUG + ImGui::ShowDemoWindow(nullptr); +#endif +} + void MainMenuState::OnJoinServerPressed() { m_NextState = std::make_shared(GetStateDataPtr(), shared_from_this()); }