Gamedev Framework (gf)  0.3.0
A C++11 framework for 2D games
Event.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  *
21  * Part of this file comes from SFML, with the same license:
22  * Copyright (C) 2007-2015 Laurent Gomila (laurent@sfml-dev.org)
23  */
24 #ifndef GF_EVENT_H
25 #define GF_EVENT_H
26 
27 #include "Gamepad.h"
28 #include "Keyboard.h"
29 #include "Mouse.h"
30 #include "Portability.h"
31 #include "Vector.h"
32 
33 namespace gf {
34 #ifndef DOXYGEN_SHOULD_SKIP_THIS
35 inline namespace v1 {
36 #endif
37 
38  /**
39  * @ingroup window
40  * @brief Enumeration of the different types of events
41  */
42  enum class EventType {
43  // window events
44  Resized, ///< The window was resized (data in event.size)
45  Closed, ///< The window requested to be closed (no data)
46  FocusGained, ///< The window gained focus (no data)
47  FocusLost, ///< The window lost focus (no data)
48 
49  // inputs events
50  KeyPressed, ///< A key was pressed (data in event.key)
51  KeyRepeated, ///< A key was repeated (data in event.key)
52  KeyReleased, ///< A key was released (data in event.key)
53 
54  MouseWheelScrolled, ///< The mouse wheel was scrolled (data in event.mouseWheel)
55  MouseButtonPressed, ///< A mouse button was pressed (data in event.mouseButton)
56  MouseButtonReleased, ///< A mouse button was released (data in event.mouseButton)
57  MouseMoved, ///< The mouse cursor moved (data in event.mouseCursor)
58  MouseEntered, ///< The mouse cursor entered the window (no data)
59  MouseLeft, ///< The mouse cursor left the window (no data)
60 
61  GamepadButtonPressed, ///< A gamepad button was pressed (data in event.gamepadButton)
62  GamepadButtonReleased, ///< A gamepad button was released (data in event.gamepadButton)
63  GamepadAxisMoved, ///< A gamepad axis was moved (data in event.gamepadAxis)
64  GamepadConnected, ///< A gamepad was connected (data in event.gamepadConnection)
65  GamepadDisconnected, ///< A gamepad was disconnected (data in event.gamepadDisconnection)
66  };
67 
68 
69  /**
70  * @ingroup window
71  * @brief Defines a system event and its parameters
72  *
73  * gf::Event holds all the informations about a system event
74  * that just happened. Events are retrieved using the
75  * `gf::Window::pollEvent()` and `gf::Window::waitEvent()` functions.
76  *
77  * A gf::Event instance contains the type of the event
78  * (mouse moved, key pressed, window closed, ...) as well
79  * as the details about this particular event. Please note that
80  * the event parameters are defined in a union, which means that
81  * only the member matching the type of the event will be properly
82  * filled; all other members will have undefined values and must not
83  * be read if the type of the event doesn't match. For example,
84  * if you received a KeyType::KeyPressed event, then you must read the
85  * `event.key` member, all other members such as `event.mouse`
86  * or `event.text` will have undefined values.
87  *
88  * Usage example:
89  *
90  * ~~~{.cc}
91  * gf::Event event;
92  *
93  * while (window.pollEvent(event)) {
94  * // Request for closing the window
95  * if (event.type == gf::EventType::Closed) {
96  * window.close();
97  * }
98  *
99  * // The escape key was pressed
100  * if (event.type == gf::EventType::KeyPressed && event.key.keycode == gf::Keycode::Escape) {
101  * window.close();
102  * }
103  *
104  * // The window was resized
105  * if (event.type == gf::EventType::Resized) {
106  * doSomethingWithTheNewSize(event.size.width, event.size.height);
107  * }
108  *
109  * // etc ...
110  * }
111  * ~~~
112  *
113  * @sa gf::EventType
114  */
115  struct GF_API Event {
116  /**
117  * @brief Keyboard event parameters (EventType::KeyPressed, EventType::KeyReleased, EventType::KeyRepeated)
118  */
119  struct KeyEvent {
120  Keycode keycode; ///< Keycode of the key
121  Scancode scancode; ///< Scancode of the key
122  Modifiers modifiers; ///< Modifiers that are pressed
123  };
124 
125  /**
126  * @brief Text event parameters (EventType::TextEntered)
127  */
128  struct TextEvent {
129  char32_t codepoint; ///< UTF-32 Unicode value of the character
130  };
131 
132  /**
133  * @brief Mouse button event parameters (EventType::MouseButtonPressed, EventType::MouseButtonReleased)
134  */
136  MouseButton button; ///< Code of the button that has been pressed
137  Vector2i coords; ///< Position of the mouse cursor
138  };
139 
140  /**
141  * @brief Mouse cursor move event parameters (EventType::MouseMoved)
142  */
144  Vector2i coords; ///< Position of the mouse cursor
145  };
146 
147  /**
148  * @brief Mouse wheel event parameters (EventType::MouseWheelScrolled)
149  */
151  Vector2i offset; ///< Offset of the mouse wheel
152  };
153 
154  /**
155  * @brief Gamepad button event parameters (EventType::GamepadButtonPressed, EventType::GamepadButtonReleased)
156  */
158  GamepadId id; ///< Id of the gamepad
159  GamepadButton button; ///< Button of the gamepad
160  };
161 
162  /**
163  * @brief Gamepad axis event parameters (EventType::GamepadAxisMoved)
164  */
166  GamepadId id; ///< Id of the gamepad
167  GamepadAxis axis; ///< Axis of the gamepad
168  int16_t value; ///< Value of the axis
169  };
170 
171  /**
172  * @brief Gamepad connection event parameters (EventType::GamepadConnected)
173  */
175  GamepadHwId id; ///< Hardware id of the gamepad
176  };
177 
178  /**
179  * @brief Gamepad disconnection event parameters (EventType::GamepadDisconnected)
180  */
182  GamepadId id; ///< Id of the gamepad
183  };
184 
185  EventType type; ///< Type of the event
186 
187  union {
188  Vector2u size; ///< Size event parameters (EventType::Resized)
189  KeyEvent key; ///< Key event parameters (EventType::KeyPressed, EventType::KeyReleased, EventType::KeyRepeated)
190  TextEvent text; ///< Text event parameters (EventType::TextEntered)
191  MouseButtonEvent mouseButton; ///< Mouse button event parameters (EventType::MouseButtonPressed, EventType::MouseButtonReleased)
192  MouseCursorEvent mouseCursor; ///< Mouse cursor move event parameters (EventType::MouseMoved)
193  MouseWheelEvent mouseWheel; ///< Mouse wheel event parameters (EventType::MouseWheelScrolled)
194  GamepadButtonEvent gamepadButton; ///< Gamepad button event parameters (EventType::GamepadButtonPressed, EventType::GamepadButtonReleased)
195  GamepadAxisEvent gamepadAxis; ///< Gamepad axis event parameters (EventType::GamepadAxisMoved)
196  GamepadConnection gamepadConnection; ///< Gamepad connection event parameters (EventType::GamepadConnected)
197  GamepadDisconnection gamepadDisconnection; ///< Gamepad disconnection event parameters (EventType::GamepadDisconnected)
198  };
199 
200  };
201 
202 #ifndef DOXYGEN_SHOULD_SKIP_THIS
203 }
204 #endif
205 }
206 
207 #endif // GL_EVENT_H
GamepadId id
Id of the gamepad.
Definition: Event.h:166
GamepadButton
The gamepad buttons.
Definition: Gamepad.h:42
Gamepad axis event parameters (EventType::GamepadAxisMoved)
Definition: Event.h:165
The mouse wheel was scrolled (data in event.mouseWheel)
Gamepad disconnection event parameters (EventType::GamepadDisconnected)
Definition: Event.h:181
The mouse cursor left the window (no data)
Vector2i coords
Position of the mouse cursor.
Definition: Event.h:144
A key was repeated (data in event.key)
A gamepad button was pressed (data in event.gamepadButton)
A key was pressed (data in event.key)
Mouse wheel event parameters (EventType::MouseWheelScrolled)
Definition: Event.h:150
The window requested to be closed (no data)
GamepadAxis axis
Axis of the gamepad.
Definition: Event.h:167
Text event parameters (EventType::TextEntered)
Definition: Event.h:128
A gamepad axis was moved (data in event.gamepadAxis)
int16_t value
Value of the axis.
Definition: Event.h:168
Modifiers modifiers
Modifiers that are pressed.
Definition: Event.h:122
A gamepad was connected (data in event.gamepadConnection)
GamepadHwId
A gamepad hardware identifier.
Definition: Gamepad.h:103
GamepadId id
Id of the gamepad.
Definition: Event.h:158
Scancode
Scancodes.
Definition: Keyboard.h:66
A gamepad button was released (data in event.gamepadButton)
Scancode scancode
Scancode of the key.
Definition: Event.h:121
The window was resized (data in event.size)
GamepadAxis
The gamepad axis.
Definition: Gamepad.h:70
Definition: Action.h:34
Keyboard event parameters (EventType::KeyPressed, EventType::KeyReleased, EventType::KeyRepeated) ...
Definition: Event.h:119
EventType
Enumeration of the different types of events.
Definition: Event.h:42
The mouse cursor entered the window (no data)
GamepadId
A gamepad identifier.
Definition: Gamepad.h:120
MouseButton button
Code of the button that has been pressed.
Definition: Event.h:136
The mouse cursor moved (data in event.mouseCursor)
Mouse cursor move event parameters (EventType::MouseMoved)
Definition: Event.h:143
GamepadHwId id
Hardware id of the gamepad.
Definition: Event.h:175
Gamepad button event parameters (EventType::GamepadButtonPressed, EventType::GamepadButtonReleased) ...
Definition: Event.h:157
A mouse button was pressed (data in event.mouseButton)
GamepadId id
Id of the gamepad.
Definition: Event.h:182
char32_t codepoint
UTF-32 Unicode value of the character.
Definition: Event.h:129
GamepadButton button
Button of the gamepad.
Definition: Event.h:159
The window gained focus (no data)
Keycode keycode
Keycode of the key.
Definition: Event.h:120
A key was released (data in event.key)
The window lost focus (no data)
EventType type
Type of the event.
Definition: Event.h:185
Gamepad connection event parameters (EventType::GamepadConnected)
Definition: Event.h:174
Vector2i coords
Position of the mouse cursor.
Definition: Event.h:137
Vector2i offset
Offset of the mouse wheel.
Definition: Event.h:151
Mouse button event parameters (EventType::MouseButtonPressed, EventType::MouseButtonReleased) ...
Definition: Event.h:135
MouseButton
Mouse buttons.
Definition: Mouse.h:36
Defines a system event and its parameters.
Definition: Event.h:115
#define GF_API
Definition: Portability.h:35
A gamepad was disconnected (data in event.gamepadDisconnection)
A mouse button was released (data in event.mouseButton)
Keycode
Keycodes.
Definition: Keyboard.h:293