Add ColorControl

This commit is contained in:
Garry Newman
2025-11-24 13:53:23 +00:00
committed by Matt Stevens
parent 1595f035d2
commit ab1ae86f88
3 changed files with 82 additions and 1 deletions

View File

@@ -0,0 +1,38 @@
namespace Sandbox.UI;
/// <summary>
/// A control for editing Color properties. Displays a text entry that can be edited, and a color swatch which pops up a mixer.
/// </summary>
[CustomEditor( typeof( Color ) )]
public partial class ColorControl : BaseControl
{
readonly TextEntry _textEntry;
readonly Panel _colorSwatch;
public ColorControl()
{
_colorSwatch = AddChild<Panel>( "colorswatch" );
_textEntry = AddChild<TextEntry>( "textentry" );
_textEntry.OnTextEdited = OnTextEntryChanged;
}
public override void Rebuild()
{
if ( Property == null ) return;
_textEntry.Value = Property.GetValue<Color>().Hex;
}
public override void Tick()
{
base.Tick();
_colorSwatch.Style.BackgroundColor = Property.GetValue<Color>();
}
void OnTextEntryChanged( string value )
{
Property.SetValue( value );
}
}

View File

@@ -0,0 +1,43 @@
ColorControl
{
gap: 0.5rem;
flex-grow: 1;
pointer-events: all;
background-color: #000a;
border-radius: 4px;
padding: 2px;
height: 32px;
}
ColorControl TextEntry
{
flex-grow: 1;
flex-shrink: 0;
color: #aaa;
font-size: 1.2rem;
&:hover, &:focus
{
color: #ddd;
.icon
{
color: #3af;
}
}
&:active
{
color: white;
}
}
ColorControl > .colorswatch
{
aspect-ratio: 1;
border-radius: 4px;
height: 100%;
flex-shrink: 0;
cursor: pointer;
border: 2px solid #000;
}

View File

@@ -1,7 +1,7 @@
namespace Sandbox.UI;
/// <summary>
/// Like TextEntry, except just for numbers
/// A control for editing enum properties. Can either display a dropdown or a button group depending on the number of options.
/// </summary>
[CustomEditor( typeof( Enum ) )]
public partial class EnumControl : BaseControl