少年修仙传客户端代码仓库
lcy
2024-12-16 a39c35fc6449430cd02bccb681c4a0a880e46cd9
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using EnhancedUI.EnhancedScroller;
using System;
using UnityEngine.UI;
 
public enum EnhanceLockType
{
    None,
    LockHorizonRight,
    LockHorizonLeft,
    LockVerticalTop,
    LockVerticalBottom,
    KeepHorizon,
    KeepVertical,
    KeepVertical2,
}
public class ScrollerController : MonoBehaviour, IEnhancedScrollerDelegate {
 
    private List<ScrollerData> _data = new List<ScrollerData>();
    public delegate void OnRefreshCellDelegate(ScrollerDataType type, CellView cell);
    public event OnRefreshCellDelegate OnRefreshCell;
 
    public delegate bool OnDynamicCell(ScrollerDataType type,int index,out float height);
    public event OnDynamicCell OnGetDynamicSize;
 
    public event Action OnRefreshCompleteEvent;
 
    public event Action<Vector2> onValueChange;
 
 
    public EnhancedScrollerCellView GetCellView(EnhancedScroller scroller, int dataIndex, int cellIndex)
    {
        CellView cell = GetCellView(scroller, dataIndex);
        if (!cell.gameObject.activeSelf) cell.SetActive(true);
        cell.SetData(_data[dataIndex], _data[dataIndex].m_ScrollerType,this);
        if (_data[dataIndex].OnClick != null) {
            cell.OnClick = _data[dataIndex].OnClick;
        }
        cell.info = _data[dataIndex].info;
        if (OnRefreshCell != null) {
            OnRefreshCell(_data[dataIndex].m_ScrollerType, cell);
        }
        cell.RefreshUI();
        return cell;
    }
 
    private CellView GetCellView(EnhancedScroller scroller, int dataIndex)
    {
        CellView cell = null;
        switch (_data[dataIndex].m_ScrollerType) {
            case ScrollerDataType.Header:
                cell = scroller.GetCellView(m_CellHeaderPrefab) as CellView;
                break;
            case ScrollerDataType.Normal:
                cell = scroller.GetCellView(m_CellNomalPrefab) as CellView;
                break;
            case ScrollerDataType.Tail:
                cell = scroller.GetCellView(m_CellTailPrefab) as CellView;
                break;
            case ScrollerDataType.Extra1:
                cell = scroller.GetCellView(m_CellExtraPrefab1) as CellView;
                break;
            case ScrollerDataType.Extra2:
                cell = scroller.GetCellView(m_CellExtraPrefab2) as CellView;
                break;
            case ScrollerDataType.Extra3:
                cell = scroller.GetCellView(m_CellExtraPrefab3) as CellView;
                break;
        }
        return cell;
    }
 
    public float GetCellViewSize(EnhancedScroller scroller, int dataIndex)
    {
        if (OnGetDynamicSize != null) {
            float height = 0;
            if (OnGetDynamicSize(_data[dataIndex].m_ScrollerType, _data[dataIndex].index,out height)) {
                return height;
            }
        }
        switch (_data[dataIndex].m_ScrollerType) {
            case ScrollerDataType.Header:
                return m_CellHeaderPrefab.height;
            case ScrollerDataType.Normal:
                return m_CellNomalPrefab.height;
            case ScrollerDataType.Tail:
                return m_CellTailPrefab.height;
            case ScrollerDataType.Extra1:
                return m_CellExtraPrefab1.height;
            case ScrollerDataType.Extra2:
                return m_CellExtraPrefab2.height;
            case ScrollerDataType.Extra3:
                return m_CellExtraPrefab3.height;
        }
        return 80;
    }
 
    public void OnRefreshCellActive(CellView cell)
    {
        if (OnRefreshCell != null) {
            OnRefreshCell(cell.type, cell);
        }
    }
 
    public int GetNumberOfCells(EnhancedScroller scroller)
    {
        return _data.Count;
    }
 
    public EnhancedScroller m_Scorller;
    private ScrollRect m_ScrollRect;
    public ScrollRect mScrollRect {
        get {
            if (m_ScrollRect == null)
                m_ScrollRect = m_Scorller.GetComponent<ScrollRect>();
            return m_ScrollRect;
        }
    }
    public CellView m_CellHeaderPrefab;
    public CellView m_CellNomalPrefab;
    public CellView m_CellTailPrefab;
    public CellView m_CellExtraPrefab1;
    public CellView m_CellExtraPrefab2;
    public CellView m_CellExtraPrefab3;
    public int maxCellCnt = 0;
    public bool fakeMiddleCenter = false; //是否需要假居中, 居中时当cell个数少 尺寸小于最大尺寸时 不可滑动; 只有header类型时才准确
    int jiaMiddleWithMaxSize = 0; //假居中的最大尺寸,自动设为预制体里的初始尺寸
 
    public bool horizontal = false;
    public bool vertical = false;
    private bool inited = false;
    private int cacheJump = -1;
    private void Awake()
    {
        m_Scorller.Delegate = this;
        horizontal = mScrollRect.horizontal;
        vertical = mScrollRect.vertical;
 
        mScrollRect.onValueChanged.AddListener(OnValueChangle);
 
        inited = m_Scorller.inited;
        m_Scorller.OnFirstLoadAllEvent += OnFirstLoadAllEvent;
        if (m_Scorller.OnCompLoad == null && !m_Scorller.inited)
        {
            m_Scorller.OnCompLoad = OnCompLoad;
        }
        HideDefaultCell();
    }
 
    private void OnValueChangle(Vector2 delta)
    {
        if (onValueChange != null)
        {
            onValueChange(delta);
        }
    }
 
    private void OnFirstLoadAllEvent()
    {
        if (cacheJump != -1)
        {
            JumpIndex(cacheJump);
        }
    }
 
    private void HideDefaultCell()
    {
        HideDefaultCell(m_CellHeaderPrefab);
        HideDefaultCell(m_CellNomalPrefab);
        HideDefaultCell(m_CellTailPrefab);
        HideDefaultCell(m_CellExtraPrefab1);
        HideDefaultCell(m_CellExtraPrefab2);
        HideDefaultCell(m_CellExtraPrefab3);
    }
 
    private void HideDefaultCell(CellView _cell)
    {
        if (_cell != null)
        {
            _cell.SetActive(false);
        }
    }
 
    List<CellView> list = new List<CellView>();
 
    private void OnCompLoad()
    {
        inited = true;
        if (_data.Count > 0)
        {
            Restart();
        }
    }
 
    public void Refresh()
    {
        inited = m_Scorller.inited;
        if (m_Scorller.OnCompLoad == null && !m_Scorller.inited)
        {
            m_Scorller.OnCompLoad = OnCompLoad;
        }
        _data.Clear();
 
        if (!inited)
        {
            return;
        }
 
        //假居中恢复尺寸
        if (fakeMiddleCenter && jiaMiddleWithMaxSize != 0)
        {
            var rect = (transform as RectTransform);
            if (horizontal)
            {
                rect.sizeDelta = rect.sizeDelta.SetX(jiaMiddleWithMaxSize);
            }
            else if (vertical)
            {
                rect.sizeDelta = rect.sizeDelta.SetY(jiaMiddleWithMaxSize);
            }
        }
 
 
        if (m_Scorller.LayoutGroup != null)
        {
            m_Scorller.LayoutGroup.spacing = m_Scorller.spacing;
        }
 
        lastScrollPos = horizontal ? mScrollRect.horizontalNormalizedPosition : mScrollRect.verticalNormalizedPosition;
        lastContentSize = m_Scorller._ScrollSize;
 
        mScrollRect.verticalNormalizedPosition = 1;
        mScrollRect.horizontalNormalizedPosition = 0;
 
        lastScrollPos = Mathf.Clamp01(lastScrollPos);
 
    }
 
    public CellView GetActiveCellView(int _index)
    {
        if (m_Scorller == null)
        {
            return null;
        }
        var _cellView = m_Scorller.GetActiveCellView(_index);
        if (_cellView != null)
        {
            return _cellView as CellView;
        }
        return null;
    }
 
    public CellView GetActiveCellView(int _index, ScrollerDataType type)
    {
        if (m_Scorller == null)
        {
            return null;
        }
        var views = m_Scorller.GetActiveCellViews();
        for (int i = 0; i < views.Count; i++)
        {
            var _cellView = views[i] as CellView;
            if (views[i].index == _index && _cellView.type == type)
            {
                return _cellView;
            }
        }
        return null;
    }
 
    public void AddCell(ScrollerDataType type, int index, Action<CellView> action = null, CellInfo? info = null)
    {
        ScrollerData data = null;
        switch (type) {
            case ScrollerDataType.Header:
                data = new ScrollerHeaderData(index, action);
                break;
            case ScrollerDataType.Normal:
                data = new ScrollerNormalData(index, action);
                break;
            case ScrollerDataType.Tail:
                data = new ScrollerTailData(index, action);
                break;
            case ScrollerDataType.Extra1:
            case ScrollerDataType.Extra2:
            case ScrollerDataType.Extra3:
                data = new ScrollerExtraData(index, type, action);
                break;
        }
        if (data != null) {
            data.info = info;
            _data.Add(data);
        }
    }
 
    public void AddCell(ScrollerDataType type, int index, CellInfo info)
    {
        ScrollerData data = null;
        switch (type) {
            case ScrollerDataType.Header:
                data = new ScrollerHeaderData(index);
                break;
            case ScrollerDataType.Normal:
                data = new ScrollerNormalData(index);
                break;
            case ScrollerDataType.Tail:
                data = new ScrollerTailData(index);
                break;
            case ScrollerDataType.Extra1:
            case ScrollerDataType.Extra2:
            case ScrollerDataType.Extra3:
                data = new ScrollerExtraData(index, type);
                break;
        }
        if (data != null) {
            data.info = info;
            _data.Add(data);
        }
    }
 
    public void OnRefreshAdd()
    {
        Restart();
    }
 
    public void Restart()
    {
        if (!inited) return;
 
 
        m_Scorller.ReloadData();
 
        if (fakeMiddleCenter && _data.Count <= maxCellCnt)
        {
            //只有设置header时,进行假居中处理才能达到效果
            if (jiaMiddleWithMaxSize == 0)
            { 
                jiaMiddleWithMaxSize = (int)m_Scorller.ScrollRectSize;
            }
 
 
            var rect = (transform as RectTransform);
            if (horizontal)
            {
                
                rect.sizeDelta = rect.sizeDelta.SetX(Math.Min(jiaMiddleWithMaxSize, (m_CellHeaderPrefab.height + m_Scorller.spacing) * _data.Count - m_Scorller.spacing));
            }
            else if (vertical)
            {
                rect.sizeDelta = rect.sizeDelta.SetY(Math.Min(jiaMiddleWithMaxSize, (m_CellHeaderPrefab.height + m_Scorller.spacing) * _data.Count - m_Scorller.spacing));
            }
            if (rect.sizeDelta.x < jiaMiddleWithMaxSize)
            {
                mScrollRect.horizontal = false;
                mScrollRect.vertical = false;
            }
            else
            {
                mScrollRect.horizontal = horizontal;
                mScrollRect.vertical = vertical;
            }
        }
        else if (maxCellCnt != 0 && _data.Count <= maxCellCnt)
        {
            mScrollRect.horizontal = false;
            mScrollRect.vertical = false;
        }
        else
        {
            mScrollRect.horizontal = horizontal;
            mScrollRect.vertical = vertical;
        }
 
        ResetScrollPos();
 
        lastDataCnt = _data.Count;
    }
 
    public void JumpIndex(int index)
    {
        if (!inited)
        {
            cacheJump = index;
            return;
        }
        if ((index < 0 || index >= _data.Count) && !m_Scorller.Loop)
        {
            return;
        }
        var _size = m_Scorller.GetScrollPositionForDataIndex(index, EnhancedScroller.CellViewPositionEnum.Before);
        var _contentSize = vertical ? m_Scorller.scrollRect.content.rect.size.y : m_Scorller.scrollRect.content.rect.size.x;
        if (_contentSize - _size < m_Scorller.ScrollRectSize)
        {
            _size = _contentSize - m_Scorller.ScrollRectSize;
        }
        m_Scorller.ScrollPosition = _size;
    }
 
    public void JumpIndex(int index,ref float offset)
    {
        if (!inited)
        {
            cacheJump = index;
            return;
        }
        if ((index < 0 || index >= _data.Count) && !m_Scorller.Loop)
        {
            return;
        }
        var _size = m_Scorller.GetScrollPositionForDataIndex(index, EnhancedScroller.CellViewPositionEnum.Before);
        var _contentSize = vertical ? m_Scorller.scrollRect.content.rect.size.y : m_Scorller.scrollRect.content.rect.size.x;
        offset = 0f;
        if (_contentSize - _size < m_Scorller.ScrollRectSize)
        {
            offset = _size - (_contentSize - m_Scorller.ScrollRectSize);
            _size = _size - offset;
        }
        m_Scorller.ScrollPosition = _size;
    }
 
    public float GetCellSize(int _dataIndex)
    {
        if (!inited)
        {
            return 0;
        }
        return m_Scorller.GetCellSize(_dataIndex);
    }
 
    public void JumpIndex(float _delta, float _time, EnhancedScroller.TweenType _tweenType)
    {
        if (!inited)
        {
            return;
        }
        m_Scorller.Tween(_tweenType, _time, m_Scorller.ScrollPosition + _delta);
    }
 
    public float GetPreScrollSize(int index)
    {
        float _size = 0;
        for (int i = 0; i < _data.Count; i++) {
            _size += GetCellViewSize(m_Scorller, i);
            if (i >= index) break;
        }
        return _size;
    }
 
    public void RefreshSingleCellView(int _index)
    {
        CellView _cell;
        if (IsDisplay(_index, out _cell))
        {
            _cell.RefreshCellView();
        }
    }
 
    public bool IsDisplay(int index, out CellView _cell)
    {
        _cell = null;
        if (m_Scorller == null)
        {
            return false;
        }
        var _base = m_Scorller.GetActiveCellView(index);
        if (_base != null)
        {
            _cell = _base as CellView;
            return true;
        }
        return false;
    }
 
    public bool IsDisplay(int index)
    {
        if (m_Scorller == null)
        {
            return false;
        }
        return m_Scorller.GetActiveCellView(index) != null;
    }
 
    public bool OutOfStartView(int index)
    {
        float value=m_Scorller.GetScrollPositionForCellViewIndex(index, EnhancedScroller.CellViewPositionEnum.Before);
        if (value < m_Scorller.ScrollPosition) {
            return true;
        }
        return false;
    }
 
    private void OnDestroy()
    {
        if (m_Scorller != null)
        {
            m_Scorller.OnFirstLoadAllEvent -= OnFirstLoadAllEvent;
        }
    }
 
    public void OnRebuildComplete()
    {
        if (OnRefreshCompleteEvent != null)
        {
            OnRefreshCompleteEvent();
        }
    }
 
    #region 滚动定位
 
    private EnhanceLockType m_lockType = EnhanceLockType.None;
 
    public EnhanceLockType lockType {
        get { return m_lockType; }
        set {
            m_lockType = value;
        }
    }
 
    private float lastContentSize = 0;
 
    private int lastDataCnt = 0;
 
    private float lastScrollPos = 0;
 
    public void ResetScrollPos()
    {
        if (_data.Count == 0 || !inited)
        {
            return;
        }
        switch (lockType) {
            case EnhanceLockType.LockHorizonRight: {
                    mScrollRect.horizontalNormalizedPosition = 1;
                }
                break;
            case EnhanceLockType.LockHorizonLeft: {
                    mScrollRect.horizontalNormalizedPosition = 0;
                }
                break;
            case EnhanceLockType.LockVerticalTop: {
                    mScrollRect.verticalNormalizedPosition = 1;
                }
                break;
            case EnhanceLockType.LockVerticalBottom: {
                    mScrollRect.verticalNormalizedPosition = 0;
                    m_Scorller.ScrollPosition = m_Scorller._ScrollSize;
                }
                break;
            case EnhanceLockType.KeepHorizon: {
                    mScrollRect.horizontalNormalizedPosition = Mathf.Clamp01(lastContentSize * lastScrollPos / m_Scorller._ScrollSize);
                }
                break;
            case EnhanceLockType.KeepVertical2: {
                    if(lastContentSize == m_Scorller._ScrollSize) {
                        mScrollRect.verticalNormalizedPosition = lastScrollPos;
                        m_Scorller.ScrollPosition = (1 - mScrollRect.verticalNormalizedPosition) * m_Scorller._ScrollSize;
                        return;
                    }
                    if (lastScrollPos == 1) {
                        mScrollRect.verticalNormalizedPosition = 1;
                    }
                    mScrollRect.verticalNormalizedPosition = Mathf.Clamp01(lastContentSize * lastScrollPos / m_Scorller._ScrollSize);
                    m_Scorller.ScrollPosition = (1 - mScrollRect.verticalNormalizedPosition) * m_Scorller._ScrollSize;
                }
                break;
            case EnhanceLockType.KeepVertical:
                {
                    if (lastContentSize == m_Scorller._ScrollSize)
                    {
                        mScrollRect.verticalNormalizedPosition = lastScrollPos;
                        m_Scorller.ScrollPosition = (1 - mScrollRect.verticalNormalizedPosition) * m_Scorller._ScrollSize;
                        return;
                    }
                    if (lastScrollPos == 1)
                    {
                        mScrollRect.verticalNormalizedPosition = 1;
                    }
                    mScrollRect.verticalNormalizedPosition = Mathf.Clamp01(1 - lastContentSize * (1 - lastScrollPos) / m_Scorller._ScrollSize);
                    m_Scorller.ScrollPosition = (1 - mScrollRect.verticalNormalizedPosition) * m_Scorller._ScrollSize;
                }
                break;
        }
    }
 
    #endregion
}