public static void Initialize(ref Database database)
        {
            _referenceDatabase = database;

            _editSettings = new DatabaseSettings(database.Settings);

            _selectedTypes = _referenceDatabase.Settings.TypeInfos;
            _defaultType   = _selectedTypes.Count > 0 ? _selectedTypes[0] : null;
            _selectedTypes = _selectedTypes.OrderBy(t => t.TypeName).ToList();

            DatabaseSettingWindow window = (DatabaseSettingWindow)GetWindow(typeof(DatabaseSettingWindow));

            window.titleContent = new GUIContent("Settings");
            window.Show();
        }
Example #2
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;
        }