1 | #include <SDL2/SDL.h> |
2 |
|
3 | #include "ZydecoCommon.hpp" |
4 | #include "CommonSDL2.hpp" |
5 | #include "EventHandlerSDL2.hpp" |
6 |
|
7 | #include "IEventKeyboardSubscriber.hpp" |
8 | #include "IEventMouseSubscriber.hpp" |
9 | #include "IEventQuitSubscriber.hpp" |
10 | #include "IEventWindowSubscriber.hpp" |
11 |
|
12 |
|
13 | static Logger LOGGER("EVENTHANDLER"); |
14 |
|
15 |
|
16 | #define DISPATCH_EVENT(TYPE, EVENT, VALS...) \ |
17 | for (IEvent##TYPE ## Subscriber *subscriber : m_event##TYPE ## Subscribers) \ |
18 | { \ |
19 | if (subscriber == nullptr) { break; } \ |
20 | LOGGER.Log(Logger::TRACE, "Dispatching " #TYPE "Event.On" #EVENT); \ |
21 | subscriber->On##EVENT(VALS); \ |
22 | } \ |
23 | break; |
24 |
|
25 | EventHandlerSDL2::EventHandlerSDL2() |
26 | { |
27 |
|
28 | } |
29 |
|
30 | EventHandlerSDL2::~EventHandlerSDL2() |
31 | { |
32 |
|
33 | } |
34 |
|
35 | bool EventHandlerSDL2::Update(uint64_t time_since_last_update_us) |
36 | { |
37 | LOGGER.Log(Logger::DEBUG, "Entering EventHandler loop"); |
38 |
|
39 | bool quit = false; |
40 | SDL_Event event; |
41 |
|
42 | while (SDL_WaitEvent(&event)) |
43 | { |
44 | LOGGER.Log(Logger::TRACE, "Received event {}", event.type); |
45 |
|
46 | switch (event.type) |
47 | { |
48 | case SDL_KEYDOWN: DISPATCH_EVENT(Keyboard, KeyPressEvent, event.key.keysym.sym, event.key.keysym.mod); |
49 | case SDL_KEYUP: DISPATCH_EVENT(Keyboard, KeyReleaseEvent, event.key.keysym.sym, event.key.keysym.mod); |
50 | case SDL_MOUSEBUTTONDOWN: |
51 | { |
52 | switch (event.button.button) |
53 | { |
54 | case SDL_BUTTON_LEFT: DISPATCH_EVENT(Mouse, MouseLeftDownEvent, event.button.x, event.button.y); |
55 | case SDL_BUTTON_MIDDLE: DISPATCH_EVENT(Mouse, MouseMiddleDownEvent, event.button.x, event.button.y); |
56 | case SDL_BUTTON_RIGHT: DISPATCH_EVENT(Mouse, MouseRightDownEvent, event.button.x, event.button.y); |
57 | default: break; |
58 | } |
59 | } |
60 | case SDL_MOUSEBUTTONUP: |
61 | { |
62 | switch (event.button.button) |
63 | { |
64 | case SDL_BUTTON_LEFT: DISPATCH_EVENT(Mouse, MouseLeftUpEvent, event.button.x, event.button.y); |
65 | case SDL_BUTTON_MIDDLE: DISPATCH_EVENT(Mouse, MouseMiddleUpEvent, event.button.x, event.button.y); |
66 | case SDL_BUTTON_RIGHT: DISPATCH_EVENT(Mouse, MouseRightUpEvent, event.button.x, event.button.y); |
67 | default: break; |
68 | } |
69 | } |
70 | case SDL_MOUSEMOTION: DISPATCH_EVENT(Mouse, MouseMoveEvent, event.motion.x, event.motion.y, event.motion.xrel, event.motion.yrel); |
71 | case SDL_MOUSEWHEEL: DISPATCH_EVENT(Mouse, MouseWheelScrollEvent, event.motion.xrel, event.motion.yrel); |
72 | case SDL_QUIT: quit = true; DISPATCH_EVENT(Quit, QuitEvent); |
73 | case SDL_WINDOWEVENT: |
74 | switch (event.window.type) |
75 | { |
76 | case SDL_WINDOWEVENT_MINIMIZED: DISPATCH_EVENT(Window, WindowMinimizedEvent); |
77 | case SDL_WINDOWEVENT_MAXIMIZED: DISPATCH_EVENT(Window, WindowMaximizedEvent); |
78 | case SDL_WINDOWEVENT_RESTORED: DISPATCH_EVENT(Window, WindowRestoredEvent); |
79 | case SDL_WINDOWEVENT_EXPOSED: DISPATCH_EVENT(Window, WindowExposedEvent); |
80 | case SDL_WINDOWEVENT_RESIZED: DISPATCH_EVENT(Window, WindowResizedEvent, event.window.data1, event.window.data2); |
81 | case SDL_WINDOWEVENT_CLOSE: DISPATCH_EVENT(Window, WindowRequestedCloseEvent); |
82 | default: break; |
83 | } |
84 | default: break; |
85 | } |
86 |
|
87 | if (quit) { break; } |
88 | } |
89 |
|
90 | LOGGER.Log(Logger::DEBUG, "Exiting EventHandler loop"); |
91 |
|
92 | return true; |
93 | } |
94 |
|
95 | bool EventHandlerSDL2::RegisterKeyboardEventSubscriber(IEventKeyboardSubscriber *p_event_keyboard_subscriber) |
96 | { |
97 | if (m_eventKeyboardSubscribers.size() >= MAXIMUM_EVENT_SUBSCRIBERS) { return false; } |
98 | m_eventKeyboardSubscribers.push_back(p_event_keyboard_subscriber); |
99 | return true; |
100 | } |
101 |
|
102 | bool EventHandlerSDL2::RegisterMouseEventSubscriber(IEventMouseSubscriber *p_event_mouse_subscriber) |
103 | { |
104 | if (m_eventMouseSubscribers.size() >= MAXIMUM_EVENT_SUBSCRIBERS) { return false; } |
105 | m_eventMouseSubscribers.push_back(p_event_mouse_subscriber); |
106 | return true; |
107 | } |
108 |
|
109 | bool EventHandlerSDL2::RegisterQuitEventSubscriber(IEventQuitSubscriber *p_event_quit_subscriber) |
110 | { |
111 | if (m_eventQuitSubscribers.size() >= MAXIMUM_EVENT_SUBSCRIBERS) { return false; } |
112 | m_eventQuitSubscribers.push_back(p_event_quit_subscriber); |
113 | return true; |
114 | } |
115 |
|
116 | bool EventHandlerSDL2::RegisterWindowEventSubscriber(IEventWindowSubscriber *p_event_window_subscriber) |
117 | { |
118 |
|
119 | if (m_eventWindowSubscribers.size() >= MAXIMUM_EVENT_SUBSCRIBERS) { return false; } |
120 | m_eventWindowSubscribers.push_back(p_event_window_subscriber); |
121 | return true; |
122 | } |
123 |
|