FancyScrollRectCell.cs 2.7 KB

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