add imgui support

This commit is contained in:
2024-08-07 15:01:42 +02:00
parent e0d4dd053f
commit 2df849b63e
9 changed files with 186 additions and 5 deletions

View File

@@ -22,6 +22,10 @@ class ClientApp : public Nz::ApplicationComponent {
void Update(Nz::Time elapsedTime) override; void Update(Nz::Time elapsedTime) override;
Nz::Window* GetWindow() {
return m_Window;
}
private: private:
Nz::Window* m_Window; Nz::Window* m_Window;
std::unique_ptr<Nz::StateMachine> m_StateMachine; std::unique_ptr<Nz::StateMachine> m_StateMachine;

View File

@@ -0,0 +1,33 @@
#pragma once
#include <Nazara/Core/ApplicationComponent.hpp>
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<Nz::Texture> m_FontTexture;
void SetStyle();
void UpdateFontTexture();
};
} // namespace client
} // namespace blitz

View File

@@ -0,0 +1,17 @@
#pragma once
#include <NazaraImgui/ImguiHandler.hpp>
namespace blitz {
namespace client {
class ImGuiDrawer : private Nz::ImguiHandler {
protected:
ImGuiDrawer();
~ImGuiDrawer();
virtual void OnRenderImgui() = 0;
};
} // namespace client
} // namespace blitz

View File

@@ -2,11 +2,12 @@
#include <Nazara/Widgets/ButtonWidget.hpp> #include <Nazara/Widgets/ButtonWidget.hpp>
#include <client/states/AbstractState.h> #include <client/states/AbstractState.h>
#include <client/states/ImGuiDrawer.h>
namespace blitz { namespace blitz {
namespace client { namespace client {
class MainMenuState : public AbstractState { class MainMenuState : public AbstractState, private ImGuiDrawer {
public: public:
MainMenuState(std::shared_ptr<StateData>); MainMenuState(std::shared_ptr<StateData>);
~MainMenuState(); ~MainMenuState();
@@ -21,6 +22,8 @@ class MainMenuState : public AbstractState {
void LayoutWidgets() override; void LayoutWidgets() override;
bool Update(Nz::StateMachine& fsm, Nz::Time elapsedTime) override; bool Update(Nz::StateMachine& fsm, Nz::Time elapsedTime) override;
void OnRenderImgui() override;
void OnJoinServerPressed(); void OnJoinServerPressed();
void OnCreateServerPressed(); void OnCreateServerPressed();
void OnOptionPressed(); void OnOptionPressed();

View File

@@ -4,14 +4,17 @@
#include <Nazara/Physics3D/Physics3D.hpp> #include <Nazara/Physics3D/Physics3D.hpp>
#include <Nazara/Platform/WindowingAppComponent.hpp> #include <Nazara/Platform/WindowingAppComponent.hpp>
#include <Nazara/Widgets/Widgets.hpp> #include <Nazara/Widgets/Widgets.hpp>
#include <NazaraImgui/NazaraImgui.hpp>
#include <client/ClientApp.h> #include <client/ClientApp.h>
#include <client/ImGuiAppComponent.h>
int main(int argc, char** argv) { int main(int argc, char** argv) {
Nz::Application<Nz::Graphics, Nz::Physics3D, Nz::Widgets, Nz::TextRenderer> app(argc, argv); Nz::Application<Nz::Graphics, Nz::Physics3D, Nz::Widgets, Nz::TextRenderer, Nz::Imgui> app(argc, argv);
app.AddComponent<Nz::EntitySystemAppComponent>(); app.AddComponent<Nz::EntitySystemAppComponent>();
app.AddComponent<Nz::WindowingAppComponent>(); app.AddComponent<Nz::WindowingAppComponent>();
app.AddComponent<blitz::client::ClientApp>(); auto& client = app.AddComponent<blitz::client::ClientApp>();
app.AddComponent<blitz::client::ImGuiAppComponent>(*client.GetWindow());
return app.Run(); return app.Run();
} }

View File

@@ -7,6 +7,7 @@
#include <Nazara/Platform.hpp> #include <Nazara/Platform.hpp>
#include <Nazara/Renderer.hpp> #include <Nazara/Renderer.hpp>
#include <Nazara/Widgets.hpp> #include <Nazara/Widgets.hpp>
#include <NazaraImgui/NazaraImgui.hpp>
#include <client/states/MainMenuState.h> #include <client/states/MainMenuState.h>
#include <random> #include <random>
@@ -36,11 +37,15 @@ ClientApp::ClientApp(Nz::ApplicationBase& app) : Nz::ApplicationComponent(app),
auto renderTarget = std::make_shared<Nz::RenderWindow>(windowSwapchain); auto renderTarget = std::make_shared<Nz::RenderWindow>(windowSwapchain);
Nz::Imgui::Instance()->Init(*m_Window, false);
auto passList = Nz::PipelinePassList::LoadFromFile("assets/example.passlist");
entt::handle cameraEntity = world.CreateEntity(); entt::handle cameraEntity = world.CreateEntity();
{ {
cameraEntity.emplace<Nz::NodeComponent>(); cameraEntity.emplace<Nz::NodeComponent>();
auto& cameraComponent = cameraEntity.emplace<Nz::CameraComponent>(renderTarget, Nz::ProjectionType::Orthographic); auto& cameraComponent = cameraEntity.emplace<Nz::CameraComponent>(renderTarget, passList, Nz::ProjectionType::Orthographic);
cameraComponent.UpdateClearColor(Nz::Color(0.0f, 0.f, .0f, 0.0f)); cameraComponent.UpdateClearColor(Nz::Color(0.0f, 0.f, .0f, 0.0f));
cameraComponent.UpdateRenderMask(RenderMaskUI); cameraComponent.UpdateRenderMask(RenderMaskUI);
@@ -52,6 +57,7 @@ ClientApp::ClientApp(Nz::ApplicationBase& app) : Nz::ApplicationComponent(app),
m_StateData->m_AppComponent = this; m_StateData->m_AppComponent = this;
m_StateData->m_RenderTarget = renderTarget; m_StateData->m_RenderTarget = renderTarget;
m_StateData->m_Window = m_Window; m_StateData->m_Window = m_Window;
m_StateData->m_Swapchain = &windowSwapchain;
m_StateData->m_World = &world; m_StateData->m_World = &world;
m_StateData->m_Canvas.emplace( m_StateData->m_Canvas.emplace(
world.GetRegistry(), m_Window->GetEventHandler(), m_Window->GetCursorController().CreateHandle(), RenderMaskUI); world.GetRegistry(), m_Window->GetEventHandler(), m_Window->GetCursorController().CreateHandle(), RenderMaskUI);

View File

@@ -0,0 +1,91 @@
#include <client/ImGuiAppComponent.h>
#include <Nazara/Core/ApplicationBase.hpp>
#include <Nazara/Platform/Window.hpp>
#include <NazaraImgui/NazaraImgui.hpp>
#include <blitz/common/Log.h>
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

View File

@@ -0,0 +1,17 @@
#include <client/states/ImGuiDrawer.h>
#include <NazaraImgui/NazaraImgui.hpp>
namespace blitz {
namespace client {
ImGuiDrawer::ImGuiDrawer() {
Nz::Imgui::Instance()->AddHandler(this);
}
ImGuiDrawer::~ImGuiDrawer() {
Nz::Imgui::Instance()->RemoveHandler(this);
}
} // namespace client
} // namespace blitz

View File

@@ -3,7 +3,8 @@
#include <Nazara/Core/ApplicationBase.hpp> #include <Nazara/Core/ApplicationBase.hpp>
#include <Nazara/Core/StateMachine.hpp> #include <Nazara/Core/StateMachine.hpp>
#include <Nazara/TextRenderer.hpp> #include <Nazara/TextRenderer.hpp>
#include <NazaraImgui/ImguiHandler.hpp>
#include <NazaraImgui/NazaraImgui.hpp>
#include <client/states/CreateServerState.h> #include <client/states/CreateServerState.h>
#include <client/states/JoinServerState.h> #include <client/states/JoinServerState.h>
#include <client/states/OptionState.h> #include <client/states/OptionState.h>
@@ -81,6 +82,12 @@ void MainMenuState::LayoutWidgets() {
} }
} }
void MainMenuState::OnRenderImgui() {
#ifndef NDEBUG
ImGui::ShowDemoWindow(nullptr);
#endif
}
void MainMenuState::OnJoinServerPressed() { void MainMenuState::OnJoinServerPressed() {
m_NextState = std::make_shared<JoinServerState>(GetStateDataPtr(), shared_from_this()); m_NextState = std::make_shared<JoinServerState>(GetStateDataPtr(), shared_from_this());
} }