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