Repository navigation
Expand file tree
/
Copy pathTrackball.cpp
More file actions
187 lines (166 loc) · 4.2 KB
/
Copy pathTrackball.cpp
File metadata and controls
187 lines (166 loc) · 4.2 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
#include "Trackball.h"
#include "Keys.h"
#include <deki/Time.h>
#include <algorithm>
#include <utility>
namespace DekiInput
{
Trackball::Trackball(Read read, Clock clock)
: m_Read(std::move(read)),
m_Clock(std::move(clock))
{
if (!m_Clock)
{
m_Clock = []() { return Deki::Time::GetTime(); };
}
}
void Trackball::SetPointerArea(int32_t width, int32_t height)
{
m_Width = width > 0 ? width : 1;
m_Height = height > 0 ? height : 1;
m_X = std::clamp<int32_t>(m_X, 0, m_Width - 1);
m_Y = std::clamp<int32_t>(m_Y, 0, m_Height - 1);
}
bool Trackball::Initialize()
{
m_Initialized = static_cast<bool>(m_Read);
m_Pressed = false;
m_DownKeys.clear();
m_X = m_Width / 2;
m_Y = m_Height / 2;
return m_Initialized;
}
void Trackball::Shutdown()
{
m_Initialized = false;
m_Callbacks.clear();
m_DownKeys.clear();
}
void Trackball::RegisterEventCallback(const InputEventCallback& callback)
{
m_Callbacks.push_back(callback);
}
bool Trackball::GetPointerPosition(int32_t* x, int32_t* y) const
{
if (m_Mode != Mode::Pointer)
{
return false;
}
if (x)
{
*x = m_X;
}
if (y)
{
*y = m_Y;
}
return true;
}
bool Trackball::IsKeyPressed(uint32_t key) const
{
if (m_Mode == Mode::Keys && key == Keys::Enter)
{
return m_Pressed;
}
return std::find(m_DownKeys.begin(), m_DownKeys.end(), key) != m_DownKeys.end();
}
void Trackball::Emit(InputEventType type, uint32_t key, int32_t x, int32_t y, bool pressed)
{
InputEvent event{};
event.type = type;
event.key = key;
event.x = x;
event.y = y;
event.pressed = pressed;
event.timestamp = m_Clock();
for (const auto& callback : m_Callbacks)
{
callback(event);
}
}
void Trackball::Update()
{
if (!m_Initialized)
{
return;
}
// Last update's arrows come up first, so a roll is a press per frame.
for (uint32_t key : m_DownKeys)
{
Emit(InputEventType::KeyUp, key, 0, 0, false);
}
m_DownKeys.clear();
const Sample s = m_Read();
if (m_Mode == Mode::Keys)
{
UpdateKeys(s);
}
else
{
UpdatePointer(s);
}
}
void Trackball::UpdateKeys(const Sample& s)
{
// Opposite steps in one frame cancel: the ball went nowhere.
const int32_t dx = static_cast<int32_t>(s.right) - static_cast<int32_t>(s.left);
const int32_t dy = static_cast<int32_t>(s.down) - static_cast<int32_t>(s.up);
if (dx != 0)
{
m_DownKeys.push_back(dx > 0 ? Keys::Right : Keys::Left);
}
if (dy != 0)
{
m_DownKeys.push_back(dy > 0 ? Keys::Down : Keys::Up);
}
for (uint32_t key : m_DownKeys)
{
Emit(InputEventType::KeyDown, key, 0, 0, true);
}
if (s.pressed != m_Pressed)
{
m_Pressed = s.pressed;
Emit(m_Pressed ? InputEventType::KeyDown : InputEventType::KeyUp, Keys::Enter, 0, 0, m_Pressed);
}
}
void Trackball::UpdatePointer(const Sample& s)
{
if (m_AreaSource)
{
int32_t w = 0, h = 0;
m_AreaSource(w, h);
if (w <= 0 || h <= 0)
{
return; // no screen yet: nowhere to move
}
if (!m_AreaKnown || w != m_Width || h != m_Height)
{
SetPointerArea(w, h);
if (!m_AreaKnown)
{
m_X = m_Width / 2;
m_Y = m_Height / 2;
m_AreaKnown = true;
}
}
}
const int32_t dx = (static_cast<int32_t>(s.right) - static_cast<int32_t>(s.left)) * m_PixelsPerStep;
const int32_t dy = (static_cast<int32_t>(s.down) - static_cast<int32_t>(s.up)) * m_PixelsPerStep;
if (dx != 0 || dy != 0)
{
const int32_t x = std::clamp<int32_t>(m_X + dx, 0, m_Width - 1);
const int32_t y = std::clamp<int32_t>(m_Y + dy, 0, m_Height - 1);
if (x != m_X || y != m_Y)
{
m_X = x;
m_Y = y;
Emit(InputEventType::MouseMove, 0, m_X, m_Y, m_Pressed);
}
}
if (s.pressed != m_Pressed)
{
m_Pressed = s.pressed;
Emit(m_Pressed ? InputEventType::MouseButtonDown : InputEventType::MouseButtonUp, 0, m_X, m_Y, m_Pressed);
}
}
} // namespace DekiInput