Files
Archimedes/modules/ImguiModule/src/ImguiModule.cpp
2025-04-09 10:52:23 -05:00

111 lines
3.2 KiB
C++

#include "ImguiModule.h"
#include "imgui.h"
#include "backends/imgui_impl_glfw.h"
#include "backends/imgui_impl_opengl3.h"
#include <GLFW/glfw3.h>
ImguiModule::ImguiModule(void* h, Archimedes::App* a) : Archimedes::GuiModule(h, a) {
name = "ImguiModule";
}
ImguiModule::~ImguiModule() {
}
void ImguiModule::onLoad() {
WindowModule* wm = (WindowModule*) depsInstances["WindowModule"];
if(!wm) {
std::cout << "No WindowModule for ImguiModule!\n";
std::abort();
}
wm->getLayerstack()->push(&layer);
}
void ImguiModule::run() {
ImGuiIO& io = ImGui::GetIO();
static float f = 0.0f;
static int counter = 0;
ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
counter++;
ImGui::SameLine();
ImGui::Text("counter = %d", counter);
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
ImGui::End();
}
ImguiModule::IGLayer::~IGLayer() {}
void ImguiModule::IGLayer::onAttach() {
WindowModule* wm = (WindowModule*) imgui->depsInstances["WindowModule"];
if(!wm) {
std::cout << "No WindowModule for ImguiModule!\n";
std::abort();
}
IMGUI_CHECKVERSION();
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
// Setup Dear ImGui style
ImGui::StyleColorsDark();
//ImGui::StyleColorsLight();
// Setup Platform/Renderer backends
if(!ImGui_ImplGlfw_InitForOpenGL(wm->getWindow()->getWindowImpl().getWindow(), true))
std::cout << "GLFWImpl failed\n";
if(!ImGui_ImplOpenGL3_Init("#version 330")) {
std::cout << "ImGui_ImplOpenGL3_Init failed!\n" << std::endl;
}
//Compute first frame ahead of first WindowModule->run()
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGui::Render();
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
}
void ImguiModule::IGLayer::onRender() {
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
}
bool ImguiModule::IGLayer::onEvent(const Archimedes::Event& e) { return false; }
void ImguiModule::IGLayer::onDetach() {
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
}