Exemple #1
0
        public override int GetBitMask()
        {
            int mask = 0;

            T[] allValues = ScriptableEnum.GetValues <T>();

            for (int i = 0; i < allValues.Length; i++)
            {
                if (_isEverything || _values.Contains(allValues[i]))
                {
                    mask |= (1 << i);
                }
            }

            return(mask);
        }
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            SerializedProperty isEverythingProperty = property.FindPropertyRelative("_isEverything");
            SerializedProperty listProperty         = property.FindPropertyRelative("_values");

            ScriptableEnumMask mask = property.GetValue <ScriptableEnumMask>();

            Type valueType = mask.GetType().BaseType.GetGenericArguments()[0];

            ScriptableEnum[] values = ScriptableEnum.GetValues(valueType);
            //  values.Sort((x, y) => x.name.CompareTo(y.name));

            string[] labels = new string[values.Length];
            for (int i = 0; i < values.Length; i++)
            {
                labels[i] = values[i].name;
            }

            EditorGUI.BeginProperty(position, label, property);

            EditorGUI.BeginChangeCheck();
            int newMaskValue = EditorGUI.MaskField(position, label, isEverythingProperty.boolValue ? ~0 : mask.GetBitMask(), labels);

            if (EditorGUI.EndChangeCheck())
            {
                mask.SetFromBitMask(newMaskValue);

                ScriptableEnum[] maskValues = mask.GetRawValues();

                listProperty.arraySize = maskValues.Length;

                for (int i = 0; i < maskValues.Length; i++)
                {
                    listProperty.GetArrayElementAtIndex(i).objectReferenceValue = maskValues[i];
                }

                isEverythingProperty.boolValue = mask.IsEverything;

                //    EditorUtility.SetDirty(property.serializedObject.targetObject);
                property.serializedObject.ApplyModifiedPropertiesWithoutUndo();
            }

            EditorGUI.EndProperty();
        }
        static int SetupValuesAndLabels(Type type, Object currentValue, out List <ScriptableEnum> values, out GUIContent[] labels, bool allowNull)
        {
            values = new List <ScriptableEnum>(ScriptableEnum.GetValues(type));
            values.Sort((x, y) => x.name.CompareTo(y.name));

            if (allowNull)
            {
                values.Insert(0, null);
            }

            int selectedIndex = 0;

            if (currentValue != null)
            {
                for (int i = 0; i < values.Count; i++)
                {
                    if (values[i] == currentValue)
                    {
                        selectedIndex = i;
                        break;
                    }
                }
            }

            labels = new GUIContent[values.Count];
            for (int i = 0; i < values.Count; i++)
            {
                if (values[i] == null)
                {
                    labels[i] = new GUIContent("NONE");
                }
                else
                {
                    labels[i] = new GUIContent(values[i].name);
                }
            }

            return(selectedIndex);
        }
Exemple #4
0
        public override void SetFromBitMask(int mask)
        {
            _values = new List <T>();
            T[] allValues = ScriptableEnum.GetValues <T>();

            for (int i = 0; i < allValues.Length; i++)
            {
                if (((1 << i) & mask) != 0)
                {
                    _values.Add(allValues[i]);
                }
            }

            if (_values.Count == allValues.Length)
            {
                _isEverything = true;
                _values.Clear();
            }
            else
            {
                _isEverything = false;
            }
        }
Exemple #5
0
        public static CodeDefintion CreateScriptableEnumConstants(Type type)
        {
            Assert.IsTrue(typeof(ScriptableEnum).IsAssignableFrom(type));

            StringBuilder code = new StringBuilder();

            ScriptableEnum[] values = ScriptableEnum.GetValues(type);

            string typeName  = type.Name;
            bool   childType = type.BaseType != typeof(ScriptableEnum);


            string allFieldName = "__all" + StringUtils.Pluralise(typeName);

            code.Append("public static ");
            code.Append(typeName);
            code.Append("[] ");
            code.Append("All" + StringUtils.Pluralise(typeName));
            code.Append(" { get { if (");
            code.Append(allFieldName);
            code.Append(" == null) ");
            code.Append(allFieldName);
            code.Append(" = GetValues<");
            code.Append(typeName);
            code.Append(">(); return ");
            code.Append(allFieldName);
            code.Append("; } }");
            code.Append(NEW_LINE);


            for (int i = 0; i < values.Length; i++)
            {
                string fieldName = StringUtils.Variableize(values[i].name, true, true);

                code.Append("public static ");
                if (childType)
                {
                    code.Append("new ");
                }
                code.Append(typeName);
                code.Append(" ");
                code.Append(StringUtils.Santise(values[i].name, false, false));
                code.Append(" { get { if (_");
                code.Append(fieldName);
                code.Append(" == null) _");
                code.Append(fieldName);
                code.Append(" = GetValue<");
                code.Append(typeName);
                code.Append(">(\"");
                code.Append(values[i].name);
                code.Append("\"); return _");
                code.Append(fieldName);
                code.Append("; } }");
                code.Append(NEW_LINE);
            }

            code.Append(NEW_LINE);

            code.Append("protected static ");
            code.Append(typeName);
            code.Append("[] ");
            code.Append(allFieldName);
            code.Append(";");


            code.Append(NEW_LINE);

            for (int i = 0; i < values.Length; i++)
            {
                code.Append("protected static ");
                if (childType)
                {
                    code.Append("new ");
                }
                code.Append(typeName);
                code.Append(" _");
                code.Append(StringUtils.Variableize(values[i].name, true, true));
                code.Append(";");

                if (i < values.Length - 1)
                {
                    code.Append(NEW_LINE);
                }
            }

            return(new CodeDefintion(code.ToString(), Scope.Class));
        }
Exemple #6
0
 public override ScriptableEnum[] GetRawValues()
 {
     return(_isEverything ? ScriptableEnum.GetValues <T>() : _values.ToArray());
 }