Skip to content

Commit 1354dd8

Browse files
committed
Add VolumetricFogVolumeTool editor tool to edit volume bounds
1 parent b3243e8 commit 1354dd8

File tree

21 files changed

+995
-293
lines changed

21 files changed

+995
-293
lines changed

engine/Sandbox.Engine/Systems/UI/Controls/Label.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ public bool ShouldDrawSelection
4848
[Category( "Selection" )]
4949
public bool Selectable { get; set; } = true;
5050

51+
/// <summary>
52+
/// If true and the text starts with #, it will be treated as a language token.
53+
/// </summary>
54+
public bool Tokenize { get; set; } = true;
55+
5156
[Hide]
5257
public int SelectionStart
5358
{
@@ -150,7 +155,7 @@ public virtual string Text
150155
{
151156
value ??= "";
152157

153-
if ( value != null && value.Length > 1 && value[0] == '#' )
158+
if ( Tokenize && value != null && value.Length > 1 && value[0] == '#' )
154159
{
155160
if ( _textToken == value ) return;
156161
_textToken = value;
@@ -417,6 +422,7 @@ Vector2 ScreenPositionToTextRectPosition( Vector2 pos )
417422
public override void LanguageChanged()
418423
{
419424
if ( _textToken == null ) return;
425+
if ( !Tokenize ) return;
420426

421427
var token = _textToken;
422428
_textToken = null; // skip cache

game/addons/base/code/UI/ButtonGroup.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class ButtonGroup : Panel
1414
/// <summary>
1515
/// Called when the value has been changed.
1616
/// </summary>
17-
[Parameter] public System.Action<string> ValueChanged { get; set; }
17+
[Parameter] public System.Action<object> ValueChanged { get; set; }
1818

1919
/// <summary>
2020
/// The selected option value.
@@ -30,9 +30,9 @@ public object Value
3030

3131
_value = value;
3232

33-
ValueChanged?.Invoke( $"{Value}" );
33+
ValueChanged?.Invoke( _value );
3434
CreateEvent( "onchange" );
35-
CreateValueEvent( "value", value );
35+
CreateValueEvent( "value", _value );
3636
SetSelectedButton();
3737
}
3838
}
@@ -49,6 +49,13 @@ public object Value
4949
[Parameter]
5050
public string ButtonClass { get; set; } = "";
5151

52+
53+
public ButtonGroup()
54+
{
55+
56+
}
57+
58+
5259
/// <summary>
5360
/// Adds a button to this group.
5461
/// </summary>

game/addons/base/code/UI/ControlSheet/ControlSheetRow.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace Sandbox.UI;
66
/// </summary>
77
public class ControlSheetRow : Panel
88
{
9+
[Parameter]
910
public SerializedProperty Property { get; set; }
1011

1112
Panel _left;
@@ -22,9 +23,20 @@ public ControlSheetRow()
2223

2324
internal void Initialize( SerializedProperty prop )
2425
{
25-
_title.Text = prop.DisplayName;
26+
Property = prop;
27+
}
28+
29+
protected override void OnParametersSet()
30+
{
31+
if ( Property is null )
32+
return;
33+
34+
_title.Text = Property?.DisplayName;
35+
36+
_right.DeleteChildren();
2637

27-
var c = BaseControl.CreateFor( prop );
38+
var c = BaseControl.CreateFor( Property );
39+
if ( c is null ) return;
2840
_right.AddChild( c );
2941
}
3042
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
namespace Sandbox.UI;
2+
3+
/// <summary>
4+
/// A control for editing Color properties. Displays a text entry that can be edited, and a color swatch which pops up a mixer.
5+
/// </summary>
6+
public partial class ColorAlphaControl : BaseControl
7+
{
8+
readonly Panel _handle;
9+
10+
public ColorAlphaControl()
11+
{
12+
_handle = AddChild<Panel>( "handle" );
13+
}
14+
15+
public override void Rebuild()
16+
{
17+
if ( Property == null ) return;
18+
}
19+
20+
public override void Tick()
21+
{
22+
base.Tick();
23+
24+
UpdateFromColor();
25+
}
26+
27+
void UpdateFromColor()
28+
{
29+
var color = Property.GetValue<Color>();
30+
_handle.Style.Left = Length.Percent( color.a * 100f );
31+
}
32+
33+
protected override void OnMouseDown( MousePanelEvent e )
34+
{
35+
base.OnMouseDown( e );
36+
37+
UpdateFromPosition( e.LocalPosition );
38+
}
39+
40+
protected override void OnMouseMove( MousePanelEvent e )
41+
{
42+
base.OnMouseMove( e );
43+
44+
if ( !PseudoClass.HasFlag( PseudoClass.Active ) )
45+
return;
46+
47+
UpdateFromPosition( e.LocalPosition );
48+
}
49+
50+
private void UpdateFromPosition( Vector2 localPosition )
51+
{
52+
// Get the bounds of the control
53+
var bounds = Box.Rect;
54+
if ( bounds.Width <= 0 || bounds.Height <= 0 ) return;
55+
56+
// Clamp position within bounds
57+
float x = Math.Clamp( localPosition.x, 0, bounds.Width );
58+
59+
// Calculate saturation and value from position
60+
var alpha = (x / bounds.Width);
61+
62+
var color = Property.GetValue<Color>();
63+
64+
// Create new color with updated saturation and value
65+
var newColor = color with { a = alpha };
66+
67+
// Set the property to the new color
68+
Property.SetValue( newColor );
69+
70+
UpdateFromColor();
71+
}
72+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
ColorAlphaControl
2+
{
3+
gap: 0.5rem;
4+
flex-grow: 1;
5+
pointer-events: all;
6+
background: linear-gradient( to right, black, white );
7+
border-radius: 4px;
8+
padding: 2px;
9+
height: 12px;
10+
position: relative;
11+
cursor: pointer;
12+
border: 1px solid #333;
13+
14+
&:hover
15+
{
16+
border: 1px solid #08f;
17+
}
18+
19+
&:active
20+
{
21+
border: 1px solid #fff;
22+
}
23+
24+
.handle
25+
{
26+
top: -5px;
27+
bottom: -5px;
28+
aspect-ratio: 1;
29+
border-radius: 100px;
30+
border: 2px solid #444;
31+
position: absolute;
32+
background-color: white;
33+
box-shadow: 2px 2px 16px #000a;
34+
transform: translateX( -50% );
35+
pointer-events: none;
36+
}
37+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
namespace Sandbox.UI;
2+
3+
/// <summary>
4+
/// A control for editing Color properties. Displays a text entry that can be edited, and a color swatch which pops up a mixer.
5+
/// </summary>
6+
[CustomEditor( typeof( Color ) )]
7+
public partial class ColorControl : BaseControl
8+
{
9+
readonly TextEntry _textEntry;
10+
readonly Panel _colorSwatch;
11+
12+
public ColorControl()
13+
{
14+
_colorSwatch = AddChild<Panel>( "colorswatch" );
15+
_colorSwatch.AddEventListener( "onmousedown", OpenPopup );
16+
17+
_textEntry = AddChild<TextEntry>( "textentry" );
18+
_textEntry.OnTextEdited = OnTextEntryChanged;
19+
}
20+
21+
public override void Rebuild()
22+
{
23+
if ( Property == null ) return;
24+
25+
_textEntry.Value = Property.GetValue<Color>().Hex;
26+
}
27+
28+
public override void Tick()
29+
{
30+
base.Tick();
31+
32+
_colorSwatch.Style.BackgroundColor = Property.GetValue<Color>();
33+
}
34+
35+
void OnTextEntryChanged( string value )
36+
{
37+
Property.SetValue( value );
38+
}
39+
40+
void OpenPopup()
41+
{
42+
var popup = new Popup( _colorSwatch, Popup.PositionMode.BelowLeft, 0 );
43+
44+
var picker = popup.AddChild<ColorPickerControl>();
45+
picker.Property = Property;
46+
}
47+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
ColorControl
2+
{
3+
gap: 0.5rem;
4+
flex-grow: 1;
5+
pointer-events: all;
6+
background-color: #000a;
7+
border-radius: 4px;
8+
padding: 2px;
9+
height: 32px;
10+
}
11+
12+
ColorControl TextEntry
13+
{
14+
flex-grow: 1;
15+
flex-shrink: 0;
16+
color: #aaa;
17+
font-size: 1.2rem;
18+
19+
&:hover, &:focus
20+
{
21+
color: #ddd;
22+
23+
.icon
24+
{
25+
color: #3af;
26+
}
27+
}
28+
29+
&:active
30+
{
31+
color: white;
32+
}
33+
}
34+
35+
ColorControl > .colorswatch
36+
{
37+
aspect-ratio: 1;
38+
border-radius: 4px;
39+
height: 100%;
40+
flex-shrink: 0;
41+
cursor: pointer;
42+
border: 2px solid #000;
43+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
namespace Sandbox.UI;
2+
3+
/// <summary>
4+
/// A control for editing Color properties. Displays a text entry that can be edited, and a color swatch which pops up a mixer.
5+
/// </summary>
6+
public partial class ColorHueControl : BaseControl
7+
{
8+
readonly Panel _handle;
9+
10+
float _hue = 0;
11+
12+
public ColorHueControl()
13+
{
14+
_handle = AddChild<Panel>( "handle" );
15+
}
16+
17+
public override void Rebuild()
18+
{
19+
if ( Property == null ) return;
20+
}
21+
22+
public override void Tick()
23+
{
24+
base.Tick();
25+
26+
UpdateFromColor();
27+
}
28+
29+
void UpdateFromColor()
30+
{
31+
var color = Property.GetValue<Color>();
32+
var hsv = color.ToHsv();
33+
34+
if ( hsv.Saturation > 0.05f && hsv.Value > 0.05f )
35+
{
36+
_hue = hsv.Hue;
37+
}
38+
39+
_handle.Style.Left = Length.Percent( (_hue / 360.0f) * 100f );
40+
}
41+
42+
protected override void OnMouseDown( MousePanelEvent e )
43+
{
44+
base.OnMouseDown( e );
45+
46+
UpdateFromPosition( e.LocalPosition );
47+
}
48+
49+
protected override void OnMouseMove( MousePanelEvent e )
50+
{
51+
base.OnMouseMove( e );
52+
53+
if ( !PseudoClass.HasFlag( PseudoClass.Active ) )
54+
return;
55+
56+
UpdateFromPosition( e.LocalPosition );
57+
}
58+
59+
private void UpdateFromPosition( Vector2 localPosition )
60+
{
61+
// Get the bounds of the control
62+
var bounds = Box.Rect;
63+
if ( bounds.Width <= 0 || bounds.Height <= 0 ) return;
64+
65+
// Clamp position within bounds
66+
float x = Math.Clamp( localPosition.x, 0, bounds.Width );
67+
68+
// Calculate saturation and value from position
69+
_hue = (x / bounds.Width) * 360.0f;
70+
_hue = _hue.Clamp( 0, 360.0f - 0.001f );
71+
72+
var color = Property.GetValue<Color>().ToHsv();
73+
74+
// Create new color with updated saturation and value
75+
var newColor = color with { Hue = _hue };
76+
77+
// Set the property to the new color
78+
Property.SetValue( newColor.ToColor() );
79+
80+
UpdateFromColor();
81+
}
82+
83+
}

0 commit comments

Comments
 (0)