summaryrefslogtreecommitdiffhomepage
path: root/editor/imgui-raii.hpp
blob: ebb99886924da2c75deb34d2de8f5e7ff00b8df8 (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
#pragma once
#include "compat/prelude.hpp"
#include <Corrade/Containers/StringView.h>
#include <Magnum/Magnum.h>
#include <imgui.h>
#include <imgui_internal.h>

namespace floormat::imgui {

struct raii_wrapper final
{
    using F = void(*)(void);
    raii_wrapper(F fn);
    raii_wrapper() = default;
    ~raii_wrapper();
    raii_wrapper(const raii_wrapper&) = delete;
    raii_wrapper& operator=(const raii_wrapper&) = delete;
    raii_wrapper& operator=(raii_wrapper&&) = delete;
    raii_wrapper(raii_wrapper&& other) noexcept;
    operator bool() const noexcept;

private:
    F dtor = nullptr;
};

[[nodiscard]] raii_wrapper begin_window(StringView name = {}, ImGuiWindowFlags_ flags = ImGuiWindowFlags_None);
[[nodiscard]] raii_wrapper begin_main_menu();
[[nodiscard]] raii_wrapper begin_menu(StringView name, bool enabled = true);
[[nodiscard]] raii_wrapper begin_list_box(StringView name, ImVec2 size = {});
[[nodiscard]] raii_wrapper tree_node(StringView name, ImGuiTreeNodeFlags_ flags = ImGuiTreeNodeFlags_None);
[[nodiscard]] raii_wrapper push_style_var(ImGuiStyleVar_ var, Vector2 value);
[[nodiscard]] raii_wrapper push_style_var(ImGuiStyleVar_ var, float value);
[[nodiscard]] raii_wrapper push_style_color(ImGuiCol_ var, const Color4& value);
void text(const char* str, std::size_t len, ImGuiTextFlags_ flags = ImGuiTextFlags_NoWidthForLargeClippedText);

template<std::size_t N>
void text(const char (&buf)[N], ImGuiTextFlags_ flags = ImGuiTextFlags_NoWidthForLargeClippedText)
{
    ImGui::TextEx(buf, buf + N - 1, flags);
}

struct style_saver final
{
    style_saver();
    ~style_saver();
private:
    ImGuiStyle style;
};

struct font_saver final
{
    font_saver(float size);
    ~font_saver();
private:
    font_saver(ImGuiContext& ctx, float size);

    float font_size, font_base_size;
};

} // namespace floormat::imgui