Files
Archimedes/src/modules/ImguiModule/ImguiModule.cpp
2026-02-17 14:09:20 -06:00

141 lines
4.0 KiB
C++

#include "ImguiModule.h"
#include "pch.hpp"
ImguiModule::ImguiModule(Archimedes::App* a, void* h = nullptr) : Archimedes::Module(a, h) {
name = "ImguiModule";
WindowModule* wm = new WindowModule(a, h);
deps[*wm] = wm;
}
ImguiModule::~ImguiModule() {
if(app) {
WindowModule* wm; { wm = (WindowModule*) moduleInstances[WindowModule()]; }
#ifdef WINDOW_SDL3
wm->removeEventFn(ecmd_it);
#endif
rendererShutdown();
windowShutdown();
ImGui::DestroyContext();
wm->releaseWindow(window);
}
}
void ImguiModule::onLoad() {
WindowModule* wm; { wm = (WindowModule*) moduleInstances[WindowModule()]; }
if(!wm) {
std::cout << "No WindowModule for ImguiModule!\n";
std::abort();
}
window = (Archimedes::WindowGLFW*) wm->aquireWindow();
#ifdef RENDERER_SDL3
renderer = window->getRenderer()->getRendererImpl()->renderer;
#endif
IMGUI_CHECKVERSION();
context = ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows
#ifdef CUSTOMFONT
io.Fonts->AddFontFromFileTTF(STRINGIZE_VALUE_OF(CUSTOMFONT), 13.0f);
#endif
// Setup Dear ImGui style
ImGui::StyleColorsDark();
//ImGui::StyleColorsLight();
// Setup scaling
ImGuiStyle& style = ImGui::GetStyle();
io.ConfigDpiScaleFonts = true; // [Experimental] Automatically overwrite style.FontScaleDpi in Begin() when Monitor DPI changes. This will scale fonts but _NOT_ scale sizes/padding for now.
io.ConfigDpiScaleViewports = true; // [Experimental] Scale Dear ImGui and Platform Windows when Monitor DPI changes.
// When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
style.WindowRounding = 0.0f;
style.Colors[ImGuiCol_WindowBg].w = 1.0f;
}
// Setup Platform/Renderer backends
if(!windowInit())
std::cout << "windowInit failed\n";
if(!rendererInit()) {
std::cout << "rendererInit failed!\n" << std::endl;
}
#ifdef WINDOW_SDL3
ecmd_it = wm->addEventFn([](Archimedes::Event* e){
if(e->userData.type() == typeid(SDL_Event*)) {
ImGui_ImplSDL3_ProcessEvent(std::any_cast<SDL_Event*>(e->userData));
}
});
#endif
//Compute first frame ahead of first WindowModule->run()
rendererNewFrame();
windowNewFrame();
ImGui::NewFrame();
}
void ImguiModule::run() {
ImGui::Render();
rendererRenderDrawData();
if(ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_ViewportsEnable) {
ImGui::UpdatePlatformWindows();
ImGui::RenderPlatformWindowsDefault();
window->getWindowImpl()->restoreContext();
}
rendererNewFrame();
windowNewFrame();
ImGui::NewFrame();
};
bool ImguiModule::onEvent(const Archimedes::Event &e) {
unsigned int type = app->getEventType(e);
ImGuiIO& io = ImGui::GetIO(); (void)io;
if(type == app->getEventType(Archimedes::KeyPressedWindowEvent())) {
return io.WantCaptureKeyboard;
} else if(type == app->getEventType(Archimedes::KeyReleasedWindowEvent())) {
return io.WantCaptureKeyboard;
} else if(type == app->getEventType(Archimedes::MouseButtonPressedWindowEvent())) {
return io.WantCaptureMouse;
} else if(type == app->getEventType(Archimedes::MouseButtonReleasedWindowEvent())) {
return io.WantCaptureMouse;
} else if(type == app->getEventType(Archimedes::ScrollWindowEvent())) {
return io.WantCaptureMouse;
} else if(type == app->getEventType(Archimedes::MouseMovedWindowEvent())) {
return io.WantCaptureMouse;
}
return false;
}