c1_unity/Assets/Scripts/Component/UI/BFScrollRectOld/VerticalScrollRectOld.cs
2023-04-03 11:04:31 +08:00

131 lines
4.4 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
namespace BF
{
[DisallowMultipleComponent]
public class VerticalScrollRectOld : ScrollRectBaseOld
{
protected override float GetSize(RectTransform item)
{
float size = ContentSpacing;
if (gridLayout != null)
{
size += gridLayout.cellSize.y;
}
else
{
size += LayoutUtility.GetPreferredHeight(item);
}
return size;
}
protected override float GetDimension(Vector2 vector)
{
return vector.y;
}
protected override Vector2 GetVector(float value)
{
return new Vector2(0, value);
}
protected override void Awake()
{
base.Awake();
directionSign = -1;
GridLayoutGroup layout = content.GetComponent<GridLayoutGroup>();
if (layout != null && layout.constraint != GridLayoutGroup.Constraint.FixedColumnCount)
{
Debug.LogError("[VerticalScrollRect] unsupported GridLayoutGroup constraint");
}
}
protected override bool UpdateItems(Bounds viewBounds, Bounds contentBounds)
{
bool changed = false;
if (viewBounds.min.y < contentBounds.min.y)
{
float size = NewItemAtEnd(), totalSize = size;
while (size > 0 && viewBounds.min.y < contentBounds.min.y - totalSize)
{
size = NewItemAtEnd();
totalSize += size;
}
if (totalSize > 0)
changed = true;
}
else if (viewBounds.min.y > contentBounds.min.y + endThreshold)
{
float size = DeleteItemAtEnd(), totalSize = size;
while (size > 0 && viewBounds.min.y > contentBounds.min.y + endThreshold + totalSize)
{
size = DeleteItemAtEnd();
totalSize += size;
}
if (totalSize > 0)
changed = true;
}
if (viewBounds.max.y > contentBounds.max.y)
{
float size = NewItemAtStart(), totalSize = size;
while (size > 0 && viewBounds.max.y > contentBounds.max.y + totalSize)
{
size = NewItemAtStart();
totalSize += size;
}
if (totalSize > 0)
changed = true;
}
else if (viewBounds.max.y < contentBounds.max.y - startThreshold)
{
float size = DeleteItemAtStart(), totalSize = size;
while (size > 0 && viewBounds.max.y < contentBounds.max.y - startThreshold - totalSize)
{
size = DeleteItemAtStart();
totalSize += size;
}
if (totalSize > 0)
changed = true;
}
return changed;
}
public override void RefreshRealShowIndex()
{
base.RefreshRealShowIndex();
Bounds viewBounds = new Bounds(viewport.rect.center, viewport.rect.size);
for (int i = content.childCount - 1; i >= 0; i--)
{
var gameObject = content.GetChild(i).gameObject;
if (!gameObject.activeSelf)
{
continue;
}
Bounds bounds = GetCellBounds(i);
if (bounds.min.y > viewBounds.max.y)
{
CellObj cell = GetCell(gameObject);
int index = cell.index;
if (index < curTopIndex)
curTopIndex = index;
}
if (bounds.max.y < viewBounds.min.y)
{
CellObj cell = GetCell(gameObject);
int cellIndex = cell.index;
if (cellIndex > curBottomIndex)
curBottomIndex = cellIndex;
}
}
}
public override bool IsNotFull()
{
return content.rect.height < viewport.rect.height || (cellEndIndex - cellStartIndex) % ContentConstraintCount > 0;
}
}
}