Skip to content

Commit 836be9e

Browse files
committed
Make ToolboxEditorHandler public; minor refactor changes
1 parent 8e113bc commit 836be9e

File tree

11 files changed

+78
-91
lines changed

11 files changed

+78
-91
lines changed

Assets/Editor Toolbox/Editor/Drawers/Helpers/DrawerDataStorageBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public abstract class DrawerDataStorageBase
66
{
77
static DrawerDataStorageBase()
88
{
9-
InspectorUtility.OnEditorReload += () =>
9+
ToolboxEditorHandler.OnEditorReload += () =>
1010
{
1111
for (var i = 0; i < storages.Count; i++)
1212
{

Assets/Editor Toolbox/Editor/Drawers/Toolbox/Decorator/DynamicHelpAttributeDrawer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class DynamicHelpAttributeDrawer : ToolboxDecoratorDrawer<DynamicHelpAttr
88
protected override void OnGuiBeginSafe(DynamicHelpAttribute attribute)
99
{
1010
var sourceHandle = attribute.SourceHandle;
11-
var targetObjects = InspectorUtility.CurrentTargetObjects;
11+
var targetObjects = ToolboxEditorHandler.CurrentTargetObjects;
1212
if (ValueExtractionHelper.TryGetValue(sourceHandle, targetObjects, out var value, out var hasMixedValues))
1313
{
1414
var messageText = hasMixedValues ? "-" : value?.ToString();

Assets/Editor Toolbox/Editor/Drawers/Toolbox/Decorator/EditorButtonAttributeDrawer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ private void CallMethods(EditorButtonAttribute attribute, Object[] targetObjects
123123

124124
protected override void OnGuiCloseSafe(EditorButtonAttribute attribute)
125125
{
126-
var targetObjects = InspectorUtility.CurrentTargetObjects;
126+
var targetObjects = ToolboxEditorHandler.CurrentTargetObjects;
127127
if (targetObjects == null || targetObjects.Length == 0)
128128
{
129129
//NOTE: something went really wrong, internal bug or OnGuiBeginSafe was called out of the Toolbox scope

Assets/Editor Toolbox/Editor/Editors/ToolboxScriptedImporterEditor.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public void IgnoreProperty(string propertyPath)
3838
Drawer.IgnoreProperty(propertyPath);
3939
}
4040

41-
4241
Editor IToolboxEditor.ContextEditor => this;
4342
public IToolboxEditorDrawer Drawer { get; } = new ToolboxEditorDrawer();
4443
}

Assets/Editor Toolbox/Editor/ToolboxDrawerModule.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ internal static class ToolboxDrawerModule
2020
[InitializeOnLoadMethod]
2121
internal static void InitializeModule()
2222
{
23-
InspectorUtility.OnEditorReload += ReloadDrawers;
23+
ToolboxEditorHandler.OnEditorReload += ReloadDrawers;
2424
}
2525

2626

@@ -405,7 +405,7 @@ internal static List<Type> GetAllPossibleTargetTypeDrawers()
405405
/// </summary>
406406
internal static ToolboxPropertyHandler GetPropertyHandler(SerializedProperty property)
407407
{
408-
if (InspectorUtility.InToolboxEditor)
408+
if (ToolboxEditorHandler.InToolboxEditor)
409409
{
410410
//NOTE: maybe type-based key?
411411
var propertyKey = property.GetPropertyHashKey();

Assets/Editor Toolbox/Editor/ToolboxEditor.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
32
using UnityEditor;
43

54
namespace Toolbox.Editor
@@ -45,7 +44,6 @@ public void IgnoreProperty(string propertyPath)
4544
Drawer.IgnoreProperty(propertyPath);
4645
}
4746

48-
4947
Editor IToolboxEditor.ContextEditor => this;
5048
/// <inheritdoc />
5149
public virtual IToolboxEditorDrawer Drawer { get; } = new ToolboxEditorDrawer();

Assets/Editor Toolbox/Editor/ToolboxEditorDrawer.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
43
using UnityEditor;
54

65
namespace Toolbox.Editor
Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,94 @@
11
using System;
2+
using System.Collections.Generic;
23

34
namespace 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
}

Assets/Editor Toolbox/Editor/ToolboxLayoutHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ static ToolboxLayoutHandler()
2323
ToolboxEditorHandler.OnCloseToolboxEditor += OnCloseEditor;
2424

2525
//we have to reset the current state between Editors to keep values independent
26-
InspectorUtility.OnEditorReload += ResetCache;
26+
ToolboxEditorHandler.OnEditorReload += ResetCache;
2727
}
2828

2929

Assets/Editor Toolbox/Editor/Utilities/InspectorUtility.cs

Lines changed: 1 addition & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -11,57 +11,8 @@ namespace Toolbox.Editor
1111
using Editor = UnityEditor.Editor;
1212
using Object = UnityEngine.Object;
1313

14-
[InitializeOnLoad]
1514
internal static partial class InspectorUtility
1615
{
17-
static InspectorUtility()
18-
{
19-
//we can use each new Editor to check if 'OnEditorReload' should be called
20-
ToolboxEditorHandler.OnBeginToolboxEditor += CheckReloads;
21-
22-
//we can use 'OnBeginToolboxEditor' and 'OnCloseToolboxEditor' to cache
23-
//processed Editors and determine the real context of the serialization
24-
ToolboxEditorHandler.OnBeginToolboxEditor += OnBeginEditor;
25-
ToolboxEditorHandler.OnBreakToolboxEditor += OnBreakEditor;
26-
ToolboxEditorHandler.OnCloseToolboxEditor += OnCloseEditor;
27-
}
28-
29-
30-
private static int lastCachedEditorId;
31-
private static Editor lastCachedEditor;
32-
private static readonly Stack<Editor> cachedEditors = new Stack<Editor>();
33-
34-
35-
private static void OnBeginEditor(Editor editor)
36-
{
37-
cachedEditors.Push(editor);
38-
}
39-
40-
private static void OnBreakEditor(Editor editor)
41-
{
42-
cachedEditors.Clear();
43-
}
44-
45-
private static void OnCloseEditor(Editor editor)
46-
{
47-
if (InToolboxEditor)
48-
{
49-
cachedEditors.Pop();
50-
}
51-
}
52-
53-
private static void CheckReloads(Editor editor)
54-
{
55-
//NOTE: it means that last Editor was null or disposed, anyway we probably want to reload drawers-related cache
56-
if (lastCachedEditor == null || lastCachedEditorId != lastCachedEditor.GetInstanceID())
57-
{
58-
lastCachedEditor = editor;
59-
lastCachedEditorId = editor.GetInstanceID();
60-
OnEditorReload?.Invoke();
61-
}
62-
}
63-
64-
6516
/// <summary>
6617
/// Forces available Inspector Windows to repaint.
6718
/// </summary>
@@ -84,7 +35,7 @@ internal static void RepaintInspectors()
8435
}
8536
}
8637

87-
//NOTE: none reflection way
38+
//NOTE: none reflection way but slower
8839
//UnityEditorInternal.InternalEditorUtility.RepaintAllViews();
8940
}
9041

@@ -132,26 +83,6 @@ internal static void SimulateOnValidate(Object target)
13283
methodInfo.Invoke(target, null);
13384
}
13485
}
135-
136-
137-
/// <summary>
138-
/// Event fired every time when <see cref="ToolboxEditor"/>s were re-created.
139-
/// </summary>
140-
internal static event Action OnEditorReload;
141-
142-
143-
/// <summary>
144-
/// Last cached targetObjects from the currently processed <see cref="ToolboxEditor"/>.
145-
/// </summary>
146-
internal static Object[] CurrentTargetObjects
147-
{
148-
get => cachedEditors.Count > 0 ? cachedEditors.Peek().targets : new Object[0];
149-
}
150-
151-
internal static bool InToolboxEditor
152-
{
153-
get => cachedEditors.Count > 0;
154-
}
15586
}
15687

15788
internal static partial class InspectorUtility

0 commit comments

Comments
 (0)