/
ScrollView.cs
101 lines (80 loc) · 2.62 KB
/
ScrollView.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
/*
* FancyScrollView (https://github.com/setchi/FancyScrollView)
* Copyright (c) 2020 setchi
* Licensed under MIT (https://github.com/setchi/FancyScrollView/blob/master/LICENSE)
*/
using System;
using System.Collections.Generic;
using UnityEngine;
using EasingCore;
namespace FancyScrollView.Example05
{
public class ScrollView : FancyScrollView<ItemData, Context>
{
[SerializeField] Scroller scroller = default;
[SerializeField] GameObject cellPrefab = default;
Action<int> onSelectionChanged;
protected override GameObject CellPrefab => cellPrefab;
public int CellInstanceCount => Mathf.CeilToInt(1f / Mathf.Max(cellInterval, 1e-3f));
protected override void Initialize()
{
base.Initialize();
Context.OnCellClicked = SelectCell;
scroller.OnValueChanged(UpdatePosition);
scroller.OnSelectionChanged(UpdateSelection);
}
public void UpdateSelection(int index)
{
if (Context.SelectedIndex == index)
{
return;
}
Context.SelectedIndex = index;
Refresh();
onSelectionChanged?.Invoke(index);
}
public void UpdateData(IList<ItemData> items)
{
UpdateContents(items);
scroller.SetTotalCount(items.Count);
}
public void OnSelectionChanged(Action<int> callback)
{
onSelectionChanged = callback;
}
public void SelectNextCell()
{
SelectCell(Context.SelectedIndex + 1);
}
public void SelectPrevCell()
{
SelectCell(Context.SelectedIndex - 1);
}
public void SelectCell(int index)
{
if (index < 0 || index >= ItemsSource.Count || index == Context.SelectedIndex)
{
return;
}
UpdateSelection(index);
ScrollTo(index, 0.35f, Ease.OutCubic);
}
public void ScrollTo(float position, float duration, Ease easing, Action onComplete = null)
{
scroller.ScrollTo(position, duration, easing, onComplete);
}
public void JumpTo(int index)
{
scroller.JumpTo(index);
}
public Vector4[] GetCellState()
{
Context.UpdateCellState?.Invoke();
return Context.CellState;
}
public void SetCellState(int cellIndex, int dataIndex, float x, float y, float selectAnimation)
{
Context.SetCellState(cellIndex, dataIndex, x, y, selectAnimation);
}
}
}