示例#1
0
        /// <inheritdoc cref="IDrawer.OnMiddleClick" />
        public override void OnMiddleClick(Event inputEvent)
        {
            base.OnMiddleClick(inputEvent);

            var typeValue = Value;

            if (typeValue != null && inputEvent.type != EventType.Used)
            {
                var monoScript = FileUtility.FindScriptFile(typeValue);
                if (monoScript != null)
                {
                                        #if DEV_MODE && UNITY_EDITOR
                    Debug.Log("Pinging script asset " + UnityEditor.AssetDatabase.GetAssetPath(monoScript) + "...", monoScript);
                                        #endif

                    DrawGUI.Active.PingObject(monoScript);
                    DrawGUI.Use(inputEvent);
                    return;
                }

                                #if DEV_MODE
                Debug.Log("script by type " + typeValue.FullName + " not found...");
                                #endif
            }
                        #if DEV_MODE
            else
            {
                Debug.Log("typeValue=" + StringUtils.ToString(typeValue) + ", Event.type=" + inputEvent.type);
            }
                        #endif
        }
示例#2
0
 /// <summary>
 /// Adds debugging entries mean for developers only to opening right click menu.
 /// Invoked when control is right clicked with DEV_MODE preprocessor directive
 /// and the control key is held down.
 /// </summary>
 /// <param name="menu"> [in,out] The opening menu into which to add the menu items. </param>
 protected virtual void AddDevModeDebuggingEntriesToRightClickMenu(ref Menu menu)
 {
                 #if DEV_MODE && UNITY_EDITOR
     string scriptFilename = FileUtility.FilenameFromType(GetType());
     menu.Add("Debugging/Edit " + scriptFilename + ".cs", () =>
     {
         var script = FileUtility.FindScriptFile(GetType());
         if (script != null)
         {
             UnityEditor.AssetDatabase.OpenAsset(script);
         }
         else
         {
             Debug.LogError("FileUtility.FindScriptFilepath could not find file " + scriptFilename + ".cs");
         }
     });
     menu.Add("Debugging/Print Full State", PrintFullStateForDevs);
                 #endif
 }
        private bool Draw(ref bool addedComponent)
        {
            if (instance == null)
            {
                return(false);
            }

            bool dirty = false;

            GUI.Label(headerLabelRect, categoryLabel, InspectorPreferences.Styles.PopupMenuTitle);

                        #if UNITY_EDITOR
            if (GUI.Button(createScriptButtonRect, GUIContent.none, InspectorPreferences.Styles.Blank))
            {
                dirty = true;
                openCreateNewScriptViewNextLayout = true;
            }
                        #endif

            bool hasFilter    = FilterString.Length > 0;
            bool hasBackArrow = !hasFilter && activeItem != null;

            if (hasBackArrow && GUI.Button(backArrowRect, GUIContent.none, InspectorPreferences.Styles.Blank))
            {
                                #if UNITY_EDITOR
                if (!openCreateNewScriptViewNextLayout)
                                #endif
                {
                    goBackLevelNextLayout = true;
                    dirty = true;
                }
            }

            if (DrawFilterField())
            {
                dirty = true;
            }

            var setScrollPos = GUI.BeginScrollView(viewRect, scrollPos, contentRect);
            {
                if (setScrollPos.y != scrollPos.y)
                {
                    SetScrollPos(setScrollPos.y, true);
                    dirty = true;
                }

                var memberRect = contentRect;
                DrawGUI.Active.ColorRect(memberRect, BgColor);

                memberRect.height = DrawGUI.SingleLineHeight;

                //only start drawing from first visible member
                memberRect.y += firstVisibleIndex * DrawGUI.SingleLineHeight;

                int last         = lastVisibleIndex - firstVisibleIndex;
                int visibleCount = visibleMembers.Length;
                if (last >= visibleCount)
                {
                                        #if DEV_MODE
                    Debug.LogWarning(ToString() + " - last (" + last + ") >= visibleCount (" + visibleCount + ") with firstVisibleIndex=" + firstVisibleIndex + " and lastVisibleIndex=" + lastVisibleIndex);
                                        #endif

                    last = visibleCount - 1;
                }

                var mousePos = Event.current.mousePosition;

                for (int n = 0; n <= last; n++)
                {
                    int memberIndex = firstVisibleIndex + n;

                    //TEMP
                    if (last >= visibleMembers.Length)
                    {
                                                #if DEV_MODE
                        Debug.LogError("n=" + n + ", last=" + last + ", visibleMembers.Length=" + visibleMembers.Length + ", ");
                                                #endif
                        break;
                    }

                    var member = visibleMembers[n];

                    bool selected = memberIndex == selectedComponentIndex;

                    if (selected)
                    {
                        DrawGUI.Active.ColorRect(memberRect, InspectorUtility.Preferences.theme.BackgroundSelected);
                    }

                    if (memberRect.Contains(mousePos))
                    {
                        if (memberIndex != selectedComponentIndex)
                        {
                            SetSelectedMember(memberIndex, false);
                            dirty = true;
                        }
                    }

                    if (visibleMembersHaveConflicts[n])
                    {
                        GUI.enabled = false;
                    }

                    if (member.Draw(memberRect))
                    {
                        switch (Event.current.button)
                        {
                        case 0:
                            break;

                        case 1:
                            member.OpenContextMenu(Event.current, memberRect, true, Part.Base);
                            return(true);

                        case 2:
                            var script = FileUtility.FindScriptFile(member.Type);
                            if (script != null)
                            {
                                DrawGUI.Active.PingObject(script);
                            }
                            return(true);

                        default:
                            return(false);
                        }

                        var type = member.Type;

                        dirty = true;

                        var itemDrawer = member as AddComponentMenuItemDrawer;

                        if (type == null)
                        {
                            SetActiveItem(itemDrawer.Item);
                            break;
                        }

                        addedComponent = true;

                        if (itemDrawer.nameBy)
                        {
                            string setName           = StringUtils.SplitPascalCaseToWords(type.Name);
                            var    gameObjectDrawers = inspector.State.drawers.Members;
                            for (int d = gameObjectDrawers.Length - 1; d >= 0; d--)
                            {
                                var gameObjectDrawer = gameObjectDrawers[d] as IGameObjectDrawer;
                                if (gameObjectDrawer != null)
                                {
                                    var gameObjects = gameObjectDrawer.GameObjects;
                                    for (int g = gameObjectDrawers.Length - 1; g >= 0; g--)
                                    {
                                        var gameObject = gameObjects[g];
                                        gameObject.name = setName;
                                    }
                                }
                            }
                        }

                        inspector.OnNextLayout(() => AddComponent(type));
                        if (Event.current.type == EventType.MouseDown)
                        {
                            DrawGUI.Use(Event.current);
                        }
                        break;
                    }

                    GUI.enabled   = true;
                    memberRect.y += DrawGUI.SingleLineHeight;
                }

                GUI.EndScrollView();
            }

            if (clearingText)
            {
                if (Event.current.type == EventType.Layout)
                {
                    clearingText = false;
                }
                GUI.changed = true;
                dirty       = true;
            }
            else
            {
                if (!DrawGUI.EditingTextField)
                {
                    DrawGUI.EditingTextField = true;
                    GUI.changed = true;
                    dirty       = true;
                }

                if (!string.Equals(GUI.GetNameOfFocusedControl(), ControlName))
                {
                    DrawGUI.FocusControl(ControlName);
                    GUI.changed = true;
                    dirty       = true;
                }
            }

            if (hasBackArrow)
            {
                DrawGUI.Active.ColorRect(backArrowBgRect, BgColorNavigationBar);
                DrawGUI.Active.AddCursorRect(backArrowRect, MouseCursor.Link);
                GUI.Label(backArrowRect, GUIContent.none, backArrowStyle);
            }

                        #if UNITY_EDITOR
            DrawGUI.Active.ColorRect(createScriptBgRect, BgColorNavigationBar);
            DrawGUI.Active.AddCursorRect(createScriptButtonRect, MouseCursor.Link);
            GUI.Label(createScriptButtonRect, GUIContent.none, createNewStyle);
                        #endif

            DrawGUI.DrawLine(dividerRect1, InspectorUtility.Preferences.theme.ComponentSeparatorLine);
            DrawGUI.DrawLine(dividerRect2, InspectorUtility.Preferences.theme.ComponentSeparatorLine);

            return(dirty);
        }
        public bool OnGUI(ref bool addedComponent)
        {
            bool dirty = false;

            var e         = Event.current;
            var eventType = e.type;

            switch (eventType)
            {
            case EventType.KeyDown:
                if (OnKeyboardInputGiven(e))
                {
                    return(true);
                }
                break;

            case EventType.MouseDown:
            case EventType.MouseMove:
                dirty = true;
                break;

            case EventType.Layout:
                if (goBackLevelNextLayout)
                {
                    goBackLevelNextLayout = false;
                    GoBackLevel();
                }
                                        #if UNITY_EDITOR
                else if (openCreateNewScriptViewNextLayout)
                {
                    openCreateNewScriptViewNextLayout = false;

                    string inNamespace = null;
                    string saveIn      = null;
                    if (activeItem != null)
                    {
                        var type = activeItem.TypeOrAnyChildType();
                        if (type != null)
                        {
                            var existingScript = FileUtility.FindScriptFile(type);
                            if (existingScript != null)
                            {
                                inNamespace = type.Namespace;
                                saveIn      = FileUtility.GetParentDirectory(UnityEditor.AssetDatabase.GetAssetPath(existingScript));
                            }
                        }
                    }

                    if (inNamespace == null)
                    {
                        inNamespace = InspectorUtility.Preferences.defaultNamespace;
                    }

                    if (saveIn == null)
                    {
                        saveIn = InspectorUtility.Preferences.defaultScriptPath;
                    }

                    if (filter.Length > 0)
                    {
                        Platform.Active.SetPrefs("PI.CreateScriptWizard/Name", filter);
                    }
                    Platform.Active.SetPrefs("PI.CreateScriptWizard/Namespace", inNamespace);
                    Platform.Active.SetPrefs("PI.CreateScriptWizard/SaveIn", saveIn);

                    Platform.Active.SetPrefs("PI.CreateScriptWizard/Template", "MonoBehaviour");
                    Platform.Active.SetPrefs("PI.CreateScriptWizard/AttachTo", target.UnityObject.GetInstanceID());

                    inspector.OnNextLayout(() => DrawGUI.ExecuteMenuItem(PowerInspectorMenuItemPaths.CreateScriptWizardFromCreateMenu));

                    Close();
                    return(true);
                }
                                        #endif
                else if (clearTextNextLayout)
                {
                    clearTextNextLayout = false;
                    ClearText();
                }
                break;
            }

            if (Draw(ref addedComponent))
            {
                dirty = true;
            }

            return(dirty);
        }
        private static void ApplyContextMenuPreferences()
        {
            // Currently cannot call Setup for InspectorPreferences because ApplyContextMenuPreferences is called outside of OnGUI.
            InspectorPreferences preferences;

            try
            {
                preferences = InspectorUtility.Preferences;
            }
                        #if DEV_MODE
            catch (NullReferenceException e)
            {
                Debug.LogError("PowerInspectorMenuItems failed to find Power Inspector Preferences asset.\n" + e);
                        #else
            catch (Exception)
            {
                        #endif
                return;
            }

            if (preferences == null)
            {
                                #if DEV_MODE
                Debug.LogWarning("PowerInspectorMenuItems failed to find Power Inspector Preferences asset.");
                                #endif
                return;
            }

            var scriptFile = FileUtility.FindScriptFile(typeof(PowerInspectorMenuItems));
            if (scriptFile == null)
            {
                Debug.LogError("PowerInspectorMenuItems failed to find script asset for itself.");
                return;
            }

            var scriptText = scriptFile.text;

            int preferencesStart = scriptText.IndexOf("#region ContextMenuPreferences", StringComparison.Ordinal) + 30;
            if (preferencesStart == -1)
            {
                throw new InvalidDataException("#region ContextMenuPreferences missing from PowerInspectorMenuItems.cs");
            }
            int preferencesEnd = scriptText.IndexOf("#endregion", preferencesStart, StringComparison.Ordinal);
            if (preferencesEnd == -1)
            {
                throw new InvalidDataException("#endregion missing from PowerInspectorMenuItems.cs");
            }
            string beforePreferences = scriptText.Substring(0, preferencesStart);
            string afterPreferences  = scriptText.Substring(preferencesEnd);
            string menuPreferences   = scriptText.Substring(preferencesStart, preferencesEnd - preferencesStart);

            bool scriptChanged = false;

            var enabledMenuItems = preferences.defaultInspector.enhanceUnityObjectContextMenu;
            SetContextMenuItemEnabled(ref menuPreferences, enabledMenuItems.HasFlag(ObjectContextMenuItems.ViewInPowerInspector), "#define PI_ENABLE_CONTEXT_INSPECT", ref scriptChanged);
            SetContextMenuItemEnabled(ref menuPreferences, enabledMenuItems.HasFlag(ObjectContextMenuItems.PeekInPowerInspector), "#define PI_ENABLE_CONTEXT_PEEK", ref scriptChanged);

            SetContextMenuItemEnabled(ref menuPreferences, !preferences.disabledMenuItems.HasFlag(MenuItems.Peek), "#define PI_ENABLE_MENU_PEEK", ref scriptChanged);
            SetContextMenuItemEnabled(ref menuPreferences, !preferences.disabledMenuItems.HasFlag(MenuItems.Reset), "#define PI_ENABLE_MENU_RESET", ref scriptChanged);

            if (scriptChanged)
            {
                string localPath = AssetDatabase.GetAssetPath(scriptFile);
                string fullPath  = FileUtility.LocalToFullPath(localPath);
                File.WriteAllText(fullPath, beforePreferences + menuPreferences + afterPreferences);
                EditorUtility.SetDirty(scriptFile);
                AssetDatabase.Refresh();
            }
        }
示例#6
0
        /// <inheritdoc/>
        public virtual void Draw(Rect toolbarPosition)
        {
            var e         = Event.current;
            var eventType = e.type;

                        #if DEV_MODE && PI_ASSERTATIONS
            Debug.Assert(BackgroundStyle != null);
                        #endif

            var backgroundStyle = BackgroundStyle;

            GUI.Label(toolbarPosition, GUIContent.none, backgroundStyle);

            bool isLayoutEvent      = eventType == EventType.Layout;
            bool toolbarMouseovered = inspector.MouseoveredPart == InspectorPart.Toolbar;

            if (updateToolbarItemBounds)
            {
                UpdateVisibleItemBounds(toolbarPosition);

                if (isLayoutEvent)
                {
                    updateToolbarItemBounds = false;
                }
            }

            int count = visibleItems.Length;

            bool detectMouseover = toolbarMouseovered && isLayoutEvent;
            if (detectMouseover)
            {
                mouseoverVisibleItemIndex = -1;
                for (int n = 0; n < count; n++)
                {
                    var item   = visibleItems[n];
                    var bounds = item.Bounds;
                    if (bounds.Contains(Event.current.mousePosition))
                    {
                        mouseoverVisibleItemIndex = n;
                    }
                }
            }

            // new: style all items with toolbar style by default
            if (Event.current.type == EventType.Repaint)
            {
                for (int n = 0; n < count; n++)
                {
                    var item   = visibleItems[n];
                    var bounds = item.Bounds;
                    item.DrawBackground(bounds, backgroundStyle);
                }
            }

            for (int n = 0; n < count; n++)
            {
                var item   = visibleItems[n];
                var bounds = item.Bounds;
                item.Draw(bounds);
            }

            if (selectedItemIndex != -1)
            {
                var item = items[selectedItemIndex];
                item.DrawSelectionRect(item.Bounds);
            }

                        #if DEV_MODE && UNITY_EDITOR
            if (e.control && (eventType == EventType.ContextClick || (eventType == EventType.MouseDown && e.button == 1)) && toolbarPosition.Contains(e.mousePosition))
            {
                var menu = Menu.Create();

                if (mouseoverVisibleItemIndex != -1)
                {
                    var item = visibleItems[mouseoverVisibleItemIndex];
                    menu.Add("Edit " + item.GetType().Name + ".cs", () =>
                    {
                        var script = FileUtility.FindScriptFile(item.GetType());
                        if (script != null)
                        {
                            UnityEditor.AssetDatabase.OpenAsset(script);
                        }
                        else
                        {
                            Debug.LogError("FileUtility.FindScriptFilepath could not find file " + GetType().Name + ".cs");
                        }
                    });
                }


                menu.Add("Edit " + GetType().Name + ".cs", () =>
                {
                    var script = FileUtility.FindScriptFile(GetType());
                    if (script != null)
                    {
                        UnityEditor.AssetDatabase.OpenAsset(script);
                    }
                    else
                    {
                        Debug.LogError("FileUtility.FindScriptFilepath could not find file " + GetType().Name + ".cs");
                    }
                });
                menu.Open();
            }
                        #endif

            // Prevent controls in the inspector viewport's scroll view from reacting to mouse inputs behind the toolbar.
            if (GUI.Button(toolbarPosition, GUIContent.none, InspectorPreferences.Styles.Blank))
            {
                                #if DEV_MODE
                Debug.LogWarning("Consumed click event behind toolbar via GUI.Button: " + StringUtils.ToString(Event.current));
                                #endif
            }
        }
示例#7
0
 public UnityEditor.MonoScript GetMonoScript()
 {
     return(FileUtility.FindScriptFile(Type));
 }
示例#8
0
        public UnityEditor.MonoScript GetMonoScript()
        {
            var type = Type;

            return(type != null?FileUtility.FindScriptFile(type) : null);
        }