Skip to content

Commit 5ef668b

Browse files
committed
Reworked Table and related components.
1 parent c008d0a commit 5ef668b

14 files changed

Lines changed: 319 additions & 686 deletions

File tree

src/reactive-bs-sdk/Reactive.BeatSaber/Components/Table/Scrollbar.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ protected override GameObject Construct() {
154154
sEnabled = RememberDerived(
155155
x => !x.Item2.Value || (x.Item1.Value?.CanScroll ?? true),
156156
(
157-
_scrollContextState.Where(x => x!.UpdateType is ScrollCompleted or Measurements),
157+
_scrollContextState.Where(x => x!.UpdateType is ScrollFinished or Measurements),
158158
_hideIfNothingToScroll
159159
)
160160
),

src/reactive-bs-sdk/Reactive.BeatSaber/Components/Table/Table.cs

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/reactive-sdk/Reactive.Components/Components/ListView/ListCell.cs

Lines changed: 0 additions & 58 deletions
This file was deleted.

src/reactive-sdk/Reactive.Components/Components/ScrollArea/ScrollArea.cs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ public ScrollContext ScrollContext {
2222
_scrollContext?.ValueChangedEvent -= HandleContextUpdated;
2323
_scrollContext = value;
2424

25-
if (_contentTransform != null) {
25+
var didInitialUpdate = DoInitialUpdate();
26+
if (!didInitialUpdate && _scrollContent != null) {
2627
RefreshMeasurements();
2728
}
2829

@@ -41,7 +42,7 @@ public IReactiveComponent ScrollContent {
4142
_scrollContent.Use(_viewport);
4243
_contentTransform = _scrollContent.ContentTransform;
4344

44-
ReloadContent();
45+
DoInitialUpdate();
4546
}
4647
}
4748

@@ -53,12 +54,22 @@ public ScrollOrientation ScrollOrientation {
5354
}
5455
} = ScrollOrientation.Vertical;
5556

56-
public float JoystickScrollSize { get; set; } = 10f;
57-
public Action? OnScrollWithJoystickFinished { get; set; }
57+
public float LineSize { get; set; }
58+
public Func<ScrollContext, float>? FinalizeScroll { get; set; }
5859

5960
private IReactiveComponent? _scrollContent;
6061
private RectTransform? _contentTransform;
6162
private ScrollContext? _scrollContext;
63+
private bool _needsInitialUpdate = true;
64+
65+
private bool DoInitialUpdate() {
66+
if (_needsInitialUpdate && _scrollContext != null && _scrollContent != null) {
67+
ReloadContent();
68+
_needsInitialUpdate = false;
69+
}
70+
71+
return !_needsInitialUpdate;
72+
}
6273

6374
#endregion
6475

@@ -70,12 +81,12 @@ public ScrollOrientation ScrollOrientation {
7081
protected override void OnUpdate() {
7182
// ReSharper disable CompareOfFloatsByEqualityOperator
7283
if (_lastScrollDeltaTime != -1f && _lastScrollDeltaTime != Time.deltaTime) {
73-
OnScrollWithJoystickFinished?.Invoke();
84+
FinalizeScrollPos();
7485
_lastScrollDeltaTime = -1f;
7586
}
7687

77-
if (_contentTransform != null) {
78-
var contentSize = Translate(_contentTransform.rect);
88+
if (_scrollContent != null) {
89+
var contentSize = Translate(_contentTransform!.rect);
7990

8091
if (_prevContentSize != contentSize) {
8192
RefreshMeasurements();
@@ -86,6 +97,12 @@ protected override void OnUpdate() {
8697
UpdateContentPos(false);
8798
}
8899

100+
private void FinalizeScrollPos() {
101+
if (FinalizeScroll != null) {
102+
_destinationPos = FinalizeScroll.Invoke(ScrollContext);
103+
}
104+
}
105+
89106
#endregion
90107

91108
#region Content
@@ -120,7 +137,10 @@ private void SetDestinationPos(float pos, bool immediately) {
120137
_posSet = false;
121138

122139
if (immediately) {
140+
FinalizeScrollPos();
123141
UpdateContentPos(true);
142+
143+
_lastScrollDeltaTime = -1f;
124144
}
125145
}
126146

@@ -159,7 +179,7 @@ private void RefreshMeasurements() {
159179
var contentSize = Translate(_contentTransform!.rect);
160180
var viewSize = Translate(_viewport.rect);
161181

162-
ScrollContext.ControllerSetMeasurements(contentSize, viewSize, 0f);
182+
ScrollContext.ControllerSetMeasurements(contentSize, viewSize, LineSize);
163183
}
164184

165185
private void RefreshContentSizeIfNeeded() {
@@ -222,10 +242,8 @@ protected override void OnRectDimensionsChanged() {
222242
#region Callbacks
223243

224244
private void HandlePointerScroll(PointerEventsHandler handler, PointerEventData eventData) {
225-
var mul = JoystickScrollSize * -0.1f;
226-
227245
var neg = ScrollOrientation is ScrollOrientation.Vertical ? -1 : 1;
228-
var destinationPos = _destinationPos + eventData.scrollDelta.y * mul * neg;
246+
var destinationPos = _destinationPos + eventData.scrollDelta.y * LineSize * neg;
229247

230248
_lastScrollDeltaTime = Time.deltaTime;
231249

src/reactive-sdk/Reactive.Components/Components/ScrollArea/ScrollContext.cs

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ namespace Reactive.Components;
77
public enum ScrollUpdateType {
88
None,
99
/// User intent to scroll (e.g. ScrollPos, ScrollItem).
10-
Intent,
10+
Intent,
1111
/// Controller measurements (e.g. ContentSize, ViewSize).
12-
Measurements,
12+
Measurements,
1313
/// Controller scroll (ActualScrollPos).
1414
Scroll,
1515
/// <summary>
1616
/// Controller scroll has just finished.
1717
/// </summary>
18-
ScrollCompleted
18+
ScrollFinished
1919
}
2020

2121
[PublicAPI]
@@ -25,13 +25,6 @@ public class ScrollContext : IState<ScrollContext> {
2525
/// </summary>
2626
public float ScrollPos { get; private set; }
2727

28-
/// <summary>
29-
/// Determines the item that the controller should scroll to.
30-
/// Has higher priority than <see cref="ScrollPos"/>, so if both are specified the first one is ignored.
31-
/// Defaults to <see cref="ScrollPos"/> if the specified object isn't presented in the view.
32-
/// </summary>
33-
public object? ScrollItem { get; private set; }
34-
3528
/// Whether the scroll animation should finish immediately. Set by the user.
3629
public bool Immediately { get; private set; }
3730

@@ -46,7 +39,7 @@ public class ScrollContext : IState<ScrollContext> {
4639
/// Set by a component controlling the view.
4740
/// </summary>
4841
public float LineSize { get; private set; }
49-
42+
5043
/// <summary>
5144
/// The size of the scroll content. Set by a component controlling the view.
5245
/// </summary>
@@ -56,7 +49,7 @@ public class ScrollContext : IState<ScrollContext> {
5649
/// The size of the viewport. Set by a component controlling the view.
5750
/// </summary>
5851
public float ViewSize { get; private set; }
59-
52+
6053
/// <summary>
6154
/// Determines a kind of the last update. Returns None if called outside the ValueChanged cycle.
6255
/// </summary>
@@ -68,7 +61,7 @@ public class ScrollContext : IState<ScrollContext> {
6861
public float NormalizedScrollPos {
6962
get => Mathf.Clamp01(ActualScrollPos / MaxScrollPos);
7063
}
71-
64+
7265
public float NormalizedPageHeight {
7366
get => Mathf.Clamp01(ViewSize / ContentSize);
7467
}
@@ -90,7 +83,7 @@ public bool CanScroll {
9083
// in the future, so moved into a separate property
9184
get => ContentSize > ViewSize;
9285
}
93-
86+
9487
/// <summary>
9588
/// Scrolls to the specified point.
9689
/// </summary>
@@ -99,30 +92,17 @@ public bool CanScroll {
9992
/// <returns>An updated context.</returns>
10093
public void ScrollTo(float pos, bool immediately = false) {
10194
var newPos = Mathf.Clamp(pos, 0f, MaxScrollPos);
102-
95+
10396
if (Mathf.Approximately(newPos, ScrollPos)) {
10497
return;
10598
}
10699

107100
ScrollPos = newPos;
108-
ScrollItem = null;
109101
Immediately = immediately;
110-
111-
NotifyValueChanged(ScrollUpdateType.Intent);
112-
}
113102

114-
/// <summary>
115-
/// Scrolls to the specified item. Defaults
116-
/// </summary>
117-
/// <param name="pos">The position to scroll to.</param>
118-
/// <param name="immediately">Whether to apply the scroll immediately.</param>
119-
/// <returns>An updated context.</returns>
120-
public void ScrollTo(object item, bool immediately = false) {
121-
ScrollItem = item;
122-
Immediately = immediately;
123103
NotifyValueChanged(ScrollUpdateType.Intent);
124104
}
125-
105+
126106
public void ScrollRelative(float offset, bool immediately = false) {
127107
ScrollTo(ScrollPos + offset, immediately);
128108
}
@@ -148,7 +128,7 @@ public void LineDown(bool immediately = false) {
148128
}
149129

150130
public void LineUp(bool immediately = false) {
151-
ScrollRelative(LineSize, immediately);
131+
ScrollRelative(LineSize * -1, immediately);
152132
}
153133

154134
/// <summary>
@@ -171,13 +151,13 @@ public void ControllerSetScrollPos(float pos) {
171151
if (Mathf.Approximately(ActualScrollPos, pos)) {
172152
return;
173153
}
174-
154+
175155
ActualScrollPos = pos;
176156
NotifyValueChanged(ScrollUpdateType.Scroll);
177157
}
178158

179159
public void ControllerNotifyScrollCompleted() {
180-
NotifyValueChanged(ScrollUpdateType.ScrollCompleted);
160+
NotifyValueChanged(ScrollUpdateType.ScrollFinished);
181161
}
182162

183163
#region State Impl
@@ -189,10 +169,10 @@ public void ControllerNotifyScrollCompleted() {
189169

190170
private void NotifyValueChanged(ScrollUpdateType type) {
191171
UpdateType = type;
192-
172+
193173
ValueChangedEvent?.Invoke(this);
194174
StateUpdatedEvent?.Invoke();
195-
175+
196176
UpdateType = ScrollUpdateType.None;
197177
}
198178

0 commit comments

Comments
 (0)