Ejemplo n.º 1
0
        void OnGUI()
        {
            if (init == false)
            {
                return;
            }

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

            Event e = Event.current;

            bool insideWindow = false;

            if (e.mousePosition.x > 0f && e.mousePosition.x < position.width && e.mousePosition.y > 0f && e.mousePosition.y < position.height && (DragAndDrop.objectReferences.Length > 0 && AssetDatabase.GetAssetPath(DragAndDrop.objectReferences[0]) != ""))
            {
                insideWindow           = true;
                DragAndDrop.visualMode = DragAndDropVisualMode.Link;

                if (e.rawType == EventType.DragPerform)
                {
                    foreach (Object obj in DragAndDrop.objectReferences)
                    {
                        if (AssetDatabase.GetAssetPath(obj) != "")
                        {
                            //if list is empty, add and continue
                            if (hotbarObjects.Count == 0)
                            {
                                hotbarObjects.Add(obj);
                                continue;
                            }

                            int index = widgetHost.FindClosestWidget(e.mousePosition);

                            if (index == hotbarObjects.IndexOf(obj))
                            {
                                selectedObj            = obj;
                                Selection.activeObject = obj;
                                break;
                            }
                            else if (hotbarObjects.Contains(obj))
                            {
                                hotbarObjects.Remove(obj);
                                hotbarPreviewCache.Remove(obj);
                            }

                            //check if widget + margin.x contains mouse position and insert there
                            if (e.mousePosition.x > widgetHost.Widgets[index].x && e.mousePosition.x <widgetHost.Widgets[index].x + widgetHost.Widgets[index].width + widgetHost.WidgetMargin.x && e.mousePosition.y> widgetHost.Widgets[index].y && e.mousePosition.y < widgetHost.Widgets[index].y + widgetHost.Widgets[index].height)
                            {
                                //left or
                                if (e.mousePosition.x < widgetHost.Widgets[index].x + widgetHost.Widgets[index].width / 2)
                                {
                                    hotbarObjects.Insert(index, obj);
                                }
                                //right
                                else
                                {
                                    if (index < hotbarObjects.Count)
                                    {
                                        hotbarObjects.Insert(index + 1, obj);
                                    }
                                    else
                                    {
                                        hotbarObjects.Add(obj);
                                    }
                                }
                            }
                            else
                            {
                                hotbarObjects.Add(obj);
                            }
                        }
                    }
                    if (useAutoSave)
                    {
                        SaveHotbar();
                    }
                }
            }

            if (e.isKey && e.keyCode == KeyCode.Delete && selectedObj != null)
            {
                hotbarObjects.Remove(selectedObj);
                hotbarPreviewCache.Remove(selectedObj);
                if (useAutoSave)
                {
                    SaveHotbar();
                }
            }

            //reset
            if (e.rawType == EventType.MouseDown)
            {
                selectedObj = null;
            }

            widgetHost.Area         = position;
            widgetHost.WidgetsCount = hotbarObjects.Count;
            widgetHost.Update();

            bool hoverSelected = false;

            float scrollviewHeight = position.height;

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

            GUISkin old = GUI.skin;

            GUI.skin           = EditorPlus.ScrollviewSkin;
            scrollviewPosition = GUI.BeginScrollView(new Rect(0, 0, position.width, position.height), scrollviewPosition, new Rect(0, 0, position.width - 30, scrollviewHeight));
            GUI.skin           = old;

            for (int i = 0; i < hotbarObjects.Count; ++i)
            {
                if (hotbarObjects[i] == null)
                {
                    hotbarObjects.RemoveAt(i);
                    --i;
                    widgetHost.WidgetsCount--;
                    continue;
                }

                GUIContent cnt = new GUIContent();
                Texture2D  preview;
                hotbarPreviewCache.TryGetValue(hotbarObjects[i], out preview);
                if (preview != null)
                {
                    cnt.image = preview;
                }
                else
                {
                    cnt.image = AssetDatabase.GetCachedIcon(AssetDatabase.GetAssetPath(hotbarObjects[i]));
                }

                if (useTooltip)
                {
                    cnt.tooltip = hotbarObjects[i].name;
                }

                Rect r = widgetHost.Widgets[i];


                //object selection
                if (e.rawType == EventType.MouseDown)
                {
                    if (r.Contains(e.mousePosition))
                    {
                        //Drag object
                        if (e.button == 0)
                        {
                            if (e.clickCount != 2)
                            {
                                selectedObj = hotbarObjects[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());

                                e.Use();
                            }
                            else if (e.clickCount == 2)
                            {
                                if (hotbarObjects[i].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)
                                        {
                                            AssetDatabase.OpenAsset(hotbarObjects[i]);
                                        }
                                        else
                                        {
                                            foreach (string file in content)
                                            {
                                                Object asset = AssetDatabase.LoadMainAssetAtPath(file);
                                                if (asset != null)
                                                {
                                                    EditorGUIUtility.PingObject(asset);
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if (hotbarObjects[i].ToString().Contains("SceneAsset")) //deselect to prevent annoying merge warnings
                                        {
                                            Selection.activeObject = null;
                                        }
                                        AssetDatabase.OpenAsset(hotbarObjects[i]);
                                    }
                                }
                                else
                                {
                                    AssetDatabase.OpenAsset(hotbarObjects[i]);
                                }
                            }
                        }
                        else if (e.button == 1)
                        {
                            hotbarPreviewCache.Remove(hotbarObjects[i]);
                            hotbarObjects.RemoveAt(i);
                            --i;
                            GUI.EndScrollView();
                            Repaint();
                            e.Use();
                            return;
                        }
                        hoverSelected = true;
                    }
                }
                else if (e.rawType == EventType.MouseUp && r.Contains(e.mousePosition))
                {
                    hoverSelected = true;
                }

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

                int limit = textLimit;
                if (limit >= hotbarObjects[i].name.Length)
                {
                    limit = hotbarObjects[i].name.Length;
                }

                cnt = new GUIContent(hotbarObjects[i].name.Substring(0, limit));

                if (cnt.text.Length > textLimit)
                {
                    cnt.text.Remove(cnt.text.Length - 1, 1);
                }
                r.height = 20f;

                //drop shadow
                GUIStyle st = new GUIStyle(GUI.skin.label);
                st.normal.textColor = new Color(0.26f, 0.26f, 0.26f);
                r.x += 1f;
                r.y += 1f;
                GUI.Label(r, cnt, st);

                //reset
                r.x -= 1f;
                r.y -= 1f;

                //name
                GUI.Label(r, cnt);
            }

            GUI.EndScrollView();


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

            if (insideWindow && !hoverSelected && e.rawType == EventType.MouseUp)
            {
                selectedObj            = null;
                Selection.activeObject = null;
            }
        }
Ejemplo n.º 2
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();
            }
        }
    }
}