Skip to content

Commit

Permalink
[Feature] ProgressBar - click and drag to set fillAmount dbrizov#326
Browse files Browse the repository at this point in the history
  • Loading branch information
njuwuyuxin committed Sep 29, 2022
1 parent 505323e commit 3ad2202
Showing 1 changed file with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ protected override void OnGUI_Internal(Rect rect, SerializedProperty property, G
height = EditorGUIUtility.singleLineHeight
};

HandleInput(barRect, property, maxValue);
DrawBar(barRect, Mathf.Clamp01(fillPercentage), barLabel, barColor, labelColor);
}
else
Expand All @@ -63,6 +64,48 @@ protected override void OnGUI_Internal(Rect rect, SerializedProperty property, G
EditorGUI.EndProperty();
}

private void HandleInput(Rect rect, SerializedProperty property, object maxValue)
{
bool changed = false;
float minValue = 0f;
var value = property.propertyType == SerializedPropertyType.Integer ? property.intValue : property.floatValue;
var controlId = GUIUtility.GetControlID(FocusType.Keyboard);

if (Event.current.type == EventType.MouseDown && Event.current.button == 0 && rect.Contains(Event.current.mousePosition) ||
GUIUtility.hotControl == controlId && (Event.current.type == EventType.MouseMove || Event.current.type == EventType.MouseDrag))
{
// Update value based on mouse position.
GUIUtility.hotControl = controlId;
value = minValue + Mathf.Abs(CastToFloat(maxValue) - minValue) *
Mathf.Clamp01((Event.current.mousePosition.x - rect.xMin) / rect.width);

changed = true;
}
else if (GUIUtility.hotControl == controlId && Event.current.rawType == EventType.MouseUp)
{
// Release hot control.
GUIUtility.hotControl = 0;
}

if (changed)
{
GUI.changed = true;

value = value <= minValue ? minValue : value >= CastToFloat(maxValue) ? CastToFloat(maxValue) : value;
switch (property.propertyType)
{
case SerializedPropertyType.Integer:
property.intValue = (int) value;
break;
case SerializedPropertyType.Float:
property.floatValue = value;
break;
}

Event.current.Use();
}
}

private object GetMaxValue(SerializedProperty property, ProgressBarAttribute progressBarAttribute)
{
if (string.IsNullOrEmpty(progressBarAttribute.MaxValueName))
Expand Down

0 comments on commit 3ad2202

Please sign in to comment.