Skip to content

Button Group Controllers

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

Overview

Button group controllers are specialized entity controllers that control a group of button controllers.

They are only able to control a group of button controllers of a specific type. This is by design; in essence they serve as a proxy for callbacks from a group of Button controllers in addition to providing more control over the group.

The base implementation of a button group controller is the AButtonGroupController<TButtonGroupProperties, TButtonController, TButtonProperties>. It inherits from AEntityController<TEntityProperties>, and as such maintains all behaviour detailed in entity controllers.

AButtonGroupController<TButtonGroupProperties, TButtonController, TButtonProperties> has two type generic type constraints:

  • TButtonGroupProperties must implement IButtonGroupProperties<TButtonProperties>. This forces whatever properties the button group controller uses to return the properties of the button controllers it will control.

    public interface IButtonGroupProperties<TButtonProperties>
    {
        List<TButtonProperties> GetButtonProperties();
    }
  • TButtonController must inherit from AButtonController<TButtonProperties>.

AButtonGroupController<TButtonGroupProperties, TButtonController, TButtonProperties> exposes a few methods that sub-classes can override:

  • abstract InitializeButton(TButtonController button): Handler to initialize the given button. Usually, this can be used to set the name of the button or any other custom behaviour required as the button is instantiated.
  • virtual Button_Clicked(TButtonController button): Handler for when a controlled button is clicked.

Examples

For an example, see the ExampleButtonGroupController that controls a group of ExampleButtonController below:

Example Button Controller

// This is a simple button controller that stores an integer in its properties
public class ExampleButtonController : AButtonController<int> {}

Example Button Group Controller

// Make sure your custom properties implements IButtonGroupProperties<TButtonGroupProperties>,
// passing the type of properties of the button controller this group will control to it.
[System.Serializable]
public class ExampleButtonGroupProperties : IButtonGroupProperties<int>
{
    public List<int> values;

    public List<int> GetButtonProperties()
    {
        // Because this method is part of the IButtonGroupProperties<TButtonProperties>
        // interface, each custom button group properties must implement it.
        // Essentially, this method creates and returns the properties of the
        // buttons this button group will control.
        return values;
    }
}

public class ExampleButtonGroupController : AButtonGroupController<ExampleButtonGroupProperties, ExampleButtonController, int>
{
    protected override InitializeButton(ExampleButtonController button)
    {
        // Handle initializing the given button here. This can include
        // naming its GameObject, coupling specific components on it
        // to this button group, etc.
        button.name = $"[ExampleButton] {button.Properties}";
    }

    protected override void Button_Clicked(ExampleButtonController button)
    {
        base.Button_Clicked(button);
        // Handle backend behaviour that occur when a controlled
        // button is clicked here.
        Debug.Log($"Controlled button {button} was clicked");
    }
}