AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
ShowIfEnumValueAttribute.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using System;
5 #if UNITY_EDITOR
6 using UnityEditor;
7 #endif
8 
9 namespace HoloToolkit.Unity
10 {
11  // Shows / hides based on enum value of a named member
12  [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
14  {
15  public int[] ShowValues { get; private set; }
16 
17  // IL2CPP doesn't support attributes with object arguments that are array types
18  public ShowIfEnumValueAttribute(string enumVariableName, object enumValue, bool showIfConditionMet = true)
19  {
20  if (!enumValue.GetType().IsEnum())
21  throw new Exception("Value must be of type Enum");
22 
23  ShowValues = new int[] { Convert.ToInt32(enumValue) };
24  MemberName = enumVariableName;
25  ShowIfConditionMet = showIfConditionMet;
26  }
27 
28  public ShowIfEnumValueAttribute(string enumVariableName, object enumValue1, object enumValue2, bool showIfConditionMet = true)
29  {
30  if (!enumValue1.GetType().IsEnum() || !enumValue2.GetType().IsEnum())
31  throw new Exception("Values must be of type Enum");
32 
33  ShowValues = new int[] { Convert.ToInt32(enumValue1), Convert.ToInt32(enumValue2) };
34  MemberName = enumVariableName;
35  ShowIfConditionMet = showIfConditionMet;
36  }
37 
38  public ShowIfEnumValueAttribute(string enumVariableName, object enumValue1, object enumValue2, object enumValue3, bool showIfConditionMet = true)
39  {
40  if (!enumValue1.GetType().IsEnum() || !enumValue2.GetType().IsEnum() || !enumValue3.GetType().IsEnum())
41  throw new Exception("Values must be of type Enum");
42 
43  ShowValues = new int[] { Convert.ToInt32(enumValue1), Convert.ToInt32(enumValue2), Convert.ToInt32(enumValue3) };
44  MemberName = enumVariableName;
45  ShowIfConditionMet = showIfConditionMet;
46  }
47 
48  public ShowIfEnumValueAttribute(string enumVariableName, object enumValue1, object enumValue2, object enumValue3, object enumValue4, bool showIfConditionMet = true)
49  {
50  if (!enumValue1.GetType().IsEnum() || !enumValue2.GetType().IsEnum() || !enumValue3.GetType().IsEnum() || !enumValue4.GetType().IsEnum())
51  throw new Exception("Values must be of type Enum");
52 
53  ShowValues = new int[] { Convert.ToInt32(enumValue1), Convert.ToInt32(enumValue2), Convert.ToInt32(enumValue3), Convert.ToInt32(enumValue4) };
54  MemberName = enumVariableName;
55  ShowIfConditionMet = showIfConditionMet;
56  }
57 
58 #if UNITY_EDITOR
59  public override bool ShouldShow(object target)
60  {
61  bool conditionMet = false;
62  int memberValue = Convert.ToInt32(GetMemberValue(target, MemberName));
63  for (int i = 0; i < ShowValues.Length; i++)
64  {
65  if (ShowValues[i] == memberValue)
66  {
67  conditionMet = true;
68  break;
69  }
70  }
71  return ShowIfConditionMet ? conditionMet : !conditionMet;
72  }
73 #endif
74 
75  private static object GetAsUnderlyingType(Enum enval)
76  {
77  Type entype = enval.GetType();
78  Type undertype = Enum.GetUnderlyingType(entype);
79  return Convert.ChangeType(enval, undertype);
80  }
81  }
82 }
ShowIfEnumValueAttribute(string enumVariableName, object enumValue1, object enumValue2, object enumValue3, bool showIfConditionMet=true)
ShowIfEnumValueAttribute(string enumVariableName, object enumValue1, object enumValue2, object enumValue3, object enumValue4, bool showIfConditionMet=true)
ShowIfEnumValueAttribute(string enumVariableName, object enumValue1, object enumValue2, bool showIfConditionMet=true)
ShowIfEnumValueAttribute(string enumVariableName, object enumValue, bool showIfConditionMet=true)