/// Credit setchi (https://github.com/setchi)
/// Sourced from - https://github.com/setchi/FancyScrollView
using System.Linq;
namespace UnityEngine.UI.Extensions
{
///
/// の行を実装するための抽象基底クラス.
///
/// アイテムのデータ型.
/// の型.
public abstract class FancyGridViewRow : FancyScrollRectCell
where TContext : class, IFancyScrollRectContext, IFancyGridViewContext, new()
{
///
/// この行で表示するセルの配列.
///
protected virtual FancyScrollViewCell[] Cells { get; private set; }
///
/// この行で表示するセルの配列をインスタンス化します.
///
/// この行で表示するセルの配列.
protected virtual FancyScrollViewCell[] InstantiateCells()
{
return Enumerable.Range(0, Context.GetColumnCount())
.Select(_ => Instantiate(Context.CellTemplate, transform))
.Select(x => x.GetComponent>())
.ToArray();
}
///
public override void SetupContext(TContext context)
{
base.SetupContext(context);
Cells = InstantiateCells();
Debug.Assert(Cells.Length == Context.GetColumnCount());
for (var i = 0; i < Cells.Length; i++)
{
Cells[i].SetupContext(context);
}
}
///
public override void UpdateContent(TItemData[] rowContents)
{
for (var i = 0; i < Cells.Length; i++)
{
Cells[i].Index = i + Index * Context.GetColumnCount();
Cells[i].SetVisible(i < rowContents.Length);
if (Cells[i].IsVisible)
{
Cells[i].UpdateContent(rowContents[i]);
}
}
}
///
public override void UpdatePosition(float position)
{
base.UpdatePosition(position);
for (var i = 0; i < Cells.Length; i++)
{
Cells[i].UpdatePosition(position);
}
}
///
protected override void UpdatePosition(float position, float viewportPosition)
{
transform.localPosition = Context.ScrollDirection == ScrollDirection.Horizontal
? new Vector2(viewportPosition, transform.localPosition.y)
: new Vector2(transform.localPosition.x, viewportPosition);
}
}
}