nomlib
Loading...
Searching...
No Matches
EventHandler.hpp
1/******************************************************************************
2
3 nomlib - C++11 cross-platform game engine
4
5Copyright (c) 2013, 2014 Jeffrey Carpenter <i8degrees@gmail.com>
6All rights reserved.
7
8Redistribution and use in source and binary forms, with or without
9modification, are permitted provided that the following conditions are met:
10
111. Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
132. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28******************************************************************************/
29#ifndef NOMLIB_SYSTEM_EVENT_HANDLER_HPP
30#define NOMLIB_SYSTEM_EVENT_HANDLER_HPP
31
32#include <deque>
33#include <vector>
34
35#include "export.hpp"
36#include "nomlib/config.hpp"
37#include "nomlib/system/Event.hpp"
38
39// Forward declarations (third-party)
40union SDL_Event;
41
42namespace nom {
43
44// Forward declarations
45struct event_watcher;
48
50class NOM_EXPORT EventHandler
51{
52 public:
53 typedef EventHandler self_type;
54
55 enum JoystickHandlerType
56 {
57 NO_EVENT_HANDLER = 0,
58 SDL_JOYSTICK_EVENT_HANDLER,
59 GAME_CONTROLLER_EVENT_HANDLER,
60 };
61
62 EventHandler();
63
64 ~EventHandler();
65
67 EventHandler(const EventHandler& rhs) = delete;
68
70 EventHandler& operator =(const EventHandler& rhs) = delete;
71
73 nom::size_type num_events() const;
74
76 nom::size_type num_event_watchers() const;
77
85
94
95 JoystickHandlerType joystick_event_type() const;
96
101
106
109
112
116 bool poll_event(Event& ev);
117
121 void push_event(const Event& ev);
122
127
132
135
140 void append_event_watch(const event_filter& filter, void* data);
141
148 void remove_event_watch(const event_filter& filter);
149
152
153 private:
154 bool pop_event(Event& ev);
155
158
160 void process_event(const SDL_Event* ev);
161
162 void process_joystick_event(const SDL_Event* ev);
163 void process_game_controller_event(const SDL_Event* ev);
164
168 std::deque<Event> events_;
169
170 std::vector<std::unique_ptr<event_watcher>> event_watchers_;
171
174 nom::size_type max_events_count_ = 0;
175
176 void* joystick_event_handler_ = nullptr;
177 JoystickHandlerType joystick_event_type_ = NO_EVENT_HANDLER;
178};
179
180NOM_EXPORT Event create_key_press(int32 sym, uint16 mod, uint8 repeat);
181NOM_EXPORT Event create_key_release(int32 sym, uint16 mod, uint8 repeat);
182
183NOM_EXPORT
184Event create_mouse_button_click(uint8 button, uint8 clicks, uint32 window_id);
185
186NOM_EXPORT
187Event create_mouse_button_release(uint8 button, uint8 clicks, uint32 window_id);
188
189NOM_EXPORT
190Event create_joystick_button_press(JoystickID id, uint8 button);
191
192NOM_EXPORT
193Event create_joystick_button_release(JoystickID id, uint8 button);
194
195NOM_EXPORT
196Event create_joystick_hat_motion(JoystickID id, uint8 hat, uint8 value);
197
198NOM_EXPORT
199Event create_game_controller_button_press(JoystickID id, uint8 button);
200NOM_EXPORT
201Event create_game_controller_button_release(JoystickID id, uint8 button);
202
203NOM_EXPORT
204Event create_user_event(int32 code, void* data1, void* data2, uint32 window_id);
205
206NOM_EXPORT Event create_quit_event(void* data1, void* data2);
207
208} // namespace nom
209
210#endif // include guard defined
211
void flush_events(Event::EventType type)
Clear events from the queue.
void flush_events()
Clear the events queue.
EventHandler(const EventHandler &rhs)=delete
Disabled copy constructor.
void process_event(const SDL_Event *ev)
Enumerate the available events from the underlying platform.
bool enable_game_controller_polling()
Initialize game controller events polling.
void process_events()
Enumerate the available events from the underlying platform.
void append_event_watch(const event_filter &filter, void *data)
Add an event listener.
bool poll_event(Event &ev)
Enumerate the available events.
void flush_event(Event::EventType type)
Clear an event from the queue.
JoystickEventHandler * joystick_event_handler() const
Get a non-owned pointer to the joystick device event handler.
void disable_game_controller_polling()
Shutdown the game controller events polling subsystem.
nom::size_type num_events() const
Get the total number of enqueued events.
GameControllerEventHandler * game_controller_event_handler() const
Get a non-owned pointer to the game controller device event handler.
void remove_event_watchers()
Erase all event watchers.
nom::size_type max_events_count_
The maximum number of events processed per queue cycle – i.e.: one frame of the game's update loop.
void disable_joystick_polling()
Shutdown the joystick events polling subsystem.
void push_event(const Event &ev)
Enqueue an event.
void remove_event_watch(const event_filter &filter)
Erase an event listener.
bool enable_joystick_polling()
Initialize joystick events polling.
std::deque< Event > events_
Enqueued events.
nom::size_type num_event_watchers() const
Get the total number of event watchers.
Internal management of hot-pluggable joystick devices handling.
Internal management of hot-pluggable joystick devices handling.
Event handling types.
Definition Event.hpp:458