Esempio n. 1
0
        public static void Flags(Rect position, FlagsOption[] options, Action <FlagsOption, SerializedProperty> onSelected, GUIContent label = null, SerializedProperty property = null)
        {
            label = label ?? property.ToGUIContent();
            int        selectedCount = options.Count(option => option.IsSelected);
            bool       nothing       = selectedCount == 0;
            bool       everything    = selectedCount == options.Length;
            GUIContent popupName;

            position = EditorGUI.PrefixLabel(position, label);

            if (nothing)
            {
                popupName = "Nothing".ToGUIContent();
            }
            else if (everything)
            {
                popupName = "Everything".ToGUIContent();
            }
            else
            {
                var name = "";

                foreach (var option in options)
                {
                    if (option.IsSelected)
                    {
                        if (string.IsNullOrEmpty(name))
                        {
                            name = option.Label.text;
                        }
                        else
                        {
                            name += " | " + option.Label.text;
                        }
                    }
                }

                if (selectedCount > 1 && name.GetWidth(EditorStyles.miniFont) > position.width)
                {
                    popupName = string.Format("Mixed ({0}) ...", selectedCount).ToGUIContent();
                }
                else
                {
                    popupName = name.ToGUIContent();
                }
            }

            int indent = EditorGUI.indentLevel;

            EditorGUI.indentLevel = 0;
            GenericMenu.MenuFunction2 callback = data => onSelected((FlagsOption)data, property);

            if (GUI.Button(position, GUIContent.none, new GUIStyle()))
            {
                var menu = new GenericMenu();

                menu.AddItem(FlagsOption.GetNothing(nothing), callback);
                menu.AddItem(FlagsOption.GetEverything(everything), callback);

                for (int i = 0; i < options.Length; i++)
                {
                    var option = options[i];
                    menu.AddItem(option.Label, option.IsSelected, callback, option);
                }

                menu.DropDown(position);
            }

            EditorGUI.LabelField(position, popupName, EditorStyles.popup);
            EditorGUI.indentLevel = indent;
        }