Loading...
Searching...
No Matches
Event.hpp
Go to the documentation of this file.
1
2//
3// SFML - Simple and Fast Multimedia Library
4// Copyright (C) 2007-2025 Laurent Gomila (laurent@sfml-dev.org)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages 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 freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25#pragma once
26
28// Headers
32#include <SFML/Window/Mouse.hpp>
34
36
37#include <type_traits>
38#include <variant>
39
40
41namespace sf
42{
47class Event
48{
49public:
54 struct Closed
55 {
56 };
57
62 struct Resized
63 {
65 };
66
71 struct FocusLost
72 {
73 };
74
80 {
81 };
82
88 {
89 char32_t unicode{};
90 };
91
97 {
100 bool alt{};
101 bool control{};
102 bool shift{};
103 bool system{};
104 };
105
119
130
140
150
156 {
158 };
159
189 {
191 };
192
198 {
199 };
200
206 {
207 };
208
214 {
215 unsigned int joystickId{};
216 unsigned int button{};
217 };
218
224 {
225 unsigned int joystickId{};
226 unsigned int button{};
227 };
228
234 {
235 unsigned int joystickId{};
237 float position{};
238 };
239
245 {
246 unsigned int joystickId{};
247 };
248
254 {
255 unsigned int joystickId{};
256 };
257
263 {
264 unsigned int finger{};
266 };
267
273 {
274 unsigned int finger{};
276 };
277
283 {
284 unsigned int finger{};
286 };
287
297
306 template <typename TEventSubtype>
307 Event(const TEventSubtype& eventSubtype);
308
317 template <typename TEventSubtype>
318 [[nodiscard]] bool is() const;
319
328 template <typename TEventSubtype>
329 [[nodiscard]] const TEventSubtype* getIf() const;
330
339 template <typename Visitor>
340 decltype(auto) visit(Visitor&& visitor) const;
341
342private:
344 // Member data
346 std::variant<Closed,
347 Resized,
348 FocusLost,
359 MouseLeft,
369 m_data;
370
372 // Helper functions
374 template <typename T, typename... Ts>
375 [[nodiscard]] static constexpr bool isInParameterPack(const std::variant<Ts...>*)
376 {
377 return std::disjunction_v<std::is_same<T, Ts>...>;
378 }
379
380 template <typename T>
381 static constexpr bool isEventSubtype = isInParameterPack<T>(decltype (&m_data)(nullptr));
382
383 friend class WindowBase;
384
385 template <typename Handler, typename... Ts>
386 [[nodiscard]] static constexpr bool isInvocableWithEventSubtype(const std::variant<Ts...>*)
387 {
388 return std::disjunction_v<std::is_invocable<Handler&, Ts&>...>;
389 }
390
391 template <typename Handler>
392 static constexpr bool isEventHandler = isInvocableWithEventSubtype<Handler>(decltype (&m_data)(nullptr));
393};
394
395} // namespace sf
396
397#include <SFML/Window/Event.inl>
398
399
friend class WindowBase
Definition Event.hpp:383
const TEventSubtype * getIf() const
Attempt to get specified event subtype.
bool is() const
Check current event subtype.
Event(const TEventSubtype &eventSubtype)
Construct from a given sf::Event subtype.
decltype(auto) visit(Visitor &&visitor) const
Apply a visitor to the event.
Axis
Axes supported by SFML joysticks.
Definition Joystick.hpp:55
Key
Key codes.
Definition Keyboard.hpp:52
Button
Mouse buttons.
Definition Mouse.hpp:50
Wheel
Mouse wheels.
Definition Mouse.hpp:66
Type
Sensor type.
Definition Sensor.hpp:45
Vector3< float > Vector3f
Definition Vector3.hpp:306
Vector2< unsigned int > Vector2u
Definition Vector2.hpp:209
Vector2< int > Vector2i
Definition Vector2.hpp:208
Closed event subtype.
Definition Event.hpp:55
Gained focus event subtype.
Definition Event.hpp:80
Lost focus event subtype.
Definition Event.hpp:72
Joystick button pressed event subtype.
Definition Event.hpp:214
unsigned int button
Index of the button that has been pressed (in range [0 .. Joystick::ButtonCount - 1])
Definition Event.hpp:216
unsigned int joystickId
Index of the joystick (in range [0 .. Joystick::Count - 1])
Definition Event.hpp:215
Joystick button released event subtype.
Definition Event.hpp:224
unsigned int button
Index of the button that has been released (in range [0 .. Joystick::ButtonCount - 1])
Definition Event.hpp:226
unsigned int joystickId
Index of the joystick (in range [0 .. Joystick::Count - 1])
Definition Event.hpp:225
Joystick connected event subtype.
Definition Event.hpp:245
unsigned int joystickId
Index of the joystick (in range [0 .. Joystick::Count - 1])
Definition Event.hpp:246
Joystick disconnected event subtype.
Definition Event.hpp:254
unsigned int joystickId
Index of the joystick (in range [0 .. Joystick::Count - 1])
Definition Event.hpp:255
Joystick axis move event subtype.
Definition Event.hpp:234
unsigned int joystickId
Index of the joystick (in range [0 .. Joystick::Count - 1])
Definition Event.hpp:235
Joystick::Axis axis
Axis on which the joystick moved.
Definition Event.hpp:236
float position
New position on the axis (in range [-100 .. 100])
Definition Event.hpp:237
Key pressed event subtype.
Definition Event.hpp:97
bool system
Is the System key pressed?
Definition Event.hpp:103
bool control
Is the Control key pressed?
Definition Event.hpp:101
bool shift
Is the Shift key pressed?
Definition Event.hpp:102
bool alt
Is the Alt key pressed?
Definition Event.hpp:100
Keyboard::Key code
Code of the key that has been pressed.
Definition Event.hpp:98
Keyboard::Scancode scancode
Physical code of the key that has been pressed.
Definition Event.hpp:99
Key released event subtype.
Definition Event.hpp:111
bool alt
Is the Alt key pressed?
Definition Event.hpp:114
bool control
Is the Control key pressed?
Definition Event.hpp:115
bool shift
Is the Shift key pressed?
Definition Event.hpp:116
bool system
Is the System key pressed?
Definition Event.hpp:117
Keyboard::Key code
Code of the key that has been released.
Definition Event.hpp:112
Keyboard::Scancode scancode
Physical code of the key that has been released.
Definition Event.hpp:113
Mouse button pressed event subtype.
Definition Event.hpp:136
Vector2i position
Position of the mouse pointer, relative to the top left of the owner window.
Definition Event.hpp:138
Mouse::Button button
Code of the button that has been pressed.
Definition Event.hpp:137
Mouse button released event subtype.
Definition Event.hpp:146
Vector2i position
Position of the mouse pointer, relative to the top left of the owner window.
Definition Event.hpp:148
Mouse::Button button
Code of the button that has been released.
Definition Event.hpp:147
Mouse entered event subtype.
Definition Event.hpp:198
Mouse left event subtype.
Definition Event.hpp:206
Mouse move raw event subtype.
Definition Event.hpp:189
Vector2i delta
Delta movement of the mouse since the last event.
Definition Event.hpp:190
Mouse move event subtype.
Definition Event.hpp:156
Vector2i position
Position of the mouse pointer, relative to the top left of the owner window.
Definition Event.hpp:157
Mouse wheel scrolled event subtype.
Definition Event.hpp:125
Mouse::Wheel wheel
Which wheel (for mice with multiple ones)
Definition Event.hpp:126
Vector2i position
Position of the mouse pointer, relative to the top left of the owner window.
Definition Event.hpp:128
float delta
Wheel offset (positive is up/left, negative is down/right). High-precision mice may use non-integral ...
Definition Event.hpp:127
Resized event subtype.
Definition Event.hpp:63
Vector2u size
New size, in pixels.
Definition Event.hpp:64
Sensor event subtype.
Definition Event.hpp:293
Sensor::Type type
Type of the sensor.
Definition Event.hpp:294
Vector3f value
Current value of the sensor on the X, Y, and Z axes.
Definition Event.hpp:295
Text event subtype.
Definition Event.hpp:88
char32_t unicode
UTF-32 Unicode value of the character.
Definition Event.hpp:89
Touch began event subtype.
Definition Event.hpp:263
Vector2i position
Start position of the touch, relative to the top left of the owner window.
Definition Event.hpp:265
unsigned int finger
Index of the finger in case of multi-touch events.
Definition Event.hpp:264
Touch ended event subtype.
Definition Event.hpp:283
Vector2i position
Final position of the touch, relative to the top left of the owner window.
Definition Event.hpp:285
unsigned int finger
Index of the finger in case of multi-touch events.
Definition Event.hpp:284
Touch moved event subtype.
Definition Event.hpp:273
Vector2i position
Current position of the touch, relative to the top left of the owner window.
Definition Event.hpp:275
unsigned int finger
Index of the finger in case of multi-touch events.
Definition Event.hpp:274