Gamedev Framework (gf)  0.4.0
A C++11 framework for 2D games
Event.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2017 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 "Rune.h"
32 #include "Vector.h"
33 
34 namespace gf {
35 #ifndef DOXYGEN_SHOULD_SKIP_THIS
36 inline namespace v1 {
37 #endif
38 
39  /**
40  * @ingroup window
41  * @brief Enumeration of the different types of events
42  */
43  enum class EventType {
44  // window events
45  Resized, ///< The window was resized (data in event.size)
46  Closed, ///< The window requested to be closed (no data)
47  FocusGained, ///< The window gained focus (no data)
48  FocusLost, ///< The window lost focus (no data)
49 
50  // inputs events
51  KeyPressed, ///< A key was pressed (data in event.key)
52  KeyRepeated, ///< A key was repeated (data in event.key)
53  KeyReleased, ///< A key was released (data in event.key)
54 
55  MouseWheelScrolled, ///< The mouse wheel was scrolled (data in event.mouseWheel)
56  MouseButtonPressed, ///< A mouse button was pressed (data in event.mouseButton)
57  MouseButtonReleased, ///< A mouse button was released (data in event.mouseButton)
58  MouseMoved, ///< The mouse cursor moved (data in event.mouseCursor)
59  MouseEntered, ///< The mouse cursor entered the window (no data)
60  MouseLeft, ///< The mouse cursor left the window (no data)
61 
62  GamepadButtonPressed, ///< A gamepad button was pressed (data in event.gamepadButton)
63  GamepadButtonReleased, ///< A gamepad button was released (data in event.gamepadButton)
64  GamepadAxisMoved, ///< A gamepad axis was moved (data in event.gamepadAxis)
65  GamepadConnected, ///< A gamepad was connected (data in event.gamepadConnection)
66  GamepadDisconnected, ///< A gamepad was disconnected (data in event.gamepadDisconnection)
67 
68  TextEntered, ///< A text was entered (data in event.text)
69  };
70 
71 
72  /**
73  * @ingroup window
74  * @brief Defines a system event and its parameters
75  *
76  * gf::Event holds all the informations about a system event
77  * that just happened. Events are retrieved using the
78  * `gf::Window::pollEvent()` and `gf::Window::waitEvent()` functions.
79  *
80  * A gf::Event instance contains the type of the event
81  * (mouse moved, key pressed, window closed, ...) as well
82  * as the details about this particular event. Please note that
83  * the event parameters are defined in a union, which means that
84  * only the member matching the type of the event will be properly
85  * filled; all other members will have undefined values and must not
86  * be read if the type of the event doesn't match. For example,
87  * if you received a KeyType::KeyPressed event, then you must read the
88  * `event.key` member, all other members such as `event.mouse`
89  * or `event.text` will have undefined values.
90  *
91  * Usage example:
92  *
93  * ~~~{.cc}
94  * gf::Event event;
95  *
96  * while (window.pollEvent(event)) {
97  * // Request for closing the window
98  * if (event.type == gf::EventType::Closed) {
99  * window.close();
100  * }
101  *
102  * // The escape key was pressed
103  * if (event.type == gf::EventType::KeyPressed && event.key.keycode == gf::Keycode::Escape) {
104  * window.close();
105  * }
106  *
107  * // The window was resized
108  * if (event.type == gf::EventType::Resized) {
109  * doSomethingWithTheNewSize(event.size.width, event.size.height);
110  * }
111  *
112  * // etc ...
113  * }
114  * ~~~
115  *
116  * @sa gf::EventType
117  */
118  struct GF_API Event {
119  /**
120  * @brief Keyboard event parameters (EventType::KeyPressed, EventType::KeyReleased, EventType::KeyRepeated)
121  */
122  struct KeyEvent {
123  Keycode keycode; ///< Keycode of the key
124  Scancode scancode; ///< Scancode of the key
125  Modifiers modifiers; ///< Modifiers that are pressed
126  };
127 
128  /**
129  * @brief Text event parameters (EventType::TextEntered)
130  */
131  struct TextEvent {
132  Rune rune; ///< The rune
133  };
134 
135  /**
136  * @brief Mouse button event parameters (EventType::MouseButtonPressed, EventType::MouseButtonReleased)
137  */
139  MouseButton button; ///< Code of the button that has been pressed
140  Vector2i coords; ///< Position of the mouse cursor
141  };
142 
143  /**
144  * @brief Mouse cursor move event parameters (EventType::MouseMoved)
145  */
147  Vector2i coords; ///< Position of the mouse cursor
148  };
149 
150  /**
151  * @brief Mouse wheel event parameters (EventType::MouseWheelScrolled)
152  */
154  Vector2i offset; ///< Offset of the mouse wheel
155  };
156 
157  /**
158  * @brief Gamepad button event parameters (EventType::GamepadButtonPressed, EventType::GamepadButtonReleased)
159  */
161  GamepadId id; ///< Id of the gamepad
162  GamepadButton button; ///< Button of the gamepad
163  };
164 
165  /**
166  * @brief Gamepad axis event parameters (EventType::GamepadAxisMoved)
167  */
169  GamepadId id; ///< Id of the gamepad
170  GamepadAxis axis; ///< Axis of the gamepad
171  int16_t value; ///< Value of the axis
172  };
173 
174  /**
175  * @brief Gamepad connection event parameters (EventType::GamepadConnected)
176  */
178  GamepadHwId id; ///< Hardware id of the gamepad
179  };
180 
181  /**
182  * @brief Gamepad disconnection event parameters (EventType::GamepadDisconnected)
183  */
185  GamepadId id; ///< Id of the gamepad
186  };
187 
188  EventType type; ///< Type of the event
189 
190  union {
191  Vector2u size; ///< Size event parameters (EventType::Resized)
192  KeyEvent key; ///< Key event parameters (EventType::KeyPressed, EventType::KeyReleased, EventType::KeyRepeated)
193  TextEvent text; ///< Text event parameters (EventType::TextEntered)
194  MouseButtonEvent mouseButton; ///< Mouse button event parameters (EventType::MouseButtonPressed, EventType::MouseButtonReleased)
195  MouseCursorEvent mouseCursor; ///< Mouse cursor move event parameters (EventType::MouseMoved)
196  MouseWheelEvent mouseWheel; ///< Mouse wheel event parameters (EventType::MouseWheelScrolled)
197  GamepadButtonEvent gamepadButton; ///< Gamepad button event parameters (EventType::GamepadButtonPressed, EventType::GamepadButtonReleased)
198  GamepadAxisEvent gamepadAxis; ///< Gamepad axis event parameters (EventType::GamepadAxisMoved)
199  GamepadConnection gamepadConnection; ///< Gamepad connection event parameters (EventType::GamepadConnected)
200  GamepadDisconnection gamepadDisconnection; ///< Gamepad disconnection event parameters (EventType::GamepadDisconnected)
201  };
202 
203  };
204 
205 #ifndef DOXYGEN_SHOULD_SKIP_THIS
206 }
207 #endif
208 }
209 
210 #endif // GL_EVENT_H
GamepadId id
Id of the gamepad.
Definition: Event.h:169
Rune rune
The rune.
Definition: Event.h:132
GamepadButton
The gamepad buttons.
Definition: Gamepad.h:42
Gamepad axis event parameters (EventType::GamepadAxisMoved)
Definition: Event.h:168
The mouse wheel was scrolled (data in event.mouseWheel)
Gamepad disconnection event parameters (EventType::GamepadDisconnected)
Definition: Event.h:184
The mouse cursor left the window (no data)
Vector2i coords
Position of the mouse cursor.
Definition: Event.h:147
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:153
The window requested to be closed (no data)
GamepadAxis axis
Axis of the gamepad.
Definition: Event.h:170
Text event parameters (EventType::TextEntered)
Definition: Event.h:131
A gamepad axis was moved (data in event.gamepadAxis)
int16_t value
Value of the axis.
Definition: Event.h:171
Modifiers modifiers
Modifiers that are pressed.
Definition: Event.h:125
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:161
Scancode
Scancodes.
Definition: Keyboard.h:66
A gamepad button was released (data in event.gamepadButton)
Scancode scancode
Scancode of the key.
Definition: Event.h:124
The window was resized (data in event.size)
GamepadAxis
The gamepad axis.
Definition: Gamepad.h:70
A text was entered (data in event.text)
The namespace for gf classes.
Definition: Action.h:34
Keyboard event parameters (EventType::KeyPressed, EventType::KeyReleased, EventType::KeyRepeated) ...
Definition: Event.h:122
EventType
Enumeration of the different types of events.
Definition: Event.h:43
The mouse cursor entered the window (no data)
A character encoded in UTF-8.
Definition: Rune.h:36
GamepadId
A gamepad identifier.
Definition: Gamepad.h:120
MouseButton button
Code of the button that has been pressed.
Definition: Event.h:139
The mouse cursor moved (data in event.mouseCursor)
Mouse cursor move event parameters (EventType::MouseMoved)
Definition: Event.h:146
GamepadHwId id
Hardware id of the gamepad.
Definition: Event.h:178
Gamepad button event parameters (EventType::GamepadButtonPressed, EventType::GamepadButtonReleased) ...
Definition: Event.h:160
A mouse button was pressed (data in event.mouseButton)
GamepadId id
Id of the gamepad.
Definition: Event.h:185
GamepadButton button
Button of the gamepad.
Definition: Event.h:162
The window gained focus (no data)
Keycode keycode
Keycode of the key.
Definition: Event.h:123
A key was released (data in event.key)
The window lost focus (no data)
EventType type
Type of the event.
Definition: Event.h:188
Gamepad connection event parameters (EventType::GamepadConnected)
Definition: Event.h:177
Vector2i coords
Position of the mouse cursor.
Definition: Event.h:140
Vector2i offset
Offset of the mouse wheel.
Definition: Event.h:154
Mouse button event parameters (EventType::MouseButtonPressed, EventType::MouseButtonReleased) ...
Definition: Event.h:138
MouseButton
Mouse buttons.
Definition: Mouse.h:36
Defines a system event and its parameters.
Definition: Event.h:118
#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