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

High & Low Values for the Progress Bar #333

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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 @@ -9,19 +9,74 @@ public class ProgressBarAttribute : DrawerAttribute
public float MaxValue { get; set; }
public string MaxValueName { get; private set; }
public EColor Color { get; private set; }
public EColor HighColor { get; private set;}
public object HighValue { get; set; }
public EColor LowColor { get; private set;}
public object LowValue { get; set; }

public ProgressBarAttribute(string name, float maxValue, EColor color, object highValue, EColor highColor,
object lowValue, EColor lowColor)
{
Name = name;
MaxValue = maxValue;
Color = color;

HighValue = highValue;
HighColor = highColor;
LowValue = lowValue;
LowColor = lowColor;
}
public ProgressBarAttribute(string name, string maxValueName, EColor color, object highValue, EColor highColor,
object lowValue, EColor lowColor)
{
Name = name;
MaxValueName = maxValueName;
Color = color;

HighValue = highValue;
HighColor = highColor;
LowValue = lowValue;
LowColor = lowColor;
}

public ProgressBarAttribute(string name, string maxValueName, EColor color, object highValue, EColor highColor)
{
Name = name;
MaxValueName = maxValueName;
Color = color;

HighValue = highValue;
HighColor = highColor;
}

public ProgressBarAttribute(string name, float maxValue, EColor color, object highValue, EColor highColor)
{
Name = name;
MaxValue = maxValue;
Color = color;

HighValue = highValue;
HighColor = highColor;
}

public ProgressBarAttribute(string name, float maxValue, EColor color = EColor.Blue)
{
Name = name;
MaxValue = maxValue;
Color = color;

HighColor = color;
LowColor = color;
}

public ProgressBarAttribute(string name, string maxValueName, EColor color = EColor.Blue)
{
Name = name;
MaxValueName = maxValueName;
Color = color;

HighColor = color;
LowColor = color;
}

public ProgressBarAttribute(float maxValue, EColor color = EColor.Blue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected override void OnGUI_Internal(Rect rect, SerializedProperty property, G
{
var fillPercentage = value / CastToFloat(maxValue);
var barLabel = (!string.IsNullOrEmpty(progressBarAttribute.Name) ? "[" + progressBarAttribute.Name + "] " : "") + valueFormatted + "/" + maxValue;
var barColor = progressBarAttribute.Color.GetColor();
var barColor = GetColor(value, property, progressBarAttribute);
var labelColor = Color.white;

var indentLength = NaughtyEditorGUI.GetIndentLength(rect);
Expand All @@ -63,38 +63,81 @@ protected override void OnGUI_Internal(Rect rect, SerializedProperty property, G
EditorGUI.EndProperty();
}

private object GetMaxValue(SerializedProperty property, ProgressBarAttribute progressBarAttribute)
private Color GetColor(float val,SerializedProperty property, ProgressBarAttribute progressBarAttribute)
{
if (string.IsNullOrEmpty(progressBarAttribute.MaxValueName))
var color = progressBarAttribute.Color.GetColor();
var highColor = progressBarAttribute.HighColor.GetColor();
var lowColor = progressBarAttribute.LowColor.GetColor();

var highColorPresent = highColor != color;
var lowColorPresent = lowColor != color;

if (highColor != color)
{
return progressBarAttribute.MaxValue;
var highVal = GetHighValue(property, progressBarAttribute);
if (highVal != null && IsNumber(highVal) && val >= CastToFloat(highVal))
return highColor;
}
else

if (lowColor != color)
{
object target = PropertyUtility.GetTargetObjectWithProperty(property);
var lowVal = GetLowValue(property, progressBarAttribute);
if (lowVal != null && IsNumber(lowVal) && val <= CastToFloat(lowVal))
return lowColor;
}

return color;
}

FieldInfo valuesFieldInfo = ReflectionUtility.GetField(target, progressBarAttribute.MaxValueName);
if (valuesFieldInfo != null)
{
return valuesFieldInfo.GetValue(target);
}
private object GetNumberValueFromProperty(string name, SerializedProperty property)
{
object target = PropertyUtility.GetTargetObjectWithProperty(property);

PropertyInfo valuesPropertyInfo = ReflectionUtility.GetProperty(target, progressBarAttribute.MaxValueName);
if (valuesPropertyInfo != null)
{
return valuesPropertyInfo.GetValue(target);
}
FieldInfo valuesFieldInfo = ReflectionUtility.GetField(target, name);
if (valuesFieldInfo != null)
{
return valuesFieldInfo.GetValue(target);
}

MethodInfo methodValuesInfo = ReflectionUtility.GetMethod(target, progressBarAttribute.MaxValueName);
if (methodValuesInfo != null &&
(methodValuesInfo.ReturnType == typeof(float) || methodValuesInfo.ReturnType == typeof(int)) &&
methodValuesInfo.GetParameters().Length == 0)
{
return methodValuesInfo.Invoke(target, null);
}
PropertyInfo valuesPropertyInfo = ReflectionUtility.GetProperty(target, name);
if (valuesPropertyInfo != null)
{
return valuesPropertyInfo.GetValue(target);
}

return null;
MethodInfo methodValuesInfo = ReflectionUtility.GetMethod(target, name);
if (methodValuesInfo != null &&
(methodValuesInfo.ReturnType == typeof(float) || methodValuesInfo.ReturnType == typeof(int)) &&
methodValuesInfo.GetParameters().Length == 0)
{
return methodValuesInfo.Invoke(target, null);
}

return null;
}

private object GetLowValue(SerializedProperty property, ProgressBarAttribute progressBarAttribute)
{
if (IsNumber(progressBarAttribute.LowValue))
return progressBarAttribute.LowValue;

return GetNumberValueFromProperty(progressBarAttribute.LowValue as string, property);
}

private object GetHighValue(SerializedProperty property, ProgressBarAttribute progressBarAttribute)
{
if (IsNumber(progressBarAttribute.HighValue))
return progressBarAttribute.HighValue;

return GetNumberValueFromProperty(progressBarAttribute.HighValue as string, property);
}

private object GetMaxValue(SerializedProperty property, ProgressBarAttribute progressBarAttribute)
{
if (string.IsNullOrEmpty(progressBarAttribute.MaxValueName))
return progressBarAttribute.MaxValue;

return GetNumberValueFromProperty(progressBarAttribute.MaxValueName, property);
}

private void DrawBar(Rect rect, float fillPercent, string label, Color barColor, Color labelColor)
Expand Down