-
Notifications
You must be signed in to change notification settings - Fork 2
/
RecyclableScrollRect.cs
181 lines (155 loc) · 5.9 KB
/
RecyclableScrollRect.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
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
//
// Copyright (C) 2021 LimeVista
// Author: LimeVista(https://github.com/LimeVista/RecyclableScrollRectX)
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the The MIT License (MIT).
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See The MIT License (MIT) for more details.
//
// You should have received a copy of The MIT License (MIT)
// along with this library.
//
using System;
using UnityEngine;
namespace RecyclableScrollRectX
{
/// <summary>
/// 本项目的终极目标,可循环系统列表
/// </summary>
public class RecyclableScrollRect : AbsRecyclableScrollRect, AbsRecyclableScrollRect.IWithDataSource
{
/// <summary>
/// 布局模式
/// </summary>
public RecyclableScrollRectLayoutMode LayoutMode
{
get => layoutMode;
set => layoutMode = value;
}
/// <summary>
/// 获取数据源
/// </summary>
public RecyclingSystem.IDataSource DataSource
{
get => _dataSource ?? dataSource;
set
{
if ((value == _dataSource && _dataSource != null) ||
(value is RecyclableScriptableDataSource v && v == dataSource))
{
return;
}
if (value is RecyclableScriptableDataSource rv)
{
_dataSource = null;
dataSource = rv;
}
else
{
dataSource = null;
_dataSource = value;
}
DataSetChanged();
}
}
public override void DataSetChanged()
{
if (RecyclingSystem == null) return;
RecyclingSystem.OnDataSetChanged(this);
}
public override void ScrollToNormalizedPosition(float normalized)
{
if (RecyclingSystem == null) return;
RecyclingSystem.OnScrollToNormalizedPosition(this, normalized);
}
public override void RefreshVisibleCells()
{
RecyclingSystemDelegate @delegate;
if (RecyclingSystem == null || (@delegate = RecyclingSystem.InternalDelegate) == null) return;
foreach (var item in @delegate.UsedPool)
{
@delegate.DataSource.OnBindCell(item.Cell.Cell, item.Index);
}
}
protected override bool AllowHorizontal
{
get
{
if (RecyclingSystem != null)
{
return RecyclingSystem.LayoutManager.Direction == Direction.Horizontal;
}
switch (layoutMode)
{
case RecyclableScrollRectLayoutMode.Horizontal: return true;
case RecyclableScrollRectLayoutMode.Vertical: return false;
case RecyclableScrollRectLayoutMode.GridVertical: return false;
case RecyclableScrollRectLayoutMode.GridHorizontal: return true;
default: throw new ArgumentOutOfRangeException();
}
}
}
protected override bool AllowVertical
{
get
{
if (RecyclingSystem != null)
{
return RecyclingSystem.LayoutManager.Direction == Direction.Vertical;
}
switch (layoutMode)
{
case RecyclableScrollRectLayoutMode.Horizontal: return false;
case RecyclableScrollRectLayoutMode.Vertical: return true;
case RecyclableScrollRectLayoutMode.GridVertical: return true;
case RecyclableScrollRectLayoutMode.GridHorizontal: return false;
default: throw new ArgumentOutOfRangeException();
}
}
}
protected override RecyclingSystem CreateRecyclingSystem()
{
LayoutManager manager;
switch (layoutMode)
{
case RecyclableScrollRectLayoutMode.Horizontal:
manager = new HorizontalLayoutManager(horizontalSpace, verticalSpace);
break;
case RecyclableScrollRectLayoutMode.Vertical:
manager = new VerticalLayoutManager(horizontalSpace, verticalSpace);
break;
case RecyclableScrollRectLayoutMode.GridVertical:
manager = new GridVerticalLayoutManager(
Math.Max(1, orthogonalCount),
horizontalSpace,
verticalSpace
);
break;
case RecyclableScrollRectLayoutMode.GridHorizontal:
manager = new GridHorizontalLayoutManager(
Math.Max(1, orthogonalCount),
horizontalSpace,
verticalSpace
);
break;
default:
throw new ArgumentOutOfRangeException();
}
return new DefaultRecyclingSystem(manager, DataSource, viewport, content);
}
#region 编辑器属性
[SerializeField] private RecyclableScrollRectLayoutMode layoutMode;
[SerializeField] private RecyclableScriptableDataSource dataSource;
[SerializeField] [HideInInspector] private int orthogonalCount;
[SerializeField] [HideInInspector] private float horizontalSpace;
[SerializeField] [HideInInspector] private float verticalSpace;
#endregion
#region 内部属性
private RecyclingSystem.IDataSource _dataSource;
#endregion
}
}