mirror of
https://github.com/Facepunch/sbox-public.git
synced 2026-04-18 05:17:53 -04:00
This commit imports the C# engine code and game files, excluding C++ source code. [Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
93 lines
2.1 KiB
Plaintext
93 lines
2.1 KiB
Plaintext
@using System
|
|
@namespace Sandbox.UI
|
|
@inherits Panel
|
|
|
|
<root>
|
|
|
|
<div class="background" onmousedown="@Close" onclick="@Close"></div>
|
|
|
|
<div class="inner">
|
|
|
|
@foreach ( var option in options )
|
|
{
|
|
if ( option.icon == "__spacer__" )
|
|
{
|
|
<div class="spacer"></div>
|
|
continue;
|
|
}
|
|
|
|
<div class="option" onclick=@( () => SelectOption( option ) )>
|
|
<div class="icon">@option.icon</div>
|
|
<div class="text">@option.text</div>
|
|
</div>
|
|
}
|
|
|
|
</div>
|
|
|
|
</root>
|
|
|
|
@code
|
|
{
|
|
record struct MenuOption( string icon, string text, Action action );
|
|
List<MenuOption> options = new List<MenuOption>();
|
|
|
|
public static MenuPanel Open( Panel source )
|
|
{
|
|
var root = source.FindRootPanel();
|
|
var menu = root.AddChild<MenuPanel>();
|
|
|
|
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<MenuPanel>() )
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
}
|