-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmouse.hpp
81 lines (71 loc) · 2.23 KB
/
mouse.hpp
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
#pragma once
#include "32blit.hpp"
class AutoRepeat {
public:
AutoRepeat(uint32_t repeat_time=200, uint32_t hold_time=1000) {
this->repeat_time = repeat_time;
this->hold_time = hold_time;
}
bool next(uint32_t time, bool state) {
bool changed = state != last_state;
last_state = state;
if(changed) {
if(state) {
pressed_time = time;
pressed = true;
last_time = time;
return true;
}
else {
pressed_time = 0;
pressed = false;
last_time = 0;
}
}
// Shortcut for no auto-repeat
if(repeat_time == 0) return false;
if(pressed) {
uint32_t repeat_rate = repeat_time;
if(hold_time > 0 && time - pressed_time > hold_time) {
repeat_rate /= 3;
}
if(time - last_time > repeat_rate) {
last_time = time;
return true;
}
}
return false;
}
private:
uint32_t repeat_time;
uint32_t hold_time;
bool pressed = false;
bool last_state = false;
uint32_t pressed_time = 0;
uint32_t last_time = 0;
};
class Mouse {
public:
Mouse();
void render(uint32_t time);
void update(uint32_t time);
blit::Vec2 cursor = blit::Vec2(0, 0);
blit::Vec2 last_cursor = blit::Vec2(0, 0);
blit::Vec2 dpad;
bool cursor_moved;
AutoRepeat ar_dpad_u = AutoRepeat(220, 400);
AutoRepeat ar_dpad_d = AutoRepeat(220, 400);
AutoRepeat ar_dpad_l = AutoRepeat(220, 400);
AutoRepeat ar_dpad_r = AutoRepeat(220, 400);
AutoRepeat ar_button_a = AutoRepeat(220, 400);
AutoRepeat ar_button_b = AutoRepeat(220, 400);
AutoRepeat ar_button_x = AutoRepeat(220, 400);
AutoRepeat ar_button_y = AutoRepeat(220, 400);
bool dpad_up, dpad_down, dpad_left, dpad_right,
dpad_up_pressed, dpad_down_pressed, dpad_left_pressed, dpad_right_pressed;
bool button_a_repeat, button_b_repeat, button_x_repeat, button_y_repeat;
bool button_a, button_b, button_x, button_y,
button_a_pressed, button_b_pressed, button_x_pressed, button_y_pressed;
bool button_menu, button_menu_pressed;
private:
};