void RebuildAvailableTools()
        {
            ComponentToolCache activeComponentTool = new ComponentToolCache(m_ActiveToolContext, activeTool);

            ClearCustomEditorTools();

            EditorToolUtility.InstantiateComponentTools(activeToolContext, m_ComponentTools);

            if (activeComponentTool.toolType != null)
            {
                var restoredTool = m_ComponentTools.Find(x => x.editorType == activeComponentTool.toolType);

                if (restoredTool != null)
                {
                    activeTool = restoredTool.GetEditor <EditorTool>();
                }
                else
                {
                    m_PreviousComponentToolCache = activeComponentTool;
                    RestorePreviousPersistentTool();
                }
            }

            availableComponentToolsChanged?.Invoke();
        }
        internal static void OnToolGUI(EditorWindow window)
        {
            activeToolContext.OnToolGUI(window);

            if (Tools.s_Hidden || instance.m_ActiveTool == null)
            {
                return;
            }

            var current = instance.m_ActiveTool;

            using (new EditorGUI.DisabledScope(!current.IsAvailable()))
            {
                current.OnToolGUI(window);
            }

            var evt = Event.current;

            if (evt.type == EventType.KeyDown && evt.keyCode == KeyCode.Escape)
            {
                if (!Tools.viewToolActive &&
                    !EditorToolUtility.IsManipulationTool(EditorToolUtility.GetEnumWithEditorTool(current, instance.m_ActiveToolContext)) &&
                    additionalContextToolTypesCache.All(toolType => toolType != current.GetType()))
                {
                    RestorePreviousPersistentTool();
                    evt.Use();
                }
                else
                {
                    //if is in a Manipulation or additional tool leaves the current context to return to GameObject Context
                    ToolManager.SetActiveContext <GameObjectToolContext>();
                }
            }
        }
Example #3
0
        public static void SetActiveTool(Type type)
        {
            if (!typeof(EditorTool).IsAssignableFrom(type) || type.IsAbstract)
            {
                throw new ArgumentException("Type must be assignable to EditorTool, and not abstract.");
            }

            var attrib = EditorToolUtility.GetEditorToolAttribute(type);

            if (attrib?.targetType != null)
            {
                var tool = EditorToolContext.GetCustomEditorToolOfType(type);

                if (tool == null)
                {
                    throw new InvalidOperationException("The current selection does not contain any objects editable by the CustomEditor tool \"" + type + ".\"");
                }

                SetActiveTool(tool);

                return;
            }

            SetActiveTool(EditorToolContext.GetSingleton(type));
        }
Example #4
0
        public static void RestorePreviousTool()
        {
            var prev = EditorToolUtility.GetEditorToolWithEnum(EditorToolManager.previousTool);

            if (!(prev is NoneTool))
            {
                EditorToolManager.activeTool = prev;
            }
        }
Example #5
0
        bool CollectCustomEditorToolsFromTracker(ActiveEditorTracker tracker, List <EditorTool> list)
        {
            list.Clear();
            var activeIsCustomEditorTool = IsCustomEditorTool(m_ActiveTool);
            var preservedActiveTool      = false;

            EditorToolUtility.GetEditorToolsForTracker(tracker, s_CustomEditorTools);

            foreach (var customEditorTool in s_CustomEditorTools)
            {
                var toolType  = customEditorTool.editorToolType;
                var toolOwner = customEditorTool.owner;

                EditorTool customEditorToolInstance;

                // The only case where a custom editor tool is serialized is when it is the active tool. All other
                // instances are discarded and rebuilt on any tracker rebuild.
                if (activeIsCustomEditorTool && CustomEditorToolIsMatch(toolOwner, toolType, m_ActiveTool))
                {
                    preservedActiveTool = true;

                    m_ActiveTool.m_Targets = toolOwner.targets;
                    m_ActiveTool.m_Target  = toolOwner.target;

                    // domain reload - the owning editor was destroyed and therefore we need to reset the EditMode active
                    if (m_ActiveTool is EditModeTool && toolOwner.GetInstanceID() != UnityEditorInternal.EditMode.ownerID)
                    {
                        UnityEditorInternal.EditMode.EditModeToolStateChanged(toolOwner, ((EditModeTool)m_ActiveTool).editMode);
                    }

                    customEditorToolInstance = m_ActiveTool;
                }
                else
                {
                    customEditorToolInstance = (EditorTool)CreateInstance(toolType, x =>
                    {
                        ((EditorTool)x).m_Targets = toolOwner.targets;
                        ((EditorTool)x).m_Target  = toolOwner.target;
                    });

                    customEditorToolInstance.hideFlags = HideFlags.DontSave;
                }

                list.Add(customEditorToolInstance);

                var editModeTool = customEditorToolInstance as EditModeTool;

                if (editModeTool != null)
                {
                    editModeTool.owner = toolOwner;
                }
            }

            return(preservedActiveTool);
        }
Example #6
0
 internal static EditorTool GetLastCustomTool()
 {
     for (int i = instance.m_ToolHistory.Count - 1; i > -1; i--)
     {
         if (EditorToolUtility.GetEnumWithEditorTool(instance.m_ToolHistory[i]) == Tool.Custom)
         {
             return(instance.m_ToolHistory[i]);
         }
     }
     return(null);
 }
        void ToolChanged()
        {
            if (m_Editor != null)
            {
                DestroyImmediate(m_Editor);
            }
            var activeTool = EditorToolContext.activeTool;

            m_Editor     = Editor.CreateEditor(activeTool);
            titleContent = new GUIContent(EditorToolUtility.GetToolName(activeTool.GetType()));
            Repaint();
        }
Example #8
0
        internal static void GetToolHistory(List <EditorTool> tools, bool customToolsOnly = false)
        {
            tools.Clear();
            instance.CleanupToolHistory();

            for (int i = instance.m_ToolHistory.Count - 1; i > -1; i--)
            {
                if (!customToolsOnly || EditorToolUtility.GetEnumWithEditorTool(instance.m_ToolHistory[i]) == Tool.Custom)
                {
                    tools.Add(instance.m_ToolHistory[i]);
                }
            }
        }
        void RebuildAvailableContexts()
        {
            var activeContextType = activeToolContext.GetType();

            ClearComponentContexts();
            EditorToolUtility.InstantiateComponentContexts(m_ComponentContexts);
            var restoredContext = m_ComponentContexts.Find(x => x.editorType == activeContextType);

            if (restoredContext != null)
            {
                activeToolContext = restoredContext.GetEditor <EditorToolContext>();
            }
        }
Example #10
0
        public static void RestorePreviousPersistentTool()
        {
            var last = EditorToolContext.GetLastTool(x => x && !EditorToolUtility.IsCustomEditorTool(x.GetType()));

            if (last != null)
            {
                SetActiveTool(last);
            }
            else
            {
                SetActiveTool <MoveTool>();
            }
        }
Example #11
0
        public UnityObject InstantiateEditor()
        {
            var toolType = editorType;

            m_Editor = ScriptableObject.CreateInstance(toolType, x =>
            {
                ((IEditor)x).SetTargets(targets);
                ((IEditor)x).SetTarget(target);
            });

            m_Editor.hideFlags = HideFlags.DontSave;

            if (m_Editor is EditorToolContext)
            {
                m_EditorToolScope = EditorToolScope.ToolContext;
            }
            else if (m_Editor is EditorTool tool &&
                     EditorToolUtility.GetEnumWithEditorTool(tool, EditorToolManager.activeToolContext) != Tool.Custom)
            {
                m_EditorToolScope = EditorToolScope.ManipulationToolOverride;
            }
Example #12
0
        public static void SetActiveTool(Type type)
        {
            if (!typeof(EditorTool).IsAssignableFrom(type) || type.IsAbstract)
            {
                throw new ArgumentException("Type must be assignable to EditorTool, and not abstract.");
            }

            if (EditorToolUtility.IsComponentEditor(type))
            {
                var tool = EditorToolManager.GetComponentTool(type);

                if (tool == null)
                {
                    throw new InvalidOperationException("The current selection does not contain any objects editable " +
                                                        $"by the component tool of type: {type}");
                }
                SetActiveTool(tool);
                return;
            }

            SetActiveTool((EditorTool)EditorToolManager.GetSingleton(type));
        }
Example #13
0
        internal static EditorTool GetCustomEditorToolsForType(Type targetType, List <EditorTool> list, bool searchLockedInspectors)
        {
            foreach (var customEditorTool in instance.m_CustomEditorTools)
            {
                if (customEditorTool != null &&
                    EditorToolUtility.GetCustomEditorToolTargetType(customEditorTool) == targetType)
                {
                    list.Add(customEditorTool);
                }
            }

            if (searchLockedInspectors)
            {
                foreach (var customEditorTool in instance.m_LockedCustomEditorTools)
                {
                    if (customEditorTool != null && EditorToolUtility.GetCustomEditorToolTargetType(customEditorTool) == targetType)
                    {
                        list.Add(customEditorTool);
                    }
                }
            }

            return(null);
        }
Example #14
0
        public static void SetActiveContext(Type context)
        {
            if (context != null && (!typeof(EditorToolContext).IsAssignableFrom(context) || context.IsAbstract))
            {
                throw new ArgumentException("Type must be assignable to EditorToolContext, and not abstract.", "context");
            }

            var ctx = context != null ? context : typeof(GameObjectToolContext);

            if (EditorToolUtility.IsComponentEditor(ctx))
            {
                var instance = EditorToolManager.GetComponentContext(ctx);
                if (instance == null)
                {
                    throw new InvalidOperationException("The current selection does not contain any objects editable " +
                                                        $"by the component tool of type: {context}");
                }
                EditorToolManager.activeToolContext = instance;
            }
            else
            {
                EditorToolManager.activeToolContext = EditorToolManager.GetSingleton(ctx) as EditorToolContext;
            }
        }
Example #15
0
 static bool IsCustomEditorTool(EditorTool tool)
 {
     return(EditorToolUtility.IsCustomEditorTool(tool != null ? tool.GetType() : null));
 }
        void RebuildAvailableCustomEditorTools()
        {
            EditorApplication.delayCall -= RebuildAvailableCustomEditorTools;

            // Do not rebuild the cache since objects are serialized, destroyed, deserialized during this phase
            if (m_PlayModeState == PlayModeStateChange.ExitingEditMode ||
                m_PlayModeState == PlayModeStateChange.ExitingPlayMode)
            {
                return;
            }

            var preservedActiveTool = false;

            ClearCustomEditorTools();

            var inspectors = InspectorWindow.GetInspectors();

            // If the shared tracker is locked, use our own tracker instance so that the current selection is always
            // represented. Addresses case where a single locked inspector is open.
            var shared = ActiveEditorTracker.sharedTracker;

            m_CustomEditorTools.Clear();
            m_LockedCustomEditorTools.Clear();

            // Collect editor tools for the shared tracker first
            EditorToolUtility.GetEditorToolsForTracker(shared.isLocked ? tracker : shared, s_CustomEditorTools);

            foreach (var customEditorTool in s_CustomEditorTools)
            {
                if (m_CustomEditorTools.Any(x => x.GetType() == customEditorTool.editorToolType && x.target == customEditorTool.owner.target))
                {
                    continue;
                }
                EditorTool tool;
                preservedActiveTool |= CreateOrRestoreTool(customEditorTool, out tool);
                m_CustomEditorTools.Add(tool);
            }

            // Next, collect tools from locked inspectors
            foreach (var inspector in inspectors)
            {
                if (inspector.isLocked)
                {
                    EditorToolUtility.GetEditorToolsForTracker(inspector.tracker, s_CustomEditorTools);

                    foreach (var customEditorTool in s_CustomEditorTools)
                    {
                        // Don't add duplicate tools to either another locked inspector with the same target, or a shared tracker
                        if (m_CustomEditorTools.Any(x => x.GetType() == customEditorTool.editorToolType && x.target == customEditorTool.owner.target) ||
                            m_LockedCustomEditorTools.Any(x => x.GetType() == customEditorTool.editorToolType && x.target == customEditorTool.owner.target))
                        {
                            continue;
                        }
                        EditorTool tool;
                        preservedActiveTool |= CreateOrRestoreTool(customEditorTool, out tool);
                        m_LockedCustomEditorTools.Add(tool);
                    }
                }
            }

            if (IsCustomEditorTool(m_ActiveTool) && !preservedActiveTool)
            {
                var previous = m_ActiveTool;
                m_PreviousCustomEditorToolContext = new CustomEditorToolContext(m_ActiveTool);
                RestorePreviousTool();
                DestroyImmediate(previous);
            }
        }