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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
#include "imgui-raii.hpp"
#include "compat/assert.hpp"
#include <Corrade/Containers/StringView.h>
#include <Magnum/Magnum.h>
#include <Magnum/Math/Color.h>
namespace floormat::imgui {
font_saver::~font_saver()
{
auto& ctx = *ImGui::GetCurrentContext();
ctx.FontSize = font_size;
ctx.FontBaseSize = font_base_size;
}
font_saver::font_saver(float size) :
font_size{ImGui::GetCurrentContext()->FontSize},
font_base_size{ImGui::GetCurrentContext()->FontBaseSize}
{
auto& ctx = *ImGui::GetCurrentContext();
ctx.FontSize = size;
ctx.FontBaseSize = size;
}
draw_list_font_saver::draw_list_font_saver(float size) :
font_size{ImGui::GetForegroundDrawList()->_Data->FontSize}
{
ImGui::GetForegroundDrawList()->_Data->FontSize = size;
}
draw_list_font_saver::~draw_list_font_saver()
{
ImGui::GetForegroundDrawList()->_Data->FontSize = font_size;
}
style_saver::style_saver() : style{ImGui::GetStyle()} {}
style_saver::~style_saver() { ImGui::GetStyle() = style; }
void text(StringView str, ImGuiTextFlags flags)
{
ImGui::TextEx(str.data(), str.data() + str.size(), flags);
}
raii_wrapper::raii_wrapper(raii_wrapper::F fn) : dtor{fn} {}
raii_wrapper::~raii_wrapper() { if (dtor) dtor(); }
raii_wrapper::raii_wrapper(raii_wrapper&& other) noexcept : dtor{other.dtor} { other.dtor = nullptr; }
raii_wrapper& raii_wrapper::operator=(raii_wrapper&& other) noexcept
{
dtor = std::exchange(other.dtor, nullptr);
return *this;
}
raii_wrapper::operator bool() const noexcept { return dtor != nullptr; }
raii_wrapper push_style_color(ImGuiCol_ var, const Color4& value)
{
ImGui::PushStyleColor(var, {value[0], value[1], value[2], value[3]});
return {[]{ ImGui::PopStyleColor(); }};
}
raii_wrapper push_id(StringView str)
{
ImGui::PushID(str.data(), str.data() + str.size());
return {[]{ ImGui::PopID(); }};
}
raii_wrapper push_style_var(ImGuiStyleVar_ var, float value)
{
ImGui::PushStyleVar(var, value);
return {[]{ ImGui::PopStyleVar(); }};
}
raii_wrapper push_style_var(ImGuiStyleVar_ var, Vector2 value)
{
ImGui::PushStyleVar(var, {value[0], value[1]});
return {[]{ ImGui::PopStyleVar(); }};
}
raii_wrapper tree_node(Containers::StringView name, ImGuiTreeNodeFlags flags)
{
fm_assert(name.flags() & StringViewFlag::NullTerminated);
if (ImGui::TreeNodeEx(name.data(), flags))
return {&ImGui::TreePop};
else
return {};
}
raii_wrapper begin_disabled(bool is_disabled)
{
ImGui::BeginDisabled(is_disabled);
return {&ImGui::EndDisabled};
}
raii_wrapper begin_combo(StringView name, StringView preview, ImGuiComboFlags flags)
{
fm_assert(name.flags() & StringViewFlag::NullTerminated);
fm_assert(preview.flags() & StringViewFlag::NullTerminated);
if (ImGui::BeginCombo(name.data(), preview.data(), flags))
return {&ImGui::EndCombo};
else
return {};
}
raii_wrapper begin_popup(StringView name, ImGuiWindowFlags flags)
{
fm_assert(name.flags() & StringViewFlag::NullTerminated);
if (ImGui::BeginPopup(name.data(), flags))
return {&ImGui::EndPopup};
else
return {};
}
raii_wrapper begin_list_box(Containers::StringView name, ImVec2 size)
{
fm_assert(name.flags() & StringViewFlag::NullTerminated);
if (ImGui::BeginListBox(name.data(), size))
return {&ImGui::EndListBox};
else
return {};
}
raii_wrapper begin_table(StringView id, int ncols, ImGuiTableFlags flags, const ImVec2& outer_size, float inner_width)
{
fm_assert(id.flags() & StringViewFlag::NullTerminated);
if (ImGui::BeginTable(id.data(), ncols, flags, outer_size, inner_width))
return {&ImGui::EndTable};
else
return {};
}
raii_wrapper begin_menu(Containers::StringView name, bool enabled)
{
if (ImGui::BeginMenu(name.data(), enabled))
return {&ImGui::EndMenu};
else
return {};
}
raii_wrapper begin_main_menu()
{
if (ImGui::BeginMainMenuBar())
return {&ImGui::EndMainMenuBar};
else
return {};
}
raii_wrapper begin_window(Containers::StringView name, bool* p_open, ImGuiWindowFlags flags)
{
if (name.isEmpty())
name = "floormat editor";
if (ImGui::Begin(name.data(), p_open, flags))
return {&ImGui::End};
else
return {};
}
raii_wrapper begin_child(StringView name, const ImVec2& size, int flags, int window_flags)
{
ImGui::BeginChild(name.data(), size, flags, window_flags);
return {&ImGui::EndChild};
}
} // namespace floormat::imgui
|