@using System @namespace Sandbox.UI @inherits Panel
@foreach ( var option in options ) { if ( option.icon == "__spacer__" ) {
continue; }
SelectOption( option ) )>
@option.icon
@option.text
}
@code { record struct MenuOption( string icon, string text, Action action ); List options = new List(); public static MenuPanel Open( Panel source ) { var root = source.FindRootPanel(); var menu = root.AddChild(); menu.Style.Left = root.MousePosition.x * root.ScaleFromScreen; menu.Style.Top = root.MousePosition.y * root.ScaleFromScreen; // Todo - constrain to screen menu.PlaySound( "sounds/kenney/ui/ui.navigate.forward.sound" ); return menu; } public void AddOption( string icon, string text, Action action ) { options.Add( new( icon, text, action ) ); } public void AddSpacer() { options.Add( new( "__spacer__", "", null ) ); } public void Close() { Panel root = FindRootPanel(); foreach (var c in root.Children.OfType() ) { c.Delete(false); } } void SelectOption( MenuOption option ) { Close(); option.action?.Invoke(); } public override void OnLayout(ref Rect layoutRect) { var padding = 10; var h = Screen.Height - padding; var w = Screen.Width - padding; if (layoutRect.Bottom > h) { layoutRect.Top -= layoutRect.Bottom - h; layoutRect.Bottom -= layoutRect.Bottom - h; } if (layoutRect.Right > w) { layoutRect.Left -= layoutRect.Right - w; layoutRect.Right -= layoutRect.Right - w; } } }