summaryrefslogtreecommitdiffhomepage
path: root/editor/imgui.cpp
blob: 83824967b60ba6b8a40573baf7a7283041cf02c9 (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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "app.hpp"
#include "floormat/main.hpp"
#include <Magnum/GL/Renderer.h>
#include "imgui-raii.hpp"

namespace floormat {

using namespace floormat::imgui;

void app::init_imgui(Vector2i size)
{
    if (!_imgui.context())
        _imgui = ImGuiIntegration::Context(Vector2{size}, size, size);
    else
        _imgui.relayout(Vector2{size}, size, size);
}

void app::render_menu()
{
    GL::Renderer::setBlendEquation(GL::Renderer::BlendEquation::Add, GL::Renderer::BlendEquation::Add);
    GL::Renderer::setBlendFunction(GL::Renderer::BlendFunction::SourceAlpha, GL::Renderer::BlendFunction::OneMinusSourceAlpha);
    GL::Renderer::enable(GL::Renderer::Feature::Blending);

    GL::Renderer::enable(GL::Renderer::Feature::ScissorTest);
    GL::Renderer::disable(GL::Renderer::Feature::FaceCulling);
    GL::Renderer::disable(GL::Renderer::Feature::DepthTest);

    _imgui.drawFrame();
}

float app::draw_main_menu()
{
    float main_menu_height = 0;
    if (auto b = begin_main_menu())
    {
        if (auto b = begin_menu("File"))
        {
            ImGui::MenuItem("Open", "Ctrl+O");
            ImGui::MenuItem("Recent");
            ImGui::Separator();
            ImGui::MenuItem("Save", "Ctrl+S");
            ImGui::MenuItem("Save as...", "Ctrl+Shift+S");
            ImGui::Separator();
            ImGui::MenuItem("Close");
        }
        if (auto b = begin_menu("Mode"))
        {
            ImGui::MenuItem("Select", "1", _editor.mode() == editor_mode::select);
            ImGui::MenuItem("Floor",  "2", _editor.mode() == editor_mode::floor);
            ImGui::MenuItem("Walls",  "3", _editor.mode() == editor_mode::walls);
        }

        main_menu_height = ImGui::GetContentRegionMax().y;
    }
    return main_menu_height;
}

void app::draw_ui()
{
    ImGui::GetIO().IniFilename = nullptr;
    _imgui.newFrame();
    ImGui::StyleColorsDark(&ImGui::GetStyle());

    const float main_menu_height = draw_main_menu();
    if (auto* ed = _editor.current(); ed != nullptr)
        draw_editor_pane(*ed, main_menu_height);
    draw_fps();
    draw_cursor_tile();
    draw_cursor_tile_text();
    ImGui::EndFrame();
}

void app::draw_editor_pane(tile_editor& ed, float main_menu_height)
{
    const auto window_size = M->window_size();

    constexpr
    Color4 color_perm_selected{1, 1, 1, .7f},
           color_selected{1, 0.843f, 0, .8f},
           color_hover{0, .8f, 1, .7f};

    if (const bool active = M->is_text_input_active();
        ImGui::GetIO().WantTextInput != active)
        active ? M->start_text_input() : M->stop_text_input();

    [[maybe_unused]] const raii_wrapper vars[] = {
        push_style_var(ImGuiStyleVar_WindowPadding, {8, 8}),
        push_style_var(ImGuiStyleVar_WindowBorderSize, 0),
        push_style_var(ImGuiStyleVar_FramePadding, {4, 4}),
        push_style_color(ImGuiCol_WindowBg, {0, 0, 0, .5}),
        push_style_color(ImGuiCol_FrameBg, {0, 0, 0, 0}),
    };

    const auto& style = ImGui::GetStyle();

    if (main_menu_height > 0)
    {
        ImGui::SetNextWindowPos({0, main_menu_height+style.WindowPadding.y});
        ImGui::SetNextFrameWantCaptureKeyboard(false);
        ImGui::SetNextWindowSize({420, window_size[1] - main_menu_height - style.WindowPadding.y});
        if (const auto flags = ImGuiWindowFlags_(ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings);
            auto b = begin_window({}, flags))
        {
            const float window_width = ImGui::GetWindowWidth() - 32;

            char buf[128];

            if (auto b = begin_list_box("##atlases", {-FLT_MIN, -1}))
            {
                for (const auto& [k, v] : ed)
                {
                    const auto& v_ = v;
                    const auto click_event = [&] {
                        if (ImGui::IsItemClicked(ImGuiMouseButton_Right))
                            ed.select_tile_permutation(v_);
                        else if (ImGui::IsItemClicked(ImGuiMouseButton_Middle))
                            ed.clear_selection();
                    };
                    const auto do_caption = [&] {
                        click_event();
                        if (ed.is_atlas_selected(v_))
                        {
                          ImGui::SameLine();
                          ImGui::Text(" (selected)");
                        }
                        std::snprintf(buf, sizeof(buf), "%zu", (std::size_t)v_->num_tiles());
                        ImGui::SameLine(window_width - ImGui::CalcTextSize(buf).x - style.FramePadding.x - 4);
                        ImGui::Text("%s", buf);
                    };
                    const auto N = v->num_tiles();
                    if (const auto flags = ImGuiTreeNodeFlags_(ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_Framed);
                        auto b = tree_node(k.data(), flags))
                    {
                        do_caption();
                        [[maybe_unused]] const raii_wrapper vars[] = {
                            push_style_var(ImGuiStyleVar_FramePadding, {2, 2}),
                            push_style_color(ImGuiCol_ButtonHovered, color_hover),
                        };
                        const bool perm_selected = ed.is_permutation_selected(v);
                        constexpr std::size_t per_row = 8;
                        for (std::size_t i = 0; i < N; i++)
                        {
                            const bool selected = ed.is_tile_selected(v, i);

                            if (i > 0 && i % per_row == 0)
                                ImGui::NewLine();

                            [[maybe_unused]] const raii_wrapper vars[] = {
                                selected ? push_style_color(ImGuiCol_Button, color_selected) : raii_wrapper{},
                                selected ? push_style_color(ImGuiCol_ButtonHovered, color_selected) : raii_wrapper{},
                                perm_selected ? push_style_color(ImGuiCol_Button, color_perm_selected) : raii_wrapper{},
                                perm_selected ? push_style_color(ImGuiCol_ButtonHovered, color_perm_selected) : raii_wrapper{},
                            };

                            std::snprintf(buf, sizeof(buf), "##item_%zu", i);
                            const auto uv = v->texcoords_for_id(i);
                            constexpr ImVec2 size_2 = { TILE_SIZE[0]*.5f, TILE_SIZE[1]*.5f };
                            ImGui::ImageButton(buf, (void*)&v->texture(), size_2,
                                               { uv[3][0], uv[3][1] }, { uv[0][0], uv[0][1] });
                            if (ImGui::IsItemClicked(ImGuiMouseButton_Left))
                                ed.select_tile(v, i);
                            else
                                click_event();
                            ImGui::SameLine();
                        }
                        ImGui::NewLine();
                    }
                    else
                        do_caption();
                }
            }
        }
    }
}

void app::draw_fps()
{
    const auto frame_time = M->smoothed_dt();
    char buf[16];
    const double hz = frame_time > 1e-6f ? (int)std::round(10./(double)frame_time + .05) * .1 : 9999;
    snprintf(buf, sizeof(buf), "%.1f FPS", hz);
    const ImVec2 size = ImGui::CalcTextSize(buf);
    ImDrawList& draw = *ImGui::GetForegroundDrawList();
    draw.AddText({M->window_size()[0] - size.x - 4, 3}, ImGui::ColorConvertFloat4ToU32({0, 1, 0, 1}), buf);
}

void app::draw_cursor_tile_text()
{
    if (!cursor.tile)
        return;

    char buf[64];
    const auto coord = *cursor.tile;
    const auto chunk = coord.chunk();
    const auto local = coord.local();
    snprintf(buf, sizeof(buf), "%hd:%hd - %hhu:%hhu", chunk.x, chunk.y, local.x, local.y);
    const auto size = ImGui::CalcTextSize(buf);
    const auto window_size = M->window_size();

    ImDrawList& draw = *ImGui::GetForegroundDrawList();
    draw.AddText({window_size[0]*.5f - size.x/2, 3}, (unsigned)-1, buf);
}

} // namespace floormat