private void Draw(Rect position, SerializedProperty property)
        {
            SerializedProperty category      = Properties.Get(PropertyName.Category, property);
            SerializedProperty name          = Properties.Get(PropertyName.Name, property);
            SerializedProperty instantAction = Properties.Get(PropertyName.InstantAction, property);

            var items = new List <string>();

            Color initialColor = GUI.color; //save the GUI color

            NumberOfLines[property.propertyPath] = 1;
            Rect drawRect = GetDrawRect(position); //calculate draw rect

            GUI.color = DGUI.Colors.PropertyColor(DrawerColorName);

            float x     = drawRect.x + DGUI.Properties.Space(2);
            float width = drawRect.width
                          - DGUI.Toggle.Switch.Width
                          - Contents.GetWidth(UILabels.InstantAction, DGUI.Label.Style(Size.S))
                          - DGUI.Properties.Space(9);


            //LINE 1
            drawRect.y += DGUI.Properties.StandardVerticalSpacing;

            //VIEW CATEGORY
            int categorySelectedIndex = Database.CategoryNames.Contains(category.stringValue)
                ? Database.CategoryNames.IndexOf(category.stringValue)
                : Database.CategoryNames.IndexOf(NamesDatabase.CUSTOM);

            EditorGUI.BeginChangeCheck();
            categorySelectedIndex = EditorGUI.Popup(new Rect(x, drawRect.y + 1, width * 0.5f, drawRect.height), categorySelectedIndex, Database.CategoryNames.ToArray());
            x += width * 0.5f + DGUI.Properties.Space();

            if (EditorGUI.EndChangeCheck())
            {
                category.stringValue = Database.CategoryNames[categorySelectedIndex];
                if (Database.CategoryNames[categorySelectedIndex] != NamesDatabase.CUSTOM)
                {
                    if (string.IsNullOrEmpty(name.stringValue.Trim()))
                    {
                        name.stringValue = Database.GetNamesList(Database.CategoryNames[categorySelectedIndex])[0];
                    }
                    else if (name.stringValue.Trim() != NamesDatabase.UNNAMED &&
                             !Database.GetNamesList(Database.CategoryNames[categorySelectedIndex]).Contains(name.stringValue.Trim()))
                    {
                        if (EditorUtility.DisplayDialog("Add Name", "Add the '" + name.stringValue.Trim() + "' name to the '" + Database.CategoryNames[categorySelectedIndex] + "' category?", "Yes", "No"))
                        {
                            string      cleanName     = name.stringValue.Trim();
                            ListOfNames categoryAsset = Database.GetCategory(Database.CategoryNames[categorySelectedIndex]);
                            categoryAsset.Names.Add(cleanName);
                            categoryAsset.SetDirty(false);
                            Database.Sort(false, true);
//                            Database.GetNamesList(Database.CategoryNames[categorySelectedIndex], true).Add(name.stringValue.Trim());
//                            Database.SetDirty(true);
                        }
                        else if (Database.CategoryNames[categorySelectedIndex] == NamesDatabase.GENERAL)
                        {
                            name.stringValue = NamesDatabase.UNNAMED;
                        }
                        else
                        {
                            name.stringValue = Database.GetNamesList(Database.CategoryNames[categorySelectedIndex])[0];
                        }
                    }
                }
            }

            bool hasCustomName = category.stringValue.Equals(NamesDatabase.CUSTOM);

            if (!Database.Contains(category.stringValue)) //database does not contain this category -> reset it to custom
            {
                hasCustomName        = true;
                category.stringValue = NamesDatabase.CUSTOM;
            }

            //VIEW NAME
            if (!hasCustomName)
            {
                items = Database.GetNamesList(category.stringValue);
                if (items.Count == 0)
                {
                    if (!Database.GetNamesList(NamesDatabase.GENERAL, true).Contains(NamesDatabase.UNNAMED))
                    {
                        Database.GetNamesList(NamesDatabase.GENERAL, true).Add(NamesDatabase.UNNAMED);
                        Database.SetDirty(true);
                    }

                    category.stringValue = NamesDatabase.GENERAL;
                    items = Database.GetNamesList(category.stringValue);
                }
            }

            if (hasCustomName)
            {
                EditorGUI.PropertyField(new Rect(x, drawRect.y + 1, width * 0.5f, drawRect.height), name, GUIContent.none, true);
            }
            else
            {
                int nameSelectedIndex = 0;
                if (items.Contains(name.stringValue))
                {
                    nameSelectedIndex = items.IndexOf(name.stringValue);
                }
                else
                {
                    if (category.stringValue.Equals(NamesDatabase.GENERAL))
                    {
                        name.stringValue  = NamesDatabase.UNNAMED;
                        nameSelectedIndex = items.IndexOf(NamesDatabase.UNNAMED);
                    }
                    else if (name.stringValue != NamesDatabase.UNNAMED &&
                             EditorUtility.DisplayDialog("Add Name", "Add the '" + name.stringValue + "' name to the '" + category.stringValue + "' category?", "Yes", "No"))
                    {
                        string cleanName = name.stringValue.Trim();
                        if (string.IsNullOrEmpty(cleanName))
                        {
                            name.stringValue = items[nameSelectedIndex];
                        }
                        else if (items.Contains(cleanName))
                        {
                            name.stringValue  = cleanName;
                            nameSelectedIndex = items.IndexOf(cleanName);
                        }
                        else
                        {
                            ListOfNames categoryAsset = Database.GetCategory(category.stringValue);
                            categoryAsset.Names.Add(cleanName);
                            categoryAsset.SetDirty(false);
                            Database.Sort(false, true);
//                            Database.GetNamesList(category.stringValue, true).Add(cleanName);
//                            Database.SetDirty(true);
                            name.stringValue  = cleanName;
                            nameSelectedIndex = items.IndexOf(name.stringValue);
                        }
                    }
                    else
                    {
                        name.stringValue = items[nameSelectedIndex];
                    }
                }

                EditorGUI.BeginChangeCheck();
                nameSelectedIndex = EditorGUI.Popup(new Rect(x, drawRect.y + 1, width * 0.5f, drawRect.height), nameSelectedIndex, items.ToArray());
                if (EditorGUI.EndChangeCheck())
                {
                    name.stringValue = items[nameSelectedIndex];
                }
            }

            x += width * 0.5f + DGUI.Properties.Space();

            GUI.color = initialColor; //restore the GUI color
            DGUI.Toggle.Switch.Draw(new Rect(x, drawRect.y + 1, DGUI.Toggle.Switch.Width, drawRect.height), instantAction, DrawerColorName);
            x += DGUI.Toggle.Switch.Width + DGUI.Properties.Space();

            GUI.color = GUI.color.WithAlpha(DGUI.Properties.TextIconAlphaValue(instantAction.boolValue));
            DGUI.Label.Draw(new Rect(x, drawRect.y, Contents.GetWidth(UILabels.InstantAction), drawRect.height), UILabels.InstantAction, Size.S, instantAction.boolValue ? DrawerColorName : DGUI.Colors.DisabledTextColorName);
            GUI.color = initialColor; //restore the GUI color
        }
예제 #2
0
            public static void DrawItemsDatabaseSelector(SerializedObject serializedObject,
                                                         SerializedProperty category, string categoryLabel,
                                                         SerializedProperty name, string nameLabel,
                                                         NamesDatabase database,
                                                         ColorName colorName)
            {
                if (s_namesList == null)
                {
                    s_namesList = new List <string>();
                }
                s_namesList.Clear();

                int  categorySelectedIndex = database.CategoryNames.Contains(category.stringValue) ? database.CategoryNames.IndexOf(category.stringValue) : database.CategoryNames.IndexOf(NamesDatabase.CUSTOM);
                bool hasCustomName         = category.stringValue.Equals(NamesDatabase.CUSTOM);
                int  nameSelectedIndex     = 0;

                if (!database.Contains(category.stringValue)) //database does not contain this category -> reset it to custom
                {
                    hasCustomName        = true;
                    category.stringValue = NamesDatabase.CUSTOM;
                }

                if (!hasCustomName)
                {
                    s_namesList = database.GetNamesList(category.stringValue);
                    if (s_namesList.Count == 0)
                    {
                        if (!database.GetNamesList(NamesDatabase.GENERAL, true).Contains(NamesDatabase.UNNAMED))
                        {
                            database.GetNamesList(NamesDatabase.GENERAL, true).Add(NamesDatabase.UNNAMED);
                            database.SetDirty(true);
                        }

                        category.stringValue = NamesDatabase.GENERAL;
                        s_namesList          = database.GetNamesList(category.stringValue);
                    }


                    if (s_namesList.Contains(name.stringValue))
                    {
                        nameSelectedIndex = s_namesList.IndexOf(name.stringValue);
                    }
                    else
                    {
                        if (category.stringValue.Equals(NamesDatabase.GENERAL))
                        {
                            name.stringValue  = NamesDatabase.UNNAMED;
                            nameSelectedIndex = s_namesList.IndexOf(NamesDatabase.UNNAMED);
                        }
                        else if (name.stringValue != NamesDatabase.UNNAMED && EditorUtility.DisplayDialog("Add Name", "Add the '" + name.stringValue + "' name to the '" + category.stringValue + "' category?", "Yes", "No"))
                        {
                            string cleanName = name.stringValue.Trim();
                            if (string.IsNullOrEmpty(cleanName))
                            {
                                name.stringValue = s_namesList[nameSelectedIndex];
                            }
                            else if (s_namesList.Contains(cleanName))
                            {
                                name.stringValue  = cleanName;
                                nameSelectedIndex = s_namesList.IndexOf(cleanName);
                            }
                            else
                            {
                                ListOfNames categoryAsset = database.GetCategory(category.stringValue);
                                categoryAsset.Names.Add(cleanName);
                                categoryAsset.SetDirty(false);
                                database.Sort(false, true);
                                name.stringValue  = cleanName;
                                nameSelectedIndex = s_namesList.IndexOf(name.stringValue);
                            }
                        }
                        else
                        {
                            name.stringValue = s_namesList[nameSelectedIndex];
                        }
                    }
                }

                float           lineHeight             = Properties.SingleLineHeight;
                float           backgroundHeight       = lineHeight * 2 + Properties.Space(2);
                GUILayoutOption backgroundHeightOption = GUILayout.Height(backgroundHeight);

                bool  isUnnamed    = name.stringValue.Equals(NamesDatabase.UNNAMED);
                Color initialColor = GUI.color;
                float labelAlpha   = Properties.TextIconAlphaValue(!isUnnamed);

                GUILayout.BeginHorizontal(backgroundHeightOption);
                {
                    GUILayout.BeginVertical(backgroundHeightOption);
                    {
                        Background.Draw(colorName, backgroundHeight);
                        GUILayout.Space(-backgroundHeight);
                        GUILayout.Space(Properties.Space());
                        GUILayout.BeginHorizontal();
                        GUILayout.Space(Properties.Space(2));
                        GUI.color = GUI.color.WithAlpha(labelAlpha);
                        Label.Draw(categoryLabel, Size.S, colorName);
                        GUI.color = initialColor;
                        GUILayout.EndHorizontal();

                        GUI.color = Colors.PropertyColor(colorName);
                        EditorGUI.BeginChangeCheck();
                        categorySelectedIndex = EditorGUILayout.Popup(categorySelectedIndex, database.CategoryNames.ToArray());
                        if (EditorGUI.EndChangeCheck())
                        {
                            category.stringValue = database.CategoryNames[categorySelectedIndex];
                            if (database.CategoryNames[categorySelectedIndex] != NamesDatabase.CUSTOM)
                            {
                                ListOfNames newCategory = database.GetCategory(database.CategoryNames[categorySelectedIndex]);

                                if (newCategory.Names.Count == 0)
                                {
                                    DDebug.Log("'" + database.CategoryNames[categorySelectedIndex] + "' " + Properties.Labels.CategoryIsEmpty);
                                }
                                else
                                {
                                    if (string.IsNullOrEmpty(name.stringValue.Trim()))
                                    {
                                        name.stringValue = database.GetNamesList(database.CategoryNames[categorySelectedIndex])[0];
                                    }
                                    else if (name.stringValue != NamesDatabase.UNNAMED && !database.GetNamesList(database.CategoryNames[categorySelectedIndex]).Contains(name.stringValue))
                                    {
                                        if (EditorUtility.DisplayDialog("Add Name", "Add the '" + name.stringValue + "' to the '" + database.CategoryNames[categorySelectedIndex] + "' category?", "Yes", "No"))
                                        {
                                            ListOfNames categoryAsset = database.GetCategory(database.CategoryNames[categorySelectedIndex]);
                                            categoryAsset.Names.Add(name.stringValue);
                                            categoryAsset.SetDirty(false);
                                            database.Sort(false, true);
                                        }
                                        else if (database.CategoryNames[categorySelectedIndex] == NamesDatabase.GENERAL)
                                        {
                                            name.stringValue = NamesDatabase.UNNAMED;
                                        }
                                        else
                                        {
                                            name.stringValue = database.GetNamesList(database.CategoryNames[categorySelectedIndex])[0];
                                        }
                                    }
                                    hasCustomName = false;
                                }
                            }

                            Properties.ResetKeyboardFocus();
                        }

                        GUI.color = initialColor;
                    }
                    GUILayout.EndVertical();
                    GUILayout.Space(Properties.Space());
                    GUILayout.BeginVertical(backgroundHeightOption);
                    {
                        Background.Draw(colorName, backgroundHeight);
                        GUILayout.Space(-backgroundHeight);
                        GUILayout.Space(Properties.Space());
                        GUILayout.BeginHorizontal();
                        GUILayout.Space(Properties.Space(2));
                        GUI.color = GUI.color.WithAlpha(labelAlpha);
                        Label.Draw(nameLabel, Size.S, colorName);
                        GUI.color = initialColor;
                        GUILayout.EndHorizontal();

                        GUI.color = Colors.PropertyColor(colorName);
                        if (hasCustomName)
                        {
                            EditorGUILayout.PropertyField(name, GUIContent.none, true);
                        }
                        else
                        {
                            EditorGUI.BeginChangeCheck();
                            nameSelectedIndex = EditorGUILayout.Popup(nameSelectedIndex, s_namesList.ToArray());
                            if (EditorGUI.EndChangeCheck())
                            {
                                name.stringValue = s_namesList[nameSelectedIndex];
                                Properties.ResetKeyboardFocus();
                            }
                        }

                        GUI.color = initialColor;
                    }
                    GUILayout.EndVertical();
                }
                GUILayout.EndHorizontal();
                GUILayout.Space(-Properties.Space() - 1);
            }