summaryrefslogtreecommitdiffhomepage
path: root/editor/scenery-editor.cpp
blob: b472fca5e2a7c14ce63ce6a6ae62933569a6dcac (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
#include "scenery-editor.hpp"
#include "src/anim-atlas.hpp"
#include "loader/loader.hpp"
#include "src/world.hpp"
#include "src/RTree-search.hpp"
#include "src/rotation.inl"
#include "app.hpp"
#include "src/scenery.hpp"

#include <Magnum/Math/Range.h>

namespace floormat {

using rotation_t = std::underlying_type_t<enum rotation>;

scenery_editor::scenery_::operator bool() const noexcept
{
    return !!proto;
}

scenery_editor::scenery_editor() noexcept
{
    load_atlases();
}

void scenery_editor::set_rotation(enum rotation r)
{
    auto& s = _selected.proto;
    s.offset = rotate_point(s.offset, s.r, r);
    s.bbox_offset = rotate_point(s.bbox_offset, s.r, r);
    s.bbox_size = rotate_size(s.bbox_size, s.r, r);
    s.r = r;
}

#if 0
enum rotation scenery_editor::rotation() const
{
    return _selected.proto.r;
}
#endif

void scenery_editor::next_rotation()
{
    set_rotation(_selected.proto.atlas->next_rotation_from(_selected.proto.r));
}

void scenery_editor::prev_rotation()
{
    set_rotation(_selected.proto.atlas->prev_rotation_from(_selected.proto.r));
}

void scenery_editor::select_tile(const scenery_& s)
{
    const auto r = s.proto.atlas && s.proto.atlas->check_rotation(_selected.proto.r)
                   ? _selected.proto.r
                   : s.proto.r;
    _selected = s;
    set_rotation(r);
}

void scenery_editor::clear_selection()
{
    _selected = {};
}

auto scenery_editor::get_selected() const -> const scenery_&
{
    return _selected;
}

bool scenery_editor::is_atlas_selected(const std::shared_ptr<anim_atlas>& atlas) const
{
    return atlas == _selected.proto.atlas;
}

bool scenery_editor::is_item_selected(const scenery_& s) const
{
    return s.name == _selected.name && s.proto.atlas == _selected.proto.atlas;
}

bool scenery_editor::is_anything_selected() const
{
    return _selected.proto.atlas != nullptr;
}

void scenery_editor::place_tile(world& w, global_coords pos, const scenery_& s, app& a)
{
    if (!s)
    {
        auto [c, t] = w[pos];
start:
        const auto& es = c.objects();
        const auto sz = es.size();
        while (auto id = a.get_object_colliding_with_cursor())
        {
            for (auto i = 0uz; i < sz; i++)
                if (const auto= es[i];->id == id)
                {->destroy_script_pre(, script_destroy_reason::kill);
                    c.remove_object(i);->destroy_script_post();
                    goto start;
                }
            break;
        }
    }
    else
    {
        auto sc = w.make_scenery(w.make_id(), pos, scenery_proto(s.proto));
        sc->init_script(sc);
    }
}

} // namespace floormat