-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFastScrollView.cs
More file actions
187 lines (158 loc) · 6.48 KB
/
FastScrollView.cs
File metadata and controls
187 lines (158 loc) · 6.48 KB
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
182
183
184
185
186
187
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace Oops.EditorUtils
{
public class FastScrollView<TElement, TReturn>
{
public delegate float GetHeight(int index, TElement element);
public delegate TReturn DrawElementReturn(Rect rect, int index, TElement element);
private readonly DrawElementReturn draw;
private readonly GetHeight getHeight;
private readonly float elementSpacing;
private readonly List<ElementLayout> elementLayout = new List<ElementLayout>();
private readonly Dictionary<TElement, TReturn> results = new Dictionary<TElement, TReturn>();
private List<TElement> elements;
private Vector2 simpleScrollPos;
public IReadOnlyDictionary<TElement, TReturn> Results => results;
public FastScrollView(DrawElementReturn draw, GetHeight getHeight, float elementSpacing = 2f) : this(new List<TElement>(), draw, getHeight, elementSpacing)
{
}
public FastScrollView(List<TElement> elements, DrawElementReturn draw, GetHeight getHeight, float elementSpacing = 2f)
{
this.elements = elements ?? throw new ArgumentException(nameof(elements));
this.draw = draw;
this.getHeight = getHeight;
this.elementSpacing = elementSpacing;
}
public FastScrollView<TElement, TReturn> UpdateData(List<TElement> newElements)
{
this.elements = newElements ?? throw new ArgumentException(nameof(newElements));
this.elementLayout.Clear();
this.elementLayout.TrimExcess();
this.results.Clear();
return this;
}
public void LayoutDraw()
{
var rect = EditorGUILayout.GetControlRect(
false,
EditorGUIUtility.singleLineHeight,
GUILayout.ExpandHeight(true), GUILayout.MinHeight(EditorGUIUtility.singleLineHeight)
);
Draw(rect);
}
public void Draw(Rect rect)
{
results.Clear();
var totalHeight = 0f;
elementLayout.Clear();
for (var index = 0; index < elements.Count; index++)
{
var element = elements[index];
var elementStart = totalHeight;
var elementHeight = getHeight(index, element);
var elementEnd = elementStart + elementHeight;
elementLayout.Add(new ElementLayout(elementStart, elementEnd));
totalHeight += elementHeight;
if (index < elements.Count - 1)
{
totalHeight += elementSpacing;
}
}
var viewRect = new Rect(rect);
viewRect.width -= 16f;
viewRect.height = totalHeight;
using (var scrollScope = new GUI.ScrollViewScope(rect, simpleScrollPos, viewRect))
{
simpleScrollPos = scrollScope.scrollPosition;
FindStarting(simpleScrollPos.y, out var startIndex, out var startY);
var elementRect = new Rect(viewRect)
{
y = startY + viewRect.y,
};
int drawn = 0;
if (elements.Count > 0)
{
int j = startIndex;
while (j < elements.Count && (elementLayout[j].ElementStart - simpleScrollPos.y <= rect.height))
{
var element = elements[j];
elementRect.height = elementLayout[j].ElementHeight;
results[element] = DrawElement(elementRect, j, elements[j]);
elementRect.y += elementRect.height;
elementRect.y += elementSpacing;
j++;
drawn++;
}
}
}
}
private void FindStarting(float scrollY, out int startingIndex, out float startingY)
{
startingIndex = 0;
startingY = 0f;
for (int i = 0; i < elements.Count; i++)
{
var layout = elementLayout[i];
if (layout.ContainsY(scrollY))
{
startingIndex = i;
startingY = layout.ElementStart;
return;
}
if (i + 1 < elements.Count)
{
if (scrollY >= layout.ElementEnd && scrollY < elementLayout[i + 1].ElementStart)
{
startingIndex = i;
startingY = layout.ElementStart;
return;
}
}
}
}
private TReturn DrawElement(Rect elementRect, int index, TElement element)
{
try
{
return draw.Invoke(elementRect, index, element);
}
catch (Exception)
{
Debug.LogError($"Error drawing element {element} at index {index}");
throw;
}
}
private struct ElementLayout
{
public readonly float ElementStart;
public readonly float ElementEnd;
public float ElementHeight => ElementEnd - ElementStart;
public ElementLayout(float elementStart, float elementEnd)
{
ElementStart = elementStart;
ElementEnd = elementEnd;
}
public bool ContainsY(float y)
{
return y >= ElementStart && y < ElementEnd;
}
}
}
public class FastScrollView<TElement> : FastScrollView<TElement, bool>
{
public delegate void DrawSingleElement(Rect rect, int index, TElement element);
public FastScrollView(DrawSingleElement draw, GetHeight getHeight, float elementSpacing = 2) : base(Wrap(draw), getHeight, elementSpacing) { }
public FastScrollView(List<TElement> elements, DrawSingleElement draw, GetHeight getHeight, float elementSpacing = 2) : base(elements, Wrap(draw), getHeight, elementSpacing) { }
private static DrawElementReturn Wrap(DrawSingleElement drawSingleElement)
{
return (rect, index, element) =>
{
drawSingleElement(rect, index, element);
return true;
};
}
}
}