Skip to content

Commit 3ad2202

Browse files
committed
[Feature] ProgressBar - click and drag to set fillAmount #326
1 parent 505323e commit 3ad2202

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/ProgressBarPropertyDrawer.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ protected override void OnGUI_Internal(Rect rect, SerializedProperty property, G
4949
height = EditorGUIUtility.singleLineHeight
5050
};
5151

52+
HandleInput(barRect, property, maxValue);
5253
DrawBar(barRect, Mathf.Clamp01(fillPercentage), barLabel, barColor, labelColor);
5354
}
5455
else
@@ -63,6 +64,48 @@ protected override void OnGUI_Internal(Rect rect, SerializedProperty property, G
6364
EditorGUI.EndProperty();
6465
}
6566

67+
private void HandleInput(Rect rect, SerializedProperty property, object maxValue)
68+
{
69+
bool changed = false;
70+
float minValue = 0f;
71+
var value = property.propertyType == SerializedPropertyType.Integer ? property.intValue : property.floatValue;
72+
var controlId = GUIUtility.GetControlID(FocusType.Keyboard);
73+
74+
if (Event.current.type == EventType.MouseDown && Event.current.button == 0 && rect.Contains(Event.current.mousePosition) ||
75+
GUIUtility.hotControl == controlId && (Event.current.type == EventType.MouseMove || Event.current.type == EventType.MouseDrag))
76+
{
77+
// Update value based on mouse position.
78+
GUIUtility.hotControl = controlId;
79+
value = minValue + Mathf.Abs(CastToFloat(maxValue) - minValue) *
80+
Mathf.Clamp01((Event.current.mousePosition.x - rect.xMin) / rect.width);
81+
82+
changed = true;
83+
}
84+
else if (GUIUtility.hotControl == controlId && Event.current.rawType == EventType.MouseUp)
85+
{
86+
// Release hot control.
87+
GUIUtility.hotControl = 0;
88+
}
89+
90+
if (changed)
91+
{
92+
GUI.changed = true;
93+
94+
value = value <= minValue ? minValue : value >= CastToFloat(maxValue) ? CastToFloat(maxValue) : value;
95+
switch (property.propertyType)
96+
{
97+
case SerializedPropertyType.Integer:
98+
property.intValue = (int) value;
99+
break;
100+
case SerializedPropertyType.Float:
101+
property.floatValue = value;
102+
break;
103+
}
104+
105+
Event.current.Use();
106+
}
107+
}
108+
66109
private object GetMaxValue(SerializedProperty property, ProgressBarAttribute progressBarAttribute)
67110
{
68111
if (string.IsNullOrEmpty(progressBarAttribute.MaxValueName))

0 commit comments

Comments
 (0)