summaryrefslogtreecommitdiffhomepage
path: root/editor/editor.cpp
blob: 011cdc18d314ac2060e82222da87a20b556e3181 (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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include "editor.hpp"
#include "serialize/json-helper.hpp"
#include "serialize/tile-atlas.hpp"
#include "src/loader.hpp"
#include "random.hpp"
#include "compat/assert.hpp"
#include "compat/unreachable.hpp"
#include "src/tile-defs.hpp"
#include "src/world.hpp"
#include "keys.hpp"

#include <Corrade/Containers/StringView.h>

#include <vector>
#include <filesystem>

namespace floormat {

tile_editor::tile_editor(editor_mode mode, StringView name) : _name{ name}, _mode{ mode}
{
    load_atlases();
}

void tile_editor::load_atlases()
{
    static const std::filesystem::path image_path{IMAGE_PATH, std::filesystem::path::generic_format};
    using atlas_array = std::vector<std::shared_ptr<tile_atlas>>;
    for (auto& atlas : json_helper::from_json<atlas_array>(image_path/(_name + ".json")))
    {
        StringView name = atlas->name();
        if (auto x = name.findLast('.'); x)
            name = name.prefix(x.data());
        auto& [_, vec] = _permutation;
        vec.reserve((std::size_t)atlas->num_tiles());
        _atlases[name] = std::move(atlas);
    }
}

std::shared_ptr<tile_atlas> tile_editor::maybe_atlas(StringView str)
{
    auto it = std::find_if(_atlases.begin(), _atlases.end(), [&](const auto& tuple) -> bool {
        const auto& [x, _] = tuple;
        return StringView{x} == str;
    });
    if (it == _atlases.end())
        return nullptr;
    else
        return it->second;
}

std::shared_ptr<tile_atlas> tile_editor::atlas(StringView str)
{
    if (auto ptr = maybe_atlas(str); ptr)
        return ptr;
    else
        fm_abort("no such atlas: %s", str.cbegin());
}

void tile_editor::clear_selection()
{
    _selected_tile = {};
    _permutation = {};
    _selection_mode = sel_none;
}

void tile_editor::select_tile(const std::shared_ptr<tile_atlas>& atlas, std::size_t variant)
{
    fm_assert(atlas);
    clear_selection();
    _selection_mode = sel_tile;
    _selected_tile = { atlas, variant_t(variant % atlas->num_tiles()) };
}

void tile_editor::select_tile_permutation(const std::shared_ptr<tile_atlas>& atlas)
{
    fm_assert(atlas);
    clear_selection();
    _selection_mode = sel_perm;
    _permutation = { atlas, {} };
}

bool tile_editor::is_tile_selected(const std::shared_ptr<const tile_atlas>& atlas, std::size_t variant) const
{
    return atlas && _selection_mode == sel_tile && _selected_tile &&
           atlas == _selected_tile.atlas && variant == _selected_tile.variant;
}

bool tile_editor::is_permutation_selected(const std::shared_ptr<const tile_atlas>& atlas) const
{
    const auto& [perm, _] = _permutation;
    return atlas && _selection_mode == sel_perm && perm == atlas;
}

bool tile_editor::is_atlas_selected(const std::shared_ptr<const tile_atlas>& atlas) const
{
    switch (_selection_mode)
    {
    default:
    case sel_none:
        return false;
    case sel_perm:
        return is_permutation_selected(atlas);
    case sel_tile:
        return atlas && _selected_tile && atlas == _selected_tile.atlas;
    }
}

template<std::random_access_iterator T>
void fisher_yates(T begin, T end)
{
    const auto N = std::distance(begin, end);
    for (auto i = N-1; i >= 1; i--)
    {
        const auto j = random(i+1);
        using std::swap;
        swap(begin[i], begin[j]);
    }
}

tile_image_proto tile_editor::get_selected_perm()
{
    auto& [atlas, vec] = _permutation;
    const auto N = (variant_t)atlas->num_tiles();
    if (N == 0)
        return {};
    if (vec.empty())
    {
        for (variant_t i = 0; i < N; i++)
            vec.push_back(i);
        fisher_yates(vec.begin(), vec.end());
    }
    const auto idx = vec.back();
    vec.pop_back();
    return {atlas, idx};
}

tile_image_proto tile_editor::get_selected()
{
    switch (_selection_mode)
    {
    case sel_none:
        return {};
    case sel_tile:
        return _selected_tile;
    case sel_perm:
        return get_selected_perm();
    default:
        fm_warn_once("invalid editor mode '%u'", (unsigned)_selection_mode);
        break;
    }
}

void tile_editor::place_tile(world& world, global_coords pos, const tile_image_proto& img)
{
    auto [c, t] = world[pos];
    switch (_mode)
    {
    case editor_mode::none:
        break;
    case editor_mode::floor:
        t.ground() = img;
        break;
    case editor_mode::walls:
        switch (_rotation)
        {
        case editor_wall_rotation::N:
            t.wall_north() = img;
            break;
        case editor_wall_rotation::W:
            t.wall_west()  = img;
            break;
        }
        break;
    }
}

void tile_editor::toggle_rotation()
{
    if (_rotation == editor::rotation_W)
        _rotation = editor::rotation_N;
    else
        _rotation = editor::rotation_W;
}

void tile_editor::set_rotation(editor_wall_rotation r)
{
    switch (r)
    {
    default:
        fm_warn_once("invalid rotation '0x%hhx", r);
        return;
    case editor::rotation_W:
    case editor::rotation_N:
        _rotation = r;
        break;
    }
}

auto tile_editor::check_snap(int mods) const -> snap_mode
{
    enum : int {
        KMOD_CTRL = 0x0040 | 0x0080,
        KMOD_SHIFT = 0x0001 | 0x0002,
    };
    const bool ctrl = mods & kmod_ctrl, shift = mods & kmod_shift;

    if (!(ctrl | shift))
        return snap_mode::none;

    switch (_mode)
    {
    default:
    case editor_mode::none:
        return snap_mode::none;
    case editor_mode::walls:
        switch (_rotation)
        {
        case editor_wall_rotation::N:
            return snap_mode::horizontal;
        case editor_wall_rotation::W:
            return snap_mode::vertical;
        default: return snap_mode::none;
        }
    case editor_mode::floor:
        if (shift)
            return snap_mode::horizontal;
        if (ctrl)
            return snap_mode::vertical;
        return snap_mode::none;
    }
}

editor::editor()
{
}

void editor::set_mode(editor_mode mode)
{
    _mode = mode;
    on_release();
}

const tile_editor* editor::current() const noexcept
{
    switch (_mode)
    {
    case editor_mode::floor:
        return &_floor;
    case editor_mode::walls:
        return &_wall; // todo
    default:
        return nullptr;
    }
}

tile_editor* editor::current() noexcept
{
    return const_cast<tile_editor*>(static_cast<const editor&>(*this).current());
}

void editor::on_release()
{
    _last_pos = std::nullopt;
}

auto editor::get_snap_value(snap_mode snap, int mods) const -> snap_mode
{

    if (const auto* mode = current(); mode != nullptr)
        return mode->check_snap(mods);
    else if (snap != snap_mode::none)
        return snap;
    else
        return snap_mode::none;
}

void editor::on_mouse_move(world& world, global_coords& pos, int mods)
{
    if (!current())
        return;

    if (_last_pos)
    {
        auto& last_pos = *_last_pos;
        const snap_mode snap = get_snap_value(last_pos.snap, mods);
        switch (snap)
        {
        default:
            break;
        case snap_mode::horizontal:
            pos.y = last_pos.coord.y;
            break;
        case snap_mode::vertical:
            pos.x = last_pos.coord.x;
            break;
        }
        last_pos = { pos, snap, last_pos.btn };
        on_click(world, pos, mods, last_pos.btn);
    }
}

void editor::on_click(world& world, global_coords pos, int mods, button b)
{
    if (auto* mode = current(); mode != nullptr)
    {
        if (auto opt = mode->get_selected(); opt)
        {
            _last_pos = { pos, mode->check_snap(mods), _last_pos ? _last_pos->btn : b };
            switch (tile_image_proto empty; b)
            {
            case button::place: return mode->place_tile(world, pos, opt);
            case button::remove: return mode->place_tile(world, pos, empty);
            default: break;
            }
        }
        on_release();
    }
}

} // namespace floormat