2021-04-25 00:21:12 +10:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using UnityEngine;
|
2021-06-30 07:49:58 +10:00
|
|
|
|
using UnityExplorer.CacheObject;
|
|
|
|
|
using UnityExplorer.Inspectors;
|
2022-04-12 05:20:35 +10:00
|
|
|
|
using UnityExplorer.UI;
|
2021-04-25 00:21:12 +10:00
|
|
|
|
using UnityExplorer.UI.Panels;
|
2021-12-02 18:35:46 +11:00
|
|
|
|
using UniverseLib;
|
2022-01-31 21:24:01 +11:00
|
|
|
|
using UniverseLib.UI.ObjectPool;
|
|
|
|
|
using UniverseLib.Utility;
|
2021-04-25 00:21:12 +10:00
|
|
|
|
|
2021-05-09 20:18:33 +10:00
|
|
|
|
namespace UnityExplorer
|
2021-04-25 00:21:12 +10:00
|
|
|
|
{
|
|
|
|
|
public static class InspectorManager
|
|
|
|
|
{
|
2022-02-19 17:50:10 +11:00
|
|
|
|
public static readonly List<InspectorBase> Inspectors = new();
|
2021-04-25 00:21:12 +10:00
|
|
|
|
|
2021-04-26 19:56:21 +10:00
|
|
|
|
public static InspectorBase ActiveInspector { get; private set; }
|
2021-05-09 20:18:33 +10:00
|
|
|
|
private static InspectorBase lastActiveInspector;
|
2021-04-27 21:22:48 +10:00
|
|
|
|
|
|
|
|
|
public static float PanelWidth;
|
|
|
|
|
|
2022-01-01 21:40:47 +11:00
|
|
|
|
public static event Action OnInspectedTabsChanged;
|
|
|
|
|
|
2021-05-03 01:29:02 +10:00
|
|
|
|
public static void Inspect(object obj, CacheObjectBase sourceCache = null)
|
2021-04-25 00:21:12 +10:00
|
|
|
|
{
|
2021-04-27 21:22:48 +10:00
|
|
|
|
if (obj.IsNullOrDestroyed())
|
|
|
|
|
return;
|
|
|
|
|
|
2021-04-26 19:56:21 +10:00
|
|
|
|
obj = obj.TryCast();
|
2021-04-28 23:58:13 +10:00
|
|
|
|
|
|
|
|
|
if (TryFocusActiveInspector(obj))
|
|
|
|
|
return;
|
|
|
|
|
|
2021-04-26 19:56:21 +10:00
|
|
|
|
if (obj is GameObject)
|
|
|
|
|
CreateInspector<GameObjectInspector>(obj);
|
2021-04-25 21:20:50 +10:00
|
|
|
|
else
|
2021-05-03 01:29:02 +10:00
|
|
|
|
CreateInspector<ReflectionInspector>(obj, false, sourceCache);
|
2021-04-25 21:20:50 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-06 15:51:40 +10:00
|
|
|
|
public static void Inspect(Type type)
|
|
|
|
|
{
|
|
|
|
|
CreateInspector<ReflectionInspector>(type, true);
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 23:58:13 +10:00
|
|
|
|
private static bool TryFocusActiveInspector(object target)
|
|
|
|
|
{
|
2022-04-12 05:20:35 +10:00
|
|
|
|
foreach (InspectorBase inspector in Inspectors)
|
2021-04-28 23:58:13 +10:00
|
|
|
|
{
|
2021-05-03 01:29:02 +10:00
|
|
|
|
if (inspector.Target.ReferenceEqual(target))
|
2021-04-28 23:58:13 +10:00
|
|
|
|
{
|
|
|
|
|
UIManager.SetPanelActive(UIManager.Panels.Inspector, true);
|
|
|
|
|
SetInspectorActive(inspector);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 19:56:21 +10:00
|
|
|
|
public static void SetInspectorActive(InspectorBase inspector)
|
2021-04-25 00:21:12 +10:00
|
|
|
|
{
|
2021-04-26 19:56:21 +10:00
|
|
|
|
UnsetActiveInspector();
|
2021-04-25 00:21:12 +10:00
|
|
|
|
|
2021-04-26 19:56:21 +10:00
|
|
|
|
ActiveInspector = inspector;
|
|
|
|
|
inspector.OnSetActive();
|
2021-04-25 00:21:12 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 19:56:21 +10:00
|
|
|
|
public static void UnsetActiveInspector()
|
2021-04-25 00:21:12 +10:00
|
|
|
|
{
|
2021-04-26 19:56:21 +10:00
|
|
|
|
if (ActiveInspector != null)
|
2021-05-09 20:18:33 +10:00
|
|
|
|
{
|
|
|
|
|
lastActiveInspector = ActiveInspector;
|
2021-04-26 19:56:21 +10:00
|
|
|
|
ActiveInspector.OnSetInactive();
|
2021-05-09 20:18:33 +10:00
|
|
|
|
ActiveInspector = null;
|
|
|
|
|
}
|
2021-04-25 00:21:12 +10:00
|
|
|
|
}
|
2021-05-16 21:46:19 +10:00
|
|
|
|
|
2021-09-06 15:51:40 +10:00
|
|
|
|
internal static void CloseAllTabs()
|
|
|
|
|
{
|
|
|
|
|
if (Inspectors.Any())
|
|
|
|
|
{
|
|
|
|
|
for (int i = Inspectors.Count - 1; i >= 0; i--)
|
|
|
|
|
Inspectors[i].CloseInspector();
|
|
|
|
|
|
|
|
|
|
Inspectors.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UIManager.SetPanelActive(UIManager.Panels.Inspector, false);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-05 19:36:09 +10:00
|
|
|
|
private static void CreateInspector<T>(object target, bool staticReflection = false,
|
2022-02-19 17:50:10 +11:00
|
|
|
|
CacheObjectBase parentObject = null) where T : InspectorBase
|
2021-04-25 00:21:12 +10:00
|
|
|
|
{
|
2022-04-12 05:20:35 +10:00
|
|
|
|
T inspector = Pool<T>.Borrow();
|
2021-04-26 19:56:21 +10:00
|
|
|
|
Inspectors.Add(inspector);
|
2021-05-03 01:29:02 +10:00
|
|
|
|
inspector.Target = target;
|
|
|
|
|
|
2022-02-19 17:50:10 +11:00
|
|
|
|
if (parentObject != null && parentObject.CanWrite)
|
2021-05-09 02:22:03 +10:00
|
|
|
|
{
|
|
|
|
|
// only set parent cache object if we are inspecting a struct, otherwise there is no point.
|
|
|
|
|
if (target.GetType().IsValueType && inspector is ReflectionInspector ri)
|
2022-02-19 17:50:10 +11:00
|
|
|
|
ri.ParentCacheObject = parentObject;
|
2021-05-09 02:22:03 +10:00
|
|
|
|
}
|
2021-04-25 00:21:12 +10:00
|
|
|
|
|
2021-04-27 21:22:48 +10:00
|
|
|
|
UIManager.SetPanelActive(UIManager.Panels.Inspector, true);
|
2021-04-26 19:56:21 +10:00
|
|
|
|
inspector.UIRoot.transform.SetParent(InspectorPanel.Instance.ContentHolder.transform, false);
|
2021-04-25 00:21:12 +10:00
|
|
|
|
|
2021-04-27 21:22:48 +10:00
|
|
|
|
if (inspector is ReflectionInspector reflectInspector)
|
|
|
|
|
reflectInspector.StaticOnly = staticReflection;
|
|
|
|
|
|
2021-04-26 19:56:21 +10:00
|
|
|
|
inspector.OnBorrowedFromPool(target);
|
|
|
|
|
SetInspectorActive(inspector);
|
2022-01-01 21:40:47 +11:00
|
|
|
|
|
|
|
|
|
OnInspectedTabsChanged?.Invoke();
|
2021-04-25 00:21:12 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 19:56:21 +10:00
|
|
|
|
internal static void ReleaseInspector<T>(T inspector) where T : InspectorBase
|
2021-04-25 00:21:12 +10:00
|
|
|
|
{
|
2021-05-09 20:18:33 +10:00
|
|
|
|
if (lastActiveInspector == inspector)
|
|
|
|
|
lastActiveInspector = null;
|
|
|
|
|
|
|
|
|
|
bool wasActive = ActiveInspector == inspector;
|
|
|
|
|
int wasIdx = Inspectors.IndexOf(inspector);
|
|
|
|
|
|
|
|
|
|
Inspectors.Remove(inspector);
|
2021-04-26 19:56:21 +10:00
|
|
|
|
inspector.OnReturnToPool();
|
|
|
|
|
Pool<T>.Return(inspector);
|
|
|
|
|
|
2021-05-09 20:18:33 +10:00
|
|
|
|
if (wasActive)
|
|
|
|
|
{
|
|
|
|
|
ActiveInspector = null;
|
|
|
|
|
// Try focus another inspector, or close the window.
|
|
|
|
|
if (lastActiveInspector != null)
|
|
|
|
|
{
|
|
|
|
|
SetInspectorActive(lastActiveInspector);
|
|
|
|
|
lastActiveInspector = null;
|
|
|
|
|
}
|
|
|
|
|
else if (Inspectors.Any())
|
|
|
|
|
{
|
|
|
|
|
int newIdx = Math.Min(Inspectors.Count - 1, Math.Max(0, wasIdx - 1));
|
|
|
|
|
SetInspectorActive(Inspectors[newIdx]);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
UIManager.SetPanelActive(UIManager.Panels.Inspector, false);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-01 21:40:47 +11:00
|
|
|
|
|
|
|
|
|
OnInspectedTabsChanged?.Invoke();
|
2021-04-25 00:21:12 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 19:56:21 +10:00
|
|
|
|
internal static void Update()
|
2021-04-25 00:21:12 +10:00
|
|
|
|
{
|
2021-04-27 21:22:48 +10:00
|
|
|
|
for (int i = Inspectors.Count - 1; i >= 0; i--)
|
|
|
|
|
Inspectors[i].Update();
|
2021-04-25 00:21:12 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-27 21:22:48 +10:00
|
|
|
|
internal static void OnPanelResized(float width)
|
2021-04-25 00:21:12 +10:00
|
|
|
|
{
|
2021-04-27 21:22:48 +10:00
|
|
|
|
PanelWidth = width;
|
|
|
|
|
|
2022-04-12 05:20:35 +10:00
|
|
|
|
foreach (InspectorBase obj in Inspectors)
|
2021-04-27 21:22:48 +10:00
|
|
|
|
{
|
|
|
|
|
if (obj is ReflectionInspector inspector)
|
|
|
|
|
{
|
|
|
|
|
inspector.SetLayouts();
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-25 00:21:12 +10:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|