Extract TriEditorCore

This commit is contained in:
VladV 2023-09-16 13:40:59 +04:00
parent 7641ff87b0
commit 1d66740c1a
5 changed files with 151 additions and 128 deletions

View File

@ -28,7 +28,7 @@ namespace TriInspector.Drawers
var visualElement = handler.CreatePropertyGUI(serializedProperty);
if (visualElement != null &&
TriEditor.UiElementsRoots.TryGetValue(property.PropertyTree, out var rootElement))
TriEditorCore.UiElementsRoots.TryGetValue(property.PropertyTree, out var rootElement))
{
return new TriUiToolkitPropertyElement(property, serializedProperty,
visualElement, rootElement);

View File

@ -1,134 +1,25 @@
using System;
using System.Collections.Generic;
using TriInspector.Utilities;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
namespace TriInspector.Editors
{
public abstract class TriEditor : Editor
{
internal static readonly Dictionary<TriPropertyTree, VisualElement> UiElementsRoots
= new Dictionary<TriPropertyTree, VisualElement>();
private TriEditorCore _core;
private TriPropertyTreeForSerializedObject _inspector;
private void OnEnable()
{
_core = new TriEditorCore(this);
}
private void OnDisable()
{
OnDisable(this, ref _inspector);
_core.Dispose();
}
public override VisualElement CreateInspectorGUI()
{
return CreateInspector(root => OnInspectorGUI(this, ref _inspector, root));
}
public override void OnInspectorGUI()
{
OnInspectorGUI(this, ref _inspector);
}
public static void OnDisable(Editor editor, ref TriPropertyTreeForSerializedObject inspector)
{
if (inspector != null)
{
UiElementsRoots.Remove(inspector);
inspector.Dispose();
}
inspector = null;
}
public static VisualElement CreateInspector(Action<VisualElement> onGui)
{
var container = new VisualElement();
var root = new VisualElement()
{
style =
{
position = Position.Absolute,
},
};
container.Add(new IMGUIContainer(() =>
{
const float labelExtraPadding = 2;
const float labelWidthRatio = 0.45f;
const float labelMinWidth = 120;
var space = container.resolvedStyle.left + container.resolvedStyle.right + labelExtraPadding;
EditorGUIUtility.wideMode = true;
EditorGUIUtility.hierarchyMode = false;
EditorGUIUtility.labelWidth = Mathf.Max(labelMinWidth,
container.resolvedStyle.width * labelWidthRatio - space);
onGui?.Invoke(root);
}));
container.Add(root);
return container;
}
public static void OnInspectorGUI(Editor editor,
ref TriPropertyTreeForSerializedObject inspector, VisualElement visualRoot = null)
{
var serializedObject = editor.serializedObject;
if (serializedObject.targetObjects.Length == 0)
{
return;
}
if (serializedObject.targetObject == null)
{
EditorGUILayout.HelpBox("Script is missing", MessageType.Warning);
return;
}
foreach (var targetObject in serializedObject.targetObjects)
{
if (TriGuiHelper.IsEditorTargetPushed(targetObject))
{
GUILayout.Label("Recursive inline editors not supported");
return;
}
}
if (inspector == null)
{
inspector = new TriPropertyTreeForSerializedObject(serializedObject);
}
if (visualRoot != null)
{
UiElementsRoots[inspector] = visualRoot;
}
serializedObject.UpdateIfRequiredOrScript();
inspector.Update();
inspector.RunValidationIfRequired();
EditorGUIUtility.hierarchyMode = false;
using (TriGuiHelper.PushEditorTarget(serializedObject.targetObject))
{
inspector.Draw();
}
if (serializedObject.ApplyModifiedProperties())
{
inspector.RequestValidation();
}
if (inspector.RepaintRequired)
{
editor.Repaint();
}
return _core.CreateVisualElement();
}
}
}

View File

@ -0,0 +1,124 @@
using System.Collections.Generic;
using TriInspector.Utilities;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
namespace TriInspector.Editors
{
public class TriEditorCore
{
internal static readonly Dictionary<TriPropertyTree, VisualElement> UiElementsRoots
= new Dictionary<TriPropertyTree, VisualElement>();
private readonly Editor _editor;
private TriPropertyTreeForSerializedObject _inspector;
public TriEditorCore(Editor editor)
{
_editor = editor;
}
public void Dispose()
{
if (_inspector != null)
{
UiElementsRoots.Remove(_inspector);
_inspector.Dispose();
}
_inspector = null;
}
public void OnInspectorGUI(VisualElement visualRoot = null)
{
var serializedObject = _editor.serializedObject;
if (serializedObject.targetObjects.Length == 0)
{
return;
}
if (serializedObject.targetObject == null)
{
EditorGUILayout.HelpBox("Script is missing", MessageType.Warning);
return;
}
foreach (var targetObject in serializedObject.targetObjects)
{
if (TriGuiHelper.IsEditorTargetPushed(targetObject))
{
GUILayout.Label("Recursive inline editors not supported");
return;
}
}
if (_inspector == null)
{
_inspector = new TriPropertyTreeForSerializedObject(serializedObject);
}
if (visualRoot != null)
{
UiElementsRoots[_inspector] = visualRoot;
}
serializedObject.UpdateIfRequiredOrScript();
_inspector.Update();
_inspector.RunValidationIfRequired();
EditorGUIUtility.hierarchyMode = false;
using (TriGuiHelper.PushEditorTarget(serializedObject.targetObject))
{
_inspector.Draw();
}
if (serializedObject.ApplyModifiedProperties())
{
_inspector.RequestValidation();
}
if (_inspector.RepaintRequired)
{
_editor.Repaint();
}
}
public VisualElement CreateVisualElement()
{
var container = new VisualElement();
var root = new VisualElement()
{
style =
{
position = Position.Absolute,
},
};
container.Add(new IMGUIContainer(() =>
{
const float labelExtraPadding = 2;
const float labelWidthRatio = 0.45f;
const float labelMinWidth = 120;
var space = container.resolvedStyle.left + container.resolvedStyle.right + labelExtraPadding;
EditorGUIUtility.wideMode = true;
EditorGUIUtility.hierarchyMode = false;
EditorGUIUtility.labelWidth = Mathf.Max(labelMinWidth,
container.resolvedStyle.width * labelWidthRatio - space);
OnInspectorGUI(root);
}));
container.Add(root);
return container;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a7bd33877ecd42dc878e2b28f0a9f581
timeCreated: 1694856077

View File

@ -9,29 +9,34 @@ namespace TriInspector.Editors
[CustomEditor(typeof(ScriptedImporter), editorForChildClasses: true)]
public sealed class TriScriptedImporterEditor : ScriptedImporterEditor
{
private TriPropertyTreeForSerializedObject _inspector;
private TriEditorCore _core;
public override void OnEnable()
{
base.OnEnable();
_core = new TriEditorCore(this);
}
public override void OnDisable()
{
TriEditor.OnDisable(this, ref _inspector);
_core.Dispose();
base.OnDisable();
}
public override VisualElement CreateInspectorGUI()
{
return TriEditor.CreateInspector(root => OnInspectorGUI(root));
var root = new VisualElement();
root.Add(_core.CreateVisualElement());
root.Add(new IMGUIContainer(() => DoImporterDefaultGUI()));
return root;
}
public override void OnInspectorGUI()
private void DoImporterDefaultGUI()
{
OnInspectorGUI(null);
}
private void OnInspectorGUI(VisualElement root)
{
TriEditor.OnInspectorGUI(this, ref _inspector, root);
if (extraDataType != null)
{
EditorProxy.DoDrawDefaultInspector(extraDataSerializedObject);