Editors/Attributes/HideIfAnyAttribute.cs
using Sandbox;
using Sandbox.Internal;
using System.Linq;
namespace ExtendedBox.Editors.Attributes;
public sealed class HideIfAnyAttribute : ConditionalVisibilityAttribute
{
public string PropertyName { get; set; }
public object[] Values { get; set; }
public HideIfAnyAttribute(string propertyName, params object[] values)
{
PropertyName = propertyName;
Values = values;
}
public override bool TestCondition(object targetObject, TypeDescription td)
{
PropertyDescription property = td.GetProperty(PropertyName);
if(property == null)
{
return true;
}
if(!property.CanRead)
{
return true;
}
object value = property.GetValue(targetObject);
if(Values.Any(x => x == value))
{
return false;
}
var valueStr = $"{value}";
if(Values.Any(x => $"{x}" == valueStr))
{
return false;
}
return true;
}
public override bool TestCondition(SerializedObject so)
{
if(so.TryGetProperty(PropertyName, out var prop))
{
var value = prop.GetValue((object)so);
return Values.Any(x => object.Equals(value, x));
}
GlobalSystemNamespace.Log.Warning($"HideIfAnyAttribute: Couldn't find property '{PropertyName}' on {so.TypeName}");
return true;
}
}