Tanoda
EnumFlags.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using UnityEngine;
10using System;
11using System.Collections.Generic;
12#if UNITY_EDITOR
13using UnityEditor;
14#endif
15
16namespace Leap.Unity.Attributes {
17 using Query;
18
20
21 public EnumFlags() { }
22
23#if UNITY_EDITOR
24 private string[] _enumNames;
25 private int[] _enumValues;
26
27 public void DrawProperty(Rect rect, SerializedProperty property, GUIContent label) {
28 if (_enumNames == null) {
29 string[] names = (string[])Enum.GetNames(fieldInfo.FieldType);
30 int[] values = (int[])Enum.GetValues(fieldInfo.FieldType);
31
32 int count = values.Query().Count(v => v != 0);
33 _enumNames = new string[count];
34 _enumValues = new int[count];
35
36 int index = 0;
37 for (int i = 0; i < names.Length; i++) {
38 if (values[i] == 0) continue;
39
40 _enumNames[index] = names[i];
41 _enumValues[index] = values[i];
42 index++;
43 }
44 }
45
46 int convertedMask = 0;
47 for (int i = 0; i < _enumValues.Length; i++) {
48 if ((property.intValue & _enumValues[i]) != 0) {
49 convertedMask |= (1 << i);
50 }
51 }
52
53 int resultMask = EditorGUI.MaskField(rect, label, convertedMask, _enumNames);
54
55 int propertyMask = 0;
56 {
57 int index = 0;
58 while (resultMask != 0 && index < _enumValues.Length) {
59 if ((resultMask & 1) != 0) {
60 propertyMask |= _enumValues[index];
61 }
62
63 index++;
64 resultMask = resultMask >> 1;
65 }
66 }
67
68 property.intValue = propertyMask;
69 }
70
71 public override IEnumerable<SerializedPropertyType> SupportedTypes {
72 get {
73 yield return SerializedPropertyType.Enum;
74 }
75 }
76#endif
77 }
78}