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

92 lines
1.6 KiB
C#

namespace Editor;
internal class EditorToolButton : Widget
{
public Action Action { get; set; }
public Func<bool> IsActive { get; set; }
public Func<string> GetIcon { get; set; } = null!;
public new bool Enabled
{
get => base.Enabled;
set
{
base.Enabled = value;
Cursor = value ? CursorShape.Finger : CursorShape.Arrow;
}
}
public Color Color { get; set; } = Theme.TextLight;
public EditorToolButton()
{
Cursor = CursorShape.Finger;
MinimumWidth = Theme.RowHeight;
}
protected override Vector2 SizeHint()
{
return new Vector2( Theme.ControlHeight );
}
protected override void OnDoubleClick( MouseEvent e )
{
// e.Accepted = false;
}
protected override void OnMousePress( MouseEvent e )
{
if ( !Enabled )
return;
if ( e.LeftMouseButton )
{
Action?.Invoke();
e.Accepted = true;
}
}
protected override void OnPaint()
{
Paint.Antialiasing = true;
Paint.TextAntialiasing = true;
bool active = IsActive?.Invoke() ?? false;
if ( active )
{
Paint.ClearPen();
Paint.SetBrush( Theme.Blue );
Paint.DrawRect( LocalRect, Theme.ControlRadius );
Paint.Pen = Theme.Text;
}
else
{
Paint.ClearPen();
Paint.SetPen( Color );
if ( Paint.HasMouseOver )
{
Paint.SetPen( Color.Lighten( 0.8f ) );
}
}
if ( !Enabled )
{
Paint.SetPen( Color.Darken( 0.2f ).WithAlpha( 0.5f ) );
}
Paint.DrawIcon( LocalRect, GetIcon(), HeaderBarStyle.IconSize, TextFlag.Center );
}
public void UpdateState()
{
if ( IsActive is null )
return;
SetContentHash( HashCode.Combine( IsActive() ), 0.1f );
}
}