Skip to content

Scroller Controllers

Daniel Colina edited this page Jul 15, 2020 · 3 revisions

Overview

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

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 class EnhancedScrollerCellView due to the plugin requiring it.

ACellViewController<TCellViewProperties> also implements the IEntityController<TEntityProperties> interface to maintain the behaviour detailed in entity controllers.

ACellViewController<TCellViewProperties> has one generic type constraint:

  • TCellViewProperties must implement ICellViewProperties. 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 implement IScrollerProperties. This forces whatever properties the scroller controller uses to return a SmallList (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 controlled EnhancedScroller through its IEnhancedScrollerDelegate 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 the GetCellViewSize() method from the ICellViewProperties at the given data index inside of IScrollerProperties.ScrollerData.

Examples

For an example of these controllers working hand-in-hand, see the ExampleScrollerControllerthat controls a group of ExampleCellViewController below:

Example Cell View Controller

// 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> {}

Example Scroller Controller

// 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