summaryrefslogtreecommitdiff
path: root/src/imgui_context.hpp
blob: 413bd0b1db005e1f58e0bd742df3fc6b2af80eb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#ifndef IMGUI_CONTEXT_HPP
#define IMGUI_CONTEXT_HPP

#include "gl_context.hpp"
#include "sdl_context.hpp"
#include "sdl_main_window.hpp"

#include <imgui/imgui.h>

namespace bookmouse {

class ImguiContext {
  public:
    ImguiContext(GlContext& glContext, SdlContext& sdlContext, SdlMainWindow& mainWindow);
    ~ImguiContext();

    const ImGuiIO& getIO() const;

    void setIOFlag(ImGuiConfigFlags_ flag) const;
    bool processEvent(SDL_Event& event) const;
    void startFrame() const;
    void render() const;

  private:
    SdlMainWindow& m_mainWindow;
};

template <typename Expr, typename Cleanup>
class ConditionalRaii {
  public:
    template <typename... Args>
    ConditionalRaii(Args&&... args) : m_expr{false}
    {
        Expr expr{};
        m_expr = expr(std::forward<Args>(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 <typename... Args>                                 \
        auto operator()(Args&&... args) -> /* FORCE BREAK */        \
            decltype(FUNCTOR_FUNCTION(std::forward<Args>(args)...)) \
        {                                                           \
            return FUNCTOR_FUNCTION(std::forward<Args>(args)...);   \
        }                                                           \
    };

STRUCT_FUNCTOR_ARGS(ImguiBeginMenuBar, Imgui::BeginMenuBar)
STRUCT_FUNCTOR(ImguiEndMenuBar, Imgui::EndMenuBar)

using ImguiMenuBar = ConditionalRaii<ImguiBeginMenuBar, ImguiEndMenuBar>;

STRUCT_FUNCTOR_ARGS(ImguiBeginMenu, Imgui::BeginMenu)
STRUCT_FUNCTOR(ImguiEndMenu, Imgui::EndMenu)

using ImguiMenu = ConditionalRaii<ImguiBeginMenu, ImguiEndMenu>;

STRUCT_FUNCTOR_ARGS(ImguiBeginPopupModal, Imgui::BeginPopupModal)
// N.B. EndPopup is needed for BeginPopupModal
STRUCT_FUNCTOR(ImguiEndPopupModal, Imgui::EndPopup)

using ImguiPopupModal = ConditionalRaii<ImguiBeginPopupModal, ImguiEndPopupModal>;

// STRUCT_FUNCTOR_ARGS(Imgui

#undef Imgui

#undef STRUCT_FUNCTOR
#undef STRUCT_FUNCTOR_ARGS

} // namespace bookmouse

#endif