summaryrefslogtreecommitdiffhomepage
path: root/main/floormat-events.cpp
blob: 5e8aa278c3486e7caac9d6356798c2243de16656 (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
#pragma once
#include "floormat-main-impl.hpp"
#include "floormat-app.hpp"
#include "floormat-events.hpp"
#include "compat/assert.hpp"
#include <SDL_events.h>
#include <SDL_video.h>

namespace floormat {

void main_impl::viewportEvent(Platform::Sdl2Application::ViewportEvent& event)
{
    fm_assert(event.framebufferSize() == event.windowSize());
    recalc_viewport(event.windowSize());
    app.on_viewport_event(event.windowSize());
}

void main_impl::mousePressEvent(Platform::Sdl2Application::MouseEvent& event)
{
    if (app.on_mouse_down(mouse_button_event{event.position(),
                                             (SDL_Keymod)(std::uint16_t)event.modifiers(),
                                             mouse_button(event.button()),
                                             std::uint8_t(std::min(255, event.clickCount()))}))
        return event.setAccepted();
}

void main_impl::mouseReleaseEvent(Platform::Sdl2Application::MouseEvent& event)
{
    if (app.on_mouse_up({event.position(),
                         (SDL_Keymod)(std::uint16_t)event.modifiers(),
                         mouse_button(event.button()),
                         std::uint8_t(std::min(255, event.clickCount()))}))
        return event.setAccepted();
}

void main_impl::mouseMoveEvent(Platform::Sdl2Application::MouseMoveEvent& event)
{
    if (app.on_mouse_move({event.position(), event.relativePosition(),
                           (mouse_button)(std::uint8_t)(std::uint32_t)event.buttons(),
                           (SDL_Keymod)(std::uint16_t)event.modifiers()}))
        return event.setAccepted();
}

void main_impl::mouseScrollEvent(Platform::Sdl2Application::MouseScrollEvent& event)
{
    if (app.on_mouse_scroll(mouse_scroll_event{event.offset(), event.position(),
                                               (SDL_Keymod)(std::uint16_t)event.modifiers()}))
        return event.setAccepted();
}

void main_impl::textInputEvent(Platform::Sdl2Application::TextInputEvent& event)
{
    if (app.on_text_input_event({event.text()}))
        return event.setAccepted();
}

void main_impl::textEditingEvent(Platform::Sdl2Application::TextEditingEvent& event)
{
    if (app.on_text_editing_event({event.text(), event.start(), event.length()}))
        return event.setAccepted();
}

void main_impl::keyPressEvent(Platform::Sdl2Application::KeyEvent& event)
{
    if (app.on_key_down({(SDL_Keycode)(std::uint32_t)event.key(),
                         (SDL_Keymod)(std::uint16_t)event.modifiers(),
                         event.isRepeated()}))
        return event.setAccepted();
}

void main_impl::keyReleaseEvent(Platform::Sdl2Application::KeyEvent& event)
{
    if (app.on_key_up({(SDL_Keycode)(std::uint32_t)event.key(),
                       (SDL_Keymod)(std::uint16_t)event.modifiers(),
                       event.isRepeated()}))
        return event.setAccepted();
}

void main_impl::anyEvent(SDL_Event& event)
{
    if (event.type == SDL_WINDOWEVENT)
        switch (event.window.event)
        {
        case SDL_WINDOWEVENT_FOCUS_LOST:
            return app.on_focus_out();
        case SDL_WINDOWEVENT_FOCUS_GAINED:
            return app.on_focus_in();
        case SDL_WINDOWEVENT_LEAVE:
            return app.on_mouse_leave();
        case SDL_WINDOWEVENT_ENTER:
            return app.on_mouse_enter();
        default:
            return app.on_any_event({event});
        }
}
} // namespace floormat