summaryrefslogtreecommitdiffhomepage
path: root/editor/imgui-editors.cpp
blob: a06da28e585724644c0aa21f647c58be33396eca (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
#include "app.hpp"
#include "imgui-raii.hpp"
#include "src/anim-atlas.hpp"
#include "loader/loader.hpp"
#include "floormat/main.hpp"
#include <Magnum/Math/Color.h>

namespace floormat {

using namespace floormat::imgui;

namespace {

StringView scenery_type_to_string(const scenery_editor::scenery_& sc)
{
    switch (sc.proto.sc_type)
    {
    case scenery_type::none:    return "none"_s;
    case scenery_type::generic: return "generic"_s;
    case scenery_type::door:    return "door"_s;
    default:                    return "unknown"_s;
    }
}

std::shared_ptr<anim_atlas> get_atlas(const scenery_editor::scenery_& sc)
{
    return sc.proto.atlas;
}

StringView scenery_name(StringView name, const scenery_editor::scenery_&)
{
    return name;
}

template<typename T> constexpr inline bool do_group_column = false;
template<> constexpr inline bool do_group_column<scenery_editor> = true;

StringView scenery_type_to_string(const vobj_editor::vobj_& vobj)
{
    return vobj.name;
}

std::shared_ptr<anim_atlas> get_atlas(const vobj_editor::vobj_& vobj)
{
    return vobj.factory->atlas();
}

StringView scenery_name(StringView, const vobj_editor::vobj_& vobj)
{
    return vobj.descr;
}

} // namespace

template<typename T>
static void impl_draw_editor_scenery_pane(T& ed, Vector2 dpi)
{
    const auto b1 = push_id("scenery-pane");

    const auto& style = ImGui::GetStyle();
    constexpr ImGuiTableFlags flags = ImGuiTableFlags_BordersInnerV | ImGuiTableFlags_ScrollY;
    constexpr int ncolumns = do_group_column<T> ? 4 : 2;
    const auto size = ImGui::GetWindowSize();
    auto b2 = imgui::begin_table("scenery-table", ncolumns, flags, size);
    const auto row_height = ImGui::GetCurrentContext()->FontSize + 10*dpi[1];
    constexpr auto thumbnail_width = 50;
    ImGui::TableSetupScrollFreeze(1, 1);
    constexpr auto colflags_ = ImGuiTableColumnFlags_NoResize | ImGuiTableColumnFlags_NoReorder | ImGuiTableColumnFlags_NoSort;
    constexpr auto colflags = colflags_ | ImGuiTableColumnFlags_WidthFixed;
    ImGui::TableSetupColumn("##thumbnail", colflags, thumbnail_width);
    ImGui::TableSetupColumn("Name", colflags_ | ImGuiTableColumnFlags_WidthStretch);
    if constexpr(do_group_column<T>)
    {
        const auto colwidth_type = ImGui::CalcTextSize("generic").x;
        const auto colwidth_group = ImGui::CalcTextSize("MMMMMMMMMMMMMMM").x;
        ImGui::TableSetupColumn("Type", colflags, colwidth_type);
        ImGui::TableSetupColumn("Group", colflags, colwidth_group);
    }
    ImGui::TableHeadersRow();

    const auto click_event = [&] {
        if (ImGui::IsItemClicked(ImGuiMouseButton_Middle))
            ed.clear_selection();
    };
    click_event();

    for (const auto& [name, scenery] : ed)
    {
        fm_debug_assert(get_atlas(scenery));
        ImGui::TableNextRow(ImGuiTableRowFlags_None, row_height);

        if (ImGui::TableSetColumnIndex(0))
        {
            auto& atlas = *get_atlas(scenery);
            const auto r = atlas.first_rotation();
            const auto& frame = atlas.frame(r, 0);
            const auto size = Vector2(frame.size);
            const float c = std::min(thumbnail_width / size[0], row_height / size[1]);
            const auto texcoords = atlas.texcoords_for_frame(r, 0, !atlas.group(r).mirror_from.isEmpty());
            const ImVec2 img_size = {size[0]*c, size[1]*c+style.CellPadding.y + 0.5f};
            const ImVec2 uv0 {texcoords[3][0], texcoords[3][1]}, uv1 {texcoords[0][0], texcoords[0][1]};
            ImGui::SetCursorPosX(ImGui::GetCursorPosX() + std::max(0.f, .5f*(thumbnail_width - img_size.x)));
            ImGui::SetCursorPosY(ImGui::GetCursorPosY() + .5f*std::max(0.f, row_height - img_size.y));
            ImGui::Image((void*)&atlas.texture(), img_size, uv0, uv1);
            click_event();
        }
        if (ImGui::TableSetColumnIndex(1))
        {
            constexpr ImGuiSelectableFlags flags = ImGuiSelectableFlags_SpanAllColumns;
            bool selected = ed.is_item_selected(scenery);
            auto name_ = scenery_name(name, scenery);
            if (ImGui::Selectable(name_.data(), &selected, flags, {0, row_height}) && selected)
                ed.select_tile(scenery);
            click_event();
        }
        if (do_group_column<T> && ImGui::TableSetColumnIndex(2))
        {
            text(scenery_type_to_string(scenery));
            click_event();
        }
        if (do_group_column<T> && ImGui::TableSetColumnIndex(3))
        {
            auto& atlas = *get_atlas(scenery);
            StringView name = loader.strip_prefix(atlas.name());
            if (auto last = name.findLast('/'))
                name = name.prefix(last.data());
            else
                name = {};
            text(name);
            click_event();
        }
    }
}

template void impl_draw_editor_scenery_pane(scenery_editor&, Vector2);
template void impl_draw_editor_scenery_pane(vobj_editor&, Vector2);

void app::draw_editor_scenery_pane(scenery_editor& ed)
{
    impl_draw_editor_scenery_pane<scenery_editor>(ed, M->dpi_scale());
}

void app::draw_editor_vobj_pane(vobj_editor& ed)
{
    impl_draw_editor_scenery_pane<vobj_editor>(ed, M->dpi_scale());
}

void app::draw_editor_pane(float main_menu_height)
{
    auto* ed = _editor.current_tile_editor();
    auto* sc = _editor.current_scenery_editor();
    auto* vo = _editor.current_vobj_editor();

    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)
    {
        constexpr auto igwf = ImGuiWindowFlags_(ImGuiWindowFlags_NoDecoration |
                                                ImGuiWindowFlags_NoMove |
                                                ImGuiWindowFlags_NoSavedSettings);
        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 (auto b = begin_window({}, nullptr, igwf))
        {
            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);
                else if (vo)
                    draw_editor_vobj_pane(*vo);
                else if (_editor.mode() == editor_mode::tests)
                    draw_tests_pane();
            }
        }
    }
}

} // namespace floormat