From dac2e7507d0172e2a87ed5b2df9c320bc9717da6 Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Thu, 26 Sep 2024 21:01:52 -0500 Subject: Refactoring of popup modal. --- src/bookmouse.cpp | 66 ++++++++++++++++++++++----------------------------- src/imgui_context.hpp | 24 +++++++++++++------ src/main.cpp | 11 --------- 3 files changed, 46 insertions(+), 55 deletions(-) (limited to 'src') diff --git a/src/bookmouse.cpp b/src/bookmouse.cpp index 4899453..1f6fa00 100644 --- a/src/bookmouse.cpp +++ b/src/bookmouse.cpp @@ -17,26 +17,19 @@ Bookmouse::Bookmouse() : /* * Load Fonts * - * - If no fonts are loaded, dear imgui will use the default - * font. You can also load multiple fonts and use - * ImGui::PushFont()/PopFont() to select them. + * - If no fonts are loaded, dear imgui will use the default font. You can + * also load multiple fonts and use ImGui::PushFont()/PopFont() to select them. * - AddFontFromFileTTF() will return the ImFont* so you can store * it if you need to select the font among multiple. - * - If the file cannot be loaded, the function will return a - * nullptr. Please handle those errors in your application - * (e.g. use an assertion, or display an error and quit). - * - The fonts will be rasterized at a given size (w/ - * oversampling) and stored into a texture when calling - * ImFontAtlas::Build()/GetTexDataAsXXXX(), which - * ImGui_ImplXXXX_NewFrame below will call. - * - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to - * - use Freetype for higher quality font rendering. + * - If the file cannot be loaded, the function will return a nullptr. + * Please handle those errors in your application (e.g. use an assertion, + * or display an error and quit). + * - The fonts will be rasterized at a given size (w/ oversampling) and + * stored into a texture when calling ImFontAtlas::Build() or + * GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call. + * - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use + * Freetype for higher quality font rendering. * - Read 'docs/FONTS.md' for more instructions and details. - * - Remember that in C/C++ if you want to include a backslash \ - * in a string literal you need to write a double backslash \\ ! - * - Our Emscripten build process allows embedding fonts to be - * - accessible at runtime from the "fonts/" folder. See - * Makefile.emscripten for details. * auto imguiIO = m_imgui.getIO(); * imguiIO.Fonts->AddFontDefault(); * imguiIO.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f); @@ -73,19 +66,15 @@ SDL_Event Bookmouse::pollEvent() * Poll and handle events (inputs, window resize, etc.) * You can read the io.WantCaptureMouse, io.WantCaptureKeyboard * flags to tell if dear imgui wants to use your inputs. - * - When io.WantCaptureMouse is true, do not dispatch mouse input - * data to your main application, or clear/overwrite your copy of - * the mouse data. - * - When io.WantCaptureKeyboard is true, do not dispatch keyboard - * input data to your main application, or clear/overwrite your - * copy of the keyboard data. - * Generally you may always pass all inputs to dear imgui, and - * hide them from your application based on those two flags. + * - When io.WantCaptureMouse is true, do not dispatch mouse input data to + * your main application, or clear/overwrite your copy of the mouse data. + * - When io.WantCaptureKeyboard is true, do not dispatch keyboard input + * data to your main application, or clear/overwrite your copy of the + * keyboard data. + * Generally you may always pass all inputs to dear imgui, and hide them + * from your application based on those two flags. */ SDL_Event event{}; - ImGui::SetNextWindowPos(ImVec2(0.0f, 0.0f)); - ImGui::SetNextWindowSize(ImGui::GetIO().DisplaySize); - while (SDL_PollEvent(&event)) { static_cast(m_imgui.processEvent(event)); if (event.type == SDL_QUIT) { @@ -102,21 +91,27 @@ SDL_Event Bookmouse::pollEvent() void Bookmouse::updateState() { ImGui::SetNextWindowPos(ImVec2(0.0f, 0.0f)); - ImGui::SetNextWindowSize(ImGui::GetIO().DisplaySize); + ImGui::SetNextWindowSize(m_imgui.getIO().DisplaySize); auto stateResult = ImGui::Begin( "My First Tool", &m_running, // Is this right? ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_MenuBar); IM_ASSERT(stateResult); + if (ImguiMenuBar menuBar{}) { + IM_ASSERT(menuBar); menuing(); } + if (m_openDialog) { m_openDialog = false; ImGui::OpenPopup(OpenDialogHandle); } - openDialog(); + if (ImguiPopupModal popup{OpenDialogHandle}) { + IM_ASSERT(popup); + openDialog(); + } // Edit a color stored as 4 floats fud::Array my_color{}; @@ -159,13 +154,10 @@ void Bookmouse::menuing() } void Bookmouse::openDialog() { - if (ImGui::BeginPopupModal(OpenDialogHandle)) { - ImGui::Text("Hello from another window!"); - if (ImGui::Button("Close Me")) { - m_openDialog = false; - ImGui::CloseCurrentPopup(); - } - ImGui::EndPopup(); + ImGui::Text("Hello from another window!"); + if (ImGui::Button("Close Me")) { + m_openDialog = false; + ImGui::CloseCurrentPopup(); } } diff --git a/src/imgui_context.hpp b/src/imgui_context.hpp index c6059d6..413bd0b 100644 --- a/src/imgui_context.hpp +++ b/src/imgui_context.hpp @@ -54,6 +54,8 @@ class ConditionalRaii { bool m_expr; }; +#define Imgui ImGui + #define STRUCT_FUNCTOR(FUNCTOR_NAME, FUNCTOR_FUNCTION) \ struct FUNCTOR_NAME { \ auto operator()() -> decltype(FUNCTOR_FUNCTION()) \ @@ -72,17 +74,25 @@ class ConditionalRaii { } \ }; -STRUCT_FUNCTOR_ARGS(ImGuiBeginMenuBar, ImGui::BeginMenuBar) -STRUCT_FUNCTOR(ImGuiEndMenuBar, ImGui::EndMenuBar) +STRUCT_FUNCTOR_ARGS(ImguiBeginMenuBar, Imgui::BeginMenuBar) +STRUCT_FUNCTOR(ImguiEndMenuBar, Imgui::EndMenuBar) + +using ImguiMenuBar = ConditionalRaii; + +STRUCT_FUNCTOR_ARGS(ImguiBeginMenu, Imgui::BeginMenu) +STRUCT_FUNCTOR(ImguiEndMenu, Imgui::EndMenu) + +using ImguiMenu = ConditionalRaii; -using ImguiMenuBar = ConditionalRaii; +STRUCT_FUNCTOR_ARGS(ImguiBeginPopupModal, Imgui::BeginPopupModal) +// N.B. EndPopup is needed for BeginPopupModal +STRUCT_FUNCTOR(ImguiEndPopupModal, Imgui::EndPopup) -STRUCT_FUNCTOR_ARGS(ImGuiBeginMenu, ImGui::BeginMenu) -STRUCT_FUNCTOR(ImGuiEndMenu, ImGui::EndMenu) +using ImguiPopupModal = ConditionalRaii; -using ImguiMenu = ConditionalRaii; +// STRUCT_FUNCTOR_ARGS(Imgui -// STRUCT_FUNCTOR_ARGS(ImGui +#undef Imgui #undef STRUCT_FUNCTOR #undef STRUCT_FUNCTOR_ARGS diff --git a/src/main.cpp b/src/main.cpp index 50ddd7e..8beda51 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -31,14 +31,3 @@ int main(int argc, char* argv[]) bookmouse::Bookmouse bookmouse{}; return bookmouse.run(); } - -void load_levels_example() -{ - // Set the log level to "info" and mylogger to "trace": - // SPDLOG_LEVEL=info,mylogger=trace && ./example - spdlog::cfg::load_env_levels(); - // or from command line: - // ./example SPDLOG_LEVEL=info,mylogger=trace - // #include "spdlog/cfg/argv.h" // for loading levels from argv - // spdlog::cfg::load_argv_levels(args, argv); -} -- cgit v1.2.3