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

Make show if attribute support multiple enums #323

Open
wants to merge 1 commit 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 @@ -22,5 +22,11 @@ public ShowIfAttribute(string enumName, object enumValue)
{
Inverted = false;
}

public ShowIfAttribute(string enumName, params object[] enumValues)
: base(enumName, enumValues)
{
Inverted = false;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;

namespace NaughtyAttributes
{
Expand All @@ -11,7 +12,7 @@ public class ShowIfAttributeBase : MetaAttribute
/// <summary>
/// If this not null, <see cref="Conditions"/>[0] is name of an enum variable.
/// </summary>
public Enum EnumValue { get; private set; }
public Enum[] EnumValues { get; private set; }

public ShowIfAttributeBase(string condition)
{
Expand All @@ -33,7 +34,18 @@ public ShowIfAttributeBase(string enumName, Enum enumValue)
throw new ArgumentNullException(nameof(enumValue), "This parameter must be an enum value.");
}

EnumValue = enumValue;
EnumValues = new []{ enumValue };
}

public ShowIfAttributeBase(string enumName, params object[] enumValues)
: this(enumName)
{
if (enumValues == null || enumValues.Any(value => value == null))
{
throw new ArgumentNullException(nameof(enumValues), "All parameters must be enum values.");
}

EnumValues = enumValues.Select(obj => obj as Enum).ToArray();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace NaughtyAttributes.Editor
Expand Down Expand Up @@ -130,14 +131,14 @@ public static bool IsVisible(SerializedProperty property)
object target = GetTargetObjectWithProperty(property);

// deal with enum conditions
if (showIfAttribute.EnumValue != null)
if (showIfAttribute.EnumValues != null)
{
Enum value = GetEnumValue(target, showIfAttribute.Conditions[0]);
if (value != null)
{
bool matched = value.GetType().GetCustomAttribute<FlagsAttribute>() == null
? showIfAttribute.EnumValue.Equals(value)
: value.HasFlag(showIfAttribute.EnumValue);
? showIfAttribute.EnumValues.Contains(value)
: showIfAttribute.EnumValues.Any(element => value.HasFlag(element));

return matched != showIfAttribute.Inverted;
}
Expand Down