HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
gui_event.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
9#pragma once
10
11#include "gui_event_type.hpp"
12#include "gui_event_variant.hpp"
13#include "keyboard_virtual_key.hpp"
14#include "keyboard_state.hpp"
15#include "keyboard_modifiers.hpp"
16#include "mouse_buttons.hpp"
17#include "../unicode/grapheme.hpp"
18#include "../geometry/vector.hpp"
19#include "../geometry/point.hpp"
20#include "../geometry/transform.hpp"
21#include "../chrono.hpp"
22#include <chrono>
23
24namespace hi { inline namespace v1 {
25
34 point2 position = {};
35
42 point2 down_position = {};
43
48 vector2 wheel_delta = {};
49
52 mouse_buttons cause = {};
53
56 mouse_buttons down = {};
57
60 uint8_t click_count = 0;
61};
62
66class gui_event {
67public:
70 utc_nanoseconds time_point;
71
77
81
89 constexpr gui_event(
91 utc_nanoseconds time_point,
92 hi::keyboard_modifiers keyboard_modifiers,
93 hi::keyboard_state keyboard_state) noexcept :
95 {
97 }
98
101 constexpr gui_event() noexcept :
102 gui_event(gui_event_type::none, utc_nanoseconds{}, keyboard_modifiers::none, keyboard_state::idle)
103 {
104 }
105
111 gui_event(type, std::chrono::utc_clock::now(), keyboard_modifiers::none, keyboard_state::idle)
112 {
113 }
114
120 gui_event(gui_event_type type, hi::grapheme grapheme) noexcept :
121 gui_event(type, std::chrono::utc_clock::now(), keyboard_modifiers::none, keyboard_state::idle)
122 {
123 hi_axiom(variant() == gui_event_variant::grapheme);
124 this->grapheme() = grapheme;
125 }
126
136 keyboard_virtual_key key,
137 hi::keyboard_modifiers keyboard_modifiers = keyboard_modifiers::none,
138 hi::keyboard_state keyboard_state = keyboard_state::idle) noexcept :
139 gui_event(type, std::chrono::utc_clock::now(), keyboard_modifiers, keyboard_state)
140 {
141 hi_axiom(variant() == gui_event_variant::keyboard);
142 this->key() = key;
143 }
144
145 constexpr gui_event(gui_event const&) noexcept = default;
146 constexpr gui_event(gui_event&&) noexcept = default;
147 constexpr gui_event& operator=(gui_event const&) noexcept = default;
148 constexpr gui_event& operator=(gui_event&&) noexcept = default;
149
154 [[nodiscard]] static gui_event make_mouse_enter(point2 position) noexcept
155 {
156 auto r = gui_event{gui_event_type::mouse_enter};
157 r.mouse().position = position;
158 return r;
159 }
160
163 [[nodiscard]] constexpr gui_event_type type() const noexcept
164 {
165 return _type;
166 }
167
173 [[nodiscard]] constexpr void set_type(gui_event_type type) noexcept
174 {
175 hilet previous_variant = variant();
176
177 _type = type;
178 if (previous_variant != variant()) {
179 switch (variant()) {
181 _mouse = {};
182 break;
184 _grapheme = hi::grapheme{};
185 break;
187 _key = {};
188 break;
189 default:;
190 }
191 }
192 }
193
198 [[nodiscard]] mouse_event_data& mouse() noexcept
199 {
200 hi_axiom(variant() == gui_event_variant::mouse);
201 return _mouse;
202 }
203
208 [[nodiscard]] mouse_event_data const& mouse() const noexcept
209 {
210 hi_axiom(variant() == gui_event_variant::mouse);
211 return _mouse;
212 }
213
218 [[nodiscard]] keyboard_virtual_key& key() noexcept
219 {
220 hi_axiom(variant() == gui_event_variant::keyboard);
221 return _key;
222 }
223
228 [[nodiscard]] keyboard_virtual_key const& key() const noexcept
229 {
230 hi_axiom(variant() == gui_event_variant::keyboard);
231 return _key;
232 }
233
238 [[nodiscard]] hi::grapheme& grapheme() noexcept
239 {
240 hi_axiom(variant() == gui_event_variant::grapheme);
241 return _grapheme;
242 }
243
248 [[nodiscard]] hi::grapheme const& grapheme() const noexcept
249 {
250 hi_axiom(variant() == gui_event_variant::grapheme);
251 return _grapheme;
252 }
253
254 [[nodiscard]] constexpr bool operator==(gui_event_type event_type) const noexcept
255 {
256 return type() == event_type;
257 }
258
259 [[nodiscard]] constexpr bool operator==(gui_event_variant event_variant) const noexcept
260 {
261 return variant() == event_variant;
262 }
263
264 [[nodiscard]] constexpr bool empty() const noexcept
265 {
266 return type() == gui_event_type::none;
267 }
268
269 constexpr operator bool() const noexcept
270 {
271 return not empty();
272 }
273
274 [[nodiscard]] constexpr gui_event_variant variant() const noexcept
275 {
276 return to_gui_event_variant(type());
277 }
278
281 [[nodiscard]] constexpr bool is_left_button_up(aarectangle active_area) const noexcept
282 {
283 using enum gui_event_type;
284 return type() == mouse_up and mouse().cause.left_button and active_area.contains(mouse().position);
285 }
286
289 [[nodiscard]] constexpr vector2 drag_delta() const noexcept
290 {
291 using enum gui_event_type;
292 return type() == mouse_drag ? mouse().position - mouse().down_position : vector2{};
293 }
294
303 [[nodiscard]] constexpr friend gui_event operator*(geo::transformer auto const& transform, gui_event const& rhs) noexcept
304 {
305 auto r = rhs;
306 if (rhs == gui_event_variant::mouse) {
307 r.mouse().position = point2{transform * rhs.mouse().position};
308 r.mouse().down_position = point2{transform * rhs.mouse().down_position};
309 r.mouse().wheel_delta = vector2{transform * rhs.mouse().wheel_delta};
310 }
311 return r;
312 }
313
314private:
315 gui_event_type _type;
316
317 union {
318 mouse_event_data _mouse;
319 keyboard_virtual_key _key;
320 hi::grapheme _grapheme;
321 };
322};
323
324}} // namespace hi::inline v1
325
326template<typename CharT>
327struct std::formatter<hi::gui_event, CharT> : std::formatter<std::string_view, CharT> {
328 auto format(hi::gui_event const& t, auto& fc)
329 {
330 return std::formatter<std::string_view, CharT>::format(hi::gui_event_type_metadata[t.type()], fc);
331 }
332};
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
Definition of a GUI event variant.
Definition of GUI event type.
gui_event_type
GUI event type.
Definition gui_event_type.hpp:22
constexpr gui_event_variant to_gui_event_variant(gui_event_type type) noexcept
Convert a gui event type, to an gui event variant.
Definition gui_event_variant.hpp:39
gui_event_variant
A granular gui event type.
Definition gui_event_variant.hpp:18
@ keyboard
The gui_event has keyboard data.
@ mouse
The gui_event has mouse data.
@ grapheme
The gui_event has grapheme data.
DOXYGEN BUG.
Definition algorithm.hpp:15
The HikoGUI namespace.
Definition ascii.hpp:19
Information for a mouse event.
Definition gui_event.hpp:29
point2 position
The current position of the mouse pointer.
Definition gui_event.hpp:34
uint8_t click_count
Number of clicks from the last button clicked.
Definition gui_event.hpp:60
vector2 wheel_delta
Change in wheel rotation, in points (pt).
Definition gui_event.hpp:48
mouse_buttons down
Buttons that are also held down.
Definition gui_event.hpp:56
mouse_buttons cause
Buttons which have caused this event.
Definition gui_event.hpp:52
point2 down_position
The position the last time a button was pressed.
Definition gui_event.hpp:42
A user interface event.
Definition gui_event.hpp:66
utc_nanoseconds time_point
The time when the event was created.
Definition gui_event.hpp:70
gui_event(gui_event_type type, hi::grapheme grapheme) noexcept
Create an grapheme GUI event.
Definition gui_event.hpp:120
constexpr vector2 drag_delta() const noexcept
Get the location of the mouse relative to the start of a drag.
Definition gui_event.hpp:289
mouse_event_data const & mouse() const noexcept
Get the mouse event information.
Definition gui_event.hpp:208
constexpr void set_type(gui_event_type type) noexcept
Change the type of the gui_event.
Definition gui_event.hpp:173
constexpr gui_event() noexcept
Create an empty GUI event.
Definition gui_event.hpp:101
hi::grapheme & grapheme() noexcept
Get the grapheme entered on the keyboard.
Definition gui_event.hpp:238
keyboard_modifiers keyboard_modifiers
Keyboard modifiers: shift, ctrl, alt, etc.
Definition gui_event.hpp:76
static gui_event make_mouse_enter(point2 position) noexcept
Create a mouse enter event.
Definition gui_event.hpp:154
keyboard_state keyboard_state
State of the keyboard; caps-lock, num-lock, scroll-lock.
Definition gui_event.hpp:80
hi::grapheme const & grapheme() const noexcept
Get the grapheme entered on the keyboard.
Definition gui_event.hpp:248
constexpr bool is_left_button_up(aarectangle active_area) const noexcept
Check if this event is for a left-button-up event while the mouse pointer is in the given area.
Definition gui_event.hpp:281
constexpr gui_event_type type() const noexcept
Get the event type.
Definition gui_event.hpp:163
keyboard_virtual_key const & key() const noexcept
Get the key from the keyboard event.
Definition gui_event.hpp:228
mouse_event_data & mouse() noexcept
Get the mouse event information.
Definition gui_event.hpp:198
constexpr friend gui_event operator*(geo::transformer auto const &transform, gui_event const &rhs) noexcept
Transform a gui-event to another coordinate system.
Definition gui_event.hpp:303
gui_event(gui_event_type type, keyboard_virtual_key key, hi::keyboard_modifiers keyboard_modifiers=keyboard_modifiers::none, hi::keyboard_state keyboard_state=keyboard_state::idle) noexcept
Create a GUI event.
Definition gui_event.hpp:134
constexpr gui_event(gui_event_type type, utc_nanoseconds time_point, hi::keyboard_modifiers keyboard_modifiers, hi::keyboard_state keyboard_state) noexcept
Create a GUI event.
Definition gui_event.hpp:89
gui_event(gui_event_type type) noexcept
Create am empty GUI event.
Definition gui_event.hpp:110
keyboard_virtual_key & key() noexcept
Get the key from the keyboard event.
Definition gui_event.hpp:218