-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make ToolboxEditorHandler public; minor refactor changes
- Loading branch information
Showing
11 changed files
with
78 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
using UnityEditor; | ||
|
||
namespace Toolbox.Editor | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,94 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Toolbox.Editor | ||
{ | ||
using Editor = UnityEditor.Editor; | ||
using Object = UnityEngine.Object; | ||
|
||
internal static class ToolboxEditorHandler | ||
public static class ToolboxEditorHandler | ||
{ | ||
private static int lastCachedEditorId; | ||
private static Editor lastCachedEditor; | ||
private static readonly Stack<Editor> cachedEditors = new Stack<Editor>(); | ||
|
||
private static void OnBeginEditor(Editor editor) | ||
{ | ||
//NOTE: it means that last Editor was null or disposed, anyway we probably want to reload drawers-related cache | ||
if (lastCachedEditor == null || lastCachedEditorId != lastCachedEditor.GetInstanceID()) | ||
{ | ||
lastCachedEditor = editor; | ||
lastCachedEditorId = editor.GetInstanceID(); | ||
OnEditorReload?.Invoke(); | ||
} | ||
|
||
cachedEditors.Push(editor); | ||
OnBeginToolboxEditor?.Invoke(editor); | ||
} | ||
|
||
private static void OnBreakEditor(Editor editor) | ||
{ | ||
cachedEditors.Clear(); | ||
OnBreakToolboxEditor?.Invoke(editor); | ||
} | ||
|
||
private static void OnCloseEditor(Editor editor) | ||
{ | ||
if (InToolboxEditor) | ||
{ | ||
cachedEditors.Pop(); | ||
} | ||
|
||
OnCloseToolboxEditor?.Invoke(editor); | ||
ContextEditor = null; | ||
} | ||
|
||
public static void HandleToolboxEditor(IToolboxEditor editor) | ||
{ | ||
try | ||
{ | ||
ContextEditor = editor.ContextEditor; | ||
OnBeginToolboxEditor?.Invoke(ContextEditor); | ||
OnBeginEditor(ContextEditor); | ||
editor.DrawCustomInspector(); | ||
} | ||
catch (Exception) | ||
{ | ||
//make sure to catch all Exceptions (especially ExitGUIException), | ||
//it will allow us to safely dispose all layout-based controls, etc. | ||
OnBreakToolboxEditor?.Invoke(ContextEditor); | ||
OnBreakEditor(ContextEditor); | ||
throw; | ||
} | ||
finally | ||
{ | ||
OnCloseToolboxEditor?.Invoke(ContextEditor); | ||
ContextEditor = null; | ||
OnCloseEditor(ContextEditor); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Event fired every time when <see cref="ToolboxEditor"/>s were re-created. | ||
/// </summary> | ||
internal static event Action OnEditorReload; | ||
|
||
internal static event Action<Editor> OnBeginToolboxEditor; | ||
internal static event Action<Editor> OnBreakToolboxEditor; | ||
internal static event Action<Editor> OnCloseToolboxEditor; | ||
|
||
public static event Action<Editor> OnBeginToolboxEditor; | ||
public static event Action<Editor> OnBreakToolboxEditor; | ||
public static event Action<Editor> OnCloseToolboxEditor; | ||
internal static bool InToolboxEditor | ||
{ | ||
get => cachedEditors.Count > 0; | ||
} | ||
|
||
/// <summary> | ||
/// Last cached targetObjects from the currently processed <see cref="ToolboxEditor"/>. | ||
/// </summary> | ||
internal static Object[] CurrentTargetObjects | ||
{ | ||
get => cachedEditors.Count > 0 ? cachedEditors.Peek().targets : new Object[0]; | ||
} | ||
|
||
/// <summary> | ||
/// Currently maintained <see cref="Editor"/>. | ||
/// </summary> | ||
public static Editor ContextEditor { get; private set; } | ||
internal static Editor ContextEditor { get; private set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters