-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMouseHandler.cpp
More file actions
130 lines (112 loc) · 3.13 KB
/
MouseHandler.cpp
File metadata and controls
130 lines (112 loc) · 3.13 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
#include "BetterWin.h"
#include "MouseHandler.h"
std::pair<int, int> MouseHandler::getCursorPos() const noexcept { return { x, y }; }
std::optional<MouseHandler::RawDelta> MouseHandler::readRawDelta() noexcept
{
if (rawDeltaBuffer.empty()) {
return std::nullopt;
}
const RawDelta d = rawDeltaBuffer.front();
rawDeltaBuffer.pop();
return d;
}
int MouseHandler::getX() const noexcept { return x; }
int MouseHandler::getY() const noexcept { return y; }
bool MouseHandler::isInWindow() const noexcept { return inWindow; }
bool MouseHandler::lmbPressed() const noexcept { return lmbDown; }
bool MouseHandler::rmbPressed() const noexcept { return rmbDown; }
std::optional<MouseHandler::Event> MouseHandler::read() noexcept
{
if (buf.size() > 0u) {
MouseHandler::Event e = buf.front();
buf.pop();
return e;
}
return {};
}
void MouseHandler::clear() noexcept { buf = std::queue<Event>(); }
void MouseHandler::enableRaw() noexcept { rawEnabled = true; }
void MouseHandler::disableRaw() noexcept { rawEnabled = false; }
bool MouseHandler::rawOn() const noexcept { return rawEnabled; }
void MouseHandler::onMove(int newx, int newy) noexcept
{
x = newx, y = newy;
buf.push(MouseHandler::Event(MouseHandler::Event::Type::MOVE, *this));
trimBuffer();
}
void MouseHandler::onWindowLeave() noexcept
{
inWindow = false;
buf.push(MouseHandler::Event(MouseHandler::Event::Type::WINDOW_LEAVE, *this));
trimBuffer();
}
void MouseHandler::onWindowEnter() noexcept
{
inWindow = true;
buf.push(MouseHandler::Event(MouseHandler::Event::Type::WINDOW_ENTER, *this));
trimBuffer();
}
void MouseHandler::onRawDelta(int dx, int dy) noexcept
{
rawDeltaBuffer.push({ dx,dy });
trimBuffer();
}
void MouseHandler::onLMBDown(int x, int y) noexcept
{
lmbDown = true;
buf.push(MouseHandler::Event(MouseHandler::Event::Type::LMB_DOWN, *this));
trimBuffer();
}
void MouseHandler::onLMBUp(int x, int y) noexcept
{
lmbDown = false;
buf.push(MouseHandler::Event(MouseHandler::Event::Type::LMP_UP, *this));
trimBuffer();
}
void MouseHandler::onRMBDown(int x, int y) noexcept
{
rmbDown = true;
buf.push(MouseHandler::Event(MouseHandler::Event::Type::RMB_DOWN, *this));
trimBuffer();
}
void MouseHandler::onRMBUp(int x, int y) noexcept
{
rmbDown = false;
buf.push(MouseHandler::Event(MouseHandler::Event::Type::RMB_UP, *this));
trimBuffer();
}
void MouseHandler::onMWheelUp(int x, int y) noexcept
{
buf.push(MouseHandler::Event(MouseHandler::Event::Type::MWHEEL_UP, *this));
trimBuffer();
}
void MouseHandler::onMWheelDown(int x, int y) noexcept
{
buf.push(MouseHandler::Event(MouseHandler::Event::Type::MWHEEL_DOWN, *this));
trimBuffer();
}
void MouseHandler::trimBuffer() noexcept
{
while (buf.size() > bufferSize) {
buf.pop();
}
}
void MouseHandler::trimRawInputBuf() noexcept
{
while (rawDeltaBuffer.size() > bufferSize) {
rawDeltaBuffer.pop();
}
}
void MouseHandler::onMWheelDelta(int x, int y, int delta) noexcept
{
wheelDeltaCarry += delta;
// generate events for every 120
while (wheelDeltaCarry >= WHEEL_DELTA) {
wheelDeltaCarry -= WHEEL_DELTA;
onMWheelUp(x, y);
}
while (wheelDeltaCarry <= -WHEEL_DELTA) {
wheelDeltaCarry += WHEEL_DELTA;
onMWheelDown(x, y);
}
}