add handlers to allow imgui code with no access to main loop

This commit is contained in:
SweetId 2022-08-09 21:40:37 +01:00
parent f6c04bbc78
commit afc12c44f8
2 changed files with 26 additions and 0 deletions

View File

@ -8,6 +8,7 @@
#include <NazaraImgui/Config.hpp> #include <NazaraImgui/Config.hpp>
#include <imgui.h> #include <imgui.h>
#include <unordered_set>
namespace Nz namespace Nz
{ {
@ -19,6 +20,12 @@ namespace Nz
class Texture; class Texture;
class Window; class Window;
struct ImguiHandler
{
public:
virtual void OnRenderImgui() = 0;
};
class Imgui : public Nz::ModuleBase<Imgui> class Imgui : public Nz::ModuleBase<Imgui>
{ {
friend ModuleBase; friend ModuleBase;
@ -34,6 +41,10 @@ namespace Nz
void Update(Nz::Window& window, float dt); void Update(Nz::Window& window, float dt);
void Render(Nz::RenderWindow& window, Nz::RenderFrame& frame); void Render(Nz::RenderWindow& window, Nz::RenderFrame& frame);
// User-defined
void AddHandler(ImguiHandler* handler);
void RemoveHandler(ImguiHandler* handler);
// Clipboard functions // Clipboard functions
static void SetClipboardText(void* userData, const char* text); static void SetClipboardText(void* userData, const char* text);
static const char* GetClipboardText(void* userData); static const char* GetClipboardText(void* userData);
@ -79,6 +90,8 @@ namespace Nz
std::shared_ptr<Nz::RenderBuffer> m_uboBuffer; std::shared_ptr<Nz::RenderBuffer> m_uboBuffer;
std::shared_ptr<Nz::Texture> m_fontTexture; std::shared_ptr<Nz::Texture> m_fontTexture;
std::unordered_set<ImguiHandler*> m_handlers;
static Imgui* s_instance; static Imgui* s_instance;
}; };
} }

View File

@ -312,10 +312,23 @@ namespace Nz
void Imgui::Render(Nz::RenderWindow& window, Nz::RenderFrame& frame) void Imgui::Render(Nz::RenderWindow& window, Nz::RenderFrame& frame)
{ {
for (auto* handler : m_handlers)
handler->OnRenderImgui();
ImGui::Render(); ImGui::Render();
RenderDrawLists(window, frame, ImGui::GetDrawData()); RenderDrawLists(window, frame, ImGui::GetDrawData());
} }
void Imgui::AddHandler(ImguiHandler* handler)
{
m_handlers.insert(handler);
}
void Imgui::RemoveHandler(ImguiHandler* handler)
{
m_handlers.erase(handler);
}
void Imgui::SetClipboardText(void* userData, const char* text) void Imgui::SetClipboardText(void* userData, const char* text)
{ {
Imgui* backend = static_cast<Imgui*>(userData); Imgui* backend = static_cast<Imgui*>(userData);