/*
* 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
{
///
/// Documentation text normalized.
/// Documentation text normalized.
/// Documentation text normalized.
/// Documentation text normalized.
///
/// Documentation text normalized.
/// Documentation text normalized.
public abstract class FancyGridView : FancyScrollRect
where TContext : class, IFancyGridViewContext, new()
{
///
/// Documentation text normalized.
///
protected abstract class DefaultCellGroup : FancyCellGroup { }
///
/// Documentation text normalized.
///
[SerializeField] protected float startAxisSpacing = 0f;
///
/// Documentation text normalized.
///
[SerializeField] protected int startAxisCellCount = 4;
///
/// Documentation text normalized.
///
[SerializeField] protected Vector2 cellSize = new Vector2(100f, 100f);
///
/// Documentation text normalized.
///
///
/// Documentation text normalized.
/// Documentation text normalized.
///
protected sealed override GameObject CellPrefab => cellGroupTemplate;
///
protected override float CellSize => Scroller.ScrollDirection == ScrollDirection.Horizontal
? cellSize.x
: cellSize.y;
///
/// Documentation text normalized.
///
public int DataCount { get; private set; }
GameObject cellGroupTemplate;
///
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();
}
///
/// Documentation text normalized.
/// Documentation text normalized.
///
///
///
/// {
/// class CellGroup : DefaultCellGroup { }
///
/// [SerializeField] Cell cellPrefab = default;
///
/// protected override void SetupCellTemplate() => Setup(cellPrefab);
/// }
/// ]]>
///
protected abstract void SetupCellTemplate();
///
/// Documentation text normalized.
///
/// Documentation text normalized.
/// Documentation text normalized.
protected virtual void Setup(FancyCell cellTemplate)
where TGroup : FancyCell
{
Context.CellTemplate = cellTemplate.gameObject;
cellGroupTemplate = new GameObject("Group").AddComponent().gameObject;
cellGroupTemplate.transform.SetParent(cellContainer, false);
cellGroupTemplate.SetActive(false);
}
///
/// Documentation text normalized.
///
/// Documentation text normalized.
public virtual void UpdateContents(IList 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);
}
///
/// Documentation text normalized.
///
/// Documentation text normalized.
/// Documentation text normalized.
protected override void JumpTo(int itemIndex, float alignment = 0.5f)
{
var groupIndex = itemIndex / startAxisCellCount;
base.JumpTo(groupIndex, alignment);
}
///
/// Documentation text normalized.
///
/// 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);
}
///
/// Documentation text normalized.
///
/// 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);
}
}
///
/// Documentation text normalized.
/// Documentation text normalized.
///
/// Documentation text normalized.
///
public abstract class FancyGridView : FancyGridView { }
}