Game.UI.Editor.FilterMenu
Assembly: Game
Namespace: Game.UI.Editor
Type: public class FilterMenu
Base: Game.UI.Widgets.Widget
Summary:
FilterMenu is a UI widget used by the editor to present and manage a list of filters. It relies on a pluggable IAdapter to provide the available filter names and the currently active filters, and to perform filter toggling / clearing operations. The widget exposes a Bindings factory that creates trigger bindings used by the UI system to call toggle and clear actions. Internally it marks itself dirty when the adapter signals that the available filters changed, causing properties to be re-emitted during the next Update cycle.
Fields
-
private bool m_Dirty
Tracks whether the widget's properties need to be re-sent to the UI (set when the adapter reports changes or when filters are changed). -
private IAdapter m_Adapter
Reference to the current adapter providing filter data and actions. The setter subscribes/unsubscribes to the adapter's onAvailableFiltersChanged callback.
Properties
public IAdapter adapter { get; private set }
Getter/setter for the adapter. On set, previous adapter's onAvailableFiltersChanged delegate is removed and the new adapter's delegate is combined with FilterMenu.OnAvailableFiltersChanged so the widget is notified when the available filters change.
Constructors
public FilterMenu()
No explicit constructor is defined in the source; the class uses the default parameterless constructor inherited from Widget.
Methods
-
private void OnAvailableFiltersChanged()
Called when the adapter signals available-filter changes. Marks the widget dirty so properties will be updated on the next Update(). -
protected override WidgetChanges Update()
Overrides Widget.Update(). If m_Dirty is set, it adds WidgetChanges.Properties to the returned WidgetChanges so the UI system knows to fetch properties. Clears m_Dirty after use. -
protected override void WriteProperties(IJsonWriter writer)
Writes the following properties into the provided JSON writer: - "availableFilters": the adapter.availableFilters list
-
"activeFilters": the adapter.activeFilters list This is used by the UI system to serialize the widget's state to the front-end.
-
private void OnToggleFilter(string filter, bool active)
Calls adapter.ToggleFilter(filter, active) and marks the widget dirty so changes will be published. -
private void OnClearFilters()
Calls adapter.ClearFilters() and marks the widget dirty.
Nested types
public interface IAdapter
The adapter that the FilterMenu expects to interact with. Members:List<string> availableFilters { get; }
— list of filter names that can be applied.List<string> activeFilters { get; }
— list of currently active filters.Action onAvailableFiltersChanged { get; set; }
— callback the adapter should invoke when availableFilters changes.void ToggleFilter(string filter, bool active)
— toggle the specified filter on/off.-
void ClearFilters()
— clear all active filters. -
public class Bindings : IWidgetBindingFactory
Creates bindings so UI actions can trigger methods on the widget. Implementation details: - CreateBindings(...) yields a TriggerBinding for "toggleFilter" which expects (IWidget, string, bool). When triggered it checks widget is a FilterMenu, calls OnToggleFilter(filter, active), and invokes onValueChanged(widget).
- CreateBindings(...) also yields a TriggerBinding for "clearFilters" which expects (IWidget). When triggered it checks widget is a FilterMenu, calls OnClearFilters(), and invokes onValueChanged(widget).
Usage Example
// Example adapter implementing the IAdapter contract
public class SimpleFilterAdapter : FilterMenu.IAdapter
{
public List<string> availableFilters { get; } = new List<string> { "Residential", "Commercial", "Industrial" };
public List<string> activeFilters { get; } = new List<string>();
public Action onAvailableFiltersChanged { get; set; }
public void ToggleFilter(string filter, bool active)
{
if (active)
{
if (!activeFilters.Contains(filter)) activeFilters.Add(filter);
}
else
{
activeFilters.Remove(filter);
}
}
public void ClearFilters()
{
activeFilters.Clear();
}
// Call this if availableFilters is changed externally:
public void NotifyAvailableFiltersChanged() => onAvailableFiltersChanged?.Invoke();
}
// Using the FilterMenu in a widget creation lifecycle
protected override void OnCreate()
{
base.OnCreate();
var filterMenu = new FilterMenu();
filterMenu.adapter = new SimpleFilterAdapter();
// The UI system will use FilterMenu.Bindings to wire up toggle/clear triggers
}
Notes: - The widget relies on the adapter to invoke onAvailableFiltersChanged when availableFilters changes; otherwise the widget won't refresh availableFilters until another action sets m_Dirty. - Bindings yield TriggerBinding entries that expect front-end triggers named "toggleFilter" and "clearFilters". These are how UI events should be wired to control the FilterMenu.