Skip to content

Commit

Permalink
Merge pull request #8 from Pechenka472/main
Browse files Browse the repository at this point in the history
Fix items header
  • Loading branch information
ooonush authored Jul 14, 2023
2 parents 492d80f + 23d60a1 commit ebaa43a
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 26 deletions.
44 changes: 29 additions & 15 deletions Assets/Plugins/Stats/Editor/StatsEditorHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ namespace Stats.Editor
{
public static class StatsEditorHelper
{
public static string GetStatItemHeader (StatItem statItem, string label)
private const BindingFlags Flags = BindingFlags.Default | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy;

public static string GetStatItemHeader (StatItem statItem, SerializedProperty property)
{
return $"{label}: {statItem.Base}";
var itemName = GetItemName<StatItem>(property);
return statItem.StatType == null ? itemName : $"{itemName}: {statItem.Base}";
}

public static string GetRuntimeStatHeader(IRuntimeStat runtimeStat)
Expand All @@ -25,25 +28,25 @@ public static string GetRuntimeStatHeader(IRuntimeStat runtimeStat)
return $"{name}: {value - modifiersValue} {modifiersText}";
}

public static string GetAttributeItemHeader(AttributeItem attributeItem, string label)
public static string GetAttributeItemHeader(AttributeItem attributeItem, SerializedProperty property)
{
float startPercent = attributeItem.StartPercent;
var startPercent = attributeItem.StartPercent;
var itemName = GetItemName<AttributeItem>(property);

return $"{label}: {Math.Round(startPercent * 100, 1)}%";
return attributeItem.AttributeType == null ? itemName : $"{itemName}: {Math.Round(startPercent * 100, 1)}%";
}

public static string GetRuntimeAttributeHeader(IRuntimeAttribute runtimeAttribute)
{
string name = runtimeAttribute.AttributeType.name;
float value = runtimeAttribute.Value;
float maxValue = runtimeAttribute.MaxValue;
var name = runtimeAttribute.AttributeType.name;
var value = runtimeAttribute.Value;
var maxValue = runtimeAttribute.MaxValue;

return $"{name}: {value} / {maxValue}";
}

public static T GetValue<T>(SerializedProperty property)
{
BindingFlags bindingFlags = BindingFlags.Default | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy;
object obj = property.serializedObject.targetObject;

string[] path = property.propertyPath.Split('.');
Expand All @@ -60,7 +63,7 @@ public static T GetValue<T>(SerializedProperty property)
FieldInfo field = null;
while (field == null)
{
field = type.GetField(path[i], bindingFlags);
field = type.GetField(path[i], Flags);

if (field == null)
{
Expand All @@ -75,24 +78,35 @@ public static T GetValue<T>(SerializedProperty property)
return (T)obj;
}

public static string GetLabel<T>(SerializedProperty property)
private static string GetItemName<T>(SerializedProperty property)
{
if (property.name == "data" && property.depth > 0 && property.displayName.Contains("Element"))
{
return GetTypeName<T>(property);
return GetItemTypeName<T>(property);
}

return property.displayName;
}

private static string GetTypeName<T>(SerializedProperty property)
private static string GetItemTypeName<T>(SerializedProperty property)
{
var label = "";

if (typeof(T) == typeof(AttributeItem))
{
return GetValue<AttributeItem>(property).AttributeType.name;
AttributeType type = GetValue<AttributeItem>(property).AttributeType;

label = type == null ? "<color=yellow>Select Attribute</color>" : type.name;
}

if (typeof(T) == typeof(StatItem))
{
StatType type = GetValue<StatItem>(property).StatType;

label = type == null ? "<color=yellow>Select Stat</color>" : type.name;
}

return typeof(T) == typeof(StatItem) ? GetValue<StatItem>(property).StatType.name : "";
return label;
}
}
}
4 changes: 2 additions & 2 deletions Assets/Plugins/Stats/Editor/TraitsEditor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Reflection;
using System;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;

namespace Stats.Editor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,7 @@ private void RepaintPercent(bool isShow)

private void UpdateHeaderText()
{
AttributeType attributeValue = _oldAttributeType;
_foldout.text = !attributeValue ? "<color=yellow>Select Attribute</color>"
: StatsEditorHelper.GetAttributeItemHeader(_attributeItem,
StatsEditorHelper.GetLabel<AttributeItem>(_property));
_foldout.text = StatsEditorHelper.GetAttributeItemHeader(_attributeItem, _property);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,7 @@ private void ResetValue()

private void UpdateHeaderText()
{
_foldout.text = _stat.objectReferenceValue == null ?
"<color=yellow>Select Stat</color>" :
StatsEditorHelper.GetStatItemHeader(_statItem, StatsEditorHelper.GetLabel<StatItem>(_property));
_foldout.text = StatsEditorHelper.GetStatItemHeader(_statItem, _property);
}
}
}
2 changes: 1 addition & 1 deletion Assets/Plugins/Stats/Runtime/Core/AttributeItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public sealed class AttributeItem
[SerializeField] private bool _changeStartPercent;
[SerializeField] [Range(0f, 1f)] private float _startPercent = 1f;

public AttributeType AttributeType => _attributeType;
public AttributeType AttributeType => _attributeType? _attributeType : null;

public float MinValue => _attributeType ? _attributeType.MinValue : 0f;
public StatType MaxValueType => _attributeType ? _attributeType.MaxValueType : null;
Expand Down
2 changes: 1 addition & 1 deletion Assets/Plugins/Stats/Runtime/Core/StatItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public sealed class StatItem
[SerializeField] private bool _changeFormula;
[SerializeField] private StatFormula _formula;

public StatType StatType => _stat.Type;
public StatType StatType => _stat? _stat.Type : null;

public float Base
{
Expand Down

0 comments on commit ebaa43a

Please sign in to comment.