Skip to content

Commit

Permalink
Make ToolboxEditorHandler public; minor refactor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
arimger committed Sep 10, 2023
1 parent 8e113bc commit 836be9e
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public abstract class DrawerDataStorageBase
{
static DrawerDataStorageBase()
{
InspectorUtility.OnEditorReload += () =>
ToolboxEditorHandler.OnEditorReload += () =>
{
for (var i = 0; i < storages.Count; i++)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class DynamicHelpAttributeDrawer : ToolboxDecoratorDrawer<DynamicHelpAttr
protected override void OnGuiBeginSafe(DynamicHelpAttribute attribute)
{
var sourceHandle = attribute.SourceHandle;
var targetObjects = InspectorUtility.CurrentTargetObjects;
var targetObjects = ToolboxEditorHandler.CurrentTargetObjects;
if (ValueExtractionHelper.TryGetValue(sourceHandle, targetObjects, out var value, out var hasMixedValues))
{
var messageText = hasMixedValues ? "-" : value?.ToString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private void CallMethods(EditorButtonAttribute attribute, Object[] targetObjects

protected override void OnGuiCloseSafe(EditorButtonAttribute attribute)
{
var targetObjects = InspectorUtility.CurrentTargetObjects;
var targetObjects = ToolboxEditorHandler.CurrentTargetObjects;
if (targetObjects == null || targetObjects.Length == 0)
{
//NOTE: something went really wrong, internal bug or OnGuiBeginSafe was called out of the Toolbox scope
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public void IgnoreProperty(string propertyPath)
Drawer.IgnoreProperty(propertyPath);
}


Editor IToolboxEditor.ContextEditor => this;
public IToolboxEditorDrawer Drawer { get; } = new ToolboxEditorDrawer();
}
Expand Down
4 changes: 2 additions & 2 deletions Assets/Editor Toolbox/Editor/ToolboxDrawerModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal static class ToolboxDrawerModule
[InitializeOnLoadMethod]
internal static void InitializeModule()
{
InspectorUtility.OnEditorReload += ReloadDrawers;
ToolboxEditorHandler.OnEditorReload += ReloadDrawers;
}


Expand Down Expand Up @@ -405,7 +405,7 @@ internal static List<Type> GetAllPossibleTargetTypeDrawers()
/// </summary>
internal static ToolboxPropertyHandler GetPropertyHandler(SerializedProperty property)
{
if (InspectorUtility.InToolboxEditor)
if (ToolboxEditorHandler.InToolboxEditor)
{
//NOTE: maybe type-based key?
var propertyKey = property.GetPropertyHashKey();
Expand Down
2 changes: 0 additions & 2 deletions Assets/Editor Toolbox/Editor/ToolboxEditor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;

using UnityEditor;

namespace Toolbox.Editor
Expand Down Expand Up @@ -45,7 +44,6 @@ public void IgnoreProperty(string propertyPath)
Drawer.IgnoreProperty(propertyPath);
}


Editor IToolboxEditor.ContextEditor => this;
/// <inheritdoc />
public virtual IToolboxEditorDrawer Drawer { get; } = new ToolboxEditorDrawer();
Expand Down
1 change: 0 additions & 1 deletion Assets/Editor Toolbox/Editor/ToolboxEditorDrawer.cs
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
Expand Down
71 changes: 62 additions & 9 deletions Assets/Editor Toolbox/Editor/ToolboxEditorHandler.cs
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; }
}
}
2 changes: 1 addition & 1 deletion Assets/Editor Toolbox/Editor/ToolboxLayoutHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ static ToolboxLayoutHandler()
ToolboxEditorHandler.OnCloseToolboxEditor += OnCloseEditor;

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


Expand Down
71 changes: 1 addition & 70 deletions Assets/Editor Toolbox/Editor/Utilities/InspectorUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,57 +11,8 @@ namespace Toolbox.Editor
using Editor = UnityEditor.Editor;
using Object = UnityEngine.Object;

[InitializeOnLoad]
internal static partial class InspectorUtility
{
static InspectorUtility()
{
//we can use each new Editor to check if 'OnEditorReload' should be called
ToolboxEditorHandler.OnBeginToolboxEditor += CheckReloads;

//we can use 'OnBeginToolboxEditor' and 'OnCloseToolboxEditor' to cache
//processed Editors and determine the real context of the serialization
ToolboxEditorHandler.OnBeginToolboxEditor += OnBeginEditor;
ToolboxEditorHandler.OnBreakToolboxEditor += OnBreakEditor;
ToolboxEditorHandler.OnCloseToolboxEditor += OnCloseEditor;
}


private static int lastCachedEditorId;
private static Editor lastCachedEditor;
private static readonly Stack<Editor> cachedEditors = new Stack<Editor>();


private static void OnBeginEditor(Editor editor)
{
cachedEditors.Push(editor);
}

private static void OnBreakEditor(Editor editor)
{
cachedEditors.Clear();
}

private static void OnCloseEditor(Editor editor)
{
if (InToolboxEditor)
{
cachedEditors.Pop();
}
}

private static void CheckReloads(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();
}
}


/// <summary>
/// Forces available Inspector Windows to repaint.
/// </summary>
Expand All @@ -84,7 +35,7 @@ internal static void RepaintInspectors()
}
}

//NOTE: none reflection way
//NOTE: none reflection way but slower
//UnityEditorInternal.InternalEditorUtility.RepaintAllViews();
}

Expand Down Expand Up @@ -132,26 +83,6 @@ internal static void SimulateOnValidate(Object target)
methodInfo.Invoke(target, null);
}
}


/// <summary>
/// Event fired every time when <see cref="ToolboxEditor"/>s were re-created.
/// </summary>
internal static event Action OnEditorReload;


/// <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];
}

internal static bool InToolboxEditor
{
get => cachedEditors.Count > 0;
}
}

internal static partial class InspectorUtility
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ public static class SceneSerializationUtility
private static readonly Dictionary<SceneAsset, SceneData> cachedScenes = new Dictionary<SceneAsset, SceneData>();
private static bool isInitialized;

public static event Action OnCacheRefreshed;

[InitializeOnLoadMethod]
private static void Initialize()
{
Expand Down Expand Up @@ -84,6 +82,15 @@ internal static bool TryGetSceneData(SceneAsset sceneAsset, out SceneData data)
return true;
}
#endif
/// <summary>
/// Event fired each time Scenes cache is rebuilt.
/// Potential triggers:
/// - Scene Build Settings changed
/// - Scene asset removed/added
///
/// You can use it to invalidate/SetDirty all assets that use <see cref="UnityEngine.SerializedScene"/> to validate serialized Scene indexes.
/// </summary>
public static event Action OnCacheRefreshed;

internal static int InvalidSceneIndex => -1;
}
Expand Down

0 comments on commit 836be9e

Please sign in to comment.