Ui/MatchPanel.cs

Abstract base UI panel for displaying match information. It binds to a CityGame instance and handles UI updates based on configuration changes and event state.

using CitySim.Match;
using Sandbox.UI;

namespace CitySim;

/// <summary>A Razor panel subscribes only to events relevant to its content.</summary>
public abstract class MatchPanel : Panel
{
	[Parameter] public CityGame City { get; set; }
	protected MatchState BoundMatch { get; private set; }
	CityGame boundCity;
	string renderedLanguage;
	int renderedUiScale = -1;
	protected override void OnParametersSet()
	{
		base.OnParametersSet();
		if (City == boundCity) return;
		if (boundCity != null) boundCity.MatchChanged -= Rebind;
		Unsubscribe(); boundCity = City;
		if (boundCity != null) boundCity.MatchChanged += Rebind;
		Rebind();
	}
	void Rebind()
	{
		Unsubscribe(); BoundMatch = City?.Match; Subscribe(); StateHasChanged();
	}
	protected virtual void Subscribe() { }
	protected virtual void Unsubscribe() { }
	protected void Refresh() => StateHasChanged();
	public override void Tick()
	{
		base.Tick();
		if (renderedLanguage == GameText.LanguageCode && renderedUiScale == PlayerPreferences.Current.UiScale) return;
		renderedLanguage = GameText.LanguageCode;
		renderedUiScale = PlayerPreferences.Current.UiScale;
		Refresh();
	}
	public override void OnDeleted()
	{
		Unsubscribe(); if (boundCity != null) boundCity.MatchChanged -= Rebind;
		base.OnDeleted();
	}
}