-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScrollComponent.cpp
More file actions
748 lines (629 loc) · 22.7 KB
/
Copy pathScrollComponent.cpp
File metadata and controls
748 lines (629 loc) · 22.7 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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
#include "ScrollComponent.h"
#include "deki-input/InputCollider.h"
#include "deki-input/InputDispatch.h"
#include "DekiObject.h"
#include "DekiLogSystem.h"
#include "ClipComponent.h"
#include "ScrollElement.h"
#include <algorithm>
#include <cmath>
#include <cstring>
#include "Prefab.h"
// All layout/scroll math here works in world meters — matching the engine's
// meters-internal convention. ScrollElement / ClipComponent / padding values
// are float meters, transform x/y are meters, and pointer callbacks deliver
// meters too. No pixels-per-meter conversion needed anywhere in this file.
ScrollComponent::ScrollComponent()
: DekiBehaviour(),
mode(ScrollMode::NonTemplate),
direction(ScrollDirection::Vertical),
item_spacing(0.0f),
padding_top(0.0f),
padding_bottom(0.0f),
padding_left(0.0f),
padding_right(0.0f),
deceleration(static_cast<float>(0.95f)),
bounce_stiffness(static_cast<float>(0.15f)),
enable_bounce(true),
enable_inertia(true),
reverse_drag(false),
m_DragThreshold(static_cast<float>(0.625f)) // ~10 px at ppm=16
{
}
ScrollComponent::~ScrollComponent()
{
// Release gesture if we own it (prevents stuck input after destruction mid-drag)
if (InputDispatch::IsGestureClaimedBy(this))
InputDispatch::ReleaseGesture();
m_ClipObj = nullptr;
m_TemplateObj = nullptr;
m_SlotObjs.clear();
m_SlotItemIndices.clear();
m_NonTemplateChildren.clear();
m_NonTemplateSizes.clear();
m_NonTemplateOffsets.clear();
}
// ============================================================================
// Child Object Management
// ============================================================================
static DekiObject* FindChildByName(DekiObject* parent, const char* name)
{
if (!parent) return nullptr;
for (auto* child : parent->GetChildren())
{
if (child->GetName() == name)
return child;
}
return nullptr;
}
DekiObject* ScrollComponent::CloneTemplate(DekiObject* tmpl, const char* name)
{
if (!tmpl || !item_prefab.Get()) return nullptr;
Prefab* ownerPrefab = tmpl->GetOwnerPrefab();
if (!ownerPrefab) return nullptr;
DekiObject* instance = item_prefab.Get()->Instantiate(ownerPrefab);
if (instance)
{
instance->SetName(name);
}
return instance;
}
float ScrollComponent::MeasureChildSize(DekiObject* child) const
{
if (!child) return 0.0f;
auto* element = child->GetComponent<ScrollElement>();
if (!element) return 0.0f;
return (direction == ScrollDirection::Vertical) ? element->height : element->width;
}
int32_t ScrollComponent::GetTotalSlotCount() const
{
if (m_ItemSize <= 0.0f) return 3;
float vpSize = (direction == ScrollDirection::Vertical) ? GetViewportHeight() : GetViewportWidth();
int32_t visible = static_cast<int32_t>(((vpSize) / (m_ItemSize))) + 3; // +1 top, +1 bottom, +1 rounding
return std::max(static_cast<int32_t>(3), visible);
}
void ScrollComponent::EnsureChildObjects(DekiObject* owner)
{
if (!owner)
{
m_ClipObj = nullptr;
m_TemplateObj = nullptr;
m_SlotObjs.clear();
m_SlotItemIndices.clear();
m_NonTemplateChildren.clear();
m_NonTemplateSizes.clear();
m_NonTemplateOffsets.clear();
return;
}
// Discover Clip child (must already exist; authored in the editor).
m_ClipObj = FindChildByName(owner, "Clip");
if (!m_ClipObj)
{
DEKI_LOG_WARNING("ScrollComponent: missing 'Clip' child on '%s'",
owner->GetName().c_str());
m_TemplateObj = nullptr;
m_SlotObjs.clear();
m_SlotItemIndices.clear();
m_NonTemplateChildren.clear();
m_NonTemplateSizes.clear();
m_NonTemplateOffsets.clear();
return;
}
if (mode == ScrollMode::Template)
{
// Find the Template child (must already exist; authored in the editor).
m_TemplateObj = FindChildByName(m_ClipObj, "Template");
if (!m_TemplateObj)
{
DEKI_LOG_WARNING("ScrollComponent: missing 'Template' child on '%s'",
owner->GetName().c_str());
m_SlotObjs.clear();
m_SlotItemIndices.clear();
return;
}
m_TemplateObj->SetActive(false); // Template is always hidden at runtime
// Measure item size from template
m_ItemSize = MeasureChildSize(m_TemplateObj);
// Collect existing Slot children
m_SlotObjs.clear();
for (auto* child : m_ClipObj->GetChildren())
{
if (child->GetName().rfind("Slot", 0) == 0)
{
child->SetActive(true);
m_SlotObjs.push_back(child);
}
}
// Sort by name for correct order
std::sort(m_SlotObjs.begin(), m_SlotObjs.end(),
[](DekiObject* a, DekiObject* b) {
return a->GetName() < b->GetName();
});
int32_t totalSlots = GetTotalSlotCount();
// Create missing slots by cloning template
while (static_cast<int32_t>(m_SlotObjs.size()) < totalSlots)
{
std::string name = "Slot" + std::to_string(m_SlotObjs.size());
DekiObject* slot = CloneTemplate(m_TemplateObj, name.c_str());
if (slot)
{
m_ClipObj->AddChild(slot);
slot->SetActive(true);
m_SlotObjs.push_back(slot);
}
else
{
break;
}
}
// Hide excess slots
for (size_t i = totalSlots; i < m_SlotObjs.size(); ++i)
{
m_SlotObjs[i]->SetActive(false);
}
if (m_SlotObjs.size() > static_cast<size_t>(totalSlots))
{
m_SlotObjs.resize(totalSlots);
}
// Initialize slot item indices
m_SlotItemIndices.resize(m_SlotObjs.size(), -1);
}
else // NonTemplate mode
{
m_TemplateObj = nullptr;
m_SlotObjs.clear();
m_SlotItemIndices.clear();
// Collect all children under Clip and measure each one
m_NonTemplateChildren.clear();
m_NonTemplateSizes.clear();
for (auto* child : m_ClipObj->GetChildren())
{
m_NonTemplateChildren.push_back(child);
m_NonTemplateSizes.push_back(MeasureChildSize(child));
}
m_ItemCount = static_cast<int32_t>(m_NonTemplateChildren.size());
// Build prefix-sum offsets (one pass)
m_NonTemplateOffsets.resize(m_ItemCount + 1);
m_NonTemplateOffsets[0] = 0.0f;
for (int32_t i = 0; i < m_ItemCount; i++)
{
float spacing = (i < m_ItemCount - 1) ? item_spacing : 0.0f;
m_NonTemplateOffsets[i + 1] = ((((m_NonTemplateOffsets[i]) + (m_NonTemplateSizes[i]))) + (spacing));
}
// Keep m_ItemSize for Template code paths
m_ItemSize = m_NonTemplateChildren.empty() ? 0.0f : m_NonTemplateSizes[0];
}
}
void ScrollComponent::SyncChildObjects(DekiObject* owner)
{
EnsureChildObjects(owner);
CalculateContentSize();
AssignSlots();
}
// ============================================================================
// Recycling Algorithm
// ============================================================================
void ScrollComponent::AssignSlots()
{
if (!m_ClipObj) return;
float scrollPos = m_ScrollOffset;
float vpSize = (direction == ScrollDirection::Vertical) ? GetViewportHeight() : GetViewportWidth();
float stride = ((m_ItemSize) + (item_spacing));
if (stride <= 0.0f) return;
float halfVp = ((vpSize) * (0.5f));
float startPad = (direction == ScrollDirection::Vertical) ? padding_top : padding_left;
float crossPad = (direction == ScrollDirection::Vertical)
? ((((padding_left) - (padding_right))) * (0.5f))
: ((((padding_top) - (padding_bottom))) * (0.5f));
m_FirstVisibleIndex = m_ItemCount;
m_LastVisibleIndex = -1;
if (mode == ScrollMode::Template)
{
if (m_SlotObjs.empty() || m_ItemCount <= 0) return;
// First item index to show (with 1 buffer before viewport).
float adjustedScroll = ((scrollPos) - (startPad));
int32_t firstIndex = static_cast<int32_t>(std::floor(((adjustedScroll) / (stride)))) - 1;
if (firstIndex < 0) firstIndex = 0;
int32_t totalSlots = static_cast<int32_t>(m_SlotObjs.size());
for (int32_t s = 0; s < totalSlots; s++)
{
int32_t itemIndex = firstIndex + s;
if (itemIndex < 0 || itemIndex >= m_ItemCount)
{
m_SlotObjs[s]->SetActive(false);
m_SlotItemIndices[s] = -1;
continue;
}
m_SlotObjs[s]->SetActive(true);
// Position: item meter offset relative to viewport center.
float pos = ((((startPad) + (((static_cast<float>(itemIndex)) * (stride))))) - (scrollPos));
float slotCenter = ((((pos) + (((m_ItemSize) * (0.5f))))) - (halfVp));
if (direction == ScrollDirection::Vertical)
m_SlotObjs[s]->SetLocalPosition(crossPad, ((0.0f) - (slotCenter)));
else
m_SlotObjs[s]->SetLocalPosition(slotCenter, crossPad);
// Track visible range
if (((pos) + (m_ItemSize)) > 0.0f && pos < vpSize)
{
if (itemIndex < m_FirstVisibleIndex) m_FirstVisibleIndex = itemIndex;
if (itemIndex > m_LastVisibleIndex) m_LastVisibleIndex = itemIndex;
}
// REUSE OPTIMIZATION: only fire callback if item index changed
if (m_SlotItemIndices[s] != itemIndex)
{
m_SlotItemIndices[s] = itemIndex;
if (m_OnBindItem)
{
m_OnBindItem(m_SlotObjs[s], itemIndex);
}
}
}
}
else // NonTemplate mode
{
for (int32_t i = 0; i < static_cast<int32_t>(m_NonTemplateChildren.size()); i++)
{
DekiObject* child = m_NonTemplateChildren[i];
if (!child) continue;
float itemSize = m_NonTemplateSizes[i];
float pos = ((((startPad) + (m_NonTemplateOffsets[i]))) - (scrollPos));
// Visibility culling with per-item size buffer
float negItemSize = ((0.0f) - (itemSize));
float vpPlusItem = ((vpSize) + (itemSize));
bool visible = (((pos) + (itemSize)) > negItemSize && pos < vpPlusItem);
child->SetActive(visible);
if (visible)
{
float center = ((((pos) + (((itemSize) * (0.5f))))) - (halfVp));
if (direction == ScrollDirection::Vertical)
child->SetLocalPosition(crossPad, ((0.0f) - (center)));
else
child->SetLocalPosition(center, crossPad);
}
// Track visible range (within actual viewport, not buffer)
if (((pos) + (itemSize)) > 0.0f && pos < vpSize)
{
if (i < m_FirstVisibleIndex) m_FirstVisibleIndex = i;
if (i > m_LastVisibleIndex) m_LastVisibleIndex = i;
}
}
}
}
// ============================================================================
// Public API
// ============================================================================
float ScrollComponent::GetViewportWidth() const
{
if (m_ClipObj)
{
if (auto* clip = m_ClipObj->GetComponent<ClipComponent>())
return clip->width;
}
return 0.0f;
}
float ScrollComponent::GetViewportHeight() const
{
if (m_ClipObj)
{
if (auto* clip = m_ClipObj->GetComponent<ClipComponent>())
return clip->height;
}
return 0.0f;
}
void ScrollComponent::SetItemSpacing(float spacing)
{
item_spacing = spacing;
CalculateContentSize();
if (GetOwner()) SyncChildObjects(GetOwner());
}
void ScrollComponent::SetItemCount(int32_t count)
{
m_ItemCount = count;
CalculateContentSize();
// Invalidate all slot assignments so they rebind
std::fill(m_SlotItemIndices.begin(), m_SlotItemIndices.end(), -1);
if (GetOwner()) SyncChildObjects(GetOwner());
}
void ScrollComponent::SetDirection(ScrollDirection dir)
{
direction = dir;
}
void ScrollComponent::SetOnBindItem(const ScrollItemCallback& callback)
{
m_OnBindItem = callback;
// Invalidate all slot assignments to trigger initial bind
std::fill(m_SlotItemIndices.begin(), m_SlotItemIndices.end(), -1);
}
void ScrollComponent::ScrollTo(float position, bool smooth)
{
// Clamp target within valid range
float maxPos = GetMaxScrollOffset();
if (position < 0.0f) position = 0.0f;
if (position > maxPos) position = maxPos;
if (smooth)
{
m_ScrollTarget = position;
m_IsSmoothScrolling = true;
m_ScrollVelocity = 0.0f;
}
else
{
m_ScrollOffset = position;
m_ScrollVelocity = 0.0f;
m_IsSmoothScrolling = false;
if (GetOwner()) SyncChildObjects(GetOwner());
}
}
void ScrollComponent::ScrollToItem(int32_t index, bool smooth)
{
if (index < 0 || index >= m_ItemCount) return;
float position;
if (mode == ScrollMode::NonTemplate && !m_NonTemplateOffsets.empty())
position = m_NonTemplateOffsets[index];
else
position = ((static_cast<float>(index)) * (((m_ItemSize) + (item_spacing))));
ScrollTo(position, smooth);
}
float ScrollComponent::GetScrollPosition() const
{
return m_ScrollOffset;
}
int32_t ScrollComponent::GetSlotCount() const
{
return static_cast<int32_t>(m_SlotObjs.size());
}
DekiObject* ScrollComponent::GetSlotObject(int32_t slotIndex) const
{
if (slotIndex >= 0 && slotIndex < static_cast<int32_t>(m_SlotObjs.size()))
return m_SlotObjs[slotIndex];
return nullptr;
}
int32_t ScrollComponent::GetSlotItemIndex(int32_t slotIndex) const
{
if (slotIndex >= 0 && slotIndex < static_cast<int32_t>(m_SlotItemIndices.size()))
return m_SlotItemIndices[slotIndex];
return -1;
}
// ============================================================================
// Scroll Physics
// ============================================================================
void ScrollComponent::CalculateContentSize()
{
if (m_ItemCount <= 0)
{
m_ContentSize = 0.0f;
return;
}
float startPad = (direction == ScrollDirection::Vertical) ? padding_top : padding_left;
float endPad = (direction == ScrollDirection::Vertical) ? padding_bottom : padding_right;
if (mode == ScrollMode::NonTemplate && !m_NonTemplateOffsets.empty())
{
m_ContentSize = ((((startPad) + (m_NonTemplateOffsets[m_ItemCount]))) + (endPad));
}
else
{
// startPad + N*itemSize + (N-1)*spacing + endPad, all float.
float items = ((static_cast<float>(m_ItemCount)) * (m_ItemSize));
float gaps = ((static_cast<float>(m_ItemCount - 1)) * (item_spacing));
m_ContentSize = ((((((startPad) + (items))) + (gaps))) + (endPad));
}
}
float ScrollComponent::GetMaxScrollOffset() const
{
float vpSize = (direction == ScrollDirection::Vertical) ? GetViewportHeight() : GetViewportWidth();
float maxScroll = ((m_ContentSize) - (vpSize));
return (maxScroll < 0.0f) ? 0.0f : maxScroll;
}
void ScrollComponent::ClampScrollOffset()
{
float maxScroll = GetMaxScrollOffset();
if (m_ScrollOffset < 0.0f)
{
m_ScrollOffset = 0.0f;
m_ScrollVelocity = 0.0f;
}
else if (m_ScrollOffset > maxScroll)
{
m_ScrollOffset = maxScroll;
m_ScrollVelocity = 0.0f;
}
}
void ScrollComponent::Update(float delta_time)
{
if (m_IsDragging) return;
bool needsSync = false;
// Thresholds in meters/frame, sized to preserve the prior pixel feel at
// ppm=16. Computed once at first call so the macro arguments evaluate in
// their proper mode.
static const float kVelocityEpsilon = static_cast<float>(0.0625f); // ~1 px/frame
static const float kSmoothArriveEpsilon = static_cast<float>(0.004f); // ~0.06 px
// Phase 1: Smooth scroll animation (from ScrollTo with smooth=true)
if (m_IsSmoothScrolling)
{
float diff = m_ScrollTarget - m_ScrollOffset;
float easeFactor = 8.0f * 0.15f * delta_time * 60.0f;
if (easeFactor > 0.5f) easeFactor = 0.5f;
m_ScrollOffset = m_ScrollOffset + diff * easeFactor;
if (DekiMath::Abs(((m_ScrollTarget) - (m_ScrollOffset))) < kSmoothArriveEpsilon)
{
m_ScrollOffset = m_ScrollTarget;
m_IsSmoothScrolling = false;
}
// Clamp smooth scroll target within bounds
float maxScroll = GetMaxScrollOffset();
if (m_ScrollOffset < 0.0f) m_ScrollOffset = 0.0f;
if (m_ScrollOffset > maxScroll) m_ScrollOffset = maxScroll;
needsSync = true;
}
// Phase 2: Momentum scrolling (after drag release)
else if (enable_inertia && DekiMath::Abs(m_ScrollVelocity) > kVelocityEpsilon)
{
float stepCoef = static_cast<float>(delta_time * 60.0f);
m_ScrollOffset = ((m_ScrollOffset) + (((m_ScrollVelocity) * (stepCoef))));
// Frame-rate independent deceleration: deceleration^(dt*60)
float decayFactor = std::pow((deceleration), (stepCoef));
m_ScrollVelocity = ((m_ScrollVelocity) * (decayFactor));
if (DekiMath::Abs(m_ScrollVelocity) < kVelocityEpsilon)
m_ScrollVelocity = 0.0f;
needsSync = true;
}
// Always clamp within bounds
ClampScrollOffset();
if (needsSync && GetOwner())
SyncChildObjects(GetOwner());
}
void ScrollComponent::HandlePointerDown(float x, float y)
{
// Pointer x/y are world meters as float (input FFI). Convert to float
// at the boundary so internal touch tracking lives in the same numeric
// space as scroll state.
float touchPos = static_cast<float>((direction == ScrollDirection::Vertical) ? y : x);
m_IsDragging = true;
m_DragConfirmed = false;
m_LastTouchPos = touchPos;
m_TouchStartPos = touchPos;
m_TouchStartOffset = m_ScrollOffset;
m_ScrollVelocity = 0.0f;
m_IsSmoothScrolling = false;
m_VelSampleIdx = 0;
m_VelSampleCount = 0;
}
void ScrollComponent::HandlePointerMove(float x, float y)
{
if (!m_IsDragging) return;
// If another component claimed the gesture, stop tracking
if (InputDispatch::IsGestureClaimed() && !InputDispatch::IsGestureClaimedBy(this))
{
m_IsDragging = false;
return;
}
float touchPos = static_cast<float>((direction == ScrollDirection::Vertical) ? y : x);
// Check drag threshold before confirming scroll gesture
if (!m_DragConfirmed)
{
float dist = DekiMath::Abs(((touchPos) - (m_TouchStartPos)));
if (dist < m_DragThreshold)
return;
m_DragConfirmed = true;
InputDispatch::ClaimGesture(this);
if (m_ClipObj)
CancelChildInput(m_ClipObj);
}
float delta = ((touchPos) - (m_LastTouchPos));
float scrollDelta = reverse_drag ? ((0.0f) - (delta)) : delta;
m_ScrollOffset = ((m_ScrollOffset) + (scrollDelta));
// Store velocity sample in ring buffer
m_VelSamples[m_VelSampleIdx % kVelocitySamples] = scrollDelta;
m_VelSampleIdx++;
if (m_VelSampleCount < kVelocitySamples) m_VelSampleCount++;
m_LastTouchPos = touchPos;
// Always clamp — never allow scrolling past limits
ClampScrollOffset();
if (GetOwner())
SyncChildObjects(GetOwner());
}
void ScrollComponent::HandlePointerUp(float x, float y)
{
(void)x; (void)y;
if (!m_IsDragging) return;
m_IsDragging = false;
m_IsSmoothScrolling = false;
if (!m_DragConfirmed)
{
// Was a tap — don't compute momentum, let children handle click
return;
}
InputDispatch::ReleaseGesture();
m_DragConfirmed = false;
// Compute velocity from ring buffer average (sum in float then back to float).
if (m_VelSampleCount > 0)
{
double sum = 0.0;
int32_t count = std::min(m_VelSampleCount, static_cast<int32_t>(kVelocitySamples));
for (int32_t i = 0; i < count; i++)
sum += (m_VelSamples[i]);
m_ScrollVelocity = static_cast<float>(sum / count);
}
// Kill velocity if already at a bound and velocity would push further
float maxScroll = GetMaxScrollOffset();
if ((m_ScrollOffset <= 0.0f && m_ScrollVelocity < 0.0f) ||
(m_ScrollOffset >= maxScroll && m_ScrollVelocity > 0.0f))
{
m_ScrollVelocity = 0.0f;
}
// Velocity-very-low threshold: ~2 px/frame at ppm=16
static const float kStopThreshold = static_cast<float>(0.125f);
if (DekiMath::Abs(m_ScrollVelocity) < kStopThreshold)
{
m_ScrollVelocity = 0.0f;
}
}
// ============================================================================
// Lifecycle
// ============================================================================
void ScrollComponent::Start()
{
if (GetOwner())
SyncChildObjects(GetOwner());
InputCollider* collider = input_collider.Get();
if (!collider)
{
DEKI_LOG_WARNING("ScrollComponent: No InputCollider referenced on '%s'",
GetOwner()->GetName().c_str());
return;
}
// Don't consume input — let children (buttons, nested scrolls) also receive events.
// Scroll will claim the gesture via InputDispatch when drag threshold is exceeded.
collider->consume_input = false;
collider->on_pointer_down.push_back([this](float x, float y) {
HandlePointerDown(x, y);
});
collider->on_pointer_move.push_back([this](float x, float y) {
HandlePointerMove(x, y);
});
collider->on_pointer_up.push_back([this](float x, float y) {
HandlePointerUp(x, y);
});
}
bool ScrollComponent::NeedsRuntimeUpdate() const
{
return true;
}
void ScrollComponent::RuntimeUpdate(float deltaTime)
{
Update(deltaTime);
}
void ScrollComponent::OnPropertyChanged(const char* propertyName)
{
static const char* layoutProps[] = {
"item_spacing", "padding_top", "padding_bottom",
"padding_left", "padding_right", "direction", "mode"
};
for (const char* prop : layoutProps)
{
if (std::strcmp(propertyName, prop) == 0)
{
if (DekiObject* owner = GetOwner())
{
SyncChildObjects(owner);
}
return;
}
}
}
void ScrollComponent::CancelChildInput(DekiObject* obj)
{
for (DekiObject* child : obj->GetChildren())
{
for (DekiComponent* comp : child->GetComponents())
{
if (comp->getType() == InputCollider::StaticType ||
comp->getBaseType() == InputCollider::StaticType)
{
static_cast<InputCollider*>(comp)->CancelInput();
break;
}
}
CancelChildInput(child);
}
}