1#include "zen/events/ZEN_Event.h"
2#include <SDL3/SDL_video.h>
3#include <include/imgui/imgui.h>
4#include <include/imgui/imgui_impl_opengl3.h>
5#include <include/imgui/imgui_impl_sdl3.h>
6#include <zen/core/ZEN_Application.h>
7#include <zen/gui/ZEN_ImGuiLayer.h>
8#include <zen/gui/ZEN_Style.h>
9#include <zen/log/ZEN_Log.h>
10#include <zen/time/ZEN_DeltaTime.h>
13 void ImGuiLayer::onAttach() {
14 ZEN_LOG_TRACE(
"ATTACHING");
16 ImGui::CreateContext();
17 ZEN_LOG_TRACE(
"imgui context created");
18 ImGuiIO &io = ImGui::GetIO();
20 io.IniFilename =
nullptr;
21 io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
22 io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
23 io.ConfigFlags |= ImGuiConfigFlags_DpiEnableScaleFonts;
24 io.ConfigFlags |= ImGuiConfigFlags_DpiEnableScaleViewports;
27 ZEN_LOG_TRACE(
"style setup?");
28 const char *glslVersion =
"#version 430";
29 Application &app = Application::get();
30 ZEN_LOG_TRACE(
"app gotten");
31 SDL_Window *window =
static_cast<SDL_Window *
>(app.getWindow().nativeWindow());
32 ZEN_LOG_TRACE(
"window gotten");
33 SDL_GLContext context =
static_cast<SDL_GLContext
>(app.getWindow().context().nativeContext());
35 ZEN_LOG_TRACE(
"Imgui init");
36 ImGui_ImplSDL3_InitForOpenGL(window, context);
37 ImGui_ImplOpenGL3_Init(glslVersion);
40 void ImGuiLayer::onDetach() {
41 ImGui_ImplOpenGL3_Shutdown();
42 ImGui_ImplSDL3_Shutdown();
43 ImGui::DestroyContext();
46 void ImGuiLayer::onUpdate(
DeltaTime deltaTime) {
50 bool ImGuiLayer::onEvent(
const ZenEvent &event) {
51 ImGuiIO &io = ImGui::GetIO();
52 switch (event.header.type) {
53 case EventType::MouseMoved:
54 case EventType::MouseButtonPressed:
55 case EventType::MouseButtonReleased:
56 case EventType::MouseScrolled:
57 return io.WantCaptureMouse;
58 case EventType::KeyPressed:
59 case EventType::KeyReleased:
60 return io.WantCaptureKeyboard;
61 case EventType::TextInput:
62 return io.WantTextInput;
68 void ImGuiLayer::begin() {
70 ImGui_ImplOpenGL3_NewFrame();
71 ImGui_ImplSDL3_NewFrame();
76 void ImGuiLayer::end() {
78 ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
80 ImGuiIO &io = ImGui::GetIO();
82 Application &app = Application::get();
83 io.DisplaySize = ImVec2((
float)app.getWindow().getWidth(), (
float)app.getWindow().getHeight());
85 if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) {
86 SDL_Window *backup_current_window = SDL_GL_GetCurrentWindow();
87 SDL_GLContext backup_current_context = SDL_GL_GetCurrentContext();
88 ImGui::UpdatePlatformWindows();
89 ImGui::RenderPlatformWindowsDefault();
90 SDL_GL_MakeCurrent(backup_current_window, backup_current_context);