186 lines
6.2 KiB
C#
186 lines
6.2 KiB
C#
/*
|
|
* FancyScrollView (https://github.com/setchi/FancyScrollView)
|
|
* Copyright (c) 2020 setchi
|
|
* Licensed under MIT (https://github.com/setchi/FancyScrollView/blob/master/LICENSE)
|
|
*/
|
|
|
|
using System;
|
|
using System.Linq;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using EasingCore;
|
|
|
|
namespace FancyScrollView
|
|
{
|
|
/// <summary>
|
|
/// Documentation text normalized.
|
|
/// Documentation text normalized.
|
|
/// Documentation text normalized.
|
|
/// Documentation text normalized.
|
|
/// </summary>
|
|
/// Documentation text normalized.
|
|
/// Documentation text normalized.
|
|
public abstract class FancyGridView<TItemData, TContext> : FancyScrollRect<TItemData[], TContext>
|
|
where TContext : class, IFancyGridViewContext, new()
|
|
{
|
|
/// <summary>
|
|
/// Documentation text normalized.
|
|
/// </summary>
|
|
protected abstract class DefaultCellGroup : FancyCellGroup<TItemData, TContext> { }
|
|
|
|
/// <summary>
|
|
/// Documentation text normalized.
|
|
/// </summary>
|
|
[SerializeField] protected float startAxisSpacing = 0f;
|
|
|
|
/// <summary>
|
|
/// Documentation text normalized.
|
|
/// </summary>
|
|
[SerializeField] protected int startAxisCellCount = 4;
|
|
|
|
/// <summary>
|
|
/// Documentation text normalized.
|
|
/// </summary>
|
|
[SerializeField] protected Vector2 cellSize = new Vector2(100f, 100f);
|
|
|
|
/// <summary>
|
|
/// Documentation text normalized.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Documentation text normalized.
|
|
/// Documentation text normalized.
|
|
/// </remarks>
|
|
protected sealed override GameObject CellPrefab => cellGroupTemplate;
|
|
|
|
/// <inheritdoc/>
|
|
protected override float CellSize => Scroller.ScrollDirection == ScrollDirection.Horizontal
|
|
? cellSize.x
|
|
: cellSize.y;
|
|
|
|
/// <summary>
|
|
/// Documentation text normalized.
|
|
/// </summary>
|
|
public int DataCount { get; private set; }
|
|
|
|
GameObject cellGroupTemplate;
|
|
|
|
/// <inheritdoc/>
|
|
protected override void Initialize()
|
|
{
|
|
base.Initialize();
|
|
|
|
Debug.Assert(startAxisCellCount > 0);
|
|
|
|
Context.ScrollDirection = Scroller.ScrollDirection;
|
|
Context.GetGroupCount = () => startAxisCellCount;
|
|
Context.GetStartAxisSpacing = () => startAxisSpacing;
|
|
Context.GetCellSize = () => Scroller.ScrollDirection == ScrollDirection.Horizontal
|
|
? cellSize.y
|
|
: cellSize.x;
|
|
|
|
SetupCellTemplate();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Documentation text normalized.
|
|
/// Documentation text normalized.
|
|
/// </summary>
|
|
/// <example>
|
|
/// <code><![CDATA[
|
|
/// using UnityEngine;
|
|
/// using FancyScrollView;
|
|
///
|
|
/// public class MyGridView : FancyGridView<ItemData, Context>
|
|
/// {
|
|
/// class CellGroup : DefaultCellGroup { }
|
|
///
|
|
/// [SerializeField] Cell cellPrefab = default;
|
|
///
|
|
/// protected override void SetupCellTemplate() => Setup<CellGroup>(cellPrefab);
|
|
/// }
|
|
/// ]]></code>
|
|
/// </example>
|
|
protected abstract void SetupCellTemplate();
|
|
|
|
/// <summary>
|
|
/// Documentation text normalized.
|
|
/// </summary>
|
|
/// Documentation text normalized.
|
|
/// Documentation text normalized.
|
|
protected virtual void Setup<TGroup>(FancyCell<TItemData, TContext> cellTemplate)
|
|
where TGroup : FancyCell<TItemData[], TContext>
|
|
{
|
|
Context.CellTemplate = cellTemplate.gameObject;
|
|
|
|
cellGroupTemplate = new GameObject("Group").AddComponent<TGroup>().gameObject;
|
|
cellGroupTemplate.transform.SetParent(cellContainer, false);
|
|
cellGroupTemplate.SetActive(false);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Documentation text normalized.
|
|
/// </summary>
|
|
/// Documentation text normalized.
|
|
public virtual void UpdateContents(IList<TItemData> items)
|
|
{
|
|
DataCount = items.Count;
|
|
|
|
var itemGroups = items
|
|
.Select((item, index) => (item, index))
|
|
.GroupBy(
|
|
x => x.index / startAxisCellCount,
|
|
x => x.item)
|
|
.Select(group => group.ToArray())
|
|
.ToArray();
|
|
|
|
UpdateContents(itemGroups);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Documentation text normalized.
|
|
/// </summary>
|
|
/// Documentation text normalized.
|
|
/// Documentation text normalized.
|
|
protected override void JumpTo(int itemIndex, float alignment = 0.5f)
|
|
{
|
|
var groupIndex = itemIndex / startAxisCellCount;
|
|
base.JumpTo(groupIndex, alignment);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Documentation text normalized.
|
|
/// </summary>
|
|
/// Documentation text normalized.
|
|
/// Documentation text normalized.
|
|
/// Documentation text normalized.
|
|
/// Documentation text normalized.
|
|
protected override void ScrollTo(int itemIndex, float duration, float alignment = 0.5f, Action onComplete = null)
|
|
{
|
|
var groupIndex = itemIndex / startAxisCellCount;
|
|
base.ScrollTo(groupIndex, duration, alignment, onComplete);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Documentation text normalized.
|
|
/// </summary>
|
|
/// Documentation text normalized.
|
|
/// Documentation text normalized.
|
|
/// Documentation text normalized.
|
|
/// Documentation text normalized.
|
|
/// Documentation text normalized.
|
|
protected override void ScrollTo(int itemIndex, float duration, Ease easing, float alignment = 0.5f, Action onComplete = null)
|
|
{
|
|
var groupIndex = itemIndex / startAxisCellCount;
|
|
base.ScrollTo(groupIndex, duration, easing, alignment, onComplete);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Documentation text normalized.
|
|
/// Documentation text normalized.
|
|
/// </summary>
|
|
/// Documentation text normalized.
|
|
/// <seealso cref="FancyGridView{TItemData, TContext}"/>
|
|
public abstract class FancyGridView<TItemData> : FancyGridView<TItemData, FancyGridViewContext> { }
|
|
}
|