Elin Decompiled Documentation EA 23.102 Nightly
Loading...
Searching...
No Matches
DynamicScrollView.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using UnityEngine;
4using UnityEngine.EventSystems;
5using UnityEngine.UI;
6
7namespace Mosframe;
8
9[RequireComponent(typeof(ScrollRect))]
10public abstract class DynamicScrollView : UIBehaviour
11{
12 public enum Direction
13 {
16 }
17
18 public int totalItemCount;
19
20 public RectTransform itemPrototype;
21
23
25
26 public LinkedList<DSVRow> containers = new LinkedList<DSVRow>();
27
28 protected float prevAnchoredPosition;
29
30 protected int nextInsertItemNo;
31
32 [NonSerialized]
33 public int prevTotalItemCount = -1;
34
35 protected ScrollRect scrollRect;
36
37 protected RectTransform viewportRect;
38
39 public RectTransform contentRect;
40
41 public abstract float contentAnchoredPosition { get; set; }
42
43 public abstract float contentSize { get; }
44
45 public abstract float viewportSize { get; }
46
47 protected abstract float itemSize { get; }
48
49 public void scrollToLastPos()
50 {
52 refresh();
53 }
54
55 public void scrollByItemIndex(int itemIndex)
56 {
57 float num = contentSize / (float)totalItemCount * (float)itemIndex;
58 contentAnchoredPosition = 0f - num;
59 Update();
60 refresh();
61 }
62
63 public void refresh()
64 {
65 int num = 0;
67 {
68 num = (int)((0f - contentAnchoredPosition) / itemSize);
69 }
70 foreach (DSVRow container in containers)
71 {
72 if (!(container == null) && !(container.Rect() == null))
73 {
74 float num2 = itemSize * (float)num;
75 container.Rect().anchoredPosition = ((direction == Direction.Vertical) ? new Vector2(0f, 0f - num2) : new Vector2(num2, 0f));
76 updateItem(num, container);
77 num++;
78 }
79 }
80 nextInsertItemNo = num - containers.Count;
82 }
83
84 protected override void Awake()
85 {
86 base.Awake();
87 scrollRect = GetComponent<ScrollRect>();
88 viewportRect = scrollRect.viewport;
89 contentRect = scrollRect.content;
90 contentRect.anchoredPosition = Vector2.zero;
92 }
93
94 public void Build()
95 {
96 itemPrototype.gameObject.SetActive(value: false);
97 int num = (int)(viewportSize / itemSize) + 3;
98 for (int i = containers.Count; i < num; i++)
99 {
100 RectTransform rectTransform = UnityEngine.Object.Instantiate(itemPrototype);
101 rectTransform.SetParent(contentRect, worldPositionStays: false);
102 rectTransform.name = i.ToString();
103 rectTransform.anchoredPosition = ((direction == Direction.Vertical) ? new Vector2(0f, (0f - itemSize) * (float)i) : new Vector2(itemSize * (float)i, 0f));
104 DSVRow component = rectTransform.GetComponent<DSVRow>();
105 containers.AddLast(component);
106 rectTransform.gameObject.SetActive(value: true);
107 updateItem(i, component);
108 }
110 }
111
112 public void Update()
113 {
114 Build();
116 {
118 bool flag = false;
120 {
121 flag = true;
122 }
124 if (flag)
125 {
127 }
128 refresh();
129 }
131 {
133 LinkedListNode<DSVRow> first = containers.First;
134 if (first == null)
135 {
136 break;
137 }
138 DSVRow value = first.Value;
139 containers.RemoveFirst();
140 containers.AddLast(value);
141 float num = itemSize * (float)(containers.Count + nextInsertItemNo);
142 value.Rect().anchoredPosition = ((direction == Direction.Vertical) ? new Vector2(0f, 0f - num) : new Vector2(num, 0f));
143 updateItem(containers.Count + nextInsertItemNo, value);
145 }
147 {
149 LinkedListNode<DSVRow> last = containers.Last;
150 if (last != null)
151 {
152 DSVRow value2 = last.Value;
153 containers.RemoveLast();
154 containers.AddFirst(value2);
156 float num2 = itemSize * (float)nextInsertItemNo;
157 value2.Rect().anchoredPosition = ((direction == Direction.Vertical) ? new Vector2(0f, 0f - num2) : new Vector2(num2, 0f));
159 continue;
160 }
161 break;
162 }
163 }
164
165 public void OnResize()
166 {
167 Build();
168 base.transform.parent.parent.parent.RebuildLayout();
169 refresh();
170 Update();
171 GetComponent<UIScrollView>().Refresh();
172 }
173
174 public void resizeContent()
175 {
176 Vector2 size = contentRect.getSize();
177 if (direction == Direction.Vertical)
178 {
179 size.y = itemSize * (float)totalItemCount;
180 }
181 else
182 {
183 size.x = itemSize * (float)totalItemCount;
184 }
185 contentRect.setSize(size);
186 }
187
188 private void updateItem(int index, DSVRow itemObj)
189 {
190 if (index < 0 || index >= totalItemCount)
191 {
192 itemObj.SetActive(enable: false);
193 return;
194 }
195 itemObj.SetActive(enable: true);
196 dynamicList.UpdateRow(itemObj, index);
197 }
198
199 [ContextMenu("Initialize")]
200 public virtual void init()
201 {
202 clear();
203 GetComponent<RectTransform>().setFullSize();
204 ScrollRect component = GetComponent<ScrollRect>();
205 component.horizontal = direction == Direction.Horizontal;
206 component.vertical = direction == Direction.Vertical;
207 component.scrollSensitivity = 15f;
208 RectTransform component2 = new GameObject("Viewport", typeof(RectTransform), typeof(Mask), typeof(Image)).GetComponent<RectTransform>();
209 component2.SetParent(component.transform, worldPositionStays: false);
210 component2.setFullSize();
211 component2.offsetMin = new Vector2(10f, 10f);
212 component2.offsetMax = new Vector2(-10f, -10f);
213 component2.GetComponent<Image>().type = Image.Type.Sliced;
214 component2.GetComponent<Mask>().showMaskGraphic = false;
215 component.viewport = component2;
216 RectTransform component3 = new GameObject("Content", typeof(RectTransform)).GetComponent<RectTransform>();
217 component3.SetParent(component2, worldPositionStays: false);
218 if (direction == Direction.Horizontal)
219 {
220 component3.setSizeFromLeft(1f);
221 }
222 else
223 {
224 component3.setSizeFromTop(1f);
225 }
226 Vector2 size = component3.getSize();
227 component3.setSize(size - size * 0.06f);
228 component.content = component3;
229 RectTransform component4 = new GameObject((direction == Direction.Horizontal) ? "Scrollbar Horizontal" : "Scrollbar Vertical", typeof(Scrollbar), typeof(Image)).GetComponent<RectTransform>();
230 component4.SetParent(component2, worldPositionStays: false);
231 if (direction == Direction.Horizontal)
232 {
233 component4.setSizeFromBottom(0.05f);
234 }
235 else
236 {
237 component4.setSizeFromRight(0.05f);
238 }
239 component4.SetParent(component.transform, worldPositionStays: true);
240 Scrollbar component5 = component4.GetComponent<Scrollbar>();
241 component5.direction = ((direction != Direction.Horizontal) ? Scrollbar.Direction.BottomToTop : Scrollbar.Direction.LeftToRight);
242 if (direction == Direction.Horizontal)
243 {
244 component.horizontalScrollbar = component5;
245 }
246 else
247 {
248 component.verticalScrollbar = component5;
249 }
250 Image component6 = component4.GetComponent<Image>();
251 component6.color = new Color(0.1f, 0.1f, 0.1f, 0.5f);
252 component6.type = Image.Type.Sliced;
253 RectTransform component7 = new GameObject("Sliding Area", typeof(RectTransform)).GetComponent<RectTransform>();
254 component7.SetParent(component4, worldPositionStays: false);
255 component7.setFullSize();
256 RectTransform component8 = new GameObject("Handle", typeof(Image)).GetComponent<RectTransform>();
257 component8.SetParent(component7, worldPositionStays: false);
258 component8.setFullSize();
259 Image component9 = component8.GetComponent<Image>();
260 component9.color = new Color(0.5f, 0.5f, 1f, 0.5f);
261 component9.type = Image.Type.Sliced;
262 component5.handleRect = component8;
263 if (component.GetComponent<ScrollbarHandleSize>() == null)
264 {
265 ScrollbarHandleSize scrollbarHandleSize = component.gameObject.AddComponent<ScrollbarHandleSize>();
266 scrollbarHandleSize.maxSize = 1f;
267 scrollbarHandleSize.minSize = 0.1f;
268 }
269 base.gameObject.setLayer(base.transform.parent.gameObject.layer);
270 }
271
272 protected virtual void clear()
273 {
274 while (base.transform.childCount > 0)
275 {
276 UnityEngine.Object.DestroyImmediate(base.transform.GetChild(0).gameObject);
277 }
278 }
279}
Definition: DSVRow.cs:7
abstract float contentAnchoredPosition
LinkedList< DSVRow > containers
void scrollByItemIndex(int itemIndex)
void updateItem(int index, DSVRow itemObj)
void UpdateRow(DSVRow dsvRow, int index)