com.alicizax.unity.ui.exten.../Runtime/RecyclerView/Layout/PageLayoutManager.cs

109 lines
3.4 KiB
C#
Raw Normal View History

using System;
2025-03-12 20:59:12 +08:00
using UnityEngine;
namespace AlicizaX.UI
2025-03-12 20:59:12 +08:00
{
2025-12-26 14:22:46 +08:00
[Serializable]
2025-03-12 20:59:12 +08:00
public class PageLayoutManager : LinearLayoutManager
{
2025-12-26 14:22:46 +08:00
[SerializeField] private float minScale = 0.9f;
2025-03-12 20:59:12 +08:00
2025-05-30 10:32:08 +08:00
public PageLayoutManager()
2025-03-12 20:59:12 +08:00
{
}
public override Vector2 CalculateContentSize()
{
int itemCount = adapter != null ? adapter.GetItemCount() : 0;
if (itemCount <= 0)
{
lineHeight = 0f;
return direction == Direction.Vertical
? new Vector2(contentSize.x, padding.y * 2)
: new Vector2(padding.x * 2, contentSize.y);
}
2025-03-12 20:59:12 +08:00
Vector2 size = viewProvider.CalculateViewSize(0);
lineHeight = direction == Direction.Vertical ? size.y : size.x;
float position;
if (direction == Direction.Vertical)
{
position = itemCount * (lineHeight + spacing.y) - spacing.y;
2025-03-12 20:59:12 +08:00
position += viewportSize.y - lineHeight;
return new Vector2(contentSize.x, position + padding.y * 2);
}
2025-05-30 10:32:08 +08:00
position = itemCount * (lineHeight + spacing.x) - spacing.x;
2025-03-12 20:59:12 +08:00
position += viewportSize.x - lineHeight;
return new Vector2(position + padding.x * 2, contentSize.y);
}
public override Vector2 CalculatePosition(int index)
{
float position;
if (direction == Direction.Vertical)
{
position = index * (lineHeight + spacing.y) - ScrollPosition;
return new Vector2(0, position + padding.y);
}
2025-05-30 10:32:08 +08:00
2025-03-12 20:59:12 +08:00
position = index * (lineHeight + spacing.x) - ScrollPosition;
return new Vector2(position + padding.x, 0);
}
public override Vector2 CalculateContentOffset()
{
return Vector2.zero;
}
public override Vector2 CalculateViewportOffset()
{
return Vector2.zero;
}
protected override float GetOffset()
{
if (lineHeight <= 0f)
{
return 0f;
}
2025-03-12 20:59:12 +08:00
float offset = direction == Direction.Vertical ? viewportSize.y - lineHeight : viewportSize.x - lineHeight;
return offset / 2;
}
public override int PositionToIndex(float position)
{
if (adapter == null || adapter.GetItemCount() <= 0)
{
return 0;
}
2025-03-12 20:59:12 +08:00
float len = direction == Direction.Vertical ? lineHeight + spacing.y : lineHeight + spacing.x;
if (len <= 0f)
{
return 0;
}
2025-03-12 20:59:12 +08:00
float pos = IndexToPosition(recyclerView.CurrentIndex);
int index = position > pos ? Mathf.RoundToInt(position / len + 0.25f) : Mathf.RoundToInt(position / len - 0.25f);
return index;
}
public override void DoItemAnimation()
{
var viewHolders = viewProvider.ViewHolders;
2025-03-12 20:59:12 +08:00
for (int i = 0; i < viewHolders.Count; i++)
{
2025-05-30 10:32:08 +08:00
float viewPos = direction == Direction.Vertical ? -viewHolders[i].RectTransform.anchoredPosition.y : viewHolders[i].RectTransform.anchoredPosition.x;
2025-03-12 20:59:12 +08:00
float scale = 1 - Mathf.Min(Mathf.Abs(viewPos) * 0.0006f, 1f);
scale = Mathf.Max(scale, minScale);
viewHolders[i].RectTransform.localScale = Vector3.one * scale;
}
}
}
}