/
ScrollView.cs
79 lines (62 loc) · 2 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
/*
* 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.Example06
{
public class ScrollView : FancyScrollView<ItemData, Context>
{
[SerializeField] Scroller scroller = default;
[SerializeField] GameObject cellPrefab = default;
Action<int, MovementDirection> onSelectionChanged;
protected override GameObject CellPrefab => cellPrefab;
protected override void Initialize()
{
base.Initialize();
Context.OnCellClicked = SelectCell;
scroller.OnValueChanged(UpdatePosition);
scroller.OnSelectionChanged(UpdateSelection);
}
void UpdateSelection(int index)
{
if (Context.SelectedIndex == index)
{
return;
}
var direction = scroller.GetMovementDirection(Context.SelectedIndex, index);
Context.SelectedIndex = index;
Refresh();
onSelectionChanged?.Invoke(index, direction);
}
public void UpdateData(IList<ItemData> items)
{
UpdateContents(items);
scroller.SetTotalCount(items.Count);
}
public void OnSelectionChanged(Action<int, MovementDirection> 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;
}
scroller.ScrollTo(index, 0.35f, Ease.OutCubic);
}
}
}