using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using XLua;
/// <summary>
/// 高性能ScrollView
/// </summary>
[RequireComponent(typeof(ScrollRect))]
public class GridScroller : MonoBehaviour
{
public enum Movement
{
Horizontal,
Vertical
}
public delegate void MoveToEndCallback();
public delegate void OnScrollViewItemChanged(Transform trans, int itemIdx);
private List<Transform> _cacheList = new List<Transform>();
private Vector2 _cellSize = Vector3.zero;
private int _col;
private HashSet<int> _currentItemIdxSet = new HashSet<int>();
private Dictionary<int, RectTransform> _currentVisibleItemIdxRTMap = new Dictionary<int, RectTransform>();
private RectTransform _grid;
private bool _hasChanged;
private Stack<RectTransform> _itemPool = new Stack<RectTransform>();
private GameObject _itemPrefab;
private Movement _moveType;
private HashSet<int> _nextVisibleItemSet = new HashSet<int>();
private OnScrollViewItemChanged _onNewItemBecomeVisibleCallback;
private int _row;
private ScrollRect _scroller;
private Rect _scrollerRect;
private Vector2 _spacing = Vector3.zero;
private int _totalItemCount;
private int _visibleItemCount;
private float cachedPos;
public MoveToEndCallback moveToEndCallBack;
public bool moveToEndLock;
public Vector2 ItemSize
{
get
{
return _spacing + _cellSize;
}
}
public Movement MoveType
{
get
{
return _moveType;
}
set
{
_moveType = value;
}
}
public RectTransform Grid
{
set
{
_grid = value;
}
}
public GameObject ItemPrefab
{
set
{
_itemPrefab = value;
}
}
private Vector2 CalcPosByIndex(int index)
{
if (_moveType == Movement.Horizontal)
{
return new Vector2(ItemSize.x * (index / _row), -ItemSize.y * (index % _row));
}
return new Vector2(ItemSize.x * (index % _col), -ItemSize.y * (index / _col));
}
private void CheckMoveToEnd()
{
if ((moveToEndCallBack != null) && !moveToEndLock)
{
bool flag = false;
if (_moveType == Movement.Horizontal)
{
if (_grid.rect.width - _scrollerRect.width > 0 && Math.Abs(_grid.anchoredPosition.x) + _scrollerRect.width > _grid.rect.width)
{
flag = true;
}
}
else if (_grid.rect.height - _scrollerRect.height > 0 && Math.Abs(_grid.anchoredPosition.y) + _scrollerRect.height > _grid.rect.height)
{
flag = true;
}
if (flag)
{
moveToEndCallBack();
}
}
}
public void Clear()
{
var enumerator = _currentVisibleItemIdxRTMap.GetEnumerator();
while (enumerator.MoveNext())
{
KeyValuePair<int, RectTransform> current = enumerator.Current;
current.Value.gameObject.Set