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 "widget_id.hpp"
12#include "gui_event_type.hpp"
13#include "gui_event_variant.hpp"
14#include "keyboard_virtual_key.hpp"
15#include "keyboard_state.hpp"
16#include "keyboard_modifiers.hpp"
17#include "keyboard_focus_group.hpp"
18#include "keyboard_focus_direction.hpp"
19#include "mouse_buttons.hpp"
20#include "../text/module.hpp"
21#include "../unicode/module.hpp"
22#include "../geometry/module.hpp"
23#include "../chrono.hpp"
24#include <chrono>
25#include <memory>
26
27namespace hi { inline namespace v1 {
28
37 point2i position = {};
38
45 point2i down_position = {};
46
51 vector2i wheel_delta = {};
52
55 mouse_buttons cause = {};
56
59 mouse_buttons down = {};
60
63 uint8_t click_count = 0;
64};
65
67 hi::widget_id widget_id = {};
68 keyboard_focus_group group = keyboard_focus_group::normal;
69 keyboard_focus_direction direction = keyboard_focus_direction::here;
70};
71
75class gui_event {
76public:
79 utc_nanoseconds time_point;
80
86
90
98 constexpr gui_event(
100 utc_nanoseconds time_point,
101 hi::keyboard_modifiers keyboard_modifiers,
102 hi::keyboard_state keyboard_state) noexcept :
103 _type(gui_event_type::none),
107 {
108 set_type(type);
109 }
110
113 constexpr gui_event() noexcept :
114 gui_event(gui_event_type::none, utc_nanoseconds{}, keyboard_modifiers::none, keyboard_state::idle)
115 {
116 }
117
123 gui_event(type, std::chrono::utc_clock::now(), keyboard_modifiers::none, keyboard_state::idle)
124 {
125 }
126
133 gui_event(type, std::chrono::utc_clock::now(), keyboard_modifiers::none, keyboard_state::idle)
134 {
136 this->rectangle() = rectangle;
137 }
138
148 keyboard_virtual_key key,
149 hi::keyboard_modifiers keyboard_modifiers = keyboard_modifiers::none,
150 hi::keyboard_state keyboard_state = keyboard_state::idle) noexcept :
151 gui_event(type, std::chrono::utc_clock::now(), keyboard_modifiers, keyboard_state)
152 {
154 this->key() = key;
155 }
156
157 constexpr ~gui_event() = default;
158 constexpr gui_event(gui_event const&) noexcept = default;
159 constexpr gui_event(gui_event&&) noexcept = default;
160 constexpr gui_event& operator=(gui_event const&) noexcept = default;
161 constexpr gui_event& operator=(gui_event&&) noexcept = default;
162
167 [[nodiscard]] static gui_event make_mouse_enter(point2i position) noexcept
168 {
169 auto r = gui_event{gui_event_type::mouse_enter};
170 r.mouse().position = position;
171 return r;
172 }
173
174 [[nodiscard]] static gui_event keyboard_grapheme(hi::grapheme grapheme) noexcept
175 {
176 auto r = gui_event{gui_event_type::keyboard_grapheme};
177 r.grapheme() = grapheme;
178 return r;
179 }
180
181 [[nodiscard]] static gui_event keyboard_partial_grapheme(hi::grapheme grapheme) noexcept
182 {
183 auto r = gui_event{gui_event_type::keyboard_partial_grapheme};
184 r.grapheme() = grapheme;
185 return r;
186 }
187
188 [[nodiscard]] static gui_event window_set_keyboard_target(
189 widget_id id,
190 keyboard_focus_group group = keyboard_focus_group::normal,
191 keyboard_focus_direction direction = keyboard_focus_direction::here) noexcept
192 {
194 r.keyboard_target().widget_id = id;
195 r.keyboard_target().group = group;
196 r.keyboard_target().direction = direction;
197 return r;
198 }
199
206 {
207 auto r = gui_event{type};
208 r.clipboard_data() = text;
209 return r;
210 }
211
214 [[nodiscard]] constexpr gui_event_type type() const noexcept
215 {
216 return _type;
217 }
218
224 [[nodiscard]] constexpr void set_type(gui_event_type type) noexcept
225 {
226 hilet previous_variant = variant();
227
228 _type = type;
229 if (previous_variant != variant()) {
230 switch (variant()) {
232 _data = mouse_event_data{};
233 break;
235 _data = hi::grapheme{};
236 break;
238 _data = keyboard_virtual_key{};
239 break;
241 _data = keyboard_target_data{};
242 break;
244 _data = aarectanglei{};
245 break;
247 _data = hi::text{};
248 default:;
249 }
250 }
251 }
252
257 [[nodiscard]] mouse_event_data& mouse() noexcept
258 {
260 return std::get<mouse_event_data>(_data);
261 }
262
267 [[nodiscard]] mouse_event_data const& mouse() const noexcept
268 {
270 return std::get<mouse_event_data>(_data);
271 }
272
277 [[nodiscard]] keyboard_virtual_key& key() noexcept
278 {
280 return std::get<keyboard_virtual_key>(_data);
281 }
282
287 [[nodiscard]] keyboard_virtual_key const& key() const noexcept
288 {
290 return std::get<keyboard_virtual_key>(_data);
291 }
292
297 [[nodiscard]] hi::grapheme& grapheme() noexcept
298 {
300 return std::get<hi::grapheme>(_data);
301 }
302
307 [[nodiscard]] hi::grapheme const& grapheme() const noexcept
308 {
310 return std::get<hi::grapheme>(_data);
311 }
312
313 [[nodiscard]] aarectanglei& rectangle() noexcept
314 {
316 return std::get<aarectanglei>(_data);
317 }
318
319 [[nodiscard]] aarectanglei const& rectangle() const noexcept
320 {
322 return std::get<aarectanglei>(_data);
323 }
324
325 [[nodiscard]] keyboard_target_data& keyboard_target() noexcept
326 {
328 return std::get<keyboard_target_data>(_data);
329 }
330
331 [[nodiscard]] keyboard_target_data const& keyboard_target() const noexcept
332 {
334 return std::get<keyboard_target_data>(_data);
335 }
336
337 [[nodiscard]] hi::text& clipboard_data() noexcept
338 {
340 return std::get<hi::text>(_data);
341 }
342
343 [[nodiscard]] hi::text const& clipboard_data() const noexcept
344 {
346 return std::get<hi::text>(_data);
347 }
348
349 [[nodiscard]] constexpr bool operator==(gui_event_type event_type) const noexcept
350 {
351 return type() == event_type;
352 }
353
354 [[nodiscard]] constexpr bool operator==(gui_event_variant event_variant) const noexcept
355 {
356 return variant() == event_variant;
357 }
358
359 [[nodiscard]] constexpr bool empty() const noexcept
360 {
361 return type() == gui_event_type::none;
362 }
363
364 constexpr operator bool() const noexcept
365 {
366 return not empty();
367 }
368
369 [[nodiscard]] constexpr gui_event_variant variant() const noexcept
370 {
371 return to_gui_event_variant(type());
372 }
373
376 [[nodiscard]] constexpr bool is_left_button_up(aarectanglei active_area) const noexcept
377 {
378 using enum gui_event_type;
379 return type() == mouse_up and mouse().cause.left_button and active_area.contains(mouse().position);
380 }
381
384 [[nodiscard]] constexpr vector2i drag_delta() const noexcept
385 {
386 using enum gui_event_type;
387 return type() == mouse_drag ? mouse().position - mouse().down_position : vector2i{};
388 }
389
398 [[nodiscard]] constexpr friend gui_event operator*(translate2i const& transform, gui_event const& rhs) noexcept
399 {
400 auto r = rhs;
401 if (rhs == gui_event_variant::mouse) {
402 r.mouse().position = transform * rhs.mouse().position;
403 r.mouse().down_position = transform * rhs.mouse().down_position;
404 }
405 return r;
406 }
407
408private:
409 using data_type =
410 std::variant<mouse_event_data, keyboard_virtual_key, keyboard_target_data, hi::grapheme, aarectanglei, hi::text>;
411
412 gui_event_type _type;
413 data_type _data;
414};
415
416}} // namespace hi::v1
417
418template<typename CharT>
419struct std::formatter<hi::gui_event, CharT> : std::formatter<std::string_view, CharT> {
420 auto format(hi::gui_event const& t, auto& fc)
421 {
422 return std::formatter<std::string_view, CharT>::format(hi::gui_event_type_metadata[t.type()], fc);
423 }
424};
Definition of a GUI event variant.
Definition of GUI event type.
#define hi_assert(expression,...)
Assert if expression is true.
Definition assert.hpp:199
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
gui_event_type
GUI event type.
Definition gui_event_type.hpp:21
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:51
gui_event_variant
A granular gui event type.
Definition gui_event_variant.hpp:18
@ window_set_keyboard_target
Change the keyboard target widget for this window.
@ keyboard
The gui_event has keyboard data.
@ mouse
The gui_event has mouse data.
@ keyboard_target
The gui_event has keyboard target data.
@ grapheme
The gui_event has grapheme data.
@ rectangle
The gui_event has rectangle data.
@ clipboard_data
The gui_event has clipboard data.
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
Information for a mouse event.
Definition gui_event.hpp:32
uint8_t click_count
Number of clicks from the last button clicked.
Definition gui_event.hpp:63
point2i position
The current position of the mouse pointer.
Definition gui_event.hpp:37
mouse_buttons down
Buttons that are also held down.
Definition gui_event.hpp:59
mouse_buttons cause
Buttons which have caused this event.
Definition gui_event.hpp:55
point2i down_position
The position the last time a button was pressed.
Definition gui_event.hpp:45
vector2i wheel_delta
Change in wheel rotation, in points (pt).
Definition gui_event.hpp:51
Definition gui_event.hpp:66
A user interface event.
Definition gui_event.hpp:75
constexpr friend gui_event operator*(translate2i const &transform, gui_event const &rhs) noexcept
Transform a gui-event to another coordinate system.
Definition gui_event.hpp:398
utc_nanoseconds time_point
The time when the event was created.
Definition gui_event.hpp:79
static gui_event make_mouse_enter(point2i position) noexcept
Create a mouse enter event.
Definition gui_event.hpp:167
gui_event(gui_event_type type, aarectanglei rectangle) noexcept
Create a rectangle event.
Definition gui_event.hpp:132
static gui_event make_clipboard_event(gui_event_type type, hi::text text) noexcept
Create clipboard event.
Definition gui_event.hpp:205
mouse_event_data const & mouse() const noexcept
Get the mouse event information.
Definition gui_event.hpp:267
constexpr void set_type(gui_event_type type) noexcept
Change the type of the gui_event.
Definition gui_event.hpp:224
constexpr gui_event() noexcept
Create an empty GUI event.
Definition gui_event.hpp:113
hi::grapheme & grapheme() noexcept
Get the grapheme entered on the keyboard.
Definition gui_event.hpp:297
keyboard_modifiers keyboard_modifiers
Keyboard modifiers: shift, ctrl, alt, etc.
Definition gui_event.hpp:85
keyboard_state keyboard_state
State of the keyboard; caps-lock, num-lock, scroll-lock.
Definition gui_event.hpp:89
hi::grapheme const & grapheme() const noexcept
Get the grapheme entered on the keyboard.
Definition gui_event.hpp:307
constexpr vector2i drag_delta() const noexcept
Get the location of the mouse relative to the start of a drag.
Definition gui_event.hpp:384
constexpr gui_event_type type() const noexcept
Get the event type.
Definition gui_event.hpp:214
constexpr bool is_left_button_up(aarectanglei 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:376
keyboard_virtual_key const & key() const noexcept
Get the key from the keyboard event.
Definition gui_event.hpp:287
mouse_event_data & mouse() noexcept
Get the mouse event information.
Definition gui_event.hpp:257
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:146
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:98
gui_event(gui_event_type type) noexcept
Create am empty GUI event.
Definition gui_event.hpp:122
keyboard_virtual_key & key() noexcept
Get the key from the keyboard event.
Definition gui_event.hpp:277