Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] ProgressBar - click and drag to set fillAmount #326 #339

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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