Repository navigation
Expand file tree
/
Copy pathFocusSystem.cpp
More file actions
379 lines (345 loc) · 9.1 KB
/
Copy pathFocusSystem.cpp
File metadata and controls
379 lines (345 loc) · 9.1 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
#include "FocusSystem.h"
#include <deki/Object.h>
#include "deki-input/DekiInput.h"
#include "deki-input/Keys.h"
#include <algorithm>
#include <cmath>
#include <vector>
namespace Deki2D
{
namespace
{
struct Scope
{
Deki::Object* root = nullptr;
IFocusable* owner = nullptr;
};
constexpr size_t kNoListener = static_cast<size_t>(-1);
std::vector<IFocusable*> s_Widgets;
std::vector<Scope> s_Scopes;
IFocusable* s_Focused = nullptr;
size_t s_Listener = kNoListener;
bool s_Wrap = true;
void Listen(bool on)
{
if (on && s_Listener == kNoListener)
{
s_Listener = DekiInput::DekiInput::RegisterEventCallback(
[](const DekiInput::InputEvent& event)
{
using DekiInput::InputEventType;
if (event.type == InputEventType::KeyDown)
{
FocusSystem::HandleKey(event.key, event.character);
}
else if (event.type == InputEventType::MouseButtonDown || event.type == InputEventType::TouchDown)
{
FocusSystem::HandlePointerDown();
}
});
}
else if (!on && s_Listener != kNoListener)
{
DekiInput::DekiInput::UnregisterEventCallback(s_Listener);
s_Listener = kNoListener;
}
}
bool IsCandidate(IFocusable* widget)
{
if (!widget || !widget->CanFocus())
{
return false;
}
Deki::Object* obj = widget->GetFocusObject();
if (!obj || !obj->IsActiveInHierarchy())
{
return false;
}
if (!s_Scopes.empty())
{
Deki::Object* root = s_Scopes.back().root;
if (obj != root && !obj->IsDescendantOf(root))
{
return false;
}
}
return true;
}
// Reading order: top to bottom, then left to right. World Y is up.
bool ReadsBefore(IFocusable* a, IFocusable* b)
{
const Deki::Object* oa = a->GetFocusObject();
const Deki::Object* ob = b->GetFocusObject();
const float ay = oa->GetWorldY();
const float by = ob->GetWorldY();
if (std::fabs(ay - by) > 1e-3f)
{
return ay > by;
}
return oa->GetWorldX() < ob->GetWorldX();
}
IFocusable* First()
{
IFocusable* best = nullptr;
for (IFocusable* w : s_Widgets)
{
if (IsCandidate(w) && (!best || ReadsBefore(w, best)))
{
best = w;
}
}
return best;
}
// A widget's box: centre and half size, in world meters.
struct FocusBox
{
float x = 0.0f;
float y = 0.0f;
float halfW = 0.0f;
float halfH = 0.0f;
};
FocusBox BoxOf(IFocusable* widget)
{
const Deki::Object* obj = widget->GetFocusObject();
float w = 0.0f;
float h = 0.0f;
widget->GetFocusSize(w, h);
FocusBox box;
box.x = obj->GetWorldX();
box.y = obj->GetWorldY();
box.halfW = w * 0.5f * std::fabs(obj->GetWorldScaleX());
box.halfH = h * 0.5f * std::fabs(obj->GetWorldScaleY());
return box;
}
// The gap between two spans on one axis, 0 when they overlap.
float SpanGap(float a, float halfA, float b, float halfB)
{
return std::max(0.0f, std::fabs(a - b) - halfA - halfB);
}
// The candidate nearest the focused widget along (dirX, dirY), one of the
// four arrow directions. Distance along the way counts once and distance off
// to the side twice, both from edge to edge, so a widget straight ahead
// beats a closer one off to the side; centre distance breaks ties. With
// nothing that way and wrapping on, the farthest one the other way, so a
// column comes round.
IFocusable* Nearest(float dirX, float dirY)
{
const FocusBox from = BoxOf(s_Focused);
const bool vertical = dirX == 0.0f;
IFocusable* ahead = nullptr;
float aheadScore = 0.0f;
float aheadCentre = 0.0f;
IFocusable* behind = nullptr;
float behindScore = 0.0f;
for (IFocusable* w : s_Widgets)
{
if (w == s_Focused || !IsCandidate(w))
{
continue;
}
const FocusBox to = BoxOf(w);
const float dx = to.x - from.x;
const float dy = to.y - from.y;
const float along = dx * dirX + dy * dirY;
const float centre = std::fabs(dx) + std::fabs(dy);
const float gapAlong =
vertical ? SpanGap(from.y, from.halfH, to.y, to.halfH) : SpanGap(from.x, from.halfW, to.x, to.halfW);
const float gapSide =
vertical ? SpanGap(from.x, from.halfW, to.x, to.halfW) : SpanGap(from.y, from.halfH, to.y, to.halfH);
if (along > 1e-3f)
{
const float score = gapAlong + gapSide * 2.0f;
if (!ahead || score < aheadScore - 1e-4f || (score < aheadScore + 1e-4f && centre < aheadCentre))
{
ahead = w;
aheadScore = score;
aheadCentre = centre;
}
}
else if (along < -1e-3f)
{
// Farthest back, then most in line.
const float score = along + gapSide * 2.0f;
if (!behind || score < behindScore)
{
behind = w;
behindScore = score;
}
}
}
if (ahead)
{
return ahead;
}
return s_Wrap ? behind : nullptr;
}
IFocusable* NextInReadingOrder()
{
IFocusable* next = nullptr;
IFocusable* first = nullptr;
for (IFocusable* w : s_Widgets)
{
if (w == s_Focused || !IsCandidate(w))
{
continue;
}
if (!first || ReadsBefore(w, first))
{
first = w;
}
if (ReadsBefore(s_Focused, w) && (!next || ReadsBefore(w, next)))
{
next = w;
}
}
return next ? next : first;
}
} // namespace
void FocusSystem::Register(IFocusable* widget)
{
if (!widget || std::find(s_Widgets.begin(), s_Widgets.end(), widget) != s_Widgets.end())
{
return;
}
s_Widgets.push_back(widget);
Listen(true);
}
void FocusSystem::Unregister(IFocusable* widget)
{
if (s_Focused == widget)
{
// No OnFocusChanged: the widget is on its way out.
s_Focused = nullptr;
}
for (Scope& scope : s_Scopes)
{
if (scope.owner == widget)
{
scope.owner = nullptr;
}
}
auto it = std::find(s_Widgets.begin(), s_Widgets.end(), widget);
if (it != s_Widgets.end())
{
s_Widgets.erase(it);
}
if (s_Widgets.empty())
{
s_Scopes.clear();
Listen(false);
}
}
IFocusable* FocusSystem::GetFocused()
{
return s_Focused;
}
void FocusSystem::SetFocus(IFocusable* widget)
{
if (widget == s_Focused)
{
return;
}
IFocusable* old = s_Focused;
s_Focused = widget;
if (old)
{
old->OnFocusChanged(false);
}
if (widget)
{
widget->OnFocusChanged(true);
}
}
bool FocusSystem::HandleKey(uint32_t key, uint32_t character)
{
namespace Keys = DekiInput::Keys;
// The focused widget can have been hidden or disabled since.
if (s_Focused && !IsCandidate(s_Focused))
{
SetFocus(nullptr);
}
if (s_Focused && s_Focused->OnFocusKey(key, character))
{
return true;
}
if (!s_Scopes.empty() && s_Scopes.back().owner && s_Scopes.back().owner != s_Focused &&
s_Scopes.back().owner->OnFocusKey(key, character))
{
return true;
}
const bool arrow = key == Keys::Up || key == Keys::Down || key == Keys::Left || key == Keys::Right;
if (!s_Focused)
{
// Enter is left alone: on a screen with a text field it types.
if (arrow || key == Keys::Tab)
{
IFocusable* first = First();
SetFocus(first);
return first != nullptr;
}
return false;
}
IFocusable* next = nullptr;
switch (key)
{
case Keys::Up: next = Nearest(0.0f, 1.0f); break;
case Keys::Down: next = Nearest(0.0f, -1.0f); break;
case Keys::Left: next = Nearest(-1.0f, 0.0f); break;
case Keys::Right: next = Nearest(1.0f, 0.0f); break;
case Keys::Tab: next = NextInReadingOrder(); break;
default: return false;
}
if (next)
{
SetFocus(next);
}
// An arrow with nowhere to go is still used: it must not reach anything
// else listening for keys while a widget has focus.
return true;
}
void FocusSystem::HandlePointerDown()
{
SetFocus(nullptr);
}
void FocusSystem::PushScope(Deki::Object* root, IFocusable* owner)
{
if (!root)
{
return;
}
s_Scopes.push_back({ root, owner });
// Focus outside the new scope goes.
if (s_Focused && !IsCandidate(s_Focused))
{
SetFocus(nullptr);
}
}
void FocusSystem::PopScope(Deki::Object* root)
{
for (size_t i = 0; i < s_Scopes.size(); ++i)
{
if (s_Scopes[i].root == root)
{
s_Scopes.resize(i);
break;
}
}
}
bool FocusSystem::IsInputBlocked(const Deki::Object* obj)
{
if (s_Scopes.empty() || !obj)
{
return false;
}
Deki::Object* root = s_Scopes.back().root;
return obj != root && !obj->IsDescendantOf(root);
}
void FocusSystem::SetWrap(bool wrap)
{
s_Wrap = wrap;
}
size_t FocusSystem::GetWidgetCount()
{
return s_Widgets.size();
}
} // namespace Deki2D