SDOM - Simple SDL Document Object Model
A lightweight, extensible Document Object Model for SDL-based applications.
Loading...
Searching...
No Matches
SDOM_Event.hpp
Go to the documentation of this file.
1/*** SDOM_Event.hpp ****************************
2 * ___ ___ ___ __ __ ___ _ _
3 * / __| \ / _ \| \/ | | __|_ _____ _ _| |_ | |_ _ __ _ __
4 * \__ \ |) | (_) | |\/| | | _|\ V / -_) ' \ _|_| ' \| '_ \ '_ \
5 * |___/___/ \___/|_| |_|_|___|\_/\___|_||_\__(_)_||_| .__/ .__/
6 * |___| |_| |_|
7 *
8 * The SDOM_Event class encapsulates the event system within the SDOM framework,
9 * providing a unified interface for handling user input and system events such
10 * as keyboard, mouse, and custom application events. It supports event propagation
11 * phases (capture, target, bubbling), payload data for custom event information,
12 * and thread-safe access to event properties. The class integrates closely with
13 * SDL3’s event system, allowing seamless translation between SDL events and SDOM’s
14 * object-oriented event model. SDOM_Event is designed for flexibility and
15 * extensibility, enabling robust event-driven programming and interactive GUI
16 * development within SDOM-based applications.
17 *
18 *
19 * This software is provided 'as-is', without any express or implied
20 * warranty. In no event will the authors be held liable for any damages
21 * arising from the use of this software.
22 *
23 * Permission is granted to anyone to use this software for any purpose,
24 * including commercial applications, and to alter it and redistribute it
25 * freely, subject to the following restrictions:
26 *
27 * 1. The origin of this software must not be misrepresented; you must not
28 * claim that you wrote the original software. If you use this software
29 * in a product, an acknowledgment in the product documentation would be
30 * appreciated but is not required.
31 * 2. Altered source versions must be plainly marked as such, and must not be
32 * misrepresented as being the original software.
33 * 3. This notice may not be removed or altered from any source distribution.
34 *
35 * Released under the ZLIB License.
36 * Original Author: Jay Faries (warte67)
37 *
38 ******************/
39
40#ifndef __SDOM_EVENT_HPP__
41#define __SDOM_EVENT_HPP__
42
43#include <SDL3/SDL.h>
46#include <mutex>
48
49namespace SDOM
50{
51 class IDisplayObject;
52 class DisplayHandle;
53
54 class Event : public IDataObject
55 {
56 friend class EventManager;
57
58 public:
59
60 // using Json = nlohmann::json;
61
62 enum class Phase {
63 Capture,
64 Target,
66 };
67
68 Event(EventType type = EventType("None"), DisplayHandle target = nullptr, float fElapsedTime = 0.0f);
69
70 virtual ~Event() = default;
71
72 // virtual methods
73 virtual bool onInit() override;
74 virtual void onQuit() override;
75
76 // Default accessors
77 EventType getType() const;
78 std::string getTypeName() const;
79
80 std::string getPhaseString() const;
81
82 Phase getPhase() const;
83 Event& setPhase(Phase phase);
84
86 Event& setTarget(DisplayHandle newTarget);
87
89 Event& setCurrentTarget(DisplayHandle newCurrentTarget);
90
92 Event& setRelatedTarget(DisplayHandle newRelatedTarget);
93
94 bool isPropagationStopped() const;
96
97 bool isDefaultBehaviorDisabled() const;
98 Event& setDisableDefaultBehavior(bool disable);
99
100 bool getUseCapture() const;
102
103 float getElapsedTime() const;
104 Event& setElapsedTime(float elapsedTime);
105
106 SDL_Event getSDL_Event() const;
107 Event& setSDL_Event(const SDL_Event& sdlEvent);
108
109 // Payload accessors
110 const sol::table& getPayload() const {
111 std::lock_guard<std::mutex> lock(eventMutex_);
112 return payload;
113 }
114 Event& setPayload(const sol::table& data) {
115 std::lock_guard<std::mutex> lock(eventMutex_);
116 payload = data;
117 return *this;
118 }
119 template<typename T>
120 Event& setPayloadValue(const std::string& key, const T& value) {
121 std::lock_guard<std::mutex> lock(eventMutex_);
122 payload[key] = value;
123 return *this;
124 }
125 template<typename T>
126 T getPayloadValue(const std::string& key) const {
127 std::lock_guard<std::mutex> lock(eventMutex_);
128 return payload[key].get_or(T{});
129 }
130
131
132 // Mouse event accessors
133 float getMouseX() const;
134 Event& setMouseX(float x);
135
136 float getMouseY() const;
137 Event& setMouseY(float y);
138
139 float getWheelX() const;
140 Event& setWheelX(float x);
141
142 float getWheelY() const;
143 Event& setWheelY(float y);
144
145 float getDragOffsetX() const;
146 Event& setDragOffsetX(float offsetX);
147
148 float getDragOffsetY() const;
149 Event& setDragOffsetY(float offsetY);
150
151 int getClickCount() const;
152 Event& setClickCount(int count);
153
154 uint8_t getButton() const;
155 Event& setButton(uint8_t btn);
156
157 // keyboard event accessors
158
159 SDL_Scancode getScanCode() const;
160 Event& setScanCode(SDL_Scancode scancode);
161
162 SDL_Keycode getKeycode() const;
163 Event& setKeycode(SDL_Keycode keycode);
164
165 Uint16 getKeymod() const;
166 Event& setKeymod(Uint16 keymod);
167
168 int getAsciiCode() const;
169 Event& setAsciiCode(int asciiCode);
170
171
172 protected:
173 EventType type; // Type of the event, e.g., KeyDown, MouseClick, etc.
174 DisplayHandle target = nullptr; // Target of the event, usually the object that triggered it
175 DisplayHandle currentTarget = nullptr; // Current target during event propagation
176 DisplayHandle relatedTarget = nullptr; // For events that involve a related target (e.g., drag and drop)
177 SDL_Event sdlEvent; // underlying SDL event
178 mutable Phase currentPhase; // Current phase of the event propagation
179 mutable bool propagationStopped = false; // Indicates if event propagation is stopped
180 mutable bool disableDefaultBehavior = false; // Indicates if default behavior is disabled
181 mutable bool useCapture = false; // Indicates if the event is in the capture phase
182 float fElapsedTime = 0.0f; // Time elapsed since the last frame
183
184 sol::table payload; // Generic event-specific data
185
186 // Mouse Event Properties: (Not yet defined as proper JSON properties)
187 float mouse_x; // mouse x-coordinate
188 float mouse_y; // mouse y-coordinate
189 float wheelX = 0.0f; // Horizontal wheel movement
190 float wheelY = 0.0f; // Vertical wheel movement
191 float dragOffsetX = 0.0f; // horizontal drag offset
192 float dragOffsetY = 0.0f; // vertical drag offset
193 int clickCount = 0; // Number of clicks for double-click detection
194 uint8_t button = 0; // Mouse button that triggered the event (SDL_BUTTON_LEFT, SDL_BUTTON_RIGHT, etc.)
195
196 // Keyboard Event Properties: (Not yet defined as proper JSON properties)
197 SDL_Scancode scancode_; // The physical key pressed
198 SDL_Keycode keycode_; // The virtual key pressed
199 Uint16 keymod_; // Modifier keys (e.g., Shift, Ctrl, Alt)
200 int asciiCode_; // ASCII code of the key pressed
201 mutable std::mutex eventMutex_;
202
203 // Prevent copying
204 // Event(const Event&) = delete; // Deleted copy constructor
205 // Event& operator=(const Event&) = delete; // Deleted copy assignment operator
206 public:
207 // Copy constructor
208 Event(const Event& other)
209 : type(other.type),
210 target(other.target),
214 sdlEvent(other.sdlEvent),
217 useCapture(other.useCapture),
219 payload(other.payload),
220 mouse_x(other.mouse_x),
221 mouse_y(other.mouse_y),
222 wheelX(other.wheelX),
223 wheelY(other.wheelY),
226 clickCount(other.clickCount),
227 button(other.button),
228 scancode_(other.scancode_),
229 keycode_(other.keycode_),
230 keymod_(other.keymod_),
231 asciiCode_(other.asciiCode_) {
232 // Note: `eventMutex_` is initialized as a new mutex
233 }
234
235 // Copy assignment operator
236 Event& operator=(const Event& other) {
237 if (this != &other) {
238 std::lock_guard<std::mutex> lock(eventMutex_);
239 type = other.type;
240 target = other.target;
244 sdlEvent = other.sdlEvent;
247 useCapture = other.useCapture;
249 payload = other.payload;
250 mouse_x = other.mouse_x;
251 mouse_y = other.mouse_y;
252 wheelX = other.wheelX;
253 wheelY = other.wheelY;
254 dragOffsetX = other.dragOffsetX;
255 dragOffsetY = other.dragOffsetY;
256 clickCount = other.clickCount;
257 button = other.button;
258 scancode_ = other.scancode_;
259 keycode_ = other.keycode_;
260 keymod_ = other.keymod_;
261 asciiCode_ = other.asciiCode_;
262 }
263 return *this;
264 }
265
266 // Register Lua bindings for Event and EventType
267 // This registers the Event userdata and convenience properties/methods
268 // so Lua scripts can use evt.dt, evt.type, evt.target, evt.name, and evt:getName().
269 static void registerLua(sol::state_view lua);
270
271 };
272
273} // namespace SDOM
274
275#endif // __SDOM_EVENT_HPP__
Definition SDOM_DisplayHandle.hpp:24
Definition SDOM_EventManager.hpp:56
Definition SDOM_EventType.hpp:49
Definition SDOM_Event.hpp:55
DisplayHandle target
Definition SDOM_Event.hpp:174
bool disableDefaultBehavior
Definition SDOM_Event.hpp:180
bool isPropagationStopped() const
Definition SDOM_Event.cpp:148
Event & setDragOffsetY(float offsetY)
Definition SDOM_Event.cpp:272
float mouse_y
Definition SDOM_Event.hpp:188
Event & setPayload(const sol::table &data)
Definition SDOM_Event.hpp:114
Event & setKeycode(SDL_Keycode keycode)
Definition SDOM_Event.cpp:324
bool getUseCapture() const
Definition SDOM_Event.cpp:172
float dragOffsetY
Definition SDOM_Event.hpp:192
Event & setWheelY(float y)
Definition SDOM_Event.cpp:248
Event & stopPropagation()
Definition SDOM_Event.cpp:153
Uint16 getKeymod() const
Definition SDOM_Event.cpp:331
float wheelX
Definition SDOM_Event.hpp:189
std::mutex eventMutex_
Definition SDOM_Event.hpp:201
float mouse_x
Definition SDOM_Event.hpp:187
Event & setClickCount(int count)
Definition SDOM_Event.cpp:284
sol::table payload
Definition SDOM_Event.hpp:184
virtual void onQuit() override
Definition SDOM_Event.cpp:78
Phase
Definition SDOM_Event.hpp:62
float getMouseY() const
Definition SDOM_Event.cpp:219
static void registerLua(sol::state_view lua)
Definition SDOM_Event.cpp:361
Event & setKeymod(Uint16 keymod)
Definition SDOM_Event.cpp:337
float getMouseX() const
Definition SDOM_Event.cpp:207
float getWheelX() const
Definition SDOM_Event.cpp:231
EventType getType() const
Definition SDOM_Event.cpp:84
Event & setUseCapture(bool useCapture)
Definition SDOM_Event.cpp:177
Event & setAsciiCode(int asciiCode)
Definition SDOM_Event.cpp:351
Event & setRelatedTarget(DisplayHandle newRelatedTarget)
Definition SDOM_Event.cpp:141
DisplayHandle getRelatedTarget() const
Definition SDOM_Event.cpp:136
Event & setPayloadValue(const std::string &key, const T &value)
Definition SDOM_Event.hpp:120
Event & setPhase(Phase phase)
Definition SDOM_Event.cpp:105
EventType type
Definition SDOM_Event.hpp:173
float getElapsedTime() const
Definition SDOM_Event.cpp:184
std::string getPhaseString() const
Definition SDOM_Event.cpp:87
const sol::table & getPayload() const
Definition SDOM_Event.hpp:110
virtual ~Event()=default
Event & setCurrentTarget(DisplayHandle newCurrentTarget)
Definition SDOM_Event.cpp:129
Event & setDragOffsetX(float offsetX)
Definition SDOM_Event.cpp:260
Event & setMouseX(float x)
Definition SDOM_Event.cpp:212
int clickCount
Definition SDOM_Event.hpp:193
int getClickCount() const
Definition SDOM_Event.cpp:279
uint8_t getButton() const
Definition SDOM_Event.cpp:291
DisplayHandle getTarget() const
Definition SDOM_Event.cpp:112
Event & setDisableDefaultBehavior(bool disable)
Definition SDOM_Event.cpp:165
SDL_Event sdlEvent
Definition SDOM_Event.hpp:177
SDL_Event getSDL_Event() const
Definition SDOM_Event.cpp:196
virtual bool onInit() override
Definition SDOM_Event.cpp:72
int getAsciiCode() const
Definition SDOM_Event.cpp:345
uint8_t button
Definition SDOM_Event.hpp:194
Event & setScanCode(SDL_Scancode scancode)
Definition SDOM_Event.cpp:312
float fElapsedTime
Definition SDOM_Event.hpp:182
SDL_Scancode getScanCode() const
Definition SDOM_Event.cpp:306
Event & operator=(const Event &other)
Definition SDOM_Event.hpp:236
Event & setElapsedTime(float elapsedTime)
Definition SDOM_Event.cpp:189
bool propagationStopped
Definition SDOM_Event.hpp:179
Event & setSDL_Event(const SDL_Event &sdlEvent)
Definition SDOM_Event.cpp:201
Phase getPhase() const
Definition SDOM_Event.cpp:99
bool isDefaultBehaviorDisabled() const
Definition SDOM_Event.cpp:160
int asciiCode_
Definition SDOM_Event.hpp:200
Phase currentPhase
Definition SDOM_Event.hpp:178
DisplayHandle currentTarget
Definition SDOM_Event.hpp:175
SDL_Keycode keycode_
Definition SDOM_Event.hpp:198
Event & setButton(uint8_t btn)
Definition SDOM_Event.cpp:296
float dragOffsetX
Definition SDOM_Event.hpp:191
float wheelY
Definition SDOM_Event.hpp:190
float getDragOffsetY() const
Definition SDOM_Event.cpp:267
T getPayloadValue(const std::string &key) const
Definition SDOM_Event.hpp:126
SDL_Scancode scancode_
Definition SDOM_Event.hpp:197
Event & setWheelX(float x)
Definition SDOM_Event.cpp:236
DisplayHandle getCurrentTarget() const
Definition SDOM_Event.cpp:124
Event & setTarget(DisplayHandle newTarget)
Definition SDOM_Event.cpp:117
DisplayHandle relatedTarget
Definition SDOM_Event.hpp:176
SDL_Keycode getKeycode() const
Definition SDOM_Event.cpp:319
Uint16 keymod_
Definition SDOM_Event.hpp:199
float getWheelY() const
Definition SDOM_Event.cpp:243
bool useCapture
Definition SDOM_Event.hpp:181
float getDragOffsetX() const
Definition SDOM_Event.cpp:255
Event(const Event &other)
Definition SDOM_Event.hpp:208
Event & setMouseY(float y)
Definition SDOM_Event.cpp:224
std::string getTypeName() const
Definition SDOM_Event.cpp:85
Definition SDOM_IDataObject.hpp:107
Contains all core classes and utilities for the SDOM library.
Definition lua_BindHelpers.hpp:7