Editor/SubTerrainStatusWidget.cs

An editor UI widget that shows the cursor height and brush footprint for the SubTerrain tool. It updates four labels each frame with height, normalized height, brush texel count and brush span based on the tool's status and terrain storage.

using System;
using Sandbox;

namespace Editor.SubTerrain;

/// <summary>
/// Sidebar readout for cursor height and brush footprint.
/// </summary>
internal class SubTerrainStatusWidget : Widget
{
	readonly SubTerrainTool _tool;
	Label _heightLabel;
	Label _normalizedLabel;
	Label _texelLabel;
	Label _spanLabel;

	public SubTerrainStatusWidget( SubTerrainTool tool, Widget parent ) : base( parent )
	{
		_tool = tool;
		Layout = Layout.Column();
		Layout.Spacing = 2;

		_heightLabel = Layout.Add( new Label( "Height: —" ) );
		_normalizedLabel = Layout.Add( new Label( "Normalized: —" ) );
		_texelLabel = Layout.Add( new Label( "Brush texels: —" ) );
		_spanLabel = Layout.Add( new Label( "Brush span: —" ) );
	}

	[EditorEvent.Frame]
	public void Refresh()
	{
		if ( !_tool.StatusValid || !_tool.StatusTerrain.IsValid() )
		{
			_heightLabel.Text = "Height: —";
			_normalizedLabel.Text = "Normalized: —";
			_texelLabel.Text = "Brush texels: —";
			_spanLabel.Text = "Brush span: —";
			return;
		}

		var storage = _tool.StatusTerrain.Storage;
		var z = _tool.StatusHitLocal.z;
		var texels = (int)Math.Floor( _tool.BrushSettings.Size / storage.TerrainSize * storage.Resolution );

		_heightLabel.Text = $"Height: {z:0.##}";
		_normalizedLabel.Text = $"Normalized: {(z / storage.TerrainHeight):0.####}";
		_texelLabel.Text = $"Brush texels: {Math.Max( texels, 1 )}";
		_spanLabel.Text = $"Brush span: {_tool.BrushSettings.Size} u";
	}
}