summaryrefslogtreecommitdiffhomepage
path: root/editor/editor.hpp
blob: 94cb7709637434d69d36203bc3c1aad42d958f24 (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
#pragma once
#include "compat/defs.hpp"
#include "compat/integer-types.hpp"
#include "global-coords.hpp"
#include "tile-image.hpp"
#include "scenery.hpp"
#include "editor-enums.hpp"
#include "tile-editor.hpp"

#include <optional>
#include <map>
#include <memory>
#include <Corrade/Containers/StringView.h>

namespace floormat {

struct world;
struct anim_atlas;
struct tile_atlas;

struct scenery_editor final
{
    struct pair final {
        std::shared_ptr<anim_atlas> atlas;
        scenery s;
    };

    scenery_editor() noexcept;

    void set_rotation(rotation r);
    void rotation() const;
    void next_rotation();
    void prev_rotation();

    void select_tile(const std::shared_ptr<anim_atlas>& atlas, enum rotation r, std::uint16_t frame);
    void clear_selection();

private:
    void load_atlases();

    std::map<StringView, std::shared_ptr<anim_atlas>> _atlases;
    pair _selected_frame;
};

struct editor final
{
    [[nodiscard]] bool dirty() const noexcept { return _dirty; }
    void set_dirty(bool value) noexcept { _dirty = value; }
    [[nodiscard]] editor_mode mode() const noexcept { return _mode; }
    void set_mode(editor_mode mode);

    tile_editor* current_tile_editor() noexcept;
    const tile_editor* current_tile_editor() const noexcept;

    enum class button : std::uint8_t { none, place, remove, };

    void on_click(world& world, global_coords pos, int mods, button b);
    void on_click_(world& world, global_coords pos, button b);
    void on_mouse_move(world& world, global_coords& pos, int modifiers);
    void on_release();

    editor();
    editor(editor&&) noexcept = default;
    editor& operator=(editor&&) noexcept = default;
    fm_DECLARE_DELETED_COPY_ASSIGNMENT(editor);

    static constexpr inline auto rotation_N = editor_wall_rotation::N;
    static constexpr inline auto rotation_W = editor_wall_rotation::W;
    using snap_mode = editor_snap_mode;

private:
    snap_mode get_snap_value(snap_mode snap, int mods) const;
    static global_coords apply_snap(global_coords pos, global_coords last, snap_mode snap) noexcept;

    tile_editor _floor{ editor_mode::floor, "floor" };
    tile_editor _wall { editor_mode::walls, "wall"  };

    struct drag_pos final {
        global_coords coord, draw_coord;
        snap_mode snap = snap_mode::none;
        button btn;
    };
    std::optional<drag_pos> _last_pos;
    editor_mode _mode = editor_mode::floor;
    bool _dirty = false;
};

} // namespace floormat