11using System ;
2+ using System . Collections . Generic ;
23
34namespace Toolbox . Editor
45{
56 using Editor = UnityEditor . Editor ;
7+ using Object = UnityEngine . Object ;
68
7- internal static class ToolboxEditorHandler
9+ public static class ToolboxEditorHandler
810 {
11+ private static int lastCachedEditorId ;
12+ private static Editor lastCachedEditor ;
13+ private static readonly Stack < Editor > cachedEditors = new Stack < Editor > ( ) ;
14+
15+ private static void OnBeginEditor ( Editor editor )
16+ {
17+ //NOTE: it means that last Editor was null or disposed, anyway we probably want to reload drawers-related cache
18+ if ( lastCachedEditor == null || lastCachedEditorId != lastCachedEditor . GetInstanceID ( ) )
19+ {
20+ lastCachedEditor = editor ;
21+ lastCachedEditorId = editor . GetInstanceID ( ) ;
22+ OnEditorReload ? . Invoke ( ) ;
23+ }
24+
25+ cachedEditors . Push ( editor ) ;
26+ OnBeginToolboxEditor ? . Invoke ( editor ) ;
27+ }
28+
29+ private static void OnBreakEditor ( Editor editor )
30+ {
31+ cachedEditors . Clear ( ) ;
32+ OnBreakToolboxEditor ? . Invoke ( editor ) ;
33+ }
34+
35+ private static void OnCloseEditor ( Editor editor )
36+ {
37+ if ( InToolboxEditor )
38+ {
39+ cachedEditors . Pop ( ) ;
40+ }
41+
42+ OnCloseToolboxEditor ? . Invoke ( editor ) ;
43+ ContextEditor = null ;
44+ }
45+
946 public static void HandleToolboxEditor ( IToolboxEditor editor )
1047 {
1148 try
1249 {
1350 ContextEditor = editor . ContextEditor ;
14- OnBeginToolboxEditor ? . Invoke ( ContextEditor ) ;
51+ OnBeginEditor ( ContextEditor ) ;
1552 editor . DrawCustomInspector ( ) ;
1653 }
1754 catch ( Exception )
1855 {
1956 //make sure to catch all Exceptions (especially ExitGUIException),
2057 //it will allow us to safely dispose all layout-based controls, etc.
21- OnBreakToolboxEditor ? . Invoke ( ContextEditor ) ;
58+ OnBreakEditor ( ContextEditor ) ;
2259 throw ;
2360 }
2461 finally
2562 {
26- OnCloseToolboxEditor ? . Invoke ( ContextEditor ) ;
27- ContextEditor = null ;
63+ OnCloseEditor ( ContextEditor ) ;
2864 }
2965 }
3066
67+ /// <summary>
68+ /// Event fired every time when <see cref="ToolboxEditor"/>s were re-created.
69+ /// </summary>
70+ internal static event Action OnEditorReload ;
71+
72+ internal static event Action < Editor > OnBeginToolboxEditor ;
73+ internal static event Action < Editor > OnBreakToolboxEditor ;
74+ internal static event Action < Editor > OnCloseToolboxEditor ;
3175
32- public static event Action < Editor > OnBeginToolboxEditor ;
33- public static event Action < Editor > OnBreakToolboxEditor ;
34- public static event Action < Editor > OnCloseToolboxEditor ;
76+ internal static bool InToolboxEditor
77+ {
78+ get => cachedEditors . Count > 0 ;
79+ }
80+
81+ /// <summary>
82+ /// Last cached targetObjects from the currently processed <see cref="ToolboxEditor"/>.
83+ /// </summary>
84+ internal static Object [ ] CurrentTargetObjects
85+ {
86+ get => cachedEditors . Count > 0 ? cachedEditors . Peek ( ) . targets : new Object [ 0 ] ;
87+ }
3588
3689 /// <summary>
3790 /// Currently maintained <see cref="Editor"/>.
3891 /// </summary>
39- public static Editor ContextEditor { get ; private set ; }
92+ internal static Editor ContextEditor { get ; private set ; }
4093 }
4194}
0 commit comments