Skip to content

Commit 977a05d

Browse files
committed
Possibility to interact with ProgressBars (optionally)
1 parent 7d7cb55 commit 977a05d

File tree

5 files changed

+73
-9
lines changed

5 files changed

+73
-9
lines changed

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

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,61 @@ namespace Toolbox.Editor.Drawers
66
[CustomPropertyDrawer(typeof(ProgressBarAttribute))]
77
public class ProgressBarAttributeDrawer : PropertyDrawerBase
88
{
9+
private static readonly int drawerHash = nameof(ProgressBarAttributeDrawer).GetHashCode();
10+
11+
private void HandleGuiEvents(SerializedProperty property, Rect progressBarRect)
12+
{
13+
var mousePosition = Event.current.mousePosition;
14+
var id = GUIUtility.GetControlID(drawerHash, FocusType.Passive, progressBarRect);
15+
switch (Event.current.GetTypeForControl(id))
16+
{
17+
case EventType.MouseDown:
18+
if (progressBarRect.Contains(mousePosition))
19+
{
20+
GUIUtility.hotControl = id;
21+
SetProgressValue(property, progressBarRect, mousePosition.x);
22+
Event.current.Use();
23+
}
24+
break;
25+
case EventType.MouseDrag:
26+
if (GUIUtility.hotControl == id)
27+
{
28+
SetProgressValue(property, progressBarRect, mousePosition.x);
29+
Event.current.Use();
30+
}
31+
break;
32+
case EventType.MouseUp:
33+
if (GUIUtility.hotControl == id)
34+
{
35+
GUIUtility.hotControl = 0;
36+
Event.current.Use();
37+
}
38+
break;
39+
}
40+
}
41+
42+
private void SetProgressValue(SerializedProperty property, Rect progressBarRect, float xPosition)
43+
{
44+
var minValue = Attribute.MinValue;
45+
var maxValue = Attribute.MaxValue;
46+
47+
var range = progressBarRect.xMax - progressBarRect.xMin;
48+
xPosition = Mathf.Clamp(xPosition - progressBarRect.xMin, 0, range);
49+
50+
var fill = Mathf.Clamp01(xPosition / range);
51+
var newValue = (maxValue - minValue) * fill + minValue;
52+
53+
switch (property.propertyType)
54+
{
55+
case SerializedPropertyType.Integer:
56+
property.intValue = Mathf.RoundToInt(newValue);
57+
break;
58+
case SerializedPropertyType.Float:
59+
property.floatValue = newValue;
60+
break;
61+
}
62+
}
63+
964
protected override float GetPropertyHeightSafe(SerializedProperty property, GUIContent label)
1065
{
1166
return Style.barHeight;
@@ -51,19 +106,23 @@ protected override void OnGUISafe(Rect position, SerializedProperty property, GU
51106
position.y -= Style.textOffset;
52107
//finally draw the progress bar label
53108
EditorGUI.DropShadowLabel(position, labelText);
54-
}
55109

110+
if (!attribute.IsInteractable)
111+
{
112+
return;
113+
}
114+
115+
HandleGuiEvents(property, position);
116+
}
56117

57118
public override bool IsPropertyValid(SerializedProperty property)
58119
{
59120
return property.propertyType == SerializedPropertyType.Float ||
60121
property.propertyType == SerializedPropertyType.Integer;
61122
}
62123

63-
64124
private ProgressBarAttribute Attribute => attribute as ProgressBarAttribute;
65125

66-
67126
private static class Style
68127
{
69128
internal static readonly float rowHeight = EditorGUIUtility.singleLineHeight;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,7 @@ public Color Color
3434
}
3535

3636
public string HexColor { get; set; }
37+
38+
public bool IsInteractable { get; set; }
3739
}
3840
}

Assets/Examples/Scenes/SampleScene.unity

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,9 +1008,10 @@ MonoBehaviour:
10081008
m_Name:
10091009
m_EditorClassIdentifier:
10101010
targetTag: Untagged
1011-
progressBar: 25.4
1012-
minMaxVector: {x: 0, y: 0}
1013-
minMaxVectorInt: {x: 0, y: 0}
1011+
progressBar1: 9.159664
1012+
progressBar2: 25.4
1013+
minMaxVector: {x: 10, y: 73.893524}
1014+
minMaxVectorInt: {x: 2, y: 7}
10141015
var8: {fileID: 977748987}
10151016
preview: {fileID: 5059060190599569102, guid: 5573ca52cac7c2d4cb2536e37e9be1f1, type: 3}
10161017
var10: 2.16

Assets/Examples/Scripts/SampleBehaviour1.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ public class SampleBehaviour1 : MonoBehaviour
1111

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

14-
[ProgressBar(minValue: -10.0f, maxValue: 50.0f, HexColor = "#234DEA")]
15-
public float progressBar = 25.4f;
14+
[ProgressBar(minValue: -10.0f, maxValue: 50.0f, HexColor = "#234DEA", IsInteractable = true)]
15+
public float progressBar1 = 25.4f;
16+
[ProgressBar(minValue: -10.0f, maxValue: 50.0f, HexColor = "#32A852", IsInteractable = false)]
17+
public float progressBar2 = 25.4f;
1618

1719
[Label("MinMax Slider", skinStyle: SkinStyle.Box)]
1820

ProjectSettings/ProjectSettings.asset

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ PlayerSettings:
153153
androidSupportedAspectRatio: 1
154154
androidMaxAspectRatio: 2.1
155155
applicationIdentifier:
156-
Standalone: com.BroWarCollective.EditorToolbox
156+
Standalone: com.BroWar-Collective.Editor-Toolbox
157157
buildNumber:
158158
Standalone: 0
159159
iPhone: 0

0 commit comments

Comments
 (0)