Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
setchi committed Jan 11, 2020
1 parent db0bc0d commit 2fa4689
Show file tree
Hide file tree
Showing 13 changed files with 112 additions and 57 deletions.
2 changes: 1 addition & 1 deletion Assets/FancyScrollView/Examples/Sources/02_FocusOn/Cell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ static class AnimatorHash
public static readonly int Scroll = Animator.StringToHash("scroll");
}

void Start()
public override void Initialize()
{
button.onClick.AddListener(() => Context.OnCellClicked?.Invoke(Index));
}
Expand Down
29 changes: 12 additions & 17 deletions Assets/FancyScrollView/Examples/Sources/04_Metaball/Cell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,21 @@ static class AnimatorHash
float hash;
bool currentSelection;

void Start()
public override void Initialize()
{
hash = Random.value * 100f;
button.onClick.AddListener(() => Context.OnCellClicked?.Invoke(Index));

Context.UpdateCellState += () =>
{
var siblingIndex = rectTransform.GetSiblingIndex();
var scale = Mathf.Min(1f, 10 * (0.5f - Mathf.Abs(currentPosition - 0.5f)));
var position = IsVisible
? this.position + GetFluctuation()
: rectTransform.rect.size.x * 10f * Vector3.left;

Context.SetCellState(siblingIndex, Index, position.x, position.y, scale);
};
}

void LateUpdate()
Expand All @@ -48,22 +59,6 @@ Vector3 GetFluctuation()
return new Vector3(fluctX, fluctY, 0f);
}

public override void SetupContext(Context context)
{
base.SetupContext(context);

Context.UpdateCellState += () =>
{
var siblingIndex = rectTransform.GetSiblingIndex();
var scale = Mathf.Min(1f, 10 * (0.5f - Mathf.Abs(currentPosition - 0.5f)));
var position = IsVisible
? this.position + GetFluctuation()
: rectTransform.rect.size.x * 10f * Vector3.left;

Context.SetCellState(siblingIndex, Index, position.x, position.y, scale);
};
}

public override void UpdateContent(ItemData cellData)
{
message.text = cellData.Message;
Expand Down
31 changes: 13 additions & 18 deletions Assets/FancyScrollView/Examples/Sources/05_Voronoi/Cell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,10 @@ static class AnimatorHash
bool currentSelection;
float updateSelectionTime;

void Start()
public override void Initialize()
{
hash = Random.value * 100f;
button.onClick.AddListener(() => Context.OnCellClicked?.Invoke(Index));
}

void LateUpdate()
{
image.rectTransform.localPosition = position + GetFluctuation();
}

Vector3 GetFluctuation()
{
var fluctX = Mathf.Sin(Time.time + hash * 40) * 10;
var fluctY = Mathf.Sin(Time.time + hash) * 10;
return new Vector3(fluctX, fluctY, 0f);
}

public override void SetupContext(Context context)
{
base.SetupContext(context);

Context.UpdateCellState += () =>
{
Expand All @@ -66,6 +49,18 @@ public override void SetupContext(Context context)
};
}

void LateUpdate()
{
image.rectTransform.localPosition = position + GetFluctuation();
}

Vector3 GetFluctuation()
{
var fluctX = Mathf.Sin(Time.time + hash * 40) * 10;
var fluctY = Mathf.Sin(Time.time + hash) * 10;
return new Vector3(fluctX, fluctY, 0f);
}

public override void UpdateContent(ItemData cellData)
{
message.text = cellData.Message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ static class AnimatorHash
public static readonly int Scroll = Animator.StringToHash("scroll");
}

void Start()
public override void Initialize()
{
button.onClick.AddListener(() => Context.OnCellClicked?.Invoke(Index));
}
Expand Down
12 changes: 3 additions & 9 deletions Assets/FancyScrollView/Examples/Sources/08_GridView/Cell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace FancyScrollView.Example08
{
public class Cell : FancyCell<ItemData, Context>
public class Cell : FancyGridViewCell<ItemData, Context>
{
[SerializeField] Text message = default;
[SerializeField] Image image = default;
Expand All @@ -26,16 +26,10 @@ public override void UpdateContent(ItemData itemData)

public override void UpdatePosition(float position)
{
var cellSize = Context.GetCellSize();
var spacing = Context.GetColumnSpacing();
var columnCount = Context.GetColumnCount();
var count = Index % columnCount;
base.UpdatePosition(position);

var x = (cellSize + spacing) * (count - (columnCount - 1) * 0.5f);
var y = transform.localPosition.y;
var wave = Mathf.Sin(position * Mathf.PI * 2) * 65;

transform.localPosition = new Vector2(x + wave, y);
transform.localPosition += Vector3.right * wave;
}
}
}
11 changes: 8 additions & 3 deletions Assets/FancyScrollView/Sources/Runtime/Core/FancyCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,15 @@ namespace FancyScrollView
protected TContext Context { get; private set; }

/// <summary>
/// <see cref="Context"/> のセットアップを行います.
/// <see cref="Context"/> をセットします.
/// </summary>
/// <param name="context">コンテキスト.</param>
public virtual void SetupContext(TContext context) => Context = context;
public virtual void SetContext(TContext context) => Context = context;

/// <summary>
/// 初期化を行います.
/// </summary>
public virtual void Initialize() { }

/// <summary>
/// このセルの可視状態を設定します.
Expand Down Expand Up @@ -66,6 +71,6 @@ namespace FancyScrollView
public abstract class FancyCell<TItemData> : FancyCell<TItemData, NullContext>
{
/// <inheritdoc/>
public sealed override void SetupContext(NullContext context) => base.SetupContext(context);
public sealed override void SetContext(NullContext context) => base.SetContext(context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ void ResizePool(float firstPosition)
typeof(TItemData).FullName, typeof(TContext).FullName, CellPrefab.name));
}

cell.SetupContext(Context);
cell.SetContext(Context);
cell.Initialize();
cell.SetVisible(false);
pool.Add(cell);
}
Expand Down
12 changes: 11 additions & 1 deletion Assets/FancyScrollView/Sources/Runtime/GridView/FancyGridView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ namespace FancyScrollView
/// <summary>
/// グリッドレイアウトのスクロールビューを実装するための抽象基底クラス.
/// 無限スクロールおよびスナップには対応していません.
/// /// <see cref="FancyScrollView{TItemData, TContext}.Context"/> が不要な場合は
/// 代わりに <see cref="FancyGridView{TItemData}"/> を使用します.
/// </summary>
/// <typeparam name="TItemData">アイテムのデータ型.</typeparam>
/// <typeparam name="TContext"><see cref="FancyScrollView{TItemData, TContext}.Context"/> の型.</typeparam>
public abstract class FancyGridView<TItemData, TContext> : FancyScrollRect<TItemData[], TContext>
where TContext : class, IFancyScrollRectContext, IFancyGridViewContext, new()
where TContext : class, IFancyGridViewContext, new()
{
/// <summary>
/// カラム同士の余白.
Expand Down Expand Up @@ -140,4 +142,12 @@ protected override void ScrollTo(int itemIndex, float duration, Ease easing, flo
base.ScrollTo(rowIndex, duration, easing, alignment, onComplete);
}
}

/// <summary>
/// グリッドレイアウトのスクロールビューを実装するための抽象基底クラス.
/// 無限スクロールおよびスナップには対応していません.
/// </summary>
/// <typeparam name="TItemData">アイテムのデータ型.</typeparam>
/// <seealso cref="FancyGridView{TItemData, TContext}"/>
public abstract class FancyGridView<TItemData> : FancyGridView<TItemData, FancyGridViewContext> { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* FancyScrollView (https://github.com/setchi/FancyScrollView)
* Copyright (c) 2019 setchi
* Licensed under MIT (https://github.com/setchi/FancyScrollView/blob/master/LICENSE)
*/

using UnityEngine;

namespace FancyScrollView
{
/// <summary>
/// <see cref="FancyGridView{TItemData, TContext}"/> のセルを実装するための抽象基底クラス.
/// </summary>
/// <typeparam name="TItemData">アイテムのデータ型.</typeparam>
/// <typeparam name="TContext"><see cref="FancyCell{TItemData, TContext}.Context"/> の型.</typeparam>
public abstract class FancyGridViewCell<TItemData, TContext> : FancyCell<TItemData, TContext>
where TContext : class, IFancyGridViewContext, new()
{
/// <inheritdoc/>
public override void UpdatePosition(float position)
{
var cellSize = Context.GetCellSize();
var spacing = Context.GetColumnSpacing();
var columnCount = Context.GetColumnCount();

var count = Index % columnCount;
var p = (cellSize + spacing) * (count - (columnCount - 1) * 0.5f);

transform.localPosition = Context.ScrollDirection == ScrollDirection.Horizontal
? new Vector2(transform.localPosition.x, p)
: new Vector2(p, transform.localPosition.y);
}
}

/// <summary>
/// <see cref="FancyGridView{TItemData}"/> のセルを実装するための抽象基底クラス.
/// </summary>
/// <typeparam name="TItemData">アイテムのデータ型.</typeparam>
/// <seealso cref="FancyGridViewCell{TItemData, TContext}"/>
public abstract class FancyGridViewCell<TItemData> : FancyGridViewCell<TItemData, FancyGridViewContext>
{
/// <inheritdoc/>
public sealed override void SetContext(FancyGridViewContext context) => base.SetContext(context);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,15 @@ protected virtual FancyCell<TItemData, TContext>[] InstantiateCells()
}

/// <inheritdoc/>
public override void SetupContext(TContext context)
public override void Initialize()
{
base.SetupContext(context);

Cells = InstantiateCells();
Debug.Assert(Cells.Length == Context.GetColumnCount());

for (var i = 0; i < Cells.Length; i++)
{
Cells[i].SetupContext(context);
Cells[i].SetContext(Context);
Cells[i].Initialize();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace FancyScrollView
/// <summary>
/// <see cref="FancyGridView{TItemData, TContext}"/> のコンテキストインターフェース.
/// </summary>
public interface IFancyGridViewContext
public interface IFancyGridViewContext : IFancyScrollRectContext
{
GameObject CellTemplate { get; set; }
ScrollDirection ScrollDirection { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ protected virtual void UpdatePosition(float position, float viewportPosition) {
public abstract class FancyScrollRectCell<TItemData> : FancyScrollRectCell<TItemData, FancyScrollRectContext>
{
/// <inheritdoc/>
public sealed override void SetupContext(FancyScrollRectContext context) => base.SetupContext(context);
public sealed override void SetContext(FancyScrollRectContext context) => base.SetContext(context);
}
}

0 comments on commit 2fa4689

Please sign in to comment.