Skip to content

Commit 86ccf55

Browse files
committed
Implement ValueStep property for the ProgressBarAttribute
1 parent 1d68a42 commit 86ccf55

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

Assets/Editor Toolbox/Editor/Drawers/Regular/ProgressBarAttributeDrawer.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,18 @@ private void SetProgressValue(SerializedProperty property, Rect progressBarRect,
4343
{
4444
var minValue = Attribute.MinValue;
4545
var maxValue = Attribute.MaxValue;
46+
var valueStep = Attribute.ValueStep;
4647

4748
var range = progressBarRect.xMax - progressBarRect.xMin;
4849
xPosition = Mathf.Clamp(xPosition - progressBarRect.xMin, 0, range);
4950

5051
var fill = Mathf.Clamp01(xPosition / range);
5152
var newValue = (maxValue - minValue) * fill + minValue;
53+
if (!Mathf.Approximately(valueStep, 0.0f))
54+
{
55+
newValue = Mathf.Round(newValue / valueStep) * valueStep;
56+
newValue = Mathf.Clamp(newValue, minValue, maxValue);
57+
}
5258

5359
switch (property.propertyType)
5460
{

Assets/Editor Toolbox/Runtime/Attributes/Property/Regular/ProgressBarAttribute.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ public ProgressBarAttribute(string name = "", float minValue = 0, float maxValue
2626

2727
public float MaxValue { get; private set; }
2828

29+
/// <summary>
30+
/// Indicates value change step if <see cref="ProgressBarAttribute"/> is interactable.
31+
/// </summary>
32+
public float ValueStep { get; set; } = 0.001f;
33+
34+
public bool IsInteractable { get; set; }
35+
2936
public Color Color
3037
{
3138
get => ColorUtility.TryParseHtmlString(HexColor, out var color)
@@ -34,7 +41,5 @@ public Color Color
3441
}
3542

3643
public string HexColor { get; set; }
37-
38-
public bool IsInteractable { get; set; }
3944
}
4045
}

Assets/Examples/Scenes/SampleScene.unity

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1287,7 +1287,7 @@ MonoBehaviour:
12871287
m_Name:
12881288
m_EditorClassIdentifier:
12891289
targetTag: Untagged
1290-
progressBar1: 9.159664
1290+
progressBar1: 30
12911291
progressBar2: 25.4
12921292
minMaxVector: {x: 10, y: 73.893524}
12931293
minMaxVectorInt: {x: 2, y: 7}

Assets/Examples/Scripts/SampleBehaviour1.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class SampleBehaviour1 : MonoBehaviour
1111

1212
[Label("Progress Bar", skinStyle: SkinStyle.Box)]
1313

14-
[ProgressBar(minValue: -10.0f, maxValue: 50.0f, HexColor = "#234DEA", IsInteractable = true)]
14+
[ProgressBar(minValue: -10.0f, maxValue: 50.0f, HexColor = "#234DEA", IsInteractable = true, ValueStep = 2.5f)]
1515
public float progressBar1 = 25.4f;
1616
[ProgressBar(minValue: -10.0f, maxValue: 50.0f, HexColor = "#32A852", IsInteractable = false)]
1717
public float progressBar2 = 25.4f;

0 commit comments

Comments
 (0)