-
Notifications
You must be signed in to change notification settings - Fork 1
Toggle Group Controllers
Toggle group controllers are specialized entity controllers that control a ToggleGroup component and a group of toggle controllers.
Toggle group controllers are only able to control a group of toggle controllers of a specific type. This is by design; in essence they serve as a proxy for callbacks from a group of toggle controllers in addition to providing more control over the group.
The base implementation of a toggle group controller is the AToggleGroupController<TToggleGroupProperties, TToggleController, TToggleProperties>
. It inherits from AEntityController<TEntityProperties>
, and as such maintains all behaviour detailed in entity controllers.
AToggleGroupController<TToggleGroupProperties, TToggleController, TToggleProperties>
has two type generic type constraints:
-
TToggleGroupProperties
must implementIToggleGroupProperties<TToggleProperties>
. This forces whatever properties the toggle group controller uses to return the properties of the toggle controllers it will control.public interface IToggleGroupProperties<TToggleProperties> { List<TToggleProperties> GetToggleProperties(); }
-
TToggleController
must inherit fromAToggleController<TToggleProperties>
.
AToggleGroupController<TToggleGroupProperties, TToggleController, TToggleProperties>
exposes a few methods that sub-classes can override:
-
abstract InitializeToggle(TToggleController toggle)
: Handler to initialize the given `toggle. Usually, this can be used to set the name of the toggle or any other custom behaviour as the toggle is instantiated. -
virtual Toggle_ValueChanged(TToggleController toggle, bool value)
: Handler for when a controlled toggle's value is changed.
For an example, see the ExampleToggleGroupController
that controls a group of ExampleToggleController
below:
// This is an simple toggle controller that stores an integer as its properties.
public class ExampleToggleController : AToggleController<int> {}
// Make sure your custom properties implements IToggleGroupProperties<TToggleGroupProperties>,
// passing the type of properties of the toggle controller this group will control to it.
[System.Serializable]
public class ExampleToggleGroupProperties : IToggleGroupProperties<int>
{
public List<int> values;
public List<int> GetToggleProperties()
{
// Because this method is part of the IToggleGroupProperties<TToggleProperties>
// interface, each custom toggle group properties must implement it.
// Essentially, this method creates and returns the properties of the
// toggles this toggle group will control.
return values;
}
}
public class ExampleToggleGroupController : AToggleGroupController<ExampleToggleGroupProperties, ExampleToggleController, int>
{
protected override InitializeToggle(ExampleToggleController toggle)
{
// Handle initializing the given toggle here. This can include
// naming its GameObject, coupling specific components on it
// to this toggle group, etc.
toggle.name = $"[ExampleToggle] {toggle.Properties}";
}
protected override void Toggle_ValueChanged(ExampleToggleController toggle, bool value)
{
base.Toggle_ValueChanged(toggle, value);
// Handle backend behaviour that occur when a controlled
// toggle is toggled on or off here. You can think of this
// as a aggregate of toggles; if a value is changed on
// the group, and then all toggles are off, then no toggles are
// on, and vice versa.
if (ToggleGroup.AnyTogglesOn())
Debug.Log("One toggle is on!");
else
Debug.Log("All toggles are off!");
}
}