summaryrefslogtreecommitdiffstats
path: root/include/event.hpp
blob: 12e898f1d159f92b17b81da9e8a5cfd9000a32df (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
#pragma once

#include "mm/mmvec.hpp"
#include <memory>

extern "C" {
#include <SDL2/SDL_events.h>
}

namespace wsdl2 {

    // forward declaration
    class window;
}

namespace wsdl2::event {

    class event_t {

        SDL_Event m_event;

    protected:

        SDL_Event& sdl() { return m_event; }

        const SDL_Event& sdl() const { return m_event; }

   public:

        event_t(const SDL_Event& e);

        // polymorphic
        virtual ~event_t() {}

        // copy constructor
        event_t(const event_t& e);

        uint32_t type() const;
    };

    struct e_key : public event_t
    {
        enum class action_t : unsigned
        {
            up = SDL_KEYUP, 
            down = SDL_KEYDOWN
        };

        using event_t::event_t;
        e_key(const event_t& e) : event_t(e) {}

        action_t action() const;

        // key pressed or released
        SDL_Keycode get() const;
    };

    namespace mouse {

        enum class action_t : unsigned
        {
            button_up = SDL_MOUSEBUTTONUP,
            button_down = SDL_MOUSEBUTTONDOWN, 
            motion = SDL_MOUSEMOTION, 
            wheel = SDL_MOUSEWHEEL
        };

        struct e_mouse : public event_t
        {
            virtual ~e_mouse() {}

            using event_t::event_t;
            e_mouse(const event_t& e) : event_t(e) {}

            action_t action() const;

            virtual mm::vec2<int> location() const = 0;
        };

        struct e_button : public e_mouse
        {
            using e_mouse::e_mouse;
            e_button(const event_t& e) : e_mouse(e) {}

            enum class button_t : unsigned
            {
                left = SDL_BUTTON_LEFT,
                middle = SDL_BUTTON_MIDDLE,
                right = SDL_BUTTON_RIGHT,
                x1 = SDL_BUTTON_X1,
                x2 = SDL_BUTTON_X2 
            };

            mm::vec2<int> clicks() const;

            button_t which() const;

            virtual mm::vec2<int> location() const override;
        };

        struct e_motion : public e_mouse 
        {
            using e_mouse::e_mouse;
            e_motion(const event_t& e) : e_mouse(e) {}

            // delta (x, y)
            mm::vec2<int> movement() const;

            virtual mm::vec2<int> location() const override;
        };

        struct e_wheel : public e_mouse
        {
            using e_mouse::e_mouse;
            e_wheel(const event_t& e) : e_mouse(e) {}

            int horizontal() const;
            int vertical() const;

            virtual mm::vec2<int> location() const override;
        };

    }

    struct e_quit : public event_t
    {
        using event_t::event_t;
        e_quit(const event_t& e) : event_t(e) {}
    };

    namespace window {

        enum class action_t : unsigned
        {
            shown = SDL_WINDOWEVENT_SHOWN, 
            hidden = SDL_WINDOWEVENT_HIDDEN, 
            exposed = SDL_WINDOWEVENT_EXPOSED, 
            moved = SDL_WINDOWEVENT_MOVED, 
            resized = SDL_WINDOWEVENT_RESIZED,
            size_changed = SDL_WINDOWEVENT_SIZE_CHANGED, 
            minimized = SDL_WINDOWEVENT_MINIMIZED, 
            maximized = SDL_WINDOWEVENT_MAXIMIZED, 
            restored = SDL_WINDOWEVENT_RESTORED, 
            enter = SDL_WINDOWEVENT_ENTER, 
            leave = SDL_WINDOWEVENT_LEAVE, 
            focus_gained = SDL_WINDOWEVENT_FOCUS_GAINED,
            focus_lost = SDL_WINDOWEVENT_FOCUS_LOST, 
            close = SDL_WINDOWEVENT_CLOSE, 
            take_focus = SDL_WINDOWEVENT_TAKE_FOCUS, 
            hit_test = SDL_WINDOWEVENT_HIT_TEST
        };


        struct e_window : public event_t
        {

            using event_t::event_t;
            e_window(const event_t& e) : event_t(e) {}

            // TODO, mapping sdl window id with wsdl2 object
            wsdl2::window * window();

            action_t action() const;
        };

        // window positional event
        struct e_move : public e_window
        {
            using e_window::e_window;
            e_move(const event_t& e) : e_window(e) {}

            mm::vec2<uint32_t> position();
        };

        // window bound event
        struct e_resize : public e_window
        {
            using e_window::e_window;
            e_resize(const event_t& e) : e_window(e) {}

            mm::vec2<uint32_t> size();
        };
    }

    // TODO other handlers

    std::shared_ptr<event_t> poll_event();
}