Пример #1
0
        private void AddItemOfType(Type type)
        {
            DatabaseWriter.AddNewAssetToDatabase(type, CurrentDatabase);

            _currentlySelected = CurrentDatabase.Assets.Last().Index;
            GUI.FocusControl(null);

            if (_currentDatabase.Settings.AutoUpdateEnum)
            {
                DatabaseWriter.CreateDatabaseEnum(_currentDatabase);
            }
        }
        private bool EnumGenerationCurrentlyPossible()
        {
            int zeros = _referenceDatabase.Assets.Count <= 0
                ? 0
                : _referenceDatabase.Assets.Max(t => t.Index).ToString().Length;

            string[] enumNames =
                _referenceDatabase.Assets
                .Select(t => DatabaseWriter.GetEnumEntryString(_editSettings, t, zeros)).ToArray();

            return(enumNames.All(EnumGenerator.IsEnumValid) && enumNames.Length == enumNames.Distinct().Count());
        }
Пример #3
0
 private static void CreateButton()
 {
     if (GUILayout.Button("Create new"))
     {
         DatabaseWriter.CreateNewDatabase(
             new DatabaseSettings(
                 DatabaseWriter.GetItteratedValidString("NewDatabase", AllDatabases.Select(t => t.Name).ToArray(),
                                                        2),
                 new Type[0]));
         _allDatabases = null;
     }
 }
Пример #4
0
        //Called by Unity
        // ReSharper disable once InconsistentNaming
        // ReSharper disable once UnusedMember.Local
        private void OnGUI()
        {
            Color defaultColor = GUI.contentColor;

            if (CurrentDatabase == null)
            {
                EditorGUILayout.LabelField("No database selected");
                return;
            }

            EditorGUILayout.Space();
            EditorGUILayout.BeginHorizontal();
            {
                GUILayout.Label("", GUILayout.Width(16));
                Rect lastRect = GUILayoutUtility.GetLastRect();
                if (GUI.Button(new Rect(lastRect.x, lastRect.y + 4, lastRect.width, lastRect.width), "", "AC LeftArrow"))
                {
                    DatabasesOverview.Initialize();
                }
                GUILayout.Label(CurrentDatabase.Name, EditorStyles.boldLabel);

                if (GUILayout.Button("", new GUIStyle(GUI.skin.FindStyle("OL Plus")), GUILayout.Width(20)))
                {
                    DatabaseSettingWindow.Initialize(ref _currentDatabase);
                }
            }
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.Space();

            _scrollPos = EditorGUILayout.BeginScrollView(_scrollPos);
            {
                DatabaseAsset[] sortedDbAssets = CurrentDatabase.Assets.ToArray();

                switch (CurrentDatabase.Settings.SortingBehaviour)
                {
                case DatabaseSortingBehaviour.OrderByIndex:
                    sortedDbAssets = sortedDbAssets.OrderBy(t => t.Index).ToArray();
                    break;

                case DatabaseSortingBehaviour.OrderByName:
                    sortedDbAssets = sortedDbAssets.OrderBy(t => t.Name).ToArray();
                    break;
                }

                for (int i = 0; i < sortedDbAssets.Length; i++)
                {
                    DatabaseAsset asset = sortedDbAssets[i];

                    EditorGUILayout.BeginHorizontal();
                    {
                        if (_currentlySelectedIndex == i)
                        {
                            int oldIndex = asset.Index;
                            int newIndex = EditorGUILayout.DelayedIntField(oldIndex, GUILayout.Width(40));

                            if (newIndex > 0 && newIndex != oldIndex)
                            {
                                DatabaseWriter.ChangeItemsIndex(CurrentDatabase, asset.Index, newIndex);
                            }

                            if (oldIndex != asset.Index)
                            {
                                _currentlySelectedIndex = -1;
                                _currentlySelected      = asset.Index;
                            }
                        }
                        else
                        {
                            if (GUILayout.Button(string.Format("#{0:000}", asset.Index), GUILayout.Width(40)))
                            {
                                _currentlySelected      = -1;
                                _currentlySelectedIndex = i;
                            }
                        }

                        if (!AssetNameIsValid(asset) || !AssetNameIsUnique(asset))
                        {
                            GUI.backgroundColor = ErrorColor;
                        }

                        if (GUILayout.Button(asset.Name))
                        {
                            //TODO This error-managemant causes unexpected behaviour : Refactor
                            bool nameFieldSelected = GUI.GetNameOfFocusedControl() == "nameField";
                            GUI.FocusControl(null);
                            if (nameFieldSelected)
                            {
                                return;
                            }
                            // -----

                            if (_currentlySelected == asset.Index)
                            {
                                _currentlySelected = -1;
                            }
                            else
                            {
                                _currentlySelected      = asset.Index;
                                _currentlySelectedIndex = -1;
                            }
                        }
                        GUI.backgroundColor = defaultColor;

                        if (GUILayout.Button("X", GUILayout.Width(40)) &&
                            EditorUtility.DisplayDialog("Detete " + asset.Name,
                                                        string.Format("Do you want to delete the asset: #{0:000} {1} " +
                                                                      "from the Database: {2}?", asset.Index, asset.Name,
                                                                      CurrentDatabase.Name),
                                                        "Yes", "Cancel"))
                        {
                            DatabaseWriter.RemoveAssetFromDatabase(i, CurrentDatabase);
                            if (_currentlySelected == asset.Index)
                            {
                                _currentlySelected = -1;
                            }

                            if (_currentDatabase.Settings.AutoUpdateEnum)
                            {
                                DatabaseWriter.CreateDatabaseEnum(_currentDatabase);
                            }
                        }
                    }
                    EditorGUILayout.EndHorizontal();

                    if (_currentlySelected == asset.Index)
                    {
                        EditorGUILayout.BeginVertical("Box");
                        {
                            EditorGUIUtility.labelWidth = 100;

                            EditorGUILayout.LabelField("< " + asset.GetType().Name + " >");

                            bool nameInvalid   = !AssetNameIsValid(asset);
                            bool nameNotUnique = !AssetNameIsUnique(asset);
                            if (nameInvalid || nameNotUnique)
                            {
                                GUI.backgroundColor = ErrorColor;
                            }

                            string oldName = asset.Name;

                            GUI.SetNextControlName("nameField");
                            string newName = EditorGUILayout.DelayedTextField("Name", oldName);

                            asset.Name = newName;

                            if (oldName != newName && _currentDatabase.Settings.AutoUpdateEnum &&
                                AssetNameIsValid(asset) && AssetNameIsUnique(asset))
                            {
                                DatabaseWriter.CreateDatabaseEnum(_currentDatabase);
                            }

                            GUI.backgroundColor = defaultColor;

                            MessageType mType = CurrentDatabase.Settings.AutoUpdateEnum
                                ? MessageType.Warning
                                : MessageType.Info;

                            if (nameInvalid)
                            {
                                EditorGUILayout.HelpBox("This name is invalid for enum-generation", mType);
                            }

                            if (nameNotUnique)
                            {
                                EditorGUILayout.HelpBox("This name is not unique for enum-generation", mType);
                            }

                            SerializedObject sObj = new SerializedObject(asset);

                            //if (GUILayout.Button("Select in assets")) //Does work ... place elsewhere
                            //    Selection.activeObject = sObj.targetObject;

                            SerializedProperty sProp = sObj.GetIterator();
                            while (sProp.NextVisible(true))
                            {
                                //Dont display "Name" cause its displayed before
                                if (sProp.displayName == "Name")
                                {
                                    continue;
                                }

                                //Dont display "Index" cause its displayed in list
                                if (sProp.displayName == "Index")
                                {
                                    continue;
                                }

                                //Dont display "Script"-Referenc
                                if (sProp.displayName == "Script")
                                {
                                    continue;
                                }

                                //Dont display in-Array-Propertys (again)
                                if (sProp.propertyPath.Contains("Array"))
                                {
                                    continue;
                                }

                                //Display current property as default (or by attribute set) field
                                EditorGUILayout.PropertyField(sProp, true);
                            }

                            sObj.ApplyModifiedProperties();
                        }
                        EditorGUILayout.EndVertical();
                    }
                }
            }
            EditorGUILayout.EndScrollView();

            if (CurrentDatabase.Settings.HasMultipleChoices)
            {
                EditorGUILayout.LabelField(string.Empty, GUILayout.Height(18));
                Rect r = GUILayoutUtility.GetLastRect();

                int  optionPanelWidth = 20;
                Rect addButtonRect    = GUILayoutUtility.GetLastRect();
                addButtonRect.x    += addButtonRect.width - optionPanelWidth;
                addButtonRect.width = optionPanelWidth;

                if (GUI.Button(addButtonRect, string.Empty))
                {
                    GenericMenu gMenu = new GenericMenu();

                    foreach (Type t in CurrentDatabase.GetAssignableTypes())
                    {
                        if (CurrentDatabase.Settings.DefaultType == t)
                        {
                            continue;
                        }

                        Type temp = t;
                        gMenu.AddItem(new GUIContent(t.Name), false, () => AddItemOfType(temp));
                    }
                    gMenu.ShowAsContext();
                }

                if (GUI.Button(r, "Create new " + CurrentDatabase.Settings.DefaultType.Name, "DropDownButton"))
                {
                    AddItemOfType(CurrentDatabase.Settings.DefaultType);
                }
            }
            else
            {
                if (CurrentDatabase.Settings.DefaultType != null)
                {
                    if (GUILayout.Button("Create new " + CurrentDatabase.Settings.DefaultType.Name))
                    {
                        AddItemOfType(CurrentDatabase.Settings.DefaultType);
                    }
                }
                else
                {
                    if (GUILayout.Button("No default type set"))
                    {
                        EditorPrefs.SetBool("typeFoldout", true);
                        DatabaseSettingWindow.Initialize(ref _currentDatabase);
                    }
                }
            }

            if (GUI.changed)
            {
                EditorUtility.SetDirty(_currentDatabase);
            }

            GUI.backgroundColor = defaultColor;
        }
Пример #5
0
        private string AsEnumName(DatabaseAsset asset)
        {
            int zeros = CurrentDatabase.Assets.Max(t => t.Index).ToString().Length;

            return(DatabaseWriter.GetEnumEntryString(CurrentDatabase.Settings, asset, zeros));
        }
        //Called by Unity
        private void OnGUI()
        {
            if (_referenceDatabase == null)
            {
                EditorGUILayout.LabelField("No database selected");
                return;
            }

            EditorGUIUtility.labelWidth = 75;
            EditorGUILayout.Space();

            string[] names           = DatabaseWriter.GetAllDatabases().Where(t => t != _referenceDatabase).Select(t => t.Name).ToArray();
            bool     nameIsNotUnique = names.Contains(_editSettings.Name);
            Color    defaultColor    = GUI.backgroundColor;

            if (nameIsNotUnique)
            {
                GUI.backgroundColor = new Color(1f, 0.3f, 0.3f);
            }
            _editSettings.Name = EditorGUILayout.TextField("Name", _editSettings.Name);

            GUI.backgroundColor = defaultColor;
            if (nameIsNotUnique)
            {
                EditorGUILayout.HelpBox("This Name is not unique! This will cause errors when trying to access the database.", MessageType.Warning);
            }
            _editSettings.SortingBehaviour =
                (DatabaseSortingBehaviour)EditorGUILayout.EnumPopup("Sorting", _editSettings.SortingBehaviour);

            EditorGUILayout.Space();

            bool typeFoldout = EditorPrefs.GetBool("typedatabases_typeFoldout");

            EditorPrefs.SetBool("typedatabases_typeFoldout", EditorGUILayout.Foldout(typeFoldout, "Type Settings"));
            if (typeFoldout)
            {
                Type[] possibleTypes = Database.GetStorableTypes();

                _scrollPos = EditorGUILayout.BeginScrollView(_scrollPos, false, false, GUILayout.MaxHeight(200));
                {
                    EditorGUILayout.BeginVertical("Box");
                    if (_selectedTypes.Count <= 0)
                    {
                        EditorGUILayout.LabelField("No type selected.");
                    }

                    for (int i = 0; i < _selectedTypes.Count; i++)
                    {
                        StorableTypeInfo selectedType = _selectedTypes[i];
                        EditorGUILayout.BeginHorizontal();
                        {
                            bool typeEnforced = TypeIsEnforced(_selectedTypes[i].StorableType);
                            //Name
                            EditorGUILayout.LabelField(GetTypeLabel(selectedType.StorableType));

                            GUILayout.FlexibleSpace();

                            //Autoinclude Inhariatants
                            if (typeEnforced || _selectedTypes[i].StorableType.IsAbstract)
                            {
                                GUI.enabled = false;
                            }
                            EditorGUILayout.LabelField("Inh.", GUILayout.Width(30));
                            _selectedTypes[i].IncludeInheritance = EditorGUILayout.Toggle("",
                                                                                          _selectedTypes[i].IncludeInheritance, GUILayout.Width(15));
                            GUI.enabled = true;

                            //Default-Type-Select
                            if (_selectedTypes[i] == _defaultType)
                            {
                                EditorGUILayout.LabelField("Is default", GUILayout.Width(75));
                            }
                            else
                            {
                                GUI.enabled = !selectedType.StorableType.IsAbstract;
                                if (GUILayout.Button("Default", GUILayout.Width(75)))
                                {
                                    _defaultType = _selectedTypes[i];
                                }
                                GUI.enabled = true;
                            }

                            //Remove
                            if (typeEnforced)
                            {
                                GUI.enabled = false;
                            }
                            if (GUILayout.Button("X", GUILayout.Width(25)))
                            {
                                _selectedTypes.Remove(selectedType);
                                i--;

                                if (selectedType == _defaultType)
                                {
                                    _defaultType = _selectedTypes.Count > 0
                                        ? _selectedTypes[0]
                                        : null;
                                }
                                continue;
                            }
                            GUI.enabled = true;
                        }
                        EditorGUILayout.EndHorizontal();
                    }
                    EditorGUILayout.EndVertical();
                    EditorGUILayout.Space();

                    EditorGUILayout.BeginVertical("Box");
                    if (possibleTypes.Length <= 0)
                    {
                        EditorGUILayout.LabelField("No more possible type found.");
                    }

                    for (int i = 0; i < possibleTypes.Length; i++)
                    {
                        Type possibleType = possibleTypes[i];
                        EditorGUILayout.BeginHorizontal();
                        {
                            EditorGUILayout.LabelField(GetTypeLabel(possibleType));
                            if (_selectedTypes.Select(t => t.StorableType).Contains(possibleType))
                            {
                                GUI.enabled = false;
                            }
                            if (GUILayout.Button("Add"))
                            {
                                _selectedTypes.Add(new StorableTypeInfo(possibleType));
                                _selectedTypes = _selectedTypes.OrderBy(t => t.TypeName).ToList();
                                if (_defaultType == null)
                                {
                                    _defaultType = _selectedTypes[0];
                                }
                            }
                            GUI.enabled = true;
                        }
                        EditorGUILayout.EndHorizontal();
                    }
                    EditorGUILayout.EndVertical();
                }
                EditorGUILayout.EndScrollView();
                EditorGUILayout.Space();
            }

            bool indexFoldout = EditorPrefs.GetBool("typedatabases_indexFoldout");

            EditorPrefs.SetBool("typedatabases_indexFoldout", EditorGUILayout.Foldout(indexFoldout, "Index Settings"));
            if (indexFoldout)
            {
                float lableWidth01 = EditorGUIUtility.labelWidth;
                EditorGUIUtility.labelWidth  = 200;
                _editSettings.IndexBehaviour =
                    (DatabaseIndexBehaviour)EditorGUILayout.EnumPopup("Prevent double-indices by",
                                                                      _editSettings.IndexBehaviour);
                EditorGUIUtility.labelWidth = lableWidth01;

                if (DatabaseWriter.ContinuallyIndecies(_referenceDatabase))
                {
                    GUI.enabled = false;
                }

                if (GUILayout.Button("Clean up indecies"))
                {
                    DatabaseWriter.CleanDatabaseIndecies(_referenceDatabase);
                }
                GUI.enabled = true;

                EditorGUILayout.Space();
            }

            bool enumFoldout = EditorPrefs.GetBool("typedatabases_enumFoldout");

            EditorPrefs.SetBool("typedatabases_enumFoldout", EditorGUILayout.Foldout(enumFoldout, "Enum Settings"));
            if (enumFoldout)
            {
                float lableWidth02 = EditorGUIUtility.labelWidth;
                EditorGUIUtility.labelWidth = 120;

                EditorGUILayout.BeginHorizontal();
                {
                    EditorGUILayout.LabelField("Enum-Name", GUILayout.Width(115));
                    _editSettings.EnumTypeNamePrefix = EditorGUILayout.TextField(_editSettings.EnumTypeNamePrefix);
                    GUI.enabled = false;
                    EditorGUILayout.TextField(_editSettings.Name, GUILayout.MinWidth(120));
                    GUI.enabled = true;
                    _editSettings.EnumTypeNameSuffix = EditorGUILayout.TextField(_editSettings.EnumTypeNameSuffix);
                }
                EditorGUILayout.EndHorizontal();

                _editSettings.EnumSettings =
                    (DatabaseEnumSettings)EditorGUILayout.EnumPopup("Enum Generation", _editSettings.EnumSettings);

                _editSettings.GenerateNoneEntry = EditorGUILayout.Toggle("None-Entry at Zero",
                                                                         _editSettings.GenerateNoneEntry);

                if (_editSettings.EnumSettings == DatabaseEnumSettings.IndexesAndNames)
                {
                    _editSettings.IndexPrefix     = EditorGUILayout.TextField("Index Prefix", _editSettings.IndexPrefix);
                    _editSettings.UseLeadingZeros = EditorGUILayout.Toggle("Use Leading Zeros",
                                                                           _editSettings.UseLeadingZeros);
                    Rect temp = GUILayoutUtility.GetLastRect();
                    _editSettings.UseSeparator = EditorGUILayout.Toggle("Use Separator", _editSettings.UseSeparator);

                    temp.x      += lableWidth02 + 50 + 20;
                    temp.height *= 2;
                    temp.width   = position.width - (lableWidth02 + 50) - 20 - 8;
                    temp.height -= 4;
                    temp.y      += 2;

                    EditorGUI.DrawRect(temp, Color.gray);

                    StringBuilder sB = new StringBuilder();
                    sB.Append(_editSettings.EnumTypeNamePrefix);
                    sB.Append(_editSettings.Name);
                    sB.Append(_editSettings.EnumTypeNameSuffix);
                    sB.Append(".");
                    sB.Append(_editSettings.IndexPrefix);
                    if (_editSettings.UseLeadingZeros)
                    {
                        sB.Append("00");
                    }
                    sB.Append("7");
                    if (_editSettings.UseSeparator)
                    {
                        sB.Append("_");
                    }
                    sB.Append("ExampleEntry");

                    int size;
                    if (sB.Length > 30)
                    {
                        size = 8;
                    }
                    else if (sB.Length > 20)
                    {
                        size = 10;
                    }
                    else if (sB.Length > 15)
                    {
                        size = 14;
                    }
                    else
                    {
                        size = 18;
                    }

                    EditorGUI.LabelField(temp, sB.ToString(),
                                         new GUIStyle(GUI.skin.label)
                    {
                        alignment = TextAnchor.MiddleCenter, fontSize = size
                    });
                }

                EditorGUIUtility.labelWidth = lableWidth02;

                bool enumGenerationImpossible = !EnumGenerationCurrentlyPossible();
                if (enumGenerationImpossible)
                {
                    MessageType mType = _editSettings.AutoUpdateEnum ? MessageType.Warning : MessageType.Info;
                    EditorGUILayout.HelpBox("The enum-generation is currently not possible! " +
                                            "Check the entry-names for invalid characters or duplicates.",
                                            mType);
                }

                EditorGUILayout.BeginHorizontal();
                {
                    GUILayout.FlexibleSpace();
                    _editSettings.AutoUpdateEnum = EditorGUILayout.ToggleLeft("Auto", _editSettings.AutoUpdateEnum,
                                                                              GUILayout.Width(45));

                    EditorGUILayout.LabelField(string.Empty, GUILayout.Height(18));
                    Rect r = GUILayoutUtility.GetLastRect();

                    if (!string.IsNullOrEmpty(_referenceDatabase.EnumPath))
                    {
                        int  optionPanelWidth = 20;
                        Rect addButtonRect    = GUILayoutUtility.GetLastRect();
                        addButtonRect.x    += addButtonRect.width - optionPanelWidth;
                        addButtonRect.width = optionPanelWidth;

                        if (GUI.Button(addButtonRect, string.Empty))
                        {
                            GenericMenu gMenu = new GenericMenu();

                            gMenu.AddItem(new GUIContent("Remove enum"), false,
                                          () => DatabaseWriter.RemoveDatabaseEnum(_referenceDatabase));
                            gMenu.ShowAsContext();
                        }
                    }

                    if (enumGenerationImpossible || _editSettings.AutoUpdateEnum)
                    {
                        GUI.enabled = false;
                    }

                    if (GUI.Button(r, "Update Enum",
                                   !string.IsNullOrEmpty(_referenceDatabase.EnumPath) ? "DropDownButton" : "Button"))
                    {
                        DatabaseWriter.CreateDatabaseEnum(_referenceDatabase, _editSettings);
                    }
                    GUI.enabled = true;
                }
                EditorGUILayout.EndHorizontal();

                EditorGUILayout.Space();
            }

            if (nameIsNotUnique)
            {
                GUI.enabled = false;
            }
            if (GUILayout.Button("Apply"))
            {
                _editSettings.TypeInfos = _selectedTypes;

                //Apply Settings to database
                if (_referenceDatabase.Name != _editSettings.Name)
                {
                    DatabaseWriter.RenameDatabase(_editSettings.Name, _referenceDatabase);
                }
                _referenceDatabase.Settings = _editSettings;

                //Set default-type to top in database-settings
                if (_defaultType != null && _referenceDatabase.Settings.ContainsType(_defaultType.StorableType))
                {
                    _referenceDatabase.Settings.TypeInfos.Remove(_defaultType);
                    _referenceDatabase.Settings.TypeInfos.Insert(0, _defaultType);
                }

                if (_referenceDatabase.Settings.AutoUpdateEnum)
                {
                    DatabaseWriter.CreateDatabaseEnum(_referenceDatabase);
                }

                DatabaseWindow.Initialize();
                GetWindow(typeof(DatabaseSettingWindow)).Close();
            }
            GUI.enabled = true;

            if (GUI.changed)
            {
                EditorUtility.SetDirty(_referenceDatabase);
            }
        }
Пример #7
0
        //Called by Unity
        // ReSharper disable once InconsistentNaming
        // ReSharper disable once UnusedMember.Local
        private void OnGUI()
        {
            Color defaultColor = GUI.backgroundColor;

            EditorGUILayout.Space();
            EditorGUILayout.BeginHorizontal();
            {
                GUILayout.Label("Databases Overview", EditorStyles.boldLabel);

                if (GUILayout.Button("", new GUIStyle(GUI.skin.FindStyle("OL Plus")), GUILayout.Width(20)))
                {
                    DatabasesWindow.Initialize();
                }
            }
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.Space();

            if (AllDatabases.Length <= 0)
            {
                EditorGUILayout.LabelField("No databases found ...");
            }

            _scrollPos = EditorGUILayout.BeginScrollView(_scrollPos);
            {
                if (AllDatabases != null)
                {
                    foreach (Database database in AllDatabases.OrderBy(t => t.Name))
                    {
                        EditorGUILayout.BeginHorizontal();
                        {
                            var temp = database;
                            if (AllDatabases.Select(t => t.Name).Count(t => t == temp.Name) > 1)
                            {
                                GUI.backgroundColor = new Color(1, 0.3f, 0.3f);
                            }

                            if (GUILayout.Button(database.Name))
                            {
                                DatabaseWindow.CurrentDatabaseIndex = Array.IndexOf(AllDatabases, database);
                                DatabaseWindow.Initialize();
                            }

                            GUI.backgroundColor = defaultColor;

                            if (GUILayout.Button("X", GUILayout.Width(40)) &&
                                EditorUtility.DisplayDialog("Detete " + database.Name,
                                                            string.Format("Do you want to delete the database: {0}?", database.Name)
                                                            , "Yes", "Cancel"))
                            {
                                DatabaseWriter.RemoveDatabase(database);
                                _allDatabases = null;
                            }
                        }
                        EditorGUILayout.EndHorizontal();
                    }
                }
            }
            EditorGUILayout.EndScrollView();

            CreateButton();

            GUI.backgroundColor = defaultColor;
        }