A Blazor-style UI component for selecting an anchor position. It renders a 3x3-like set of clickable cells for each Anchor enum value and stores the current selection, calling an optional Setter action when a cell is picked.
@namespace Sunless.Libraries.UI
@using Sandbox
@using Sandbox.UI
@attribute [StyleSheet]
@inherits Panel
<root class="anchor-pad @ExtraClass">
@foreach ( var a in _all )
{
var current = a;
<div class="anchor-cell anchor-@(current.ToString().ToLowerInvariant()) @(Value == current ? "active" : "")"
@onclick=@(() => Pick( current ))></div>
}
</root>
@code
{
public enum Anchor { TopLeft, TopRight, BottomLeft, BottomRight, Center }
public Anchor Value { get; set; } = Anchor.Center;
public Action<Anchor> Setter { get; set; }
public string ExtraClass { get; set; }
static readonly Anchor[] _all = (Anchor[])Enum.GetValues( typeof( Anchor ) );
void Pick( Anchor a )
{
Value = a;
try { Setter?.Invoke( a ); } catch ( Exception e ) { Log.Warning( $"[AnchorPad] Setter threw: {e.Message}" ); }
}
protected override int BuildHash() => HashCode.Combine( Value, ExtraClass );
}