Repository navigation
Expand file tree
/
Copy pathListComponent.cpp
More file actions
408 lines (375 loc) · 11.5 KB
/
Copy pathListComponent.cpp
File metadata and controls
408 lines (375 loc) · 11.5 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
#include "ListComponent.h"
#include <deki/Object.h>
#include "deki-input/InputCollider.h"
#include "deki-input/Keys.h"
#include "deki-rendering/Fingerprint.h"
#include "WidgetShapes.h"
#include "WidgetText.h"
#include <algorithm>
#include <cmath>
#include <cstdio>
namespace Deki2D
{
namespace
{
// How a section title starts.
constexpr const char* kTitlePrefix = "# ";
constexpr size_t kTitlePrefixLength = 2;
} // namespace
ListComponent::~ListComponent()
{
FocusSystem::Unregister(this);
}
void ListComponent::Start()
{
SyncParts();
FocusSystem::Register(this);
Listen();
}
void ListComponent::Listen()
{
if (m_Listening || !m_Rows)
{
return;
}
auto* rows = m_Rows->GetComponent<DekiInput::InputCollider>();
if (!rows)
{
return;
}
m_Listening = true;
rows->onPointerDown.push_back(
[this](float x, float y)
{
(void)x;
if (!isEnabled || FocusSystem::IsInputBlocked(GetOwner()))
{
m_PressedItem = -1;
return;
}
m_PressedItem = ItemAt(y);
m_PressY = y;
m_PressScroll = m_Scroll;
m_Dragged = false;
if (m_PressedItem >= 0 && !IsTitle(m_PressedItem))
{
m_Highlight = m_PressedItem;
SyncParts();
}
});
rows->onPointerMove.push_back(
[this](float x, float y)
{
(void)x;
if (m_PressedItem < 0 && !m_Dragged)
{
return;
}
// A drag of half a row starts scrolling, and the press is no
// longer a tap. Dragging up shows the items further down.
const float moved = y - m_PressY;
if (!m_Dragged && std::fabs(moved) < rowHeight * 0.5f)
{
return;
}
m_Dragged = true;
const int32_t rowsMoved = static_cast<int32_t>(std::lround(moved / rowHeight));
ScrollTo(m_PressScroll + rowsMoved);
});
rows->onPointerUp.push_back(
[this](float x, float y)
{
(void)x;
if (!m_Dragged && m_PressedItem >= 0 && ItemAt(y) == m_PressedItem)
{
Click(m_PressedItem);
}
m_PressedItem = -1;
m_Dragged = false;
});
}
void ListComponent::AddOnItemClickedCallback(const std::function<void(int32_t, const std::string&)>& callback)
{
onItemClicked.push_back(callback);
}
void ListComponent::Click(int32_t index)
{
if (!isEnabled || index < 0 || index >= static_cast<int32_t>(items.size()) || IsTitle(index))
{
return;
}
m_Highlight = index;
SyncParts();
const std::string text = GetItemText(index);
for (const auto& callback : onItemClicked)
{
callback(index, text);
}
}
bool ListComponent::IsTitle(int32_t index) const
{
return index >= 0 && index < static_cast<int32_t>(items.size()) &&
items[index].compare(0, kTitlePrefixLength, kTitlePrefix) == 0;
}
std::string ListComponent::GetItemText(int32_t index) const
{
if (index < 0 || index >= static_cast<int32_t>(items.size()))
{
return {};
}
return IsTitle(index) ? items[index].substr(kTitlePrefixLength) : items[index];
}
int32_t ListComponent::RowsShown() const
{
return std::max<int32_t>(1, std::min<int32_t>(visibleRows, static_cast<int32_t>(m_RowParts.size())));
}
int32_t ListComponent::MaxScroll() const
{
return std::max<int32_t>(0, static_cast<int32_t>(items.size()) - RowsShown());
}
void ListComponent::ScrollTo(int32_t index)
{
const int32_t scroll = std::clamp<int32_t>(index, 0, MaxScroll());
if (scroll != m_Scroll)
{
m_Scroll = scroll;
SyncParts();
}
}
float ListComponent::ListHeight() const
{
return rowHeight * static_cast<float>(std::max<int32_t>(1, visibleRows));
}
int32_t ListComponent::ItemAt(float worldY) const
{
const Deki::Object* owner = GetOwner();
if (!owner || rowHeight <= 0.0f)
{
return -1;
}
const float top = owner->GetWorldY() + ListHeight() * 0.5f;
const int32_t row = static_cast<int32_t>(std::floor((top - worldY) / rowHeight));
if (row < 0 || row >= RowsShown())
{
return -1;
}
const int32_t item = m_Scroll + row;
return item < static_cast<int32_t>(items.size()) ? item : -1;
}
int32_t ListComponent::NextClickable(int32_t from, int32_t step) const
{
for (int32_t i = from; i >= 0 && i < static_cast<int32_t>(items.size()); i += step)
{
if (!IsTitle(i))
{
return i;
}
}
return -1;
}
void ListComponent::MoveHighlight(int32_t index)
{
if (index < 0)
{
return;
}
m_Highlight = index;
// In view: a title just above it shows too, so the section is named.
const int32_t rows = RowsShown();
const int32_t top = (index > 0 && IsTitle(index - 1)) ? index - 1 : index;
if (top < m_Scroll)
{
m_Scroll = top;
}
else if (index >= m_Scroll + rows)
{
m_Scroll = index - rows + 1;
}
m_Scroll = std::clamp<int32_t>(m_Scroll, 0, MaxScroll());
SyncParts();
}
void ListComponent::OnPropertyChanged(const char* propertyName)
{
(void)propertyName;
m_LastLayout = 0;
SyncParts();
}
void ListComponent::RuntimeUpdate(float deltaTime)
{
(void)deltaTime;
SyncParts();
}
void ListComponent::OnFocusChanged(bool focused)
{
m_Focused = focused;
WidgetParts::ShowFocus(GetOwner(), focused);
// Focus arriving shows where the arrows start.
if (focused && m_Highlight < 0)
{
MoveHighlight(NextClickable(m_Scroll, 1));
}
}
bool ListComponent::OnFocusKey(uint32_t key, uint32_t character)
{
(void)character;
namespace Keys = DekiInput::Keys;
if (key == Keys::Up || key == Keys::Down)
{
const int32_t step = key == Keys::Down ? 1 : -1;
const int32_t next = NextClickable(m_Highlight < 0 ? m_Scroll : m_Highlight + step, step);
if (next < 0)
{
return false; // past the end: the focus moves on
}
MoveHighlight(next);
return true;
}
if ((key == Keys::Enter || key == Keys::Space) && m_Highlight >= 0)
{
Click(m_Highlight);
return true;
}
return false;
}
void ListComponent::SyncParts()
{
Deki::Object* owner = GetOwner();
if (!owner)
{
return;
}
if (owner->GetChildren().size() != m_KnownChildren || static_cast<int32_t>(m_RowParts.size()) < visibleRows)
{
EnsureParts();
m_KnownChildren = owner->GetChildren().size();
m_LastLayout = 0;
}
m_Scroll = std::clamp<int32_t>(m_Scroll, 0, MaxScroll());
if (m_Highlight >= static_cast<int32_t>(items.size()))
{
m_Highlight = -1;
}
DekiRendering::Fingerprint f;
f.Add(width);
f.Add(rowHeight);
f.Add(visibleRows);
f.Add(static_cast<uint32_t>(items.size()));
f.Add(static_cast<uint32_t>(isEnabled));
f.Add(m_Highlight);
f.Add(m_Scroll);
// The texts by content: an item can be edited in place.
for (const std::string& item : items)
{
uint32_t h = 2166136261u;
for (char c : item)
{
h = (h ^ static_cast<uint8_t>(c)) * 16777619u;
}
f.Add(h);
}
if (f.Value() == m_LastLayout)
{
return;
}
m_LastLayout = f.Value();
LayoutParts();
}
void ListComponent::EnsureParts()
{
Deki::Object* owner = GetOwner();
const float corner = rowHeight * 0.25f;
m_Back = WidgetParts::ShapePart(owner, "Back", ShapeKind::Rectangle, WidgetColors::Panel, corner);
m_HighlightPart = WidgetParts::ShapePart(owner, "Highlight", ShapeKind::Rectangle, WidgetColors::Track, corner);
m_Thumb = WidgetParts::ShapePart(owner, "Thumb", ShapeKind::Rectangle, WidgetColors::DimText, corner);
m_Focus = WidgetParts::ShapePart(owner, "Focus", ShapeKind::Rectangle, WidgetColors::Focus, corner * 1.5f);
if (m_Focus)
{
m_Focus->SetActive(m_Focused);
}
bool created = false;
m_Rows = WidgetParts::FindOrCreate<DekiInput::InputCollider>(owner, "Rows", created);
m_RowParts.clear();
for (int32_t i = 0; i < visibleRows; ++i)
{
char name[16];
std::snprintf(name, sizeof(name), "Row%d", static_cast<int>(i));
Deki::Object* row = WidgetParts::TextPart(owner, name, WidgetColors::Text, TextAlign::Left);
if (!row)
{
break; // on the device: the rows the scene has
}
m_RowParts.push_back(row);
}
if (HasStarted())
{
Listen();
}
}
void ListComponent::LayoutParts()
{
const float listHeight = ListHeight();
const float pad = rowHeight * 0.4f;
const int32_t rows = RowsShown();
const bool scrolls = static_cast<int32_t>(items.size()) > rows;
const float thumbWidth = rowHeight * 0.12f;
const float textWidth = std::max(0.0f, width - pad * 2.0f - (scrolls ? thumbWidth + pad * 0.5f : 0.0f));
WidgetParts::PlaceShape(m_Back, 0.0f, 0.0f, width, listHeight);
if (m_Rows)
{
m_Rows->SetLocalPosition(0.0f, 0.0f);
if (auto* collider = m_Rows->GetComponent<DekiInput::InputCollider>())
{
collider->width = width;
collider->height = listHeight;
}
}
for (int32_t i = 0; i < static_cast<int32_t>(m_RowParts.size()); ++i)
{
Deki::Object* row = m_RowParts[i];
const int32_t item = m_Scroll + i;
const bool used = i < rows && item < static_cast<int32_t>(items.size());
row->SetActive(used);
if (!used)
{
continue;
}
const float y = listHeight * 0.5f - rowHeight * (static_cast<float>(i) + 0.5f);
WidgetParts::PlaceText(row, -width * 0.5f + pad + textWidth * 0.5f, y, textWidth, rowHeight);
WidgetParts::SetPartText(row, GetItemText(item));
if (TextComponent* text = WidgetParts::Text(row))
{
text->color = (IsTitle(item) || !isEnabled) ? WidgetColors::DimText : WidgetColors::Text;
}
}
const int32_t highlightRow = m_Highlight - m_Scroll;
if (m_HighlightPart)
{
const bool visible = m_Highlight >= 0 && highlightRow >= 0 && highlightRow < rows;
m_HighlightPart->SetActive(visible);
const float y = listHeight * 0.5f - rowHeight * (static_cast<float>(highlightRow) + 0.5f);
WidgetParts::PlaceShape(m_HighlightPart, 0.0f, y, width - rowHeight * 0.2f, rowHeight * 0.85f);
}
// The thumb: as tall as the share of the items in view, as low as the
// scroll has gone.
if (m_Thumb)
{
m_Thumb->SetActive(scrolls);
if (scrolls)
{
const float track = listHeight - pad;
const float share = static_cast<float>(rows) / static_cast<float>(items.size());
const float thumbHeight = std::max(rowHeight * 0.5f, track * share);
const float travel = track - thumbHeight;
const float along = MaxScroll() > 0 ? static_cast<float>(m_Scroll) / static_cast<float>(MaxScroll()) : 0.0f;
const float y = track * 0.5f - thumbHeight * 0.5f - travel * along;
WidgetParts::PlaceShape(m_Thumb, width * 0.5f - pad * 0.5f - thumbWidth * 0.5f, y, thumbWidth, thumbHeight);
}
}
const float ring = rowHeight * 0.3f;
WidgetParts::PlaceShape(m_Focus, 0.0f, 0.0f, width + ring, listHeight + ring);
if (ShapeComponent* shape = WidgetParts::Shape(m_Focus))
{
shape->lineWidth = rowHeight * 0.08f;
}
}
} // namespace Deki2D