Skip to content

Commit 54d8618

Browse files
committed
add support for nonlinear float sliders, color picker range slider is now nonlinear for finer control
1 parent 158fc3e commit 54d8618

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

engine/Sandbox.Tools/ControlWidget/FloatControlWidget.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ public class FloatControlWidget : StringControlWidget
1111
public string Icon { get; set; }
1212
public string Label { get; set; }
1313
public Action<Rect, float> SliderPaint { get; set; }
14+
public float SliderCurve {
15+
get => SliderWidget?.Curve ?? 1f;
16+
set => SliderWidget?.Curve = value;
17+
}
1418

1519
/// <summary>
1620
/// If true we can draw a slider
@@ -277,6 +281,7 @@ public class FloatSlider : Widget
277281
{
278282
public float Minimum { get; set; }
279283
public float Maximum { get; set; }
284+
public float Curve { get; set; } = 1f;
280285
public Action OnValueEdited { get; set; }
281286
public Color HighlightColor { get; set; } = Theme.TextLight;
282287
public Action<Rect, float> SliderPaint { get; set; }
@@ -304,12 +309,23 @@ public float DeltaValue
304309
{
305310
get
306311
{
307-
return MathX.LerpInverse( Value, Minimum, Maximum, true );
312+
var v = Value;
313+
if ( Curve != 1 )
314+
{
315+
var range = Maximum - Minimum;
316+
v = MathF.Pow( v / range, 1f / Curve ) * range;
317+
}
318+
return MathX.LerpInverse( v, Minimum, Maximum, true );
308319
}
309320

310321
set
311322
{
312323
var v = MathX.LerpTo( Minimum, Maximum, value, true );
324+
if ( Curve != 1 )
325+
{
326+
var range = Maximum - Minimum;
327+
v = MathF.Pow( v / range, Curve ) * range;
328+
}
313329
Value = v;
314330
}
315331

game/addons/tools/Code/Widgets/ColorPicker/ColorPicker.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public Color ValueWithoutRange
150150

151151
var hueSlider = new HsvFloatControlWidget( so.GetProperty( nameof( Hue ) ), HueSliderPaint );
152152
AlphaSlider = new HsvFloatControlWidget( so.GetProperty( nameof( Alpha ) ), AlphaSliderPaint );
153-
RangeSlider = new HsvFloatControlWidget( so.GetProperty( nameof( Range ) ), RangeSliderPaint );
153+
RangeSlider = new HsvFloatControlWidget( so.GetProperty( nameof( Range ) ), RangeSliderPaint, 3 );
154154

155155
so.OnPropertyStartEdit += ( p ) => EditingStarted?.Invoke();
156156
so.OnPropertyFinishEdit += ( p ) => EditingFinished?.Invoke();
@@ -382,10 +382,11 @@ class HsvFloatControlWidget : FloatControlWidget
382382
public override bool IncludeLabel => false;
383383
public override bool IsWideMode => true;
384384

385-
public HsvFloatControlWidget( SerializedProperty property, Action<Rect, float> sliderPaint ) : base( property )
385+
public HsvFloatControlWidget( SerializedProperty property, Action<Rect, float> sliderPaint, float curve = 1f ) : base( property )
386386
{
387387
Label = null;
388388
Icon = "multiple_stop";
389389
SliderPaint = sliderPaint;
390+
SliderCurve = curve;
390391
}
391392
}

0 commit comments

Comments
 (0)