From 13c40743bf0ec13a09dfe8ea326431403462f49c Mon Sep 17 00:00:00 2001 From: Nick Koirala Date: Thu, 13 Nov 2025 22:15:59 +1300 Subject: [PATCH] chore: refactor to GuiManager --- CMakeLists.txt | 1 + imgui.ini | 39 ++++++++------ include/Application.h | 2 + include/GuiManager.h | 20 ++++++++ src/Application.cpp | 115 +++++------------------------------------- src/GuiManager.cpp | 84 ++++++++++++++++++++++++++++++ 6 files changed, 142 insertions(+), 119 deletions(-) create mode 100644 include/GuiManager.h create mode 100644 src/GuiManager.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 1dbc02d..935d25f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -77,6 +77,7 @@ add_executable(simian src/ScriptEngine.cpp src/ScriptBindings.cpp src/HotReload.cpp + src/GuiManager.cpp src/log/log.c ) diff --git a/imgui.ini b/imgui.ini index 364fba7..95b9b81 100644 --- a/imgui.ini +++ b/imgui.ini @@ -15,43 +15,50 @@ Collapsed=0 [Window][Left Panel] Pos=8,31 -Size=389,561 +Size=359,561 Collapsed=0 DockId=0x00000001,0 [Window][Right Panel] -Pos=8,31 +Pos=403,31 Size=389,561 Collapsed=0 -DockId=0x00000001,1 +DockId=0x00000004,0 [Window][##TOAST2] -Pos=530,314 -Size=250,82 +Pos=558,365 +Size=222,65 Collapsed=0 [Window][##TOAST1] -Pos=530,406 -Size=250,82 +Pos=558,440 +Size=222,65 Collapsed=0 [Window][##TOAST0] -Pos=530,498 -Size=250,82 +Pos=558,515 +Size=222,65 Collapsed=0 [Window][##TOAST4] -Pos=530,130 -Size=250,82 +Pos=558,215 +Size=222,65 Collapsed=0 [Window][##TOAST3] -Pos=530,222 -Size=250,82 +Pos=558,290 +Size=222,65 +Collapsed=0 + +[Window][##TOAST5] +Pos=558,140 +Size=222,65 Collapsed=0 [Docking][Data] -DockSpace ID=0x9076BACA Window=0x34F970D7 Pos=8,31 Size=784,561 Split=X - DockNode ID=0x00000001 Parent=0x9076BACA SizeRef=389,561 Selected=0x6D1308E5 - DockNode ID=0x00000002 Parent=0x9076BACA SizeRef=393,561 CentralNode=1 +DockSpace ID=0x9076BACA Window=0x34F970D7 Pos=8,31 Size=784,561 Split=X + DockNode ID=0x00000003 Parent=0x9076BACA SizeRef=393,561 Split=X + DockNode ID=0x00000001 Parent=0x00000003 SizeRef=389,561 Selected=0x995FC207 + DockNode ID=0x00000002 Parent=0x00000003 SizeRef=393,561 CentralNode=1 + DockNode ID=0x00000004 Parent=0x9076BACA SizeRef=389,561 Selected=0x6D1308E5 diff --git a/include/Application.h b/include/Application.h index 0a25659..53af60c 100644 --- a/include/Application.h +++ b/include/Application.h @@ -1,6 +1,7 @@ #pragma once #include "ScriptEngine.h" #include "HotReload.h" +#include "GuiManager.h" class Application { public: @@ -16,6 +17,7 @@ private: HotReload* hotReload; bool scriptCompilationError; FILE* logFile; + GuiManager guiManager; static const int WINDOW_WIDTH = 800; static const int WINDOW_HEIGHT = 600; diff --git a/include/GuiManager.h b/include/GuiManager.h new file mode 100644 index 0000000..b7e702d --- /dev/null +++ b/include/GuiManager.h @@ -0,0 +1,20 @@ +#pragma once +#include "rlImGui.h" +#include "imgui.h" +#include "extras/IconsFontAwesome6.h" +#include "gui/fa-solid-900.h" +#include "gui/ImGuiNotify.hpp" + +class GuiManager { +public: + GuiManager(); + ~GuiManager(); + + void Initialize(); + void Render(); + void Shutdown(); + +private: + void SetupDockspace(); + void RenderNotifications(); +}; \ No newline at end of file diff --git a/src/Application.cpp b/src/Application.cpp index 8623142..9f32580 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -8,8 +8,8 @@ #include "rlImGui.h" #include "imgui.h" #include "extras/IconsFontAwesome6.h" -#include "gui/fa-solid-900.h" #include "gui/ImGuiNotify.hpp" +#include "GuiManager.h" #endif const char *Application::WINDOW_TITLE = "Raylib + AngelScript"; const char *Application::SCRIPT_FILE = "scripts/test.as"; @@ -56,23 +56,7 @@ bool Application::Initialize() scriptCompilationError = !scriptEngine.CompileScript(SCRIPT_FILE); #if HAVE_RLIMGUI - // Initialize rlImGui if available (sets up ImGui context & fonts) - rlImGuiSetup(true); - // Enable docking support if ImGui is available - ImGuiIO &io = ImGui::GetIO(); - io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; - io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; - io.Fonts->AddFontDefault(); - - float baseFontSize = 16.0f; - float iconFontSize = baseFontSize * 2.0f / 3.0f; // FontAwesome fonts need to have their sizes reduced by 2.0f/3.0f in order to align correctly - - static constexpr ImWchar iconsRanges[] = {ICON_MIN_FA, ICON_MAX_16_FA, 0}; - ImFontConfig iconsConfig; - iconsConfig.MergeMode = true; - iconsConfig.PixelSnapH = true; - iconsConfig.GlyphMinAdvanceX = iconFontSize; - io.Fonts->AddFontFromMemoryCompressedTTF(fa_solid_900_compressed_data, fa_solid_900_compressed_size, iconFontSize, &iconsConfig, iconsRanges); + guiManager.Initialize(); #endif return true; @@ -108,6 +92,12 @@ void Application::Update(float deltaTime) // Call script Update function scriptEngine.CallScriptFunction(scriptEngine.GetUpdateFunction(), deltaTime); + + #if HAVE_RLIMGUI + if (IsKeyPressed(KEY_SPACE)) { + ImGui::InsertNotification({ImGuiToastType::Success,3000, "Space key was pressed!"}); + } + #endif } void Application::Draw() @@ -125,87 +115,7 @@ void Application::Draw() scriptEngine.CallScriptFunction(scriptEngine.GetDrawFunction()); #if HAVE_RLIMGUI - // Simple ImGui demo window to verify integration - rlImGuiBegin(); - { - // Dockspace host - static bool dockspaceOpen = true; - static bool opt_fullscreen = true; - static ImGuiDockNodeFlags dockspace_flags = ImGuiDockNodeFlags_None; - - ImGuiWindowFlags window_flags = ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoDocking; - if (opt_fullscreen) - { - ImGuiViewport *viewport = ImGui::GetMainViewport(); - ImGui::SetNextWindowPos(viewport->WorkPos); - ImGui::SetNextWindowSize(viewport->WorkSize); - ImGui::SetNextWindowViewport(viewport->ID); - ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f); - ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f); - window_flags |= ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove; - window_flags |= ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNavFocus; - } - - // Begin host window - ImGui::Begin("DockSpaceHost", &dockspaceOpen, window_flags); - if (opt_fullscreen) - ImGui::PopStyleVar(2); - - // DockSpace - ImGuiID dockspace_id = ImGui::GetID("MyDockSpace"); - ImGui::DockSpace(dockspace_id, ImVec2(0.0f, 0.0f), dockspace_flags); - - // Example child windows that can be docked - ImGui::Begin("Left Panel"); - ImGui::Text("This is a dockable left panel."); - ImGui::End(); - - ImGui::Begin("Right Panel"); - ImGui::Text("This is a dockable right panel. Drag the tabs around to dock."); - ImGui::End(); - - ImGui::End(); // DockSpaceHost - if (IsKeyPressed(KEY_SPACE)) - { - log_info("Space pressed"); - ImGui::InsertNotification({ImGuiToastType::Success, 3000, "That is a success! %s", "(Format here)"}); - } - } - /** - * Notifications Rendering Start - */ - - // Notifications style setup - ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.f); // Disable round borders - ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.f); // Disable borders - - // Notifications color setup - ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(0.10f, 0.10f, 0.10f, 1.00f)); // Background color - - - // Main rendering function - ImGui::RenderNotifications(); - - - //——————————————————————————————— WARNING ——————————————————————————————— - // Argument MUST match the amount of ImGui::PushStyleVar() calls - ImGui::PopStyleVar(2); - // Argument MUST match the amount of ImGui::PushStyleColor() calls - ImGui::PopStyleColor(1); - - /** - * Notifications Rendering End - */ - - - - - - rlImGuiEnd(); - - ImGui::UpdatePlatformWindows(); - ImGui::RenderPlatformWindowsDefault(); - + guiManager.Render(); #endif EndDrawing(); @@ -222,9 +132,11 @@ void Application::Shutdown() scriptEngine.Shutdown(); #if HAVE_RLIMGUI + guiManager.Shutdown(); +#endif + // Shutdown rlImGui first while the GL context and window are still valid rlImGuiShutdown(); -#endif // Shutdown script engine scriptEngine.Shutdown(); @@ -239,7 +151,4 @@ void Application::Shutdown() fclose(logFile); logFile = nullptr; } - -#if HAVE_RLIMGUI -#endif } \ No newline at end of file diff --git a/src/GuiManager.cpp b/src/GuiManager.cpp new file mode 100644 index 0000000..384653d --- /dev/null +++ b/src/GuiManager.cpp @@ -0,0 +1,84 @@ +#include "GuiManager.h" +#include "raylib.h" + +GuiManager::GuiManager() {} + +GuiManager::~GuiManager() {} + +void GuiManager::Initialize() { + rlImGuiSetup(true); + ImGuiIO &io = ImGui::GetIO(); + io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; + io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; + io.Fonts->AddFontDefault(); + + float baseFontSize = 16.0f; + float iconFontSize = baseFontSize * 2.0f / 3.0f; + + static constexpr ImWchar iconsRanges[] = {ICON_MIN_FA, ICON_MAX_16_FA, 0}; + ImFontConfig iconsConfig; + iconsConfig.MergeMode = true; + iconsConfig.PixelSnapH = true; + iconsConfig.GlyphMinAdvanceX = iconFontSize; + io.Fonts->AddFontFromMemoryCompressedTTF(fa_solid_900_compressed_data, fa_solid_900_compressed_size, iconFontSize, &iconsConfig, iconsRanges); +} + +void GuiManager::Render() { + rlImGuiBegin(); + SetupDockspace(); + RenderNotifications(); + rlImGuiEnd(); + + ImGui::UpdatePlatformWindows(); + ImGui::RenderPlatformWindowsDefault(); +} + +void GuiManager::Shutdown() { + rlImGuiShutdown(); +} + +void GuiManager::SetupDockspace() { + static bool dockspaceOpen = true; + static bool opt_fullscreen = true; + static ImGuiDockNodeFlags dockspace_flags = ImGuiDockNodeFlags_None; + + ImGuiWindowFlags window_flags = ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoDocking; + if (opt_fullscreen) { + ImGuiViewport *viewport = ImGui::GetMainViewport(); + ImGui::SetNextWindowPos(viewport->WorkPos); + ImGui::SetNextWindowSize(viewport->WorkSize); + ImGui::SetNextWindowViewport(viewport->ID); + ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f); + ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f); + window_flags |= ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove; + window_flags |= ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNavFocus; + } + + ImGui::Begin("DockSpaceHost", &dockspaceOpen, window_flags); + if (opt_fullscreen) + ImGui::PopStyleVar(2); + + ImGuiID dockspace_id = ImGui::GetID("MyDockSpace"); + ImGui::DockSpace(dockspace_id, ImVec2(0.0f, 0.0f), dockspace_flags); + + ImGui::Begin("Left Panel"); + ImGui::Text("This is a dockable left panel."); + ImGui::End(); + + ImGui::Begin("Right Panel"); + ImGui::Text("This is a dockable right panel. Drag the tabs around to dock."); + ImGui::End(); + + ImGui::End(); +} + +void GuiManager::RenderNotifications() { + ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.f); + ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.f); + ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(0.10f, 0.10f, 0.10f, 1.00f)); + + ImGui::RenderNotifications(); + + ImGui::PopStyleVar(2); + ImGui::PopStyleColor(1); +} \ No newline at end of file