summaryrefslogtreecommitdiffhomepage
path: root/editor/events.cpp
blob: 94a8f94fded5433a235a9c257231f973aa8c53e9 (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
#include "app.hpp"

#include "floormat/main.hpp"
#include "floormat/events.hpp"
#include "src/world.hpp"
#include "keys.hpp"
#include <tuple>
#include <Magnum/Platform/Sdl2Application.h>
#include <Magnum/ImGuiIntegration/Context.hpp>

namespace floormat {

void app::on_focus_in() noexcept {}
void app::on_mouse_enter() noexcept {}
void app::on_any_event(const any_event&) noexcept {}

#define accessor(type, name) \
    type m_##name = {}; auto name() const noexcept { return m_##name; }

static constexpr int fixup_mods_(int mods, int value, int mask)
{
    return !!(mods & mask) * value;
}

static constexpr int fixup_mods(int mods)
{
    int ret = 0;
    ret |= fixup_mods_(mods, kmod_ctrl,  KMOD_CTRL);
    ret |= fixup_mods_(mods, kmod_shift, KMOD_SHIFT);
    ret |= fixup_mods_(mods, kmod_alt,   KMOD_ALT);
    ret |= fixup_mods_(mods, kmod_super, KMOD_GUI);
    return ret;
}

void app::clear_keys(key min_inclusive, key max_exclusive)
{
    using key_type = decltype(keys)::value_type;
    for (key_type i = key_type(min_inclusive); i < key_type(max_exclusive); i++)
    {
        const auto idx = key(i);
        keys[idx] = false;
        key_modifiers[i] = kmod_none;
    }
}

void app::clear_keys()
{
    keys.reset();
    key_modifiers = {};
}

void app::on_mouse_move(const mouse_move_event& event) noexcept
{
    if (!(event.position >= Vector2i() && event.position < M->window_size()))
        return;

    struct {
        accessor(Vector2i, position)
    } e = {event.position};

    if ((cursor.in_imgui = _imgui.handleMouseMoveEvent(e)))
        void();
    else if (_editor.mode() == editor_mode::tests && tests_handle_mouse_move(event))
        void();
    update_cursor_tile(event.position);
    do_mouse_move(fixup_mods(event.mods));
}

void app::on_mouse_up_down(const mouse_button_event& event, bool is_down) noexcept
{
    if (!(event.position >= Vector2i() && event.position < M->window_size()))
        return;

    struct ev {
        enum class Button : std::underlying_type_t<mouse_button> {
            Left = mouse_button_left,
            Right = mouse_button_right,
            Middle = mouse_button_middle,
        };
        accessor(Vector2i, position)
        accessor(Button, button)
    } e = {event.position, ev::Button(event.button)};

    if ((cursor.in_imgui = is_down ? _imgui.handleMousePressEvent(e) : _imgui.handleMouseReleaseEvent(e)))
        void();
    else if (_editor.mode() == editor_mode::tests && tests_handle_mouse_click(event, is_down))
        void();
    else
        do_mouse_up_down(event.button, is_down, fixup_mods(event.mods));
}

void app::on_mouse_scroll(const mouse_scroll_event& event) noexcept
{
    if (!(event.position >= Vector2i() && event.position < M->window_size()))
        return;

    struct {
        accessor(Vector2, offset)
        accessor(Vector2i, position)
    } e = {event.offset, event.position};

    if (!(cursor.in_imgui = _imgui.handleMouseScrollEvent(e)))
        do_mouse_scroll((int)e.offset()[1]);
}

auto app::resolve_keybinding(int k_, int mods_) -> std::tuple<key, int>
{
    [[maybe_unused]] constexpr int CTRL  = kmod_ctrl;
    [[maybe_unused]] constexpr int SHIFT = kmod_shift;
    [[maybe_unused]] constexpr int ALT   = kmod_alt;
    [[maybe_unused]] constexpr int SUPER = kmod_super;

    const int k = k_ | fixup_mods(mods_);
    constexpr kmod list[] = { kmod_none, kmod_super, kmod_alt, kmod_shift, kmod_ctrl, };
    int last = ~0;
    for (int k1 = k; kmod mod1 : list)
    {
        k1 &= ~mod1;
        for (int k2 = k1; kmod mod2 : list)
        {
            k2 &= ~mod2;
            if (k2 == last)
                continue;
            last = k2;
            auto mods = k2 & kmod_mask;
            auto ret = [=]
            {
                switch (k2)
                {
                default:            return key_noop;
                case SDLK_w:        return key_camera_up;
                case SDLK_a:        return key_camera_left;
                case SDLK_s:        return key_camera_down;
                case SDLK_d:        return key_camera_right;
                case SDLK_HOME:     return key_camera_reset;
                case SDLK_r:        return key_rotate_tile;
                case SDLK_1:        return key_mode_none;
                case SDLK_2:        return key_mode_floor;
                case SDLK_3:        return key_mode_walls;
                case SDLK_4:        return key_mode_scenery;
                case SDLK_5:        return key_mode_vobj;
                // for things like:
                // - detect collisions with a line placed using the cursor (can be diagonal)
                // - make charactere pathfind somewhere
                // - make character walk around waypoints
                case SDLK_6:        return key_mode_tests;
                case SDLK_c | ALT:  return key_render_collision_boxes;
                case SDLK_l | ALT:  return key_render_clickables;
                case SDLK_v | ALT:  return key_render_vobjs;
                case SDLK_t:        return key_render_all_z_levels;
                case SDLK_F5:       return key_quicksave;
                case SDLK_F9:       return key_quickload;
                case SDLK_q | CTRL: return key_quit;
                case SDLK_n | CTRL: return key_new_file;
                case SDLK_ESCAPE:   return key_escape;
                case SDLK_LEFT:     return key_left;
                case SDLK_RIGHT:    return key_right;
                case SDLK_UP:       return key_up;
                case SDLK_DOWN:     return key_down;
                }
            }();
            if (ret == key_noop)
                continue;
            else
                return {ret, mods};
        }
    }
    return { key_COUNT, k & kmod_mask };
}

void app::clear_non_global_keys() { clear_keys(key_MIN, key_GLOBAL); }
void app::clear_non_repeated_keys() { clear_keys(key_NO_REPEAT, key_COUNT); }

void app::on_key_up_down(const key_event& event, bool is_down) noexcept
{
    using KeyEvent = Platform::Sdl2Application::KeyEvent;
    struct Ev final {
        using Key = KeyEvent::Key;
        using Modifier = KeyEvent::Modifier;
        using Modifiers = KeyEvent::Modifiers;
        accessor(Key, key)
        accessor(Modifiers, modifiers)
    } e = {Ev::Key(event.key), Ev::Modifier(event.mods)};

    auto [x, mods] = resolve_keybinding(event.key, event.mods);
    static_assert(key_GLOBAL >= key_NO_REPEAT);

    if (x == key_COUNT && (is_down ? _imgui.handleKeyPressEvent(e) : _imgui.handleKeyReleaseEvent(e)) ||
        (x == key_COUNT || x == key_escape) && _editor.mode() == editor_mode::tests && tests_handle_key(event, is_down))
        clear_non_global_keys();
    else if (x >= key_NO_REPEAT)
        is_down && !event.is_repeated ? do_key(x, mods) : void();
    else
    {
        keys[x] = is_down;
        key_modifiers[size_t(x)] = mods;
    }
}

void app::on_text_input_event(const text_input_event& event) noexcept
{
    struct {
        accessor(Containers::StringView, text)
    } e = {event.text};
    if (_imgui.handleTextInputEvent(e))
        clear_non_global_keys();
}

void app::on_viewport_event(const Math::Vector2<int>& size) noexcept
{
    init_imgui(size);
}

void app::on_focus_out() noexcept
{
    update_cursor_tile(NullOpt);
    clear_keys();
}

void app::on_mouse_leave() noexcept
{
    update_cursor_tile(NullOpt);
}

void app::do_key(key k)
{
    do_key(k, get_key_modifiers());
}

int app::get_key_modifiers()
{
    return fixup_mods(M->get_mods());
}

void app::set_cursor_from_imgui()
{
    _imgui.updateApplicationCursor(M->application());
}

} // namespace floormat