Skip to content

Commit

Permalink
#222 - fixed a bug where ProgressBar wasn't working with integers
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrizov committed Apr 25, 2021
1 parent d02322d commit faa02a1
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
3 changes: 0 additions & 3 deletions Assets/NaughtyAttributes/Samples/DemoScene/DemoScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -526,9 +526,6 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 9c928ea15ae74a44089beb2e534c1a35, type: 3}
m_Name:
m_EditorClassIdentifier:
mask:
serializedVersion: 2
m_Bits: 32
--- !u!1 &732714203
GameObject:
m_ObjectHideFlags: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class ProgressBarAttribute : DrawerAttribute
public string MaxValueName { get; private set; }
public EColor Color { get; private set; }

public ProgressBarAttribute(string name, int maxValue, EColor color = EColor.Blue)
public ProgressBarAttribute(string name, float maxValue, EColor color = EColor.Blue)
{
Name = name;
MaxValue = maxValue;
Expand All @@ -24,7 +24,7 @@ public ProgressBarAttribute(string name, string maxValueName, EColor color = ECo
Color = color;
}

public ProgressBarAttribute(int maxValue, EColor color = EColor.Blue)
public ProgressBarAttribute(float maxValue, EColor color = EColor.Blue)
: this("", maxValue, color)
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ protected override float GetPropertyHeight_Internal(SerializedProperty property,
ProgressBarAttribute progressBarAttribute = PropertyUtility.GetAttribute<ProgressBarAttribute>(property);
var maxValue = GetMaxValue(property, progressBarAttribute);

return IsNumber(property) && maxValue is float
return IsNumber(property) && IsNumber(maxValue)
? GetPropertyHeight(property)
: GetPropertyHeight(property) + GetHelpBoxHeight();
}
Expand All @@ -33,9 +33,9 @@ protected override void OnGUI_Internal(Rect rect, SerializedProperty property, G
var valueFormatted = property.propertyType == SerializedPropertyType.Integer ? value.ToString() : string.Format("{0:0.00}", value);
var maxValue = GetMaxValue(property, progressBarAttribute);

if (maxValue != null && maxValue is float)
if (maxValue != null && IsNumber(maxValue))
{
var fillPercentage = value / (float)maxValue;
var fillPercentage = value / CastToFloat(maxValue);
var barLabel = (!string.IsNullOrEmpty(progressBarAttribute.Name) ? "[" + progressBarAttribute.Name + "] " : "") + valueFormatted + "/" + maxValue;
var barColor = progressBarAttribute.Color.GetColor();
var labelColor = Color.white;
Expand All @@ -54,7 +54,7 @@ protected override void OnGUI_Internal(Rect rect, SerializedProperty property, G
else
{
string message = string.Format(
"The provided dynamic max value for the progress bar is not correct. Please check if the '{0}' is correct, or the return type is float",
"The provided dynamic max value for the progress bar is not correct. Please check if the '{0}' is correct, or the return type is float/int",
nameof(progressBarAttribute.MaxValueName));

DrawDefaultPropertyAndHelpBox(rect, property, message, MessageType.Warning);
Expand Down Expand Up @@ -87,7 +87,7 @@ private object GetMaxValue(SerializedProperty property, ProgressBarAttribute pro

MethodInfo methodValuesInfo = ReflectionUtility.GetMethod(target, progressBarAttribute.MaxValueName);
if (methodValuesInfo != null &&
methodValuesInfo.ReturnType == typeof(float) &&
(methodValuesInfo.ReturnType == typeof(float) || methodValuesInfo.ReturnType == typeof(int)) &&
methodValuesInfo.GetParameters().Length == 0)
{
return methodValuesInfo.Invoke(target, null);
Expand Down Expand Up @@ -133,5 +133,22 @@ private bool IsNumber(SerializedProperty property)
bool isNumber = property.propertyType == SerializedPropertyType.Float || property.propertyType == SerializedPropertyType.Integer;
return isNumber;
}

private bool IsNumber(object obj)
{
return (obj is float) || (obj is int);
}

private float CastToFloat(object obj)
{
if (obj is int)
{
return (int)obj;
}
else
{
return (float)obj;
}
}
}
}
4 changes: 2 additions & 2 deletions Assets/NaughtyAttributes/Scripts/Test/ProgressBarTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public class ProgressBarTest : MonoBehaviour

[Header("Dynamic ProgressBar")]
[ProgressBar("Elixir", "maxElixir", color: EColor.Violet)]
public float elixir = 50.0f;
public float maxElixir = 100.0f;
public int elixir = 50;
public int maxElixir = 100;
}

[System.Serializable]
Expand Down
4 changes: 0 additions & 4 deletions Assets/NaughtyAttributes/Scripts/Test/_NaughtyComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ namespace NaughtyAttributes.Test
{
public class _NaughtyComponent : MonoBehaviour
{
[CurveRange(0, 0, 1, 1, EColor.Red)]
public AnimationCurve red;

public MyClass myClass;
}

[System.Serializable]
Expand Down

0 comments on commit faa02a1

Please sign in to comment.