Zen 0.3.0
Loading...
Searching...
No Matches
ZEN_ImGuiLayer.cpp
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>
11
12namespace Zen {
13 void ImGuiLayer::onAttach() {
14 ZEN_LOG_TRACE("ATTACHING");
15 IMGUI_CHECKVERSION();
16 ImGui::CreateContext();
17 ZEN_LOG_TRACE("imgui context created");
18 ImGuiIO &io = ImGui::GetIO();
19
20 io.IniFilename = nullptr;
21 io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
22 io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
23 io.ConfigFlags |= ImGuiConfigFlags_DpiEnableScaleFonts;
24 io.ConfigFlags |= ImGuiConfigFlags_DpiEnableScaleViewports;
25
26 styleSetup();
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());
34
35 ZEN_LOG_TRACE("Imgui init");
36 ImGui_ImplSDL3_InitForOpenGL(window, context);
37 ImGui_ImplOpenGL3_Init(glslVersion);
38 }
39
40 void ImGuiLayer::onDetach() {
41 ImGui_ImplOpenGL3_Shutdown();
42 ImGui_ImplSDL3_Shutdown();
43 ImGui::DestroyContext();
44 }
45
46 void ImGuiLayer::onUpdate(DeltaTime deltaTime) {
47 // ImGui::ShowDemoWindow();
48 }
49
50 bool ImGuiLayer::onEvent(const ZenEvent &event) {
51 ImGuiIO &io = ImGui::GetIO(); // valid after NewFrame()
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;
63 default:
64 return false;
65 }
66 }
67
68 void ImGuiLayer::begin() {
69 // LOG_INFO("new frame");
70 ImGui_ImplOpenGL3_NewFrame();
71 ImGui_ImplSDL3_NewFrame();
72 ImGui::NewFrame();
73 // LOG_INFO("Done begin frame");
74 }
75
76 void ImGuiLayer::end() {
77 ImGui::Render();
78 ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
79
80 ImGuiIO &io = ImGui::GetIO();
81
82 Application &app = Application::get();
83 io.DisplaySize = ImVec2((float)app.getWindow().getWidth(), (float)app.getWindow().getHeight());
84
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);
91 }
92 }
93} // namespace Zen