129 lines
3.5 KiB
C#
129 lines
3.5 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.EventSystems;
|
|
|
|
namespace BF
|
|
{
|
|
[DisallowMultipleComponent]
|
|
[RequireComponent(typeof(ScrollRect))]
|
|
public abstract class BFScrollRectBase : MonoBehaviour, IBeginDragHandler, IEndDragHandler
|
|
{
|
|
public bool isDrag = false;
|
|
bool unityScrollRectInited = false;
|
|
ScrollRect unityScrollRect;
|
|
protected ScrollRect UnityScrollRect
|
|
{
|
|
get
|
|
{
|
|
if (!unityScrollRectInited)
|
|
{
|
|
unityScrollRect = GetComponent<ScrollRect>();
|
|
if (!ReferenceEquals(unityScrollRect, null))
|
|
{
|
|
unityScrollRectInited = true;
|
|
}
|
|
}
|
|
return unityScrollRect;
|
|
}
|
|
}
|
|
|
|
bool contentTransInited = false;
|
|
public RectTransform contentTrans;
|
|
public RectTransform ContentTrans
|
|
{
|
|
get
|
|
{
|
|
if (!contentTransInited)
|
|
{
|
|
contentTrans = UnityScrollRect.content;
|
|
if (!ReferenceEquals(contentTrans, null))
|
|
{
|
|
contentTransInited = true;
|
|
}
|
|
}
|
|
return contentTrans;
|
|
}
|
|
}
|
|
|
|
bool viewportTransInited = false;
|
|
RectTransform viewportTrans;
|
|
protected RectTransform ViewPortTrans
|
|
{
|
|
get
|
|
{
|
|
if (!viewportTransInited)
|
|
{
|
|
viewportTrans = UnityScrollRect.viewport;
|
|
if (!ReferenceEquals(viewportTrans, null))
|
|
{
|
|
viewportTransInited = true;
|
|
}
|
|
}
|
|
return viewportTrans;
|
|
}
|
|
}
|
|
|
|
protected Action<int, int> RefreshAction;
|
|
protected Action<float, float> OnAnchoredPositionAction;
|
|
protected Action<GameObject> OnInstantiateCell;
|
|
protected Action<bool, int> Selected;
|
|
protected int selectedIndex;
|
|
|
|
public void SetRefreshAction(Action<int, int> action)
|
|
{
|
|
RefreshAction = action;
|
|
}
|
|
|
|
public void SetOnInstantiateCellAction(Action<GameObject> action)
|
|
{
|
|
OnInstantiateCell = action;
|
|
}
|
|
|
|
public void SetSelectedAction(Action<bool, int> action)
|
|
{
|
|
Selected = action;
|
|
}
|
|
|
|
public int GetSelectedIndex()
|
|
{
|
|
return selectedIndex;
|
|
}
|
|
|
|
public void SetAnchoredPositionChange(Action<float, float> action)
|
|
{
|
|
OnAnchoredPositionAction = action;
|
|
}
|
|
|
|
public abstract void RefillCells(int totalCount, int targetIndex);
|
|
|
|
public abstract void SetTotalCount(int totalCount);
|
|
|
|
public abstract void RefreshAll();
|
|
|
|
public abstract void ClearCells();
|
|
|
|
public abstract void SetSelected(int index);
|
|
public abstract void AnchoredPositionChange();
|
|
|
|
protected abstract void TryFullFill();
|
|
|
|
public virtual void MoveToIndex(int index) { }
|
|
|
|
public virtual void RemoveCell(int index) { }
|
|
|
|
public void OnBeginDrag(PointerEventData eventData)
|
|
{
|
|
isDrag = true;
|
|
}
|
|
public void OnEndDrag(PointerEventData eventData)
|
|
{
|
|
isDrag = false;
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
TryFullFill();
|
|
}
|
|
}
|
|
} |