Beispiel #1
0
        public void OnGUI()
        {
            Repaint();
            if (Application.isPlaying)
            {
                GUI.enabled = false;
            }

            if (database == null)
            {
                database = Resources.Load <TagDatabase>("PCTK/AllTheTags/TagDatabase");

                if (database == null)
                {
                    database = ScriptableObject.CreateInstance <TagDatabase>();
                    string path = "Assets/Plugins/Dependent/_GameLibrary/ThirdLibrary/PigeonCoopToolkit/AllTheTags/Resources/PCTK/AllTheTags/TagDatabase.asset";

                    AssetDatabase.CreateAsset(database, path);
                    AssetDatabase.SaveAssets();
                    AssetDatabase.Refresh();
                    database = Resources.Load <TagDatabase>("PCTK/AllTheTags/TagDatabase");
                }

                target = database;
            }

            _scrollPos = GUILayout.BeginScrollView(_scrollPos);
            GUILayout.BeginHorizontal();

            GUILayout.BeginVertical(GUILayout.ExpandWidth(true));

            for (int i = 0; i < database.Tags.Count; i++)
            {
                GUILayout.BeginHorizontal("box", GUILayout.MinHeight(30));

                Tag tag = database.Tags[i];
                if (tag == _tagBeingRenamed)
                {
                    GUI.color = tag.ReferenceColor * new Color(1, 1, 1, 0.25f);
                    GUILayout.BeginHorizontal(editorSkin.GetStyle("TagPreviewRename"), GUILayout.Width(position.width - 200));
                    GUI.color = tag.ReferenceColor;
                    _tempName = GUILayout.TextField(_tempName, editorSkin.GetStyle("TagLabel"));
                    GUILayout.EndHorizontal();
                    GUI.color = Color.white;
                }
                else
                {
                    GUI.color = tag.ReferenceColor;
                    GUILayout.BeginHorizontal(editorSkin.GetStyle("TagPreview"), GUILayout.Width(position.width - 200));
                    GUILayout.Label(tag.TagFriendly, editorSkin.GetStyle("TagLabel"));
                    GUILayout.EndHorizontal();
                    GUI.color = Color.white;
                }

                GUILayout.BeginHorizontal(editorSkin.GetStyle("TagPreviewOptionsContainer"));

                if (_tagBeingRenamed == tag)
                {
                    if (GUILayout.Button("Save", EditorStyles.miniButton))
                    {
                        bool uniqueName = true;

                        foreach (Tag searchSameName in database.Tags)
                        {
                            if (searchSameName.TagFriendly == _tempName && searchSameName != _tagBeingRenamed)
                            {
                                uniqueName = false;
                                break;
                            }
                        }

                        if (!uniqueName)
                        {
                            EditorUtility.DisplayDialog("Error", "There is already a tag with that name, tag must be unique",
                                                        "Ok");
                        }
                        else
                        {
                            Undo.RecordObject(database, "Rename Tag");
                            _tagBeingRenamed.TagFriendly = _tempName;
                            EditorUtility.SetDirty(target);
                            _tagBeingRenamed = null;
                        }
                    }
                    if (GUILayout.Button("Cancel", EditorStyles.miniButton))
                    {
                        _tagBeingRenamed = null;
                    }
                }
                else
                {
                    if (GUILayout.Button("Rename", EditorStyles.miniButton))
                    {
                        _tagBeingRenamed = tag;
                        _tempName        = tag.TagFriendly;
                    }
                    if (GUILayout.Button("Delete", EditorStyles.miniButton))
                    {
                        if (EditorUtility.DisplayDialog("Warning",
                                                        "You cannot undo this. Tag will be destroyed and any object with this tag will be untagged. You will have to retag everything if you wish to add this tag again in the future. Are you sure?",
                                                        "Yes", "No"))
                        {
                            database.Tags.RemoveAt(i);
                            i--;
                            EditorUtility.SetDirty(target);
                        }
                    }
                }


                tag.ReferenceColor = EditorGUILayout.ColorField(tag.ReferenceColor);

                GUILayout.EndHorizontal();
                GUILayout.EndHorizontal();

                GUILayout.Space(5);
            }

            GUILayout.EndVertical();


            GUILayout.EndHorizontal();

            GUILayout.FlexibleSpace();
            GUILayout.EndScrollView();

            GUILayout.BeginHorizontal("box");

            GUILayout.Label("Enter tag:");
            bool  returnPressed = false;
            Event e             = Event.current;

            if (e.keyCode == KeyCode.Return && e.rawType == EventType.KeyDown)
            {
                returnPressed = true;
            }

            GUI.SetNextControlName("TagAddField");
            _tagToAdd = EditorGUILayout.TextField(_tagToAdd);

            if (!string.IsNullOrEmpty(_tagToAdd) && returnPressed && GUI.GetNameOfFocusedControl() == "TagAddField")
            {
                database.FindOrAddTag(_tagToAdd);
                EditorUtility.SetDirty(database);
                _tagToAdd = "";
            }

            GUILayout.EndHorizontal();

            if (GUI.changed)
            {
                EditorUtility.SetDirty(target);
            }
        }
Beispiel #2
0
        public override void OnInspectorGUI()
        {
            if (Application.isPlaying)
            {
                GUI.enabled = false;
            }
            else
            {
                Repaint();
            }

            GUILayout.Space(10);


            AllTheTags att = (AllTheTags)target;

            if (tagDatabase == null)
            {
                tagDatabase = Resources.Load <TagDatabase>("PCTK/AllTheTags/TagDatabase");

                if (tagDatabase == null)
                {
                    tagDatabase = ScriptableObject.CreateInstance <TagDatabase>();
                    string path = "Assets/PigeonCoopToolkit/AllTheTags/Resources/PCTK/AllTheTags/TagDatabase.asset";

                    AssetDatabase.CreateAsset(tagDatabase, path);
                    AssetDatabase.SaveAssets();
                    AssetDatabase.Refresh();
                    tagDatabase = Resources.Load <TagDatabase>("PCTK/AllTheTags/TagDatabase");
                }
            }


            List <int> activeTagList =
                typeof(AllTheTags).GetField("_tags", BindingFlags.Instance | BindingFlags.NonPublic)
                .GetValue(att) as List <int>;
            bool activeTagListModified = false;

            if (activeTagList == null)
            {
                activeTagListModified = true;
                activeTagList         = new List <int>();
            }


            float currentLabelPos = 0;

            GUILayout.BeginHorizontal();

            for (int i = 0; i < activeTagList.Count; i++)
            {
                Tag  tag            = tagDatabase.GetTagViaID(activeTagList[i]);
                bool removeFromList = false;

                if (tag == null)
                {
                    removeFromList = true;
                }
                else
                {
                    float minWidth, maxWidth;
                    editorSkin.GetStyle("Tag").CalcMinMaxWidth(new GUIContent(tag.TagFriendly), out minWidth, out maxWidth);
                    currentLabelPos += (maxWidth + 20);
                    if (currentLabelPos > Screen.width)
                    {
                        GUILayout.EndHorizontal();
                        GUILayout.BeginHorizontal();
                        currentLabelPos = maxWidth;
                    }

                    GUI.color = tag.ReferenceColor;
                    GUILayout.Label(new GUIContent(tag.TagFriendly), editorSkin.GetStyle("Tag"));
                    Rect labelRect = GUILayoutUtility.GetLastRect();

                    labelRect = new Rect(labelRect.xMax - 18, labelRect.yMin + 4, 14, 14);
                    if (GUI.Button(labelRect, "", editorSkin.GetStyle("Remove")))
                    {
                        removeFromList = true;
                    }
                }

                if (removeFromList)
                {
                    Undo.RecordObject(att, "Remove Tag");
                    activeTagList.RemoveAt(i);
                    activeTagListModified = true;
                    i--;
                }
            }
            GUI.color = Color.white;
            GUILayout.EndHorizontal();
            GUILayout.Space(5);

            GUILayout.BeginVertical("box");
            GUILayout.BeginHorizontal();
            GUILayout.Label("Enter tag:");

            bool  returnPressed = false, tabPressed = false;
            Event e = Event.current;

            if (e.keyCode == KeyCode.Return && e.rawType == EventType.KeyDown)
            {
                returnPressed = true;
            }
            if (e.keyCode == KeyCode.Tab && e.rawType == EventType.KeyDown)
            {
                tabPressed = true;
            }
            GUI.SetNextControlName("TagAddField");
            _tagToAdd = EditorGUILayout.TextField(_tagToAdd);
            Rect suggestionRect = GUILayoutUtility.GetLastRect();

            GUILayout.EndHorizontal();

            if (GUILayout.Button("Manage Tags", EditorStyles.miniButton))
            {
                TagDatabaseUtility.Spawn();
                //Selection.activeObject = tagDatabase;
            }
            GUILayout.EndVertical();


            if (!string.IsNullOrEmpty(_tagToAdd) && returnPressed && GUI.GetNameOfFocusedControl() == "TagAddField")
            {
                Tag newTag = tagDatabase.FindOrAddTag(_tagToAdd);
                EditorUtility.SetDirty(tagDatabase);

                if (!activeTagList.Contains(newTag.TagID))
                {
                    Undo.RecordObject(att, "Attach Tag");
                    activeTagList.Add(newTag.TagID);
                    activeTagListModified = true;
                    _tagToAdd             = "";
                }
                else
                {
                    EditorUtility.DisplayDialog("Error", "This object has already been tagged as " + _tagToAdd + ".", "Ok");
                }
            }

            if (!string.IsNullOrEmpty(_tagToAdd))
            {
                Tag suggestedTag = null;

                foreach (var tag in tagDatabase.Tags)
                {
                    if (tag.TagFriendly.Length > _tagToAdd.Length && tag.TagFriendly.StartsWith(_tagToAdd) && !activeTagList.Contains(tag.TagID))
                    {
                        if (suggestedTag != null && suggestedTag.TagFriendly.Length < tag.TagFriendly.Length)
                        {
                            continue;
                        }

                        suggestedTag = tag;
                    }
                }

                if (suggestedTag != null)
                {
                    suggestionRect.xMin += 1;
                    suggestionRect.xMax += 1;
                    Color restoreColor = GUI.color;
                    GUI.color = restoreColor * new Color(1, 1, 1, 0.25f);
                    GUI.Label(suggestionRect, suggestedTag.TagFriendly);
                    GUI.color = restoreColor;

                    if (tabPressed)
                    {
                        e.Use();

                        Undo.RecordObject(att, "Attach Tag");
                        activeTagList.Add(suggestedTag.TagID);
                        activeTagListModified = true;
                        _tagToAdd             = "";
                    }
                }
            }

            if (activeTagListModified)
            {
                typeof(AllTheTags).GetField("_tags", BindingFlags.Instance | BindingFlags.NonPublic)
                .SetValue(att, activeTagList);

                EditorUtility.SetDirty(att);

                Repaint();
            }
        }