#ifndef IMGUI_CONTEXT_HPP #define IMGUI_CONTEXT_HPP #include "gl_context.hpp" #include "sdl_context.hpp" #include "sdl_main_window.hpp" #include namespace bookmouse { class ImguiContext { public: ImguiContext(GlContext& glContext, SdlContext& sdlContext, SdlMainWindow& mainWindow); ~ImguiContext(); const ImGuiIO& getIO() const; ImGuiIO& getIO(); void setIOFlag(ImGuiConfigFlags_ flag) const; bool processEvent(SDL_Event& event) const; void startFrame() const; void render() const; private: SdlMainWindow& m_mainWindow; }; template class ConditionalRaii { public: template ConditionalRaii(Args&&... args) : m_expr{false} { Expr expr{}; m_expr = expr(std::forward(args)...); } ~ConditionalRaii() { Cleanup cleanup{}; if (m_expr) { cleanup(); } } ConditionalRaii(const ConditionalRaii& rhs) = delete; ConditionalRaii(ConditionalRaii&& rhs) = delete; ConditionalRaii& operator=(const ConditionalRaii& rhs) = delete; ConditionalRaii& operator=(ConditionalRaii&& rhs) = delete; operator bool() const { return m_expr; } private: bool m_expr; }; #define Imgui ImGui #define STRUCT_FUNCTOR(FUNCTOR_NAME, FUNCTOR_FUNCTION) \ struct FUNCTOR_NAME { \ auto operator()() -> decltype(FUNCTOR_FUNCTION()) \ { \ return FUNCTOR_FUNCTION(); \ } \ }; #define STRUCT_FUNCTOR_ARGS(FUNCTOR_NAME, FUNCTOR_FUNCTION) \ struct FUNCTOR_NAME { \ template \ auto operator()(Args&&... args) -> /* FORCE BREAK */ \ decltype(FUNCTOR_FUNCTION(std::forward(args)...)) \ { \ return FUNCTOR_FUNCTION(std::forward(args)...); \ } \ }; STRUCT_FUNCTOR_ARGS(ImBeginMenuBar, Imgui::BeginMenuBar) STRUCT_FUNCTOR(ImEndMenuBar, Imgui::EndMenuBar) using ImMenuBar = ConditionalRaii; STRUCT_FUNCTOR_ARGS(ImBeginMenu, Imgui::BeginMenu) STRUCT_FUNCTOR(ImEndMenu, Imgui::EndMenu) using ImMenu = ConditionalRaii; STRUCT_FUNCTOR_ARGS(ImBeginPopupModal, Imgui::BeginPopupModal) // N.B. EndPopup is needed for BeginPopupModal STRUCT_FUNCTOR(ImEndPopupModal, Imgui::EndPopup) using ImPopupModal = ConditionalRaii; STRUCT_FUNCTOR_ARGS(ImBeginTable, Imgui::BeginTable) STRUCT_FUNCTOR(ImEndTable, Imgui::EndTable) using ImTable = ConditionalRaii; // STRUCT_FUNCTOR_ARGS(Im #undef Imgui #undef STRUCT_FUNCTOR #undef STRUCT_FUNCTOR_ARGS constexpr auto ImBegin = ImGui::Begin; constexpr auto ImEnd = ImGui::End; inline void ImSameLine(float xOffset = 0.0, float spacing = 0.0) { ImGui::SameLine(xOffset, spacing); } #define ImText ImGui::Text inline bool ImButton(const char* label, const ImVec2& size = ImVec2(0, 0)) { return ImGui::Button(label, size); } constexpr auto ImGetTime = ImGui::GetTime; inline void ImNextRow(ImGuiTableRowFlags row_flags = 0, float min_row_height = 0.0f) { ImGui::TableNextRow(row_flags, min_row_height); } constexpr auto ImNextColumn = ImGui::TableNextColumn; #define ImSelectable ImGui::Selectable } // namespace bookmouse #endif