Skip to content

Commit

Permalink
Add default position calculation to UpdatePosition of FancyScrollRect…
Browse files Browse the repository at this point in the history
…Cell
  • Loading branch information
setchi committed Jan 13, 2020
1 parent d1711f5 commit 150df8d
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 17 deletions.
8 changes: 4 additions & 4 deletions Assets/FancyScrollView/Examples/Sources/07_ScrollRect/Cell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public override void UpdateContent(ItemData itemData)
: new Color32(255, 255, 255, 77);
}

protected override void UpdatePosition(float position, float viewportPosition)
protected override void UpdatePosition(float normalizedPosition, float localPosition)
{
var x = Mathf.Sin(position * Mathf.PI * 2) * 65;
var y = viewportPosition;
base.UpdatePosition(normalizedPosition, localPosition);

transform.localPosition = new Vector2(x, y);
var wave = Mathf.Sin(normalizedPosition * Mathf.PI * 2) * 65;
transform.localPosition += Vector3.right * wave;
}
}
}
6 changes: 3 additions & 3 deletions Assets/FancyScrollView/Examples/Sources/08_GridView/Cell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public override void UpdateContent(ItemData itemData)
: new Color32(255, 255, 255, 77);
}

public override void UpdatePosition(float position)
protected override void UpdatePosition(float normalizedPosition, float localPosition)
{
base.UpdatePosition(position);
base.UpdatePosition(normalizedPosition, localPosition);

var wave = Mathf.Sin(position * Mathf.PI * 2) * 65;
var wave = Mathf.Sin(normalizedPosition * Mathf.PI * 2) * 65;
transform.localPosition += Vector3.right * wave;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public abstract class FancyGridViewCell<TItemData, TContext> : FancyScrollRectCe
where TContext : class, IFancyGridViewContext, new()
{
/// <inheritdoc/>
protected override void UpdatePosition(float position, float scrollPosition)
protected override void UpdatePosition(float normalizedPosition, float localPosition)
{
var cellSize = Context.GetCellSize();
var spacing = Context.GetStartAxisSpacing();
Expand All @@ -29,8 +29,8 @@ protected override void UpdatePosition(float position, float scrollPosition)
var positionInGroup = (cellSize + spacing) * (indexInGroup - (groupCount - 1) * 0.5f);

transform.localPosition = Context.ScrollDirection == ScrollDirection.Horizontal
? new Vector2(-scrollPosition, -positionInGroup)
: new Vector2(positionInGroup, scrollPosition);
? new Vector2(-localPosition, -positionInGroup)
: new Vector2(positionInGroup, localPosition);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ namespace FancyScrollView
/// </summary>
public class FancyGridViewContext : IFancyGridViewContext
{
ScrollDirection IFancyScrollRectContext.ScrollDirection { get; set; }
Func<(float ScrollSize, float ReuseMargin)> IFancyScrollRectContext.CalculateScrollSize { get; set; }
GameObject IFancyCellGroupContext.CellTemplate { get; set; }
ScrollDirection IFancyGridViewContext.ScrollDirection { get; set; }
Func<int> IFancyCellGroupContext.GetGroupCount { get; set; }
Func<float> IFancyGridViewContext.GetStartAxisSpacing { get; set; }
Func<float> IFancyGridViewContext.GetCellSize { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ namespace FancyScrollView
/// </summary>
public interface IFancyGridViewContext : IFancyScrollRectContext, IFancyCellGroupContext
{
ScrollDirection ScrollDirection { get; set; }
Func<float> GetStartAxisSpacing { get; set; }
Func<float> GetCellSize { get; set ; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ protected override void Initialize()
{
base.Initialize();

Context.ScrollDirection = Scroller.ScrollDirection;
Context.CalculateScrollSize = () =>
{
var interval = CellSize + spacing;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,29 @@ public override void UpdatePosition(float position)
{
var (scrollSize, reuseMargin) = Context.CalculateScrollSize();

var unclampedPosition = (Mathf.Lerp(0f, scrollSize, position) - reuseMargin) / (scrollSize - reuseMargin * 2f);
var normalizedPosition = (Mathf.Lerp(0f, scrollSize, position) - reuseMargin) / (scrollSize - reuseMargin * 2f);

var start = 0.5f * scrollSize;
var end = -start;

UpdatePosition(unclampedPosition, Mathf.Lerp(start, end, position));
UpdatePosition(normalizedPosition, Mathf.Lerp(start, end, position));
}

/// <summary>
/// このセルの位置を更新します.
/// </summary>
/// <param name="position">
/// <param name="normalizedPosition">
/// ビューポートの範囲で正規化されたスクロール位置.
/// <see cref="FancyScrollRect{TItemData, TContext}.reuseCellMarginCount"/> の値に基づいて
/// <c>0.0</c> ~ <c>1.0</c> の範囲を超えた値が渡されることがあります.
/// </param>
/// <param name="viewportPosition">ローカル位置.</param>
protected virtual void UpdatePosition(float position, float viewportPosition) { }
/// <param name="localPosition">ローカル位置.</param>
protected virtual void UpdatePosition(float normalizedPosition, float localPosition)
{
transform.localPosition = Context.ScrollDirection == ScrollDirection.Horizontal
? new Vector2(-localPosition, 0)
: new Vector2(0, localPosition);
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace FancyScrollView
/// </summary>
public class FancyScrollRectContext : IFancyScrollRectContext
{
ScrollDirection IFancyScrollRectContext.ScrollDirection { get; set; }
Func<(float ScrollSize, float ReuseMargin)> IFancyScrollRectContext.CalculateScrollSize { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace FancyScrollView
/// </summary>
public interface IFancyScrollRectContext
{
ScrollDirection ScrollDirection { get; set; }
Func<(float ScrollSize, float ReuseMargin)> CalculateScrollSize { get; set; }
}
}

0 comments on commit 150df8d

Please sign in to comment.