AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
EnumCheckboxAttribute.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 using System.Reflection;
6 using UnityEngine;
7 #if UNITY_EDITOR
8 using UnityEditor;
9 #endif
10 
11 namespace HoloToolkit.Unity
12 {
13  // Displays an enum value as a set of checkboxes
14  [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
16  {
17  public string DefaultName { get; private set; }
18  public int DefaultValue { get; private set; }
19  public int ValueOnZero { get; private set; }
20  public bool IgnoreNone { get; private set; }
21  public bool IgnoreAll { get; private set; }
22  public string CustomLabel { get; private set; }
23 
24  public EnumCheckboxAttribute(string customLabel = null)
25  {
26  DefaultName = null;
27  DefaultValue = 0;
28  ValueOnZero = 0;
29  IgnoreNone = true;
30  IgnoreAll = true;
31  CustomLabel = customLabel;
32  }
33 
34  public EnumCheckboxAttribute(bool ignoreNone, bool ignoreAll, string customLabel = null)
35  {
36  DefaultName = null;
37  DefaultValue = 0;
38  ValueOnZero = 0;
39  IgnoreNone = ignoreNone;
40  IgnoreAll = ignoreAll;
41  CustomLabel = customLabel;
42  }
43 
44  public EnumCheckboxAttribute(string defaultName, object defaultValue, object valueOnZero = null, bool ignoreNone = true, bool ignoreAll = true, string customLabel = null)
45  {
46  DefaultName = defaultName;
47  DefaultValue = Convert.ToInt32(defaultValue);
48  ValueOnZero = Convert.ToInt32(valueOnZero);
49  IgnoreNone = ignoreNone;
50  IgnoreAll = ignoreAll;
51  CustomLabel = customLabel;
52  }
53 
54 #if UNITY_EDITOR
55  public override void DrawEditor(UnityEngine.Object target, FieldInfo field, SerializedProperty property)
56  {
57  int value = EnumCheckbox(
58  (string.IsNullOrEmpty(CustomLabel) ? SplitCamelCase(field.Name) : CustomLabel),
59  field.GetValue(target),
60  DefaultName,
61  DefaultValue,
62  ValueOnZero,
63  IgnoreNone,
64  IgnoreAll);
65 
66  field.SetValue(target, value);
67  }
68 
69  public override void DrawEditor(UnityEngine.Object target, PropertyInfo prop)
70  {
71  int value = EnumCheckbox(
72  (string.IsNullOrEmpty(CustomLabel) ? SplitCamelCase(prop.Name) : CustomLabel),
73  prop.GetValue(target, null),
74  DefaultName,
75  DefaultValue,
76  ValueOnZero,
77  IgnoreNone,
78  IgnoreAll);
79 
80  prop.SetValue(target, value, null);
81  }
82 
83  private static int EnumCheckbox(string label, object enumObj, string defaultName, object defaultVal, object valOnZero, bool ignoreNone = true, bool ignoreAll = true)
84  {
85  if (!enumObj.GetType().IsEnum)
86  {
87  throw new ArgumentException("enumObj must be an enum.");
88  }
89 
90  // Convert enum value to an int64 so we can treat it as a flag set
91  int enumFlags = Convert.ToInt32(enumObj);
92 
93  EditorGUILayout.BeginVertical(EditorStyles.helpBox);
94  EditorGUILayout.LabelField(label, EditorStyles.miniLabel);
95 
96  GUIStyle styleHR = new GUIStyle(GUI.skin.box);
97  styleHR.stretchWidth = true;
98  styleHR.fixedHeight = 2;
99  GUILayout.Box("", styleHR);
100 
101  System.Array enumVals = Enum.GetValues(enumObj.GetType());
102  int lastvalue = Convert.ToInt32(enumVals.GetValue(enumVals.GetLength(0) - 1));
103 
104  foreach (object enumVal in enumVals)
105  {
106  int flagVal = Convert.ToInt32(enumVal);
107  if (ignoreNone && flagVal == 0 && enumVal.ToString().ToLower() == "none")
108  {
109  continue;
110  }
111  if (ignoreAll && flagVal == lastvalue && enumVal.ToString().ToLower() == "all")
112  {
113  continue;
114  }
115  bool selected = (flagVal & enumFlags) != 0;
116  selected = EditorGUILayout.Toggle(enumVal.ToString(), selected);
117  // If it's selected add it to the enumObj, otherwise remove it
118  if (selected)
119  {
120  enumFlags |= flagVal;
121  }
122  else
123  {
124  enumFlags &= ~flagVal;
125  }
126  }
127  if (!string.IsNullOrEmpty(defaultName))
128  {
129  if (GUILayout.Button(defaultName, EditorStyles.miniButton))
130  {
131  enumFlags = Convert.ToInt32(defaultVal);
132  }
133  }
134  EditorGUILayout.EndVertical();
135 
136  if (enumFlags == 0)
137  {
138  enumFlags = Convert.ToInt32(valOnZero);
139  }
140  return enumFlags;
141  }
142 #endif
143 
144  }
145 }
EnumCheckboxAttribute(bool ignoreNone, bool ignoreAll, string customLabel=null)
EnumCheckboxAttribute(string defaultName, object defaultValue, object valueOnZero=null, bool ignoreNone=true, bool ignoreAll=true, string customLabel=null)