UI/Screen/Components/InputGlyph.razor

A UI component (Razor) that displays an input glyph icon for a given action or analog input. It binds CSS classes to show pressed state, accepts properties (action, analog, size, outline), and chooses the correct glyph texture from the Input API.

Native Interop
@using Sandbox
@using Sandbox.UI
@using System
@inherits Sandbox.UI.Panel
@namespace Sandbox

<root />

@code {
	private string _inputAction;
	private InputAnalog _inputAnalog;
	private InputGlyphSize _inputGlyphSize;
	private bool _outline;

	protected override void OnAfterTreeRender(bool firstTime)
	{
		Update();
	}

	public void SetAction(string inputAction)
	{
		if (_inputAction == inputAction)
			return;

		_inputAction = inputAction;
		BindClass("pressed", () => Input.Down(_inputAction));
		Update();
	}

	public void SetAnalog(string inputAnalog)
	{
		if (!Enum.TryParse<InputAnalog>(inputAnalog, true, out var analog))
			return;

		if (analog == InputAnalog.LeftStickX || analog == InputAnalog.LeftStickY)
			BindClass("pressed", () => !Input.AnalogMove.IsNearlyZero());
		else if (analog == InputAnalog.RightStickX || analog == InputAnalog.RightStickY)
			BindClass("pressed", () => !Input.AnalogLook.IsNearlyZero());

		_inputAnalog = analog;
	}

	public override void SetProperty(string name, string value)
	{
		switch (name)
		{
			case "action":
				{
					SetAction(value);
					Update();

					break;
				}

			case "analog":
				{
					SetAnalog(value);
					Update();

					break;
				}

			case "size":
				{
					Enum.TryParse(value, true, out _inputGlyphSize);
					Update();

					break;
				}

			case "outline":
				{
					_outline = value switch
					{
						"true" => true,
						"false" => false,
						_ => _outline
					};

					Update();
					break;
				}
		}

		base.SetProperty(name, value);
	}

	private void Update()
	{
		var texture = _inputAction is null ? Input.GetGlyph(_inputAnalog, _inputGlyphSize, _outline) : Input.GetGlyph(_inputAction, _inputGlyphSize, _outline);
		Style.BackgroundImage = texture;
	}

	protected override int BuildHash()
	{
		return HashCode.Combine(Input.UsingController);
	}
}