Files
sbox-public/game/addons/tools/Code/Scene/SceneView/ViewportTools.Tools.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

103 lines
2.4 KiB
C#

namespace Editor;
partial class ViewportTools
{
private void BuildToolbarLeft( Layout layout )
{
var button = layout.Add( new ToolsModeButton() );
button.FixedHeight = Theme.ControlHeight;
}
}
internal class ToolsModeButton : Button
{
private IGrouping<string, (TypeDescription Type, EditorToolAttribute Attribute)>[] _toolGroups;
public ToolsModeButton() : base( null )
{
SetStyles( $"padding-left: 32px; padding-right: 32px; font-family: '{Theme.DefaultFont}'; padding-top: 6px; padding-bottom: 6px;" );
FixedWidth = 210;
FixedHeight = Theme.RowHeight + 6;
InitializeToolGroups();
UpdateButtonText();
Clicked = Click;
}
private void InitializeToolGroups()
{
_toolGroups = EditorTypeLibrary.GetTypesWithAttribute<EditorToolAttribute>()
.GroupBy( x => string.IsNullOrEmpty( x.Type.Group ) ? "aaa" : x.Type.Group )
.OrderBy( x => x.Key )
.ToArray();
}
private void UpdateButtonText()
{
foreach ( var group in _toolGroups )
{
foreach ( var type in group.OrderBy( x => x.Type.Name ) )
{
if ( EditorToolManager.CurrentModeName != type.Type.Name )
continue;
Text = type.Type.Title;
Icon = type.Type.Icon;
return;
}
}
}
private void Click()
{
var menu = new ContextMenu();
foreach ( var group in _toolGroups )
{
foreach ( var type in group.OrderBy( x => x.Type.Name ) )
{
var attr = type.Type.GetAttribute<EditorToolAttribute>();
if ( attr.Hidden )
continue;
var option = new Option();
option.Text = type.Type.Title;
option.ShortcutName = attr.Shortcut;
option.Icon = type.Type.Icon;
option.Triggered = () =>
{
EditorToolManager.CurrentModeName = type.Type.Name;
UpdateButtonText();
};
menu.AddOption( option );
}
}
menu.OpenAt( ScreenRect.BottomLeft, false );
}
[EditorEvent.Frame]
public void Frame()
{
UpdateButtonText();
}
protected override void OnPaint()
{
Paint.Antialiasing = true;
Paint.ClearPen();
Paint.SetBrush( Theme.ControlBackground );
Paint.DrawRect( LocalRect, Theme.ControlRadius );
var fg = Theme.Text;
Paint.SetDefaultFont();
Paint.SetPen( fg.WithAlphaMultiplied( Paint.HasMouseOver ? 1.0f : 0.9f ) );
Paint.DrawIcon( LocalRect.Shrink( 8, 0, 0, 0 ), Icon, 14, TextFlag.LeftCenter );
Paint.DrawText( LocalRect.Shrink( 32, 0, 0, 0 ), Text, TextFlag.LeftCenter );
Paint.DrawIcon( LocalRect.Shrink( 4, 0 ), "arrow_drop_down", 18, TextFlag.RightCenter );
}
}