forked from liblava/liblava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.hpp
More file actions
733 lines (594 loc) · 13.4 KB
/
input.hpp
File metadata and controls
733 lines (594 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
/**
* @file liblava/frame/input.hpp
* @brief Input handling
* @authors Lava Block OÜ and contributors
* @copyright Copyright (c) 2018-present, MIT License
*/
#pragma once
#include "liblava/core/id.hpp"
#include "liblava/core/misc.hpp"
namespace lava {
/**
* @brief Input keys
*/
enum class key : i32 {
unknown = undef,
/* printable keys */
space = 32,
apostrophe = 29, /* ' */
comma = 44, /* , */
minus = 45, /* - */
period = 46, /* . */
slash = 47, /* / */
_0 = 48,
_1,
_2,
_3,
_4,
_5,
_6,
_7,
_8,
_9,
semicolon = 59, /* ; */
equal = 61, /* = */
a = 65,
b,
c,
d,
e,
f,
g,
h,
i,
j,
k,
l,
m,
n,
o,
p,
q,
r,
s,
t,
u,
v,
w,
x,
y,
z,
left_bracket = 91, /* [ */
backslash = 92, /* \ */
right_bracket = 93, /* ] */
grave_accent = 96, /* ` */
world_1 = 161, /* non-US #1 */
world_2 = 162, /* non-US #2 */
/* function keys */
escape = 256,
enter,
tab,
backspace,
insert,
del, /* delete */
right,
left,
down,
up,
page_up,
page_down,
home,
end,
caps_lock = 280,
scroll_lock,
num_lock,
print_screen,
pause,
f1 = 290,
f2,
f3,
f4,
f5,
f6,
f7,
f8,
f9,
f10,
f11,
f12,
f13,
f14,
f15,
f16,
f17,
f18,
f19,
f20,
f21,
f22,
f23,
f24,
f25,
kp_0 = 320,
kp_1,
kp_2,
kp_3,
kp_4,
kp_5,
kp_6,
kp_7,
kp_8,
kp_9,
kp_decimal,
kp_divide,
kp_multiply,
kp_subtract,
kp_add,
kp_enter,
kp_equal,
left_shift = 340,
left_control,
left_alt,
left_super,
right_shift,
right_control,
right_alt,
right_super,
menu,
last = menu,
};
/// Reference to key
using key_ref = key const&;
/// List of keys
using keys = std::vector<key>;
/// Reference to list of keys
using keys_ref = keys const&;
/**
* @brief Convert input key to string
* @param k Input key
* @return string String representation
*/
string to_string(key k);
/**
* @brief Get scancode based on key
* @param key Input key
* @return i32 Input scan code
*/
i32 get_scancode(key key);
/**
* @brief Input actions
*/
enum class action : index {
release = 0,
press,
repeat
};
/// Refernece to action
using action_ref = action const&;
/**
* @brief Input mods
*/
enum class mod : flag {
none = 0 << 0,
shift = 1 << 0,
control = 1 << 1,
alt = 1 << 2,
super = 1 << 3,
caps_lock = 1 << 4,
num_lock = 1 << 5,
};
ENUM_FLAG_OPERATORS(mod)
/**
* @brief Check if mod is active
* @param m Target mod
* @param c Mod to check
* @return Mod is active or not
*/
inline bool check_mod(mod m, mod c) {
return (m & c) != mod::none;
}
/// Reference to mod
using mod_ref = mod const&;
/**
* @brief Convert input mod to string
* @param m Input mod
* @return string String representation
*/
string to_string(mod m);
/**
* @brief Key event
*/
struct key_event {
/// Reference to key event
using ref = key_event const&;
/// Key event function
using func = std::function<bool(ref)>;
/// List of key event listeners
using listeners = std::map<id, func>;
/// List of key events
using list = std::vector<key_event>;
/// Sender id
id sender;
/// Input key
lava::key key;
/// Input action
lava::action action;
/// Input mod
lava::mod mod;
/// Input scan code
i32 scancode = 0;
/**
* @brief Check if key is pressed
* @param k Key to check
* @return Key is pressed or not
*/
bool pressed(key_ref k) const {
return (action == action::press) && (key == k);
}
/**
* @brief Check if key is released
* @param k Key to check
* @return Key is released or not
*/
bool released(key_ref k) const {
return (action == action::release) && (key == k);
}
/**
* @brief Check if key is repeated
* @param k Key to check
* @return Key is repeated or not
*/
bool repeated(key_ref k) const {
return (action == action::repeat) && (key == k);
}
/**
* @brief Check if key is active
* @return Key is pressed (and repeated) or not active
*/
bool active() const {
return (action == action::press) || (action == action::repeat);
}
/**
* @brief Check if key is pressed with mod
* @param k Key to check
* @param m Mods to check
* @return Key is pressed with mod or not
*/
bool pressed(key_ref k, mod_ref m) const {
return pressed(k) && (mod == m);
}
};
/**
* @brief Input scroll offset
*/
struct scroll_offset {
/// X offset
r64 x = 0.0;
/// Y offset
r64 y = 0.0;
};
/**
* @brief Scroll event
*/
struct scroll_event {
/// Reference to scroll event
using ref = scroll_event const&;
/// Scroll event function
using func = std::function<bool(ref)>;
/// List of scroll event listeners
using listeners = std::map<id, func>;
/// List of scroll events
using list = std::vector<scroll_event>;
/// Sender id
id sender;
/// Input scroll offset
scroll_offset offset;
};
/**
* @brief Input mouse position
*/
struct mouse_position {
/// X position
r64 x = 0.0;
/// Y position
r64 y = 0.0;
};
/// Reference to mouse position
using mouse_position_ref = mouse_position const&;
/**
* @brief Mouse move event
*/
struct mouse_move_event {
/// Reference to mouse move event
using ref = mouse_move_event const&;
/// Mouse move event function
using func = std::function<bool(ref)>;
/// List of mouse move event listeners
using listeners = std::map<id, func>;
/// List of mouse move events
using list = std::vector<mouse_move_event>;
/// Sender id
id sender;
/// Input mouse position
mouse_position position;
};
/**
* @brief Input mouse buttons
*/
enum class mouse_button : index {
_1 = 0,
_2,
_3,
_4,
_5,
_6,
_7,
_8,
last = _8,
left = _1,
right = _2,
middle = _3,
};
/// Reference to mouse button
using mouse_button_ref = mouse_button const&;
/**
* @brief Mouse button event
*/
struct mouse_button_event {
/// Reference to mouse button event
using ref = mouse_button_event const&;
/// Mouse button event function
using func = std::function<bool(ref)>;
/// List of mouse button event listeners
using listeners = std::map<id, func>;
/// List of mouse button events
using list = std::vector<mouse_button_event>;
/// Sender id
id sender;
/// Input mouse button
mouse_button button;
/// Input action
lava::action action;
/// Input mod
lava::mod mod;
/**
* @brief Check if mouse button is pressed
* @param b Mouse button to check
* @return Mouse button is pressed or not
*/
bool pressed(mouse_button_ref b) const {
return action == action::press && button == b;
}
/**
* @brief Check if mouse button is released
* @param b Mouse button to check
* @return Mouse button is released or not
*/
bool released(mouse_button_ref b) const {
return action == action::release && button == b;
}
};
/**
* @brief Path drop event
*/
struct path_drop_event {
/// Reference to path drop event
using ref = path_drop_event const&;
/// Path drop event function
using func = std::function<bool(ref)>;
/// List of path drop event listeners
using listeners = std::map<id, func>;
/// List of path drop events
using list = std::vector<path_drop_event>;
/// Sender id
id sender;
/// List of files
string_list files;
};
/**
* @brief Mouse active event
*/
struct mouse_active_event {
/// Reference to mouse active event
using ref = mouse_active_event const&;
/// Mouse active event function
using func = std::function<bool(ref)>;
/// List of mouse active event listeners
using listeners = std::map<id, func>;
/// List of mouse active events
using list = std::vector<mouse_active_event>;
/// Sender id
id sender;
/// Active state
bool active = false;
};
/**
* @brief Input callback
*/
struct input_callback {
/// Const pointer to input callback
using cptr = input_callback const*;
/// List of input callbacks
using list = std::vector<input_callback*>;
/// List of const input callbacks
using clist = std::vector<cptr>;
/**
* @brief Input callback functions
* @tparam T Type of callback
*/
template <typename T>
using func = std::function<bool(typename T::ref)>;
/// Called on key event
key_event::func on_key_event;
/// Called on scroll event
scroll_event::func on_scroll_event;
/// Called on mouse move event
mouse_move_event::func on_mouse_move_event;
/// Called on mouse button event
mouse_button_event::func on_mouse_button_event;
/// Called on mouse active event
mouse_active_event::func on_mouse_active_event;
/// Called on path drop event
path_drop_event::func on_path_drop_event;
};
/**
* @brief List of input events
* @tparam T Type of event
*/
template <typename T>
struct input_events : T::list {
/**
* @brief Add event to list
* @param event Event to add
*/
void add(typename T::ref event) {
this->push_back(event);
}
/// List of event listeners
id_listeners<T> listeners;
};
/// List of key events
using input_key_events = input_events<key_event>;
/// List of scroll events
using input_scroll_events = input_events<scroll_event>;
/// List of mouse move events
using input_mouse_move_events = input_events<mouse_move_event>;
/// List of mouse button events
using input_mouse_button_events = input_events<mouse_button_event>;
/// List of mouse active events
using input_mouse_active_events = input_events<mouse_active_event>;
/// List of path drop events
using input_path_drop_events = input_events<path_drop_event>;
/// Input ignore result
constexpr bool const input_ignore = false;
/// Input done result
constexpr bool const input_done = true;
/**
* @brief Input handling
*/
struct input {
/// Pointer to input
using ptr = input*;
/// List of key events
input_key_events key;
/// List of scroll events
input_scroll_events scroll;
/// List of mouse move events
input_mouse_move_events mouse_move;
/// List of mouse button events
input_mouse_button_events mouse_button;
/// List of mouse active events
input_mouse_active_events mouse_active;
/// List of path drop events
input_path_drop_events path_drop;
/**
* @brief Handle events
*/
void handle_events();
/**
* @brief Add callback to the input handling
* @param callback Callback to add
*/
void add(input_callback::cptr callback) {
m_callbacks.push_back(callback);
}
/**
* @brief Remove callback from the input handling
* @param callback Callback to remove
*/
void remove(input_callback::cptr callback) {
lava::remove(m_callbacks, callback);
}
/**
* @brief Get the mouse position
* @return mouse_position_ref Current mouse position
*/
mouse_position_ref get_mouse_position() const {
return m_current_position;
}
/**
* @brief Set the mouse position
* @param position Current mouse position
*/
void set_mouse_position(mouse_position_ref position) {
m_current_position = position;
}
private:
/**
* @brief Handle mouse events
*/
void handle_mouse_events();
/// Current mouse position
mouse_position m_current_position;
/// List of input callbacks
input_callback::clist m_callbacks;
};
/**
* @brief Tooltip
*/
struct tooltip {
/**
* @brief Construct a new tooltip
* @param name Name of tooltip
* @param key Input key
* @param mod Input mod
*/
tooltip(string_ref name,
key key,
mod mod)
: name(name), key(key), mod(mod) {
}
/// List of tooltips
using list = std::vector<tooltip>;
/// Name of tooltip
string name;
/// Input key
lava::key key;
/// Input mod
lava::mod mod;
};
/**
* @brief Tooltip list
*/
struct tooltip_list {
/**
* @brief Add a tooltip
* @param name Name of tooltip
* @param key Input key
* @param mod Input mod (default: none)
*/
void add(string_ref name,
key key,
mod mod = mod::none) {
m_tooltips.emplace_back(name, key, mod);
}
/**
* @brief Clear tooltips
*/
void clear() {
m_tooltips.clear();
}
/**
* @brief Get tooltips
* @return tooltip::list List of tooltips
*/
tooltip::list const& get_list() const {
return m_tooltips;
}
/**
* @brief Set a new tooltip list
* @param list List of tooltips
*/
void set(tooltip::list const& list) {
m_tooltips = list;
}
/**
* @brief Convert tooltips to string
* @return string String representation
*/
string format_string() const;
private:
/// List of tooltips
tooltip::list m_tooltips;
};
} // namespace lava