summaryrefslogtreecommitdiffhomepage
path: root/editor/draw.cpp
blob: d1e18292ea56bd9dced3e6a8dad9505edf483223 (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
#include "app.hpp"
#include "src/tile-constants.hpp"
#include "floormat/main.hpp"
#include "floormat/settings.hpp"
#include "shaders/shader.hpp"
#include "main/clickable.hpp"
#include "editor.hpp"
#include "ground-editor.hpp"
#include "wall-editor.hpp"
#include "scenery-editor.hpp"
#include "vobj-editor.hpp"
#include "src/anim-atlas.hpp"
#include "draw/anim.hpp"
#include "draw/wireframe-meshes.hpp"
#include "src/camera-offset.hpp"
#include "src/world.hpp"
#include "src/critter.hpp"
#include "src/RTree-search.hpp"
#include <bit>
#include <Magnum/Math/Color.h>
#include <Magnum/Math/Vector3.h>

namespace floormat {

void app::draw_cursor()
{
    constexpr float LINE_WIDTH = 2;
    auto& shader = M->shader();
    auto& w = M->world();
    const auto inactive_color = 0xff00ffff_rgbaf;

    global_coords tile;
    if (auto pos = _editor->mouse_drag_pos())
        tile = *pos;
    else if (cursor.tile)
        tile = *cursor.tile;
    else
        return;

    if (!cursor.in_imgui)
    {
        const auto draw = [&, pos = tile](auto& mesh, const auto& size) {
            const auto center = Vector3(pos) * TILE_SIZE;
            mesh.draw(shader, {center, size, LINE_WIDTH});
        };

        shader.set_tint({1, 0, 0, 1});

        if (const auto* ed = _editor->current_ground_editor())
        {
            if (!ed->is_anything_selected())
                shader.set_tint(inactive_color);
            draw(_wireframe->quad, TILE_SIZE2);
        }
        else if (const auto* ed = _editor->current_wall_editor())
        {
            if (!ed->is_anything_selected())
                shader.set_tint(inactive_color);
            switch (ed->rotation())
            {
            case rotation::N: draw(_wireframe->wall_n, TILE_SIZE); break;
            case rotation::W: draw(_wireframe->wall_w, TILE_SIZE); break;
            default: std::unreachable();
            }
        }
        else if (const auto* ed = _editor->current_scenery_editor())
        {
            if (!ed->is_anything_selected())
                shader.set_tint(inactive_color);
            const auto& sel = ed->get_selected().proto;
            draw(_wireframe->quad, TILE_SIZE2);
            if (ed->is_anything_selected())
            {
                shader.set_tint({1, 1, 1, 0.75f});
                auto [_g, _w, anim_mesh] = M->meshes();
                const auto offset = Vector3i(Vector2i(sel.offset), 0);
                const auto pos = Vector3i(tile)*iTILE_SIZE + offset;
                auto [ch, t] = w[tile];
                if (!ch.can_place_object(sel, tile.local()))
                    shader.set_tint({1, 0, 1, 0.5f});
                anim_mesh.draw(shader, *sel.atlas, sel.r, sel.frame, Vector3(pos), 1);
            }
        }
        else if (const auto* vo = _editor->current_vobj_editor())
        {
            if (!vo->is_anything_selected())
                shader.set_tint(inactive_color);
            if (vo->is_anything_selected())
            {
                const auto& atlas = vo->get_selected()->factory->atlas();
                draw(_wireframe->quad, TILE_SIZE2);
                shader.set_tint({1, 1, 1, 0.75f});
                auto [_g, _w, anim_mesh] = M->meshes();
                const auto pos = Vector3i(tile)*iTILE_SIZE;
                anim_mesh.draw(shader, *atlas, rotation::N, 0, Vector3(pos), 1);
            }
        }

        shader.set_tint({1, 1, 1, 1});
    }
}

void app::draw_collision_boxes()
{
    auto [z_min, z_max, z_cur, only] = get_z_bounds();
    if (only)
        z_min = z_max = z_cur;
    const auto [minx, maxx, miny, maxy] = M->get_draw_bounds();
    const auto sz = M->window_size();
    auto& world = M->world();
    auto& shader = M->shader();

    using rtree_type = std::decay_t<decltype(*world[chunk_coords_{}].rtree())>;
    using rect_type = typename rtree_type::Rect;

    for (int8_t z = z_min; z <= z_max; z++)
    {
        constexpr Vector4 pass_tint = {.7f, .7f, .7f, .6f};
        const auto tint = z == _z_level ? Vector4{0, .5f, 1, 1} : Vector4{.7f, .7f, .7f, .6f};

        for (int16_t y = miny; y <= maxy; y++)
            for (int16_t x = minx; x <= maxx; x++)
            {
                const chunk_coords_ pos{x, y, z};
                auto*= world.at(pos);
                if (!)
                    continue;
                auto& c = *;
                c.ensure_passability();
                const with_shifted_camera_offset o{shader, pos, {minx, miny}, {maxx, maxy}};
                if (floormat_main::check_chunk_visible(shader.camera_offset(), sz))
                {
                    constexpr float maxf = 1 << 24, max2f[] = { maxf, maxf }, min2f[] = { -maxf, -maxf };
                    const auto* rtree = c.rtree();
                    rtree->Search(min2f, max2f, [&](object_id data, const rect_type& rect) {
                        [[maybe_unused]] auto x = std::bit_cast<collision_data>(data);
#if 1
                        if (x.tag == (uint64_t)collision_type::geometry)
                            return true;
#endif
                        Vector2 start(rect.m_min[0], rect.m_min[1]), end(rect.m_max[0], rect.m_max[1]);
                        auto size = (end - start);
                        auto center = Vector3(start + size*.5f, 0.f);
                        shader.set_tint(x.pass == (uint64_t)pass_mode::pass ? pass_tint : tint);
                        _wireframe->rect.draw(shader, { center, size, 3 });
                        return true;
                    });
                }
            }
    }

    shader.set_tint({1, 0, 1, 1});

    if (cursor.pixel)
    {
        auto pos = tile_shader::project(Vector3d{0., 0., -_z_level*dTILE_SIZE[2]});
        auto pixel = Vector2d{*cursor.pixel} + pos;
        auto coord = M->pixel_to_tile(pixel);
        auto tile = global_coords{coord.chunk(), coord.local(), 0};

        constexpr auto eps = 1e-6f;
        constexpr auto m = TILE_SIZE2 * Vector2(1- eps, 1- eps);
        const auto tile_ = Vector2(M->pixel_to_tile_(Vector2d(pixel)));
        const auto curchunk = Vector2(tile.chunk()), curtile = Vector2(tile.local());
        const auto subpixel_ = Math::fmod(tile_, 1.f);
        // todo use this formula for dragging objs
        const auto subpixel = m * Vector2(curchunk[0] < 0 ? 1 + subpixel_[0] : subpixel_[0],
                                          curchunk[1] < 0 ? 1 + subpixel_[1] : subpixel_[1]);
        for (int16_t y = miny; y <= maxy; y++)
            for (int16_t x = minx; x <= maxx; x++)
            {
                const chunk_coords_ c_pos{x, y, _z_level};
                auto*= world.at(c_pos);
                if (!)
                    continue;
                auto& c = *;
                c.ensure_passability();
                const with_shifted_camera_offset o{shader, c_pos, {minx, miny}, {maxx, maxy}};
                if (floormat_main::check_chunk_visible(shader.camera_offset(), sz))
                {
                    constexpr auto half_tile = TILE_SIZE2/2;
                    constexpr auto chunk_size = TILE_SIZE2 * TILE_MAX_DIM;
                    auto chunk_dist = (curchunk - Vector2(c_pos.x, c_pos.y))*chunk_size;
                    auto t0 = chunk_dist + curtile*TILE_SIZE2 + subpixel - half_tile;
                    auto t1 = t0+Vector2(1e-4f);
                    const auto* rtree = c.rtree();
                    rtree->Search(t0.data(), t1.data(), [&](uint64_t data, const rect_type& rect) {
                        [[maybe_unused]] auto x = std::bit_cast<collision_data>(data);
                        if (x.tag == (uint64_t)collision_type::geometry)
                            return true;
                        Vector2 start(rect.m_min[0], rect.m_min[1]), end(rect.m_max[0], rect.m_max[1]);
                        auto size = end - start;
                        auto center = Vector3(start + size*.5f, 0.f);
                        _wireframe->rect.draw(shader, { center, size, 3 });
                        return true;
                    });
                }
            }
    }

    shader.set_tint({1, 1, 1, 1});
}

void app::draw()
{
    do_lightmap_test();
    if (_render_bboxes)
        draw_collision_boxes();
    if (_editor->current_ground_editor() || _editor->current_wall_editor() ||
        _editor->current_scenery_editor() && _editor->current_scenery_editor()->is_anything_selected() ||
        _editor->current_vobj_editor() && _editor->current_vobj_editor()->is_anything_selected())
        draw_cursor();
    draw_ui();
    render_menu();

    M->texture_unit_cache().output_stats();
}

clickable* app::find_clickable_scenery(const Optional<Vector2i>& pixel)
{
    if (!pixel || _editor->mode() != editor_mode::none)
        return nullptr;

    clickable* item = nullptr;
    float depth = -(1 << 24);

    const auto array = M->clickable_scenery();
    const auto p = *pixel;
    for (clickable& c : array)
        if (c.depth > depth && c.dest.contains(p))
        {
            const auto pos_ = *pixel - c.dest.min() + Vector2i(c.src.min());
            const auto pos = !c.mirrored ? pos_ : Vector2i(int(c.src.sizeX()) - 1 - pos_[0], pos_[1]);
            size_t idx = unsigned(pos.y()) * c.stride + unsigned(pos.x());
            fm_assert(c.bitmask.isEmpty() || idx < c.bitmask.size());
            if (c.bitmask.isEmpty() || c.bitmask[idx])
            {
                depth = c.depth;
                item = &c;
            }
        }
    if (item)
        return item;
    else
        return nullptr;
}

} // namespace floormat