summaryrefslogtreecommitdiffhomepage
path: root/editor/imgui.cpp
blob: 535f028bacd8a97c9424054f365445b68780e9cf (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include "app.hpp"
#include "floormat/main.hpp"
#include "compat/format.hpp"
#include "imgui-raii.hpp"
#include "src/world.hpp"
#include "src/anim-atlas.hpp"
#include "floormat/main.hpp"
#include "main/clickable.hpp"
#include <Corrade/Containers/Optional.h>
#include <Magnum/Math/Color.h>

namespace floormat {

using namespace floormat::imgui;

bool popup_target::operator==(const popup_target&) const = default;

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()
{
    _imgui.drawFrame();
}

float app::draw_main_menu()
{
    float main_menu_height = 0;
    if (auto b = begin_main_menu())
    {
        ImGui::SetWindowFontScale(M->dpi_scale().min());
        if (auto b = begin_menu("File"))
        {
#if 0
            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");
            ImGui::Separator();
#endif
            bool do_new = false, do_quickload = false, do_quit = false;
            ImGui::MenuItem("New", nullptr, &do_new);
            ImGui::Separator();
            ImGui::MenuItem("Load quicksave", nullptr, &do_quickload);
            ImGui::Separator();
            ImGui::MenuItem("Quit", "Ctrl+Q", &do_quit);
            if (do_new)
                do_key(key_new_file, kmod_none);
            else if (do_quickload)
                do_key(key_quickload, kmod_none);
            else if (do_quit)
                do_key(key_quit, kmod_none);
        }
        if (auto b = begin_menu("Mode"))
        {
            bool can_rotate = false;
            if (auto* ed = _editor.current_tile_editor())
                can_rotate = ed->is_anything_selected();
            else if (auto* ed = _editor.current_scenery_editor())
                can_rotate = ed->is_anything_selected();
            auto mode = _editor.mode();
            using m = editor_mode;
            bool b_none = mode == m::none, b_floor = mode == m::floor, b_walls = mode == m::walls,
                 b_rotate = false, b_scenery = mode == m::scenery,
                 b_collisions = _render_bboxes, b_clickables = _render_clickables;
            if (ImGui::MenuItem("Select",  "1", &b_none))
                do_key(key_mode_none);
            if (ImGui::MenuItem("Floor",   "2", &b_floor))
                do_key(key_mode_floor);
            if (ImGui::MenuItem("Walls",   "3", &b_walls))
                do_key(key_mode_walls);
            if (ImGui::MenuItem("Scenery", "4", &b_scenery))
                do_key(key_mode_scenery);
            if (ImGui::MenuItem("Show collisions", "Alt+C", &b_collisions))
                do_key(key_render_collision_boxes);
            if (ImGui::MenuItem("Show clickables", "Alt+L", &b_clickables))
                do_key(key_render_clickables);
            ImGui::Separator();
            if (ImGui::MenuItem("Rotate", "R", &b_rotate, can_rotate))
                do_key(key_rotate_tile);
        }

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

void app::draw_ui()
{
    const auto dpi = M->dpi_scale().min();
    [[maybe_unused]] const auto style_ = style_saver{};
    auto& style = ImGui::GetStyle();
    auto& ctx = *ImGui::GetCurrentContext();

    ImGui::StyleColorsDark(&style);
    style.ScaleAllSizes(dpi);

    ImGui::GetIO().IniFilename = nullptr;
    _imgui.newFrame();

    if (_render_clickables)
        draw_clickables();

    const float main_menu_height = draw_main_menu();
    [[maybe_unused]] auto font = font_saver{ctx.FontSize*dpi};
    if (_editor.current_tile_editor() || _editor.current_scenery_editor())
        draw_editor_pane(main_menu_height);
    draw_fps();
    draw_tile_under_cursor();
    if (_editor.mode() == editor_mode::none)
        draw_inspector();
    if (_popup_target.target != popup_target_type::none)
        do_popup_menu();
    ImGui::EndFrame();
}

void app::draw_fps()
{
    const auto dpi = M->dpi_scale();
    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;
    snformat(buf, "{:.1f} FPS"_cf, hz);
    const ImVec2 size = ImGui::CalcTextSize(buf);
    ImDrawList& draw = *ImGui::GetForegroundDrawList();
    draw.AddText(nullptr, ImGui::GetCurrentContext()->FontSize,
                 {M->window_size()[0] - size.x - 3.5f*dpi[0], 3*dpi[1]}, ImGui::ColorConvertFloat4ToU32({0, 1, 0, 1}), buf);
#if 0
    static auto timer = fm_begin( Timeline t; t.start(); return t; );
    if (timer.currentFrameDuration() >= 2)
    {
        timer.start();
        std::printf("FPS %f\n", (double)(frame_time > 1e-6f ? 1/frame_time : 0));
        std::fflush(stdout);
    }
#endif
}

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

    const auto dpi = M->dpi_scale();
    char buf[64];
    const auto coord = *cursor.tile;
    const auto chunk = coord.chunk();
    const auto local = coord.local();
    snformat(buf, "{}:{} - {}:{}"_cf, 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(nullptr, ImGui::GetCurrentContext()->FontSize,
                 {window_size[0]*.5f - size.x/2, 3*dpi[1]}, (unsigned)-1, buf);
}

void app::draw_clickables()
{
    ImDrawList& draw = *ImGui::GetForegroundDrawList();
    const auto color = ImGui::ColorConvertFloat4ToU32({0, .8f, .8f, .95f});

    for (const auto& x : M->clickable_scenery())
    {
        auto dest = Math::Range2D<float>(x.dest);
        auto min = dest.min(), max = dest.max();
        draw.AddRect({ min.x(), min.y() }, { max.x(), max.y() },
                     color, 0, ImDrawFlags_None, 2.5f);
    }
}

void app::draw_editor_pane(float main_menu_height)
{
    auto* ed = _editor.current_tile_editor();
    auto* sc = _editor.current_scenery_editor();
    fm_assert(!ed || !sc);

    const auto window_size = M->window_size();
    const auto dpi = M->dpi_scale();

    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*dpi[0], 8*dpi[1]}),
        push_style_var(ImGuiStyleVar_WindowBorderSize, 0),
        push_style_var(ImGuiStyleVar_FramePadding, {4*dpi[0], 4*dpi[1]}),
        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)
    {
        const auto b = push_id("editor");

        ImGui::SetNextWindowPos({0, main_menu_height+style.WindowPadding.y});
        ImGui::SetNextFrameWantCaptureKeyboard(false);
        ImGui::SetNextWindowSize({425 * dpi[0], window_size[1] - main_menu_height - style.WindowPadding.y});
        if (const auto flags = ImGuiWindowFlags_(ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings);
            auto b = begin_window({}, nullptr, flags))
        {
            const auto b2 = push_id("editor-pane");
            if (auto b3 = begin_list_box("##atlases", {-FLT_MIN, -1}))
            {
                if (ed)
                    for (const auto& [k, v] : *ed)
                        draw_editor_tile_pane_atlas(*ed, k, v);
                else if (sc)
                    draw_editor_scenery_pane(*sc);
            }
        }
    }
}

const StringView app::SCENERY_POPUP_NAME = "##scenery-popup"_s;

void app::do_open_popup()
{
    fm_assert(_popup_target.target != popup_target_type::none);
    _pending_popup = true;
}

bool app::check_inspector_exists(popup_target p)
{
    if (p.target == popup_target_type::none) [[unlikely]]
        return true;
    for (const auto& p2 : inspectors)
        if (p2 == p)
            return true;
    return false;
}

void app::do_popup_menu()
{
    fm_assert(_popup_target.target != popup_target_type::none);
    auto& w = M->world();

    auto b0 = push_id(SCENERY_POPUP_NAME);

    const auto [ch, pos, type] = _popup_target;
    auto [c, t] = w[{ch, pos}];
    //if (_popup_target.target != popup_target_type::scenery) {...}
    auto sc = t.scenery();

    if (_pending_popup)
    {
        _pending_popup = false;
        fm_assert(type != popup_target_type::none);
        //if (type != popup_target_type::scenery) {...}
        if (sc)
            ImGui::OpenPopup(SCENERY_POPUP_NAME.data(), ImGuiPopupFlags_NoOpenOverItems);
    }

    if (auto b1 = begin_popup(SCENERY_POPUP_NAME))
    {
        if (ImGui::MenuItem("Activate", nullptr, false, sc.can_activate()))
            sc.activate();
        if (auto next_rot = sc.atlas->next_rotation_from(sc.frame.r);
            ImGui::MenuItem("Rotate", nullptr, false, next_rot != sc.frame.r))
            sc.rotate(next_rot);

        ImGui::Separator();

        if (bool b_ins = sc && !check_inspector_exists(_popup_target);
            ImGui::MenuItem("Inspect", nullptr, false, b_ins))
            inspectors.push_back(std::exchange(_popup_target, {}));
    }
}

void app::kill_popups(bool hard)
{
    _popup_target = { .target = popup_target_type::none };
    ImGui::CloseCurrentPopup();

    if (hard)
    {
        inspectors.clear();
    }

    ImGui::FocusWindow(nullptr);
}

} // namespace floormat