FancyScrollRectCell.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /// Credit setchi (https://github.com/setchi)
  2. /// Sourced from - https://github.com/setchi/FancyScrollView
  3. namespace UnityEngine.UI.Extensions
  4. {
  5. /// <summary>
  6. /// <see cref="FancyScrollRect{TItemData, TContext}"/> のセルを実装するための抽象基底クラス.
  7. /// <see cref="FancyCell{TItemData, TContext}.Context"/> が不要な場合は
  8. /// 代わりに <see cref="FancyScrollRectCell{TItemData}"/> を使用します.
  9. /// </summary>
  10. /// <typeparam name="TItemData">アイテムのデータ型.</typeparam>
  11. /// <typeparam name="TContext"><see cref="FancyCell{TItemData, TContext}.Context"/> の型.</typeparam>
  12. public abstract class FancyScrollRectCell<TItemData, TContext> : FancyCell<TItemData, TContext>
  13. where TContext : class, IFancyScrollRectContext, new()
  14. {
  15. /// <inheritdoc/>
  16. public override void UpdatePosition(float position)
  17. {
  18. var (scrollSize, reuseMargin) = Context.CalculateScrollSize();
  19. var normalizedPosition = (Mathf.Lerp(0f, scrollSize, position) - reuseMargin) / (scrollSize - reuseMargin * 2f);
  20. var start = 0.5f * scrollSize;
  21. var end = -start;
  22. UpdatePosition(normalizedPosition, Mathf.Lerp(start, end, position));
  23. }
  24. /// <summary>
  25. /// このセルの位置を更新します.
  26. /// </summary>
  27. /// <param name="normalizedPosition">
  28. /// ビューポートの範囲で正規化されたスクロール位置.
  29. /// <see cref="FancyScrollRect{TItemData, TContext}.reuseCellMarginCount"/> の値に基づいて
  30. /// <c>0.0</c> ~ <c>1.0</c> の範囲を超えた値が渡されることがあります.
  31. /// </param>
  32. /// <param name="localPosition">ローカル位置.</param>
  33. protected virtual void UpdatePosition(float normalizedPosition, float localPosition)
  34. {
  35. transform.localPosition = Context.ScrollDirection == ScrollDirection.Horizontal
  36. ? new Vector2(-localPosition, 0)
  37. : new Vector2(0, localPosition);
  38. }
  39. }
  40. /// <summary>
  41. /// <see cref="FancyScrollRect{TItemData}"/> のセルを実装するための抽象基底クラス.
  42. /// </summary>
  43. /// <typeparam name="TItemData">アイテムのデータ型.</typeparam>
  44. /// <seealso cref="FancyScrollRectCell{TItemData, TContext}"/>
  45. public abstract class FancyScrollRectCell<TItemData> : FancyScrollRectCell<TItemData, FancyScrollRectContext>
  46. {
  47. /// <inheritdoc/>
  48. public sealed override void SetContext(FancyScrollRectContext context) => base.SetContext(context);
  49. }
  50. }