Skip to content

Tab Menu Controllers

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

Overview

Tab menu controllers are specialized entity controllers that a control group of tab page controllers and corresponding tabs, which are simply toggle controllers.

Tab Page Controllers

Tab page controllers are specialized view controllers that control a page opened by a tab controller in a tab menu system.

The default implementation of a tab page is ATabPageController<TTabPageProperties>.

ATabPageController<TTabPageProperties> has one type generic type constraint:

  • TTabPageProperties must implement ITabPageProperties. This forces whatever properties the tab page controller uses to return the properties of the tab controller that will open it.

    public interface ITabPageProperties
    {
        object TabProperties { get; }
    }

API Reference

The default implementation of a tab menu is ATabMenuController<TTabMenuProperties>. It inherits from AEntityController<TEntityProperties>, and as such maintains all behaviour detailed in entity controllers.

ATabMenuController<TTabMenuProperties> has one generic type constraint:

  • TTabMenuProperties must implement ITabMenuProperties. This forces whatever properties the tab menu controller uses to return the properties of the tab pages it will contain.

    public interface ITabMenuProperties
    {
        List<ITabPageProperties> TabPageProperties { get; }
    }

ATabMenuController<TTabMenuProperties> exposes a few methods that sub-classes can override:

  • abstract GetTab(object tabProperties): Handler to determine the tab prefab to spawn based on the given tabProperties.
  • abstract GetTabPage(ITabPageProperties tabPageProperties): Handler to determine the tab page prefab to spawn based on the given tabPageProperties.

Examples

For an example of these controllers working hand-in-hand, see the ExampleTabMenuController which supports receiving IntegerTabPageProperties and BooleanTabPageProperties and will spawn IntegerTabPageController and BooleanTabPageController objects as well as ExampleTabController objects to open them.

Example Tab Controller

// This is a simple tab controller that stores a string as its properties
// and displays it on its TextMeshProUGUI component.
public class ExampleTabController : AToggleController<string>
{
    [SerializeField] private TextMeshProUGUI label;

    protected override void OnPropertiesSet()
    {
        base.OnPropertiesSet();
        label.text = Properties;
    }
}

Integer Tab Page Controller

// Make sure your custom tab page properties implement ITabPageProperties.
[System.Serializable]
public class IntegerTabPageProperties : ITabPageProperties
{
    // Each tab page's properties must return the properties of the tab the menu
    // will spawn to open it. Then, the impelementation of the tab menu that uses
    // this tab page decides which prefab to spawn based on the type of properties.
    public object TabProperties => new ExampleTabProperties("Integer");
    public int value;
    public IntegerTabPageProperties(int value)
    {
        this.value = value;
    }
}

// This is a simple tab page controller that stores a integer in its properties.
public class IntegerTabPageController : ATabPageController<IntegerTabPageProperties {}

Boolean Tab Page Controller

// Make sure your custom tab page properties implement ITabPageProperties.
public class BooleanTabPageProperties : ITabPageProperties
{
    // Each tab page's properties must return the properties of the tab the menu
    // will spawn to open it. Then, the impelementation of the tab menu that uses
    // this tab page decides which prefab to spawn based on the type of properties.
    public object TabProperties => new ExampleTabProperties("Boolean");
    public bool value;
    public BooleanTabPageProperties(bool value)
    {
        this.value = value;
    }
}

// This is a simple tab page controller that stores a boolean in its properties.
public class BooleanTabPageController : ATabPageController<BooleanTabPageProperties> {}

Example Tab Menu Controller

// Make sure your custom tab menu properties implement ITabMenuProperties.
public class ExampleTabMenuProperties : ITabMenuProperties.
{
    // IMPORTANT: Note that these properties are not marked [System.Serializable];
    // this is because it stores a list of interfaces, which Unity cannot serialize.
    public List<ITabPageProperties> TabPageProperties { get; }
    public ExampleTabMenuProperties(List<ITabPageProperties> tabPageProperties)
    {
        TabPageProperties = tabPageProperties;
    }
}

public class ExampleTabMenuController : ATabMenuController<ExampleTabMenuProperties>
{
    [SerializeField] private ExampleTabController exampleTabPrefab;
    [SerializeField] private IntegerTabPageController integerTabPagePrefab;
    [SerializeField] private BooleanTabPageController booleanTabPagePrefab;

    protected override IToggleController GetTab(object tabProperties)
    {
        if (tabProperties is ExampleTabProperties exampleTabProperties)
        {
            ExampleTabController exampleTab = SpawnTab(exampleTabPrefab) as ExampleTabController;
            exampleTab.SetProperties(exampleTabProperties);
            exampleTab.name = $"[ExampleTab] {exampleTabProperties.label}";
            return exampleTab;
        }
        else
            return null;
    }

    protected override IViewController GetTabPage(ITabPageProperties tabPageProperties)
    {
        if (tabPageProperties is BooleanTabPageProperties booleanTabPageProperties)
        {
            BooleanTabPageController booleanTabPage = InstantiateTabPage(booleanTabPagePrefab) as BooleanTabPageController;
            booleanTabPage.SetProperties(booleanTabPageProperties);
            booleanTabPage.name = $"[BooleanTabPage] {booleanTabPageProperties.value}";
            booleanTabPage.UIView.ViewName = $"[BooleanTabPage] {booleanTabPageProperties.value}";
            return booleanTabPage;
        }
        else if (tabPageProperties is IntegerTabPageProperties integerTabPageProperties)
        {
            IntegerTabPageController integerTabPage = InstantiateTabPage(integerTabPagePrefab) as IntegerTabPageController;
            integerTabPage.SetProperties(integerTabPageProperties);
            integerTabPage.name = $"[IntegerTabPage] {integerTabPageProperties.value}";
            integerTabPage.UIView.ViewName = $"[IntegerTabPage] {integerTabPageProperties.value}";
            return integerTabPage;
        }
        else
            return null;
    }
}