Пример #1
0
        void OnGUI()
        {
            if (!init)
            {
                Init();
            }

            bool  hoverSelected = false;
            Event e             = Event.current;

            EditorPlusHistoryInternal.CheckConsistency();

            widgetHostFavs.Area         = position;
            widgetHostFavs.WidgetsCount = EditorPlusHistoryInternal.favorites.Count + 1;
            widgetHostFavs.Update();

            favsView = new Rect(0, 0, position.width, widgetHostFavs.GetYMax());

            historyView = new Rect(0, widgetHostFavs.GetYMax(), position.width, position.height - favsView.height);

            widgetHostHistory.Area         = position;
            widgetHostHistory.WidgetsCount = EditorPlusHistoryInternal.history.Count;

            widgetHostHistory.Update();

            //drag and drop sorting
            if (favsView.Contains(e.mousePosition) && DragAndDrop.objectReferences.Length > 0 && DragAndDrop.objectReferences[0] == selectedObj && (EditorPlusHistoryInternal.favorites.Contains(selectedObj) || EditorPlusHistoryInternal.history.Contains(selectedObj)))
            {
                DragAndDrop.visualMode = DragAndDropVisualMode.Link;
                if (e.rawType == EventType.DragPerform)
                {
                    int index = widgetHostFavs.FindClosestWidget(e.mousePosition);

                    bool abort = false;

                    if (index - 1 == EditorPlusHistoryInternal.favorites.IndexOf(selectedObj))
                    {
                        DragAndDrop.PrepareStartDrag();
                        abort = true;
                    }
                    else if (index - 1 <= 0)
                    {
                        if (!EditorPlusHistoryInternal.favorites.Contains(selectedObj))
                        {
                            EditorPlusHistoryInternal.AddToFavorites(selectedObj);
                            widgetHostFavs.WidgetsCount = EditorPlusHistoryInternal.favorites.Count + 1;
                            widgetHostFavs.Update();
                        }
                        EditorPlusHistoryInternal.SetFavoritePosition(0, selectedObj);
                        abort = true;
                    }

                    //check if widget + margin.x contains mouse position and insert there
                    if (!abort && e.mousePosition.x > widgetHostFavs.Widgets[index].x && e.mousePosition.x <widgetHostFavs.Widgets[index].x + widgetHostFavs.Widgets[index].width + widgetHostFavs.WidgetMargin.x && e.mousePosition.y> widgetHostFavs.Widgets[index].y && e.mousePosition.y < widgetHostFavs.Widgets[index].y + widgetHostFavs.Widgets[index].height)
                    {
                        //left or right
                        if (e.mousePosition.x < widgetHostFavs.Widgets[index].x + widgetHostFavs.Widgets[index].width / 2)
                        {
                            if (!EditorPlusHistoryInternal.favorites.Contains(selectedObj))
                            {
                                EditorPlusHistoryInternal.AddToFavorites(selectedObj);
                                widgetHostFavs.WidgetsCount = EditorPlusHistoryInternal.favorites.Count + 1;
                                widgetHostFavs.Update();
                            }
                            EditorPlusHistoryInternal.SetFavoritePosition(index - 1, selectedObj);
                        }
                        else
                        {
                            if (!EditorPlusHistoryInternal.favorites.Contains(selectedObj))
                            {
                                EditorPlusHistoryInternal.AddToFavorites(selectedObj);
                                widgetHostFavs.WidgetsCount = EditorPlusHistoryInternal.favorites.Count + 1;
                                widgetHostFavs.Update();
                            }
                            EditorPlusHistoryInternal.SetFavoritePosition(index, selectedObj);
                        }
                    }
                    else if (!abort)
                    {
                        if (!EditorPlusHistoryInternal.favorites.Contains(selectedObj))
                        {
                            EditorPlusHistoryInternal.AddToFavorites(selectedObj);
                            widgetHostFavs.WidgetsCount = EditorPlusHistoryInternal.favorites.Count + 1;
                            widgetHostFavs.Update();
                        }
                        EditorPlusHistoryInternal.SetFavoritePosition(EditorPlusHistoryInternal.favorites.Count, selectedObj);
                    }
                }
            }


            float scrollViewFavsHeight = 0f;

            if (widgetHostFavs.Widgets.Count > 0)
            {
                scrollViewFavsHeight = widgetHostFavs.Widgets[widgetHostFavs.Widgets.Count - 1].yMax;
            }


            float scrollViewHistHeight = 0f;

            if (widgetHostHistory.Widgets.Count > 0)
            {
                scrollViewHistHeight = widgetHostHistory.Widgets[widgetHostHistory.Widgets.Count - 1].yMax;
            }

            if (position.height < scrollViewFavsHeight + scrollViewHistHeight)
            {
                widgetHostFavs.Area = new Rect(0, 0, favsView.width - GUI.skin.verticalScrollbar.normal.background.width, favsView.height);
                widgetHostFavs.Update();

                widgetHostHistory.Area = new Rect(0, historyView.y, historyView.width - GUI.skin.verticalScrollbar.normal.background.width, historyView.height);
                widgetHostHistory.Update();
            }

            GUISkin old = GUI.skin;

            GUI.skin       = EditorPlus.ScrollviewSkin;
            historyViewPos = GUI.BeginScrollView(new Rect(0, 0, position.width, position.height), historyViewPos, new Rect(0, 0, position.width - 30, scrollViewFavsHeight + scrollViewHistHeight));
            GUI.skin       = old;

            if (GUI.skin != IconsSkinFav)
            {
                GUI.skin = IconsSkinFav;
            }

            //Navigation Arrows
            Rect r = widgetHostFavs.Widgets[0];

            if (GUI.Button(new Rect(r.x, r.y, r.width / 2 - ButtonMargin.x - LockClosed.width, r.height), new GUIContent("Prev", Prev)) || (e.isMouse && e.button == 3))
            {
                EditorPlusHistoryInternal.HistoryNavigationBack();
            }

            if (GUI.Button(new Rect(r.x + r.width / 2, r.y, r.width / 2, r.height), new GUIContent("Next", Next)))
            {
                EditorPlusHistoryInternal.HistoryNavigationForward();
            }


            //-----------------------------------------------
            //draw favorites
            for (int i = 0; i < EditorPlusHistoryInternal.favorites.Count; ++i)
            {
                if (GUI.skin != IconsSkinFav)
                {
                    GUI.skin = IconsSkinFav;
                }


                r = widgetHostFavs.Widgets[i + 1];

                if (e.rawType == EventType.MouseDown)
                {
                    if (r.Contains(e.mousePosition))
                    {
                        //Drag object
                        if (e.button == 0)
                        {
                            highlightInFavs = true;
                            if (e.clickCount != 2)
                            {
                                selectedObj = EditorPlusHistoryInternal.favorites[i];

                                DragAndDrop.PrepareStartDrag();
                                if (selectedObj.GetType() != typeof(UnityEngine.GameObject))
                                {
                                    DragAndDrop.paths = new string[] { AssetDatabase.GetAssetPath(selectedObj) }
                                }
                                ;
                                DragAndDrop.objectReferences = new Object[] { selectedObj };

                                DragAndDrop.StartDrag(selectedObj.ToString());
                            }
                            else if (e.clickCount == 2)
                            {
                                Selection.activeObject = EditorPlusHistoryInternal.favorites[i];

                                if (Selection.activeObject.GetType() != typeof(UnityEngine.GameObject))
                                {
                                    if (Selection.activeObject.GetType() == EditorPlus.EPlusDefaultType)
                                    {
                                        string path = AssetDatabase.GetAssetPath(Selection.activeObject);
                                        if (Directory.Exists(path))
                                        {
                                            string[] subFolders = Directory.GetDirectories(path);
                                            if (subFolders.Length != 0)
                                            {
                                                AssetDatabase.OpenAsset(AssetDatabase.LoadMainAssetAtPath(subFolders[0]));
                                                break;
                                            }

                                            string[] content = Directory.GetFiles(path);
                                            if (content.Length == 0)
                                            {
                                                EditorGUIUtility.PingObject(Selection.activeObject);
                                            }
                                            else
                                            {
                                                foreach (string file in content)
                                                {
                                                    Object asset = AssetDatabase.LoadMainAssetAtPath(file);
                                                    if (asset != null)
                                                    {
                                                        EditorGUIUtility.PingObject(asset);
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {
                                        EditorGUIUtility.PingObject(Selection.activeObject);
                                    }
                                }
                            }
                        }
                        hoverSelected = true;
                    }
                }
                else if (e.rawType == EventType.MouseUp && r.Contains(e.mousePosition))
                {
                    hoverSelected = true;
                }


                GUIContent cnt = new GUIContent(EditorPlusHistoryInternal.favorites[i].name);

                if (cnt.text == "")
                {
                    cnt.text = "Noname";
                }

                if (showLabels)
                {
                    Texture2D prev = AssetPreview.GetAssetPreview(EditorPlusHistoryInternal.favorites[i]);
                    if (prev == null)
                    {
                        prev = (Texture2D)AssetDatabase.GetCachedIcon(AssetDatabase.GetAssetPath(EditorPlusHistoryInternal.favorites[i]));
                        if (EditorPlusHistoryInternal.favorites[i].GetType() == typeof(UnityEngine.GameObject))
                        {
                            prev = GameObjectLabel;
                        }
                    }
                    GUI.Label(new Rect(r.x, r.y, 32 * widgetHostFavs.WidgetScale, r.height), prev, ((selectedObj == EditorPlusHistoryInternal.favorites[i] && highlightInFavs)) ? new GUIStyle(GUI.skin.box) : new GUIStyle(GUI.skin.button));
                    r.x     += 20 * widgetHostFavs.WidgetScale;
                    r.width -= 20 * widgetHostFavs.WidgetScale;
                }
                GUI.Label(r, cnt, ((selectedObj == EditorPlusHistoryInternal.favorites[i] && highlightInFavs)) ? new GUIStyle(GUI.skin.box) : new GUIStyle(GUI.skin.button));

                Rect lockButton = new Rect(r.x + r.width, r.y + 5, LockClosed.width, LockClosed.height);

                if (GUI.skin != IconsSkinLock)
                {
                    GUI.skin = IconsSkinLock;
                }

                if (GUI.Button(lockButton, LockClosed))
                {
                    EditorPlusHistoryInternal.RemoveFromFavorites(EditorPlusHistoryInternal.favorites[i]);
                    --i;
                    hoverSelected     = true;
                    repaintNextUpdate = true;
                    continue;
                }
            }

            if (GUI.skin != IconsSkin)
            {
                GUI.skin = IconsSkin;
            }


            //historyViewPos = GUI.BeginScrollView(historyView, historyViewPos, new Rect(0, 0, position.width - 30, scrollViewHistHeight));
            historyView.height = scrollViewHistHeight;
            GUI.BeginGroup(historyView);

            //-----------------------------------------------
            //draw history
            for (int i = 0; i < EditorPlusHistoryInternal.history.Count; ++i)
            {
                if (i == historySize)
                {
                    break;
                }

                if (GUI.skin != IconsSkin)
                {
                    GUI.skin = IconsSkin;
                }

                if (EditorPlusHistoryInternal.history[i] == null)
                {
                    EditorPlusHistoryInternal.RemoveFromHistory(EditorPlusHistoryInternal.history[i], true);
                    --i;
                    continue;
                }

                r = widgetHostHistory.Widgets[i];

                if (e.rawType == EventType.MouseDown)
                {
                    if (r.Contains(e.mousePosition))
                    {
                        //Drag object
                        if (e.button == 0)
                        {
                            highlightInFavs = false;
                            if (e.clickCount != 2)
                            {
                                selectedObj = EditorPlusHistoryInternal.history[i];

                                DragAndDrop.PrepareStartDrag();
                                string path = AssetDatabase.GetAssetPath(selectedObj);
                                if (path != "")
                                {
                                    DragAndDrop.paths = new string[] { path }
                                }
                                ;
                                if (selectedObj != null)
                                {
                                    DragAndDrop.objectReferences = new Object[] { selectedObj }
                                }
                                ;

                                DragAndDrop.StartDrag(selectedObj.ToString());
                            }
                            else if (e.clickCount == 2)
                            {
                                Selection.activeObject = EditorPlusHistoryInternal.history[i];

                                if (Selection.activeObject.GetType() != typeof(UnityEngine.GameObject))
                                {
                                    if (Selection.activeObject.GetType() == EditorPlus.EPlusDefaultType)
                                    {
                                        string path = AssetDatabase.GetAssetPath(Selection.activeObject);
                                        if (Directory.Exists(path))
                                        {
                                            string[] subFolders = Directory.GetDirectories(path);
                                            if (subFolders.Length != 0)
                                            {
                                                AssetDatabase.OpenAsset(AssetDatabase.LoadMainAssetAtPath(subFolders[0]));
                                                break;
                                            }

                                            string[] content = Directory.GetFiles(path);
                                            if (content.Length == 0)
                                            {
                                                EditorGUIUtility.PingObject(Selection.activeObject);
                                            }
                                            else
                                            {
                                                foreach (string file in content)
                                                {
                                                    Object asset = AssetDatabase.LoadMainAssetAtPath(file);
                                                    if (asset != null)
                                                    {
                                                        EditorGUIUtility.PingObject(asset);
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {
                                        EditorGUIUtility.PingObject(Selection.activeObject);
                                    }
                                }
                            }
                        }
                        hoverSelected = true;
                    }
                }
                else if (e.rawType == EventType.MouseUp && r.Contains(e.mousePosition))
                {
                    hoverSelected = true;
                }


                GUIContent cnt = new GUIContent(EditorPlusHistoryInternal.history[i].name);

                if (cnt.text == "")
                {
                    cnt.text = "Noname";
                }

                //Debug.Log(GUI.skin.box.CalcSize(new GUIContent(cnt.text)).x + " " + GUI.skin.box.padding.left + " " + GUI.skin.box.padding.right);

                if (showLabels)
                {
                    Texture2D prev = AssetPreview.GetAssetPreview(EditorPlusHistoryInternal.history[i]);
                    if (prev == null)
                    {
                        prev = (Texture2D)AssetDatabase.GetCachedIcon(AssetDatabase.GetAssetPath(EditorPlusHistoryInternal.history[i]));
                        if (EditorPlusHistoryInternal.history[i].GetType() == typeof(UnityEngine.GameObject))
                        {
                            prev = GameObjectLabel;
                        }
                    }
                    GUI.Label(new Rect(r.x, r.y, 32 * widgetHostHistory.WidgetScale, r.height), prev, ((selectedObj == EditorPlusHistoryInternal.history[i] && !highlightInFavs)) ? new GUIStyle(GUI.skin.box) : new GUIStyle(GUI.skin.button));
                    r.x     += 20 * widgetHostHistory.WidgetScale;
                    r.width -= 20 * widgetHostHistory.WidgetScale;
                }

                GUI.Label(r, cnt, ((selectedObj == EditorPlusHistoryInternal.history[i] && !highlightInFavs)) ?  new GUIStyle(GUI.skin.box) : new GUIStyle(GUI.skin.button));

                Rect lockButton = new Rect(r.x + r.width, r.y + 5, LockOpen.width, LockOpen.height);

                if (GUI.skin != IconsSkinLock)
                {
                    GUI.skin = IconsSkinLock;
                }

                if (!EditorPlusHistoryInternal.favorites.Contains(EditorPlusHistoryInternal.history[i]) && GUI.Button(lockButton, LockOpen))
                {
                    EditorPlusHistoryInternal.AddToFavorites(EditorPlusHistoryInternal.history[i]);
                    --i;
                    hoverSelected     = true;
                    repaintNextUpdate = true;
                    continue;
                }
            }
            GUI.EndGroup();
            GUI.EndScrollView();



            //Reset Inspector if mouse up inside the window but not on button

            bool inside = false;

            if (e.mousePosition.x > 0f && e.mousePosition.x < window.position.width && e.mousePosition.y > 0f && e.mousePosition.y < window.position.height)
            {
                inside = true;
            }
            if (inside && !hoverSelected && e.rawType == EventType.MouseUp)
            {
                selectedObj            = null;
                Selection.activeObject = null;
            }

            if ((e.rawType == EventType.MouseUp || e.rawType == EventType.dragPerform) && DragAndDrop.objectReferences != null)
            {
                DragAndDrop.PrepareStartDrag();
            }
        }
    }
}