-
Notifications
You must be signed in to change notification settings - Fork 1
Scroller Controllers
Scroller controllers are specialized entity controllers that control an EnhancedScroller component and a group of cell view controllers.
The majority of the implementation of scroller controllers is built on the EnhancedScroller plugin. If parts of the implementation don't make sense reference the EnhancedScroller documentation.
Cell view controllers are specialized entity controllers that control an individual cell view in a scroller controller.
The default implementation for a cell view controller is
ACellViewController<TCellViewProperties>
. It inherits from the EnhancedScroller plugin's classEnhancedScrollerCellView
due to the plugin requiring it.
ACellViewController<TCellViewProperties>
also implements theIEntityController<TEntityProperties>
interface to maintain the behaviour detailed in entity controllers.
ACellViewController<TCellViewProperties>
has one generic type constraint:
TCellViewProperties
must implementICellViewProperties
. This forces whatever properties the cell view controller uses to return the size of the cell view it controls.public interface ICellViewProperties { float GetCellViewSize(); }
The default implementation of a scroller controller is AScrollerController<TScrollerProperties>
. It inherits from AEntityController<TEntityProperties>
, and as such maintains all behaviour detailed in entity controllers.
AScrollerController<TScrollerProperties>
also implements the IScrollerController
interface, which in turn implements the IEnhancedScrollerDelegate
interface required by the EnhancedScroller plugin.
AScrollerController<TScrollerProperties>
has one generic type restraint:
-
TScrollerProperties
must implementIScrollerProperties
. This forces whatever properties the scroller controller uses to return aSmallList
(an EnhancedScroller type) of the properties of the cell view controllers it will control.public interface IScrollerProperties { SmallList<ICellViewProperties> CellViewProperties { get; } }
AScrollerController<TScrollerProperties>
exposes the following methods that sub-classes can override:
-
abstract GetCellView(EnhancedScroller scroller, int dataIndex, int cellIndex)
: Gets and returns the prefab for the cell view to be spawned based on the given data and cell index. Called by the controlledEnhancedScroller
through itsIEnhancedScrollerDelegate
interface. -
virtual GetNumberOfCells(EnhancedScroller scroller)
: Gets and returns the number of cell view objects to be populated. -
virtual GetCellViewSize(EnhancedScroller scroller, int dataIndex)
: Given a data index, return the size of the cell view at that index. By default, delegates to theGetCellViewSize()
method from theICellViewProperties
at the given data index inside ofIScrollerProperties.ScrollerData
.
For an example of these controllers working hand-in-hand, see the ExampleScrollerController
that controls a group of ExampleCellViewController
below:
// Make sure your custom cell view properties implement ICellViewProperties.
[System.Serializable]
public class ExampleCellViewProperties : ICellViewProperties
{
public int value;
public ExampleCellViewProperties(int value)
{
this.value = value;
}
public float GetCellViewSize()
{
// This value can by dynamic based on the cell view that holds it,
// or a constant value.
return 50.0f;
}
}
// This is just an simple cell view controller that stores an integer in its properties.
public class ExampleCellViewController : ACellViewController<ExampleCellViewProperties> {}
// Make sure your custom scroller properties implement IScrollerProperties.
[System.Serializable]
public class ExampleScrollerProperties : IScrollerProperties
{
public List<int> values;
public SmallList<ICellViewProperties> CellViewProperties { get; private set; } = new SmallList<ICellViewProperties>();
// Populate the SmallList with a list of cell view data.
public ExampleScrollerProperties(List<int> values)
{
this.values = values;
foreach (var value in values)
CellViewProperties.Add(new ExampleCellViewProperties(value));
}
}
public class ExampleScrollerController : AScrollerController<ExampleScrollerProperties>
{
// Make sure you store the prefabs for the cell view types.
[SerializeField] private ExampleCellViewController exampleCellViewPrefab;
public override EnhancedScrollerCellView GetCellView(EnhancedScroller scroller, int dataIndex, int cellIndex)
{
// This syntax is pulled straight from the EnhancedScroller examples.
if (Properties.CellViewProperties[dataIndex] is ExampleCellViewProperties exampleCellViewProperties)
{
ExampleCellViewController exampleCellView = scroller.GetCellView(exampleCellViewPrefab) as ExampleCellViewPrefab;
exampleCellView.SetProperties(exampleCellViewProperties);
return cellView;
}
else
return null;
}
}
#endregion