Skip to content

Button Controllers

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

Overview

Button controllers are specialized entity controllers that control a UIButton component.

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

AButtonController<TButtonProperties> exposes a few methods sub-classes can override:

  • virtual HandleOnClick(): Handler for backend behaviour (passing data, sending out events, etc) when clicked.
  • virtual AnimateOnClick(): Handler for frontend behaviour when clicked.
  • virtual HandleInteractabilityChanged(bool value): Handler for backend behaviour when interactability has been changed.
  • virtual AnimateInteractabilityChanged(bool value): Handler for frontend behaviour when interactability has been changed.

In addition, AButtonController<TButtonProperties> exposes a public method:

  • ToggleInteractability(bool value): Toggles interactability on controlled UIButton component to the given value.

Examples

For an example, see the ExampleButtonController below:

Example Button Controller

// This is a simple button controller that stores a string as its properties.
public class ExampleButtonController : AButtonController<string>
{
    [SerializeField] private Gesture gesture;
    [SerializeField] private Animator anim;

    protected override void HandleOnClick()
    {
        // Handle backend behaviour like dispatching a global
        // message to other scripts here.
        base.HandleOnClick();
        Debug.Log($"My key is {Properties}, and I was clicked!");
    }

    protected override void AnimateOnClick()
    {
        // In most cases, DoozyUI can manage visual behaviour,
        // but in the case that a button controller needs to change
        // visually when clicked in a format that is not supported
        // by DoozyUI, do it here.
        base.AnimateOnClick();
        anim.SetTrigger("Clicked");
    }

    protected override void HandleInteractabilityChanged(bool value)
    {
        // Use cases for this method are slim, but handle specific
        // non-visual behaviour like disabling controlling gestures here.
        base.HandleInteractabilityChanged();
        gesture.Cancel();
    }

    protected override void AnimateInteractabilityChanged(bool value)
    {
        // As with AnimateOnClick, DoozyUI can usually manage visual
        // behaviour, but in the case that a button controller needs to
        // change visually when interactability is enabled or disabled
        // that is not supported by DoozyUI, do it here.
        base.AnimateInteractabilityChanged();
        anim.SetBool("Interactable", value);
    }
}