Files
sbox-public/game/addons/tools/Code/NodeGraph/Extensions.cs
s&box team 71f266059a Open source release
This commit imports the C# engine code and game files, excluding C++ source code.

[Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
2025-11-24 09:05:18 +00:00

68 lines
1.2 KiB
C#

using System;
namespace Editor.NodeEditor;
public static class MenuExtensions
{
public static LineEdit AddLineEdit( this Menu menu, string label,
string value = null, string placeholder = null, bool autoFocus = false,
Action<string> onChange = null, Action<string> onSubmit = null )
{
var w = new Widget( menu );
w.Layout = Layout.Row();
w.Layout.Margin = 6;
w.Layout.Spacing = 4;
var lineEdit = new MenuLineEdit( w );
lineEdit.PlaceholderText = placeholder ?? $"Enter {label}..";
lineEdit.Text = value ?? "";
if ( onChange is not null )
{
lineEdit.TextChanged += onChange;
}
if ( onSubmit is not null )
{
var firstTime = true;
lineEdit.ReturnPressed += () =>
{
if ( !firstTime ) return;
firstTime = false;
onSubmit( lineEdit.Value );
menu.RootMenu.Close();
};
}
w.Layout.Add( new Label( $"{label}:", w ) );
w.Layout.Add( lineEdit );
menu.AddWidget( w );
if ( autoFocus )
{
lineEdit.Focus();
}
return lineEdit;
}
}
file class MenuLineEdit : LineEdit
{
public MenuLineEdit( Widget parent ) : base( parent )
{
}
// Stops the context menu from closing!!
protected override void OnMouseReleased( MouseEvent e )
{
base.OnMouseReleased( e );
e.Accepted = true;
}
}