-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathScrollGridFlow.cs
127 lines (100 loc) · 4.28 KB
/
ScrollGridFlow.cs
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
namespace UniMob.UI.Widgets
{
using System;
using UnityEngine;
public class ScrollGridFlow : MultiChildLayoutWidget
{
public static WidgetViewReference DefaultView =
WidgetViewReference.Resource("UniMob.ScrollGridFlow");
public MainAxisAlignment MainAxisAlignment { get; set; } = MainAxisAlignment.Start;
public CrossAxisAlignment CrossAxisAlignment { get; set; } = CrossAxisAlignment.Start;
public RectPadding Padding { get; set; }
public Vector2 Spacing { get; set; }
public int MaxCrossAxisCount { get; set; } = int.MaxValue;
public float MaxCrossAxisExtent { get; set; } = int.MaxValue;
public GridLayoutDelegate LayoutDelegate { get; set; }
public bool UseMask { get; set; } = true;
public Key Sticky { get; set; } = null;
public StickyModes StickyMode { get; set; } = StickyModes.Top;
public Widget BackgroundContent { get; set; } = null;
public WidgetViewReference View { get; set; } = DefaultView;
public ScrollController ScrollController { get; set; }
public override State CreateState() => new ScrollGridFlowState();
}
public class ScrollGridFlowState : MultiChildLayoutState<ScrollGridFlow>, IScrollGridFlowState
{
private static readonly ScrollEasing CircEaseInOutEasing = (t, d) =>
(t /= d / 2) < 1 ? -0.5f * (Mathf.Sqrt(1 - t * t) - 1) : 0.5f * (Mathf.Sqrt(1 - (t -= 2) * t) + 1);
private readonly StateHolder _backgroundContent;
private ScrollGridFlowView _gridView;
[Atom] public override WidgetViewReference View => Widget.View;
[Atom] public WidgetSize InnerSize => CalculateInnerSize();
public MainAxisAlignment MainAxisAlignment => Widget.MainAxisAlignment;
public CrossAxisAlignment CrossAxisAlignment => Widget.CrossAxisAlignment;
public bool UseMask => Widget.UseMask;
public Key Sticky => Widget.Sticky;
public StickyModes StickyMode => Widget.StickyMode;
public IState BackgroundContent => _backgroundContent.Value;
public GridLayoutSettings LayoutSettings => new GridLayoutSettings
{
children = Children,
gridPadding = Widget.Padding,
spacing = Widget.Spacing,
maxLineWidth = Widget.MaxCrossAxisExtent,
maxLineChildNum = Widget.MaxCrossAxisCount,
};
public GridLayoutDelegate LayoutDelegate => Widget.LayoutDelegate ?? GridLayoutUtility.DefaultLayoutDelegate;
[Atom] public ScrollController ScrollController { get; private set; }
public ScrollGridFlowState()
{
_backgroundContent = CreateChild(_ => Widget.BackgroundContent ?? new Empty());
}
public override void InitState()
{
base.InitState();
ScrollController = Widget.ScrollController ?? new ScrollController(StateLifetime);
}
public override WidgetSize CalculateSize()
{
return WidgetSize.Stretched;
}
private WidgetSize CalculateInnerSize()
{
return GridLayoutUtility.CalculateSize(LayoutSettings, LayoutDelegate);
}
public override void DidViewMount(IView view)
{
base.DidViewMount(view);
_gridView = view as ScrollGridFlowView;
}
public override void DidViewUnmount(IView view)
{
base.DidViewUnmount(view);
_gridView = null;
}
public override void DidUpdateWidget(ScrollGridFlow oldWidget)
{
base.DidUpdateWidget(oldWidget);
if (Widget.ScrollController != null && Widget.ScrollController != ScrollController)
{
ScrollController = Widget.ScrollController;
}
}
public bool ScrollTo(Key key, float duration, float offset = 0, ScrollEasing easing = null)
{
if (_gridView == null)
{
return false;
}
return _gridView.ScrollTo(key, duration, offset, easing ?? CircEaseInOutEasing);
}
}
[Flags]
public enum StickyModes
{
Top = 1 << 0,
Bottom = 1 << 1,
TopAndBottom = Top | Bottom,
}
public delegate float ScrollEasing(float t, float duration);
}