Gamp v0.0.7-36-g24b1eb6
Gamp: Graphics, Audio, Multimedia and Processing
Loading...
Searching...
No Matches
Event.hpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2025 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24#ifndef GAMP_WTEVENT_HPP_
25#define GAMP_WTEVENT_HPP_
26
27#include <string>
28#include <vector>
29
30#include <jau/enum_util.hpp>
31#include <jau/int_math_ct.hpp>
32#include <jau/fraction_type.hpp>
33
34namespace gamp::wt {
35 class Window;
36 typedef std::shared_ptr<Window> WindowRef;
37 typedef std::weak_ptr<Window> WindowWeakPtr;
38}
39
40namespace gamp::wt::event {
41 /** \addtogroup Gamp_WT
42 *
43 * @{
44 */
45
46 using namespace jau::enums;
47
48 inline static constexpr uint16_t EVENT_WINDOW_RESIZED = 100;
49 inline static constexpr uint16_t EVENT_WINDOW_MOVED = 101;
50 inline static constexpr uint16_t EVENT_WINDOW_DESTROY_NOTIFY = 102;
51 inline static constexpr uint16_t EVENT_WINDOW_FOCUS_CHANGED = 103;
52 inline static constexpr uint16_t EVENT_WINDOW_REPAINT = 104;
53 inline static constexpr uint16_t EVENT_WINDOW_DESTROYED = 105;
54 inline static constexpr uint16_t EVENT_WINDOW_VISIBILITY_CHANGED = 106;
55
56 /** A key has been pressed, excluding {@link #isAutoRepeat() auto-repeat}-{@link #isModifierKey() modifier} keys. */
57 inline static constexpr uint16_t EVENT_KEY_PRESSED = 300;
58 /** A key has been released, excluding {@link #isAutoRepeat() auto-repeat}-{@link #isModifierKey() modifier} keys. */
59 inline static constexpr uint16_t EVENT_KEY_RELEASED = 301;
60
61 inline static constexpr short EVENT_POINTER_CLICKED = 200;
62 /** Only generated for PointerType::mouse */
63 inline static constexpr short EVENT_POINTER_ENTERED = 201;
64 /** Only generated for PointerType::mouse */
65 inline static constexpr short EVENT_POINTER_EXITED = 202;
66 inline static constexpr short EVENT_POINTER_PRESSED = 203;
67 inline static constexpr short EVENT_POINTER_RELEASED = 204;
68 inline static constexpr short EVENT_POINTER_MOVED = 205;
69 inline static constexpr short EVENT_POINTER_DRAGGED = 206;
70 inline static constexpr short EVENT_POINTER_WHEEL = 207;
71
72 class WTEvent {
73 private:
74 uint16_t m_type;
76 WindowWeakPtr m_source;
77 bool m_consumed;
78
79 public:
80 WTEvent(uint16_t type, const jau::fraction_timespec& when, const WindowRef& source) noexcept
81 : m_type(type), m_when(when), m_source(source), m_consumed(false) {}
82
83 constexpr uint16_t type() const noexcept { return m_type; }
84 constexpr const jau::fraction_timespec& when() const noexcept { return m_when; }
85 constexpr const WindowWeakPtr& source() const noexcept { return m_source; }
86
87 /** Consumed events will stop traversing through listener. */
88 constexpr bool consumed() const noexcept { return m_consumed; }
89 /** Consumed events will stop traversing through listener. */
90 constexpr void setConsumed(bool v) noexcept { m_consumed = v; }
91 std::string toString() const noexcept {
92 std::string s = "WTEvent[consumed ";
93 s.append(m_consumed ? "true" : "false");
94 s.append(", when ").append(m_when.to_string());
95 s.append("]");
96 return s;
97 }
98 };
99 inline std::ostream& operator<<(std::ostream& out, const WTEvent& v) {
100 return out << v.toString();
101 }
102
103 enum class InputButton : uint16_t {
104 none = 0,
121
122 /// button property description, no actual bitmask value
124 };
131
132 enum class InputModifier : uint32_t {
133 none = 0,
134 lshift = 1U << 0,
135 rshift = 1U << 1,
136 lctrl = 1U << 2,
137 rctrl = 1U << 3,
138 lalt = 1U << 4,
139 ralt = 1U << 5,
140 lmeta = 1U << 6,
141 rmeta = 1U << 7,
142
148
149 /// button property description, no actual bitmask value
151
153 button2 = 1U << 9,
154 button3 = 1U << 10,
155 button4 = 1U << 11,
156 button5 = 1U << 12,
157 button6 = 1U << 13,
158 button7 = 1U << 14,
159 button8 = 1U << 15,
160 button9 = 1U << 16,
161 button10 = 1U << 17,
162 button11 = 1U << 18,
163 button12 = 1U << 19,
164 button13 = 1U << 20,
165 button14 = 1U << 21,
166 button15 = 1U << 22,
167 button16 = 1U << 23,
168
169 /// button property description, no actual bitmask value
171 /// mask for all 16 buttons, see InputButton::button_count
172 button_all = 0xffffU << 8,
173
174 /** Event is caused by auto-repeat. */
175 repeat = 1U << 29,
176
177 /** Pointer is confined, see {@link Window#confinePointer(boolean)}. */
178 confined = 1U << 30,
179
180 /** Pointer is invisible, see {@link Window#setPointerVisible(boolean)}. */
181 invisible = 1U << 31
182 };
187
188 class InputEvent : public WTEvent {
189 private:
190 InputModifier m_mods;
191 public:
192 /**
193 * Returns the corresponding button mask for the given button.
194 * <p>
195 * In case the given button lies outside
196 * of the valid range [InputButton::button1 .. InputButton::button16],
197 * InputModifier::none is returned.
198 * </p>
199 */
200 constexpr static InputModifier buttonMask(InputButton button) noexcept {
201 if( InputButton::none < button && button <= InputButton::button_count ) {
202 return InputModifier(1U << ( *InputModifier::button1_bit - 1 + *button ));
203 }
204 return InputModifier::none;
205 }
206
207 InputEvent(uint16_t type, const jau::fraction_timespec& when, const WindowRef& source, InputModifier mods) noexcept
208 : WTEvent(type, when, source), m_mods(mods) {}
209
210 constexpr InputModifier modifier() const noexcept { return m_mods; }
211
212 /** Use with single bits, e.g. InputModifier::lshift etc */
213 constexpr bool is_set(InputModifier bits) const noexcept { return gamp::wt::event::is_set(m_mods, bits); }
214 /** Use with groups of bits / mask, e.g. InputModifier::shift etc */
215 constexpr bool has_any(InputModifier bits) const noexcept { return gamp::wt::event::has_any(m_mods, bits); }
216
217 /**
218 * See also {@link MouseEvent}'s section about <i>Multiple-Pointer Events</i>.
219 * @param button the button to test
220 * @return true if the given button is down
221 */
222 constexpr bool isButtonDown(InputButton button) const noexcept {
223 return ( m_mods & buttonMask(button) ) != InputModifier::none;
224 }
225
226 /**
227 * Returns the number of pressed buttons by counting the set bits:
228 * <pre>
229 * jau::ct_bit_count(modifier() & InputModifier::button_all);
230 * </pre>
231 * <p>
232 * See also {@link MouseEvent}'s section about <i>Multiple-Pointer Events</i>.
233 * </p>
234 * @see InputModifier::button_all
235 */
236 constexpr int buttonDownCount() const noexcept {
237 return (int) jau::ct_bit_count(*m_mods & *InputModifier::button_all);
238 }
239
240 /**
241 * Returns true if at least one button is pressed, otherwise false:
242 * <pre>
243 * 0 != ( modifier() & InputModifier::button_all )
244 * </pre>
245 * <p>
246 * See also {@link MouseEvent}'s section about <i>Multiple-Pointer Events</i>.
247 * </p>
248 * @see InputModifier::button_all
249 */
250 constexpr bool isAnyButtonDown() const noexcept {
251 return InputModifier::none != ( m_mods & InputModifier::button_all );
252 }
253 /**
254 * See also {@link MouseEvent}'s section about <i>Multiple-Pointer Events</i>.
255 * @return List of pressed mouse buttons [InputButton::button1 .. InputButton::button16].
256 * If none is down, the resulting list is of length 0.
257 */
258 std::vector<InputButton> buttonsDown() const {
259 const int len = buttonDownCount();
260 std::vector<InputButton> res;
261 res.reserve(len);
262
263 const InputButton_info_t& ei = InputButton_info_t::get();
264 InputButton_info_t::iterator end = ei.end();
265 for(typename InputButton_info_t::iterator iter = ei.begin(); iter != end; ++iter) {
266 const InputButton ev = *iter;
267 if( isButtonDown(ev) ) { res.push_back( ev ); }
268 }
269 return res;
270 }
271
272 /** Returns true if modifier() contains InputModifier::alt */
273 constexpr bool isAltDown() const noexcept {
275 }
276 /** Returns true if modifier() contains InputModifier::ctrl */
277 constexpr bool isControlDown() const noexcept {
279 }
280 /** Returns true if modifier() contains InputModifier::meta */
281 constexpr bool isMetaDown() const noexcept {
283 }
284 /** Returns true if modifier() contains InputModifier::shift */
285 constexpr bool isShiftDown() const noexcept {
287 }
288 /** Returns true if modifier() contains InputModifier::autorepeat */
289 constexpr bool isAutorepeat() const noexcept {
291 }
292
293 /** Returns true if modifier() contains InputModifier::confined */
294 constexpr bool isConfined() const noexcept {
296 }
297 /** Returns true if modifier() contains InputModifier::invisible */
298 constexpr bool isInvisible() const noexcept {
300 }
301
302 std::string toString() const noexcept {
303 std::string s = "InputEvent[modifier";
304 s.append(to_string(m_mods));
305 s.append(", ").append(WTEvent::toString());
306 s.append("]");
307 return s;
308 }
309 };
310 inline std::ostream& operator<<(std::ostream& out, const InputEvent& v) {
311 return out << v.toString();
312 }
313
314 /**@}*/
315}
316
317#endif /* GAMP_WTEVENT_HPP_ */
constexpr InputModifier modifier() const noexcept
Definition Event.hpp:210
constexpr bool isButtonDown(InputButton button) const noexcept
See also MouseEvent's section about Multiple-Pointer Events.
Definition Event.hpp:222
constexpr bool isInvisible() const noexcept
Returns true if modifier() contains InputModifier::invisible.
Definition Event.hpp:298
constexpr bool isAutorepeat() const noexcept
Returns true if modifier() contains InputModifier::autorepeat.
Definition Event.hpp:289
constexpr bool isShiftDown() const noexcept
Returns true if modifier() contains InputModifier::shift.
Definition Event.hpp:285
static constexpr InputModifier buttonMask(InputButton button) noexcept
Returns the corresponding button mask for the given button.
Definition Event.hpp:200
InputEvent(uint16_t type, const jau::fraction_timespec &when, const WindowRef &source, InputModifier mods) noexcept
Definition Event.hpp:207
constexpr bool has_any(InputModifier bits) const noexcept
Use with groups of bits / mask, e.g.
Definition Event.hpp:215
constexpr bool isControlDown() const noexcept
Returns true if modifier() contains InputModifier::ctrl.
Definition Event.hpp:277
constexpr bool isConfined() const noexcept
Returns true if modifier() contains InputModifier::confined.
Definition Event.hpp:294
std::string toString() const noexcept
Definition Event.hpp:302
constexpr bool isMetaDown() const noexcept
Returns true if modifier() contains InputModifier::meta.
Definition Event.hpp:281
constexpr bool isAltDown() const noexcept
Returns true if modifier() contains InputModifier::alt.
Definition Event.hpp:273
constexpr int buttonDownCount() const noexcept
Returns the number of pressed buttons by counting the set bits:
Definition Event.hpp:236
std::vector< InputButton > buttonsDown() const
See also MouseEvent's section about Multiple-Pointer Events.
Definition Event.hpp:258
constexpr bool isAnyButtonDown() const noexcept
Returns true if at least one button is pressed, otherwise false:
Definition Event.hpp:250
constexpr bool is_set(InputModifier bits) const noexcept
Use with single bits, e.g.
Definition Event.hpp:213
constexpr const WindowWeakPtr & source() const noexcept
Definition Event.hpp:85
constexpr const jau::fraction_timespec & when() const noexcept
Definition Event.hpp:84
constexpr uint16_t type() const noexcept
Definition Event.hpp:83
std::string toString() const noexcept
Definition Event.hpp:91
constexpr void setConsumed(bool v) noexcept
Consumed events will stop traversing through listener.
Definition Event.hpp:90
constexpr bool consumed() const noexcept
Consumed events will stop traversing through listener.
Definition Event.hpp:88
WTEvent(uint16_t type, const jau::fraction_timespec &when, const WindowRef &source) noexcept
Definition Event.hpp:80
constexpr uint32_t ct_bit_count(uint32_t n) noexcept
Returns the number of set bits within given 32bit integer (w/o branching) in O(1) and constant time (...
constexpr bool has_any(const E mask, const E bits) noexcept
#define JAU_MAKE_ENUM_STRING(type,...)
#define JAU_MAKE_BITFIELD_ENUM_STRING(type,...)
constexpr bool is_set(const E mask, const E bits) noexcept
std::ostream & operator<<(std::ostream &os, const T v)
#define JAU_MAKE_ENUM_INFO(type,...)
static constexpr uint16_t EVENT_WINDOW_RESIZED
Definition Event.hpp:48
static constexpr short EVENT_POINTER_WHEEL
Definition Event.hpp:70
static constexpr short EVENT_POINTER_PRESSED
Definition Event.hpp:66
static constexpr short EVENT_POINTER_RELEASED
Definition Event.hpp:67
static constexpr short EVENT_POINTER_CLICKED
Definition Event.hpp:61
static constexpr uint16_t EVENT_WINDOW_MOVED
Definition Event.hpp:49
static constexpr uint16_t EVENT_WINDOW_DESTROY_NOTIFY
Definition Event.hpp:50
static constexpr uint16_t EVENT_KEY_PRESSED
A key has been pressed, excluding auto-repeat-modifier keys.
Definition Event.hpp:57
static constexpr uint16_t EVENT_WINDOW_DESTROYED
Definition Event.hpp:53
static constexpr short EVENT_POINTER_EXITED
Only generated for PointerType::mouse.
Definition Event.hpp:65
static constexpr short EVENT_POINTER_DRAGGED
Definition Event.hpp:69
static constexpr uint16_t EVENT_WINDOW_REPAINT
Definition Event.hpp:52
std::shared_ptr< Window > WindowRef
Definition Event.hpp:36
static constexpr uint16_t EVENT_WINDOW_FOCUS_CHANGED
Definition Event.hpp:51
static constexpr short EVENT_POINTER_MOVED
Definition Event.hpp:68
static constexpr short EVENT_POINTER_ENTERED
Only generated for PointerType::mouse.
Definition Event.hpp:63
static constexpr uint16_t EVENT_WINDOW_VISIBILITY_CHANGED
Definition Event.hpp:54
static constexpr uint16_t EVENT_KEY_RELEASED
A key has been released, excluding auto-repeat-modifier keys.
Definition Event.hpp:59
@ button_all
mask for all 16 buttons, see InputButton::button_count
Definition Event.hpp:172
@ button1_bit
button property description, no actual bitmask value
Definition Event.hpp:150
@ repeat
Event is caused by auto-repeat.
Definition Event.hpp:175
@ invisible
Pointer is invisible, see Window#setPointerVisible(boolean).
Definition Event.hpp:181
@ confined
Pointer is confined, see Window#confinePointer(boolean).
Definition Event.hpp:178
@ button_last
button property description, no actual bitmask value
Definition Event.hpp:170
@ button_count
button property description, no actual bitmask value
Definition Event.hpp:123
std::string to_string(const math_error_t v) noexcept
Returns std::string representation of math_error_t.
std::weak_ptr< Window > WindowWeakPtr
Definition Event.hpp:37
Author: Sven Gothel sgothel@jausoft.com Copyright Gothel Software e.K.
Definition enum_util.hpp:65
Timespec structure using int64_t for its components in analogy to struct timespec_t on 64-bit platfor...