Skip to content

Commit 1d66740

Browse files
committed
Extract TriEditorCore
1 parent 7641ff8 commit 1d66740

File tree

5 files changed

+150
-127
lines changed

5 files changed

+150
-127
lines changed

Editor.Extras/Drawers/CustomBuiltInDrawer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public override TriElement CreateElement(TriValue<object> propertyValue, TriElem
2828
var visualElement = handler.CreatePropertyGUI(serializedProperty);
2929

3030
if (visualElement != null &&
31-
TriEditor.UiElementsRoots.TryGetValue(property.PropertyTree, out var rootElement))
31+
TriEditorCore.UiElementsRoots.TryGetValue(property.PropertyTree, out var rootElement))
3232
{
3333
return new TriUiToolkitPropertyElement(property, serializedProperty,
3434
visualElement, rootElement);

Editor/Editors/TriEditor.cs

Lines changed: 7 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,134 +1,25 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using TriInspector.Utilities;
41
using UnityEditor;
5-
using UnityEngine;
62
using UnityEngine.UIElements;
73

84
namespace TriInspector.Editors
95
{
106
public abstract class TriEditor : Editor
117
{
12-
internal static readonly Dictionary<TriPropertyTree, VisualElement> UiElementsRoots
13-
= new Dictionary<TriPropertyTree, VisualElement>();
8+
private TriEditorCore _core;
149

15-
private TriPropertyTreeForSerializedObject _inspector;
16-
17-
private void OnDisable()
18-
{
19-
OnDisable(this, ref _inspector);
20-
}
21-
22-
public override VisualElement CreateInspectorGUI()
23-
{
24-
return CreateInspector(root => OnInspectorGUI(this, ref _inspector, root));
25-
}
26-
27-
public override void OnInspectorGUI()
10+
private void OnEnable()
2811
{
29-
OnInspectorGUI(this, ref _inspector);
12+
_core = new TriEditorCore(this);
3013
}
3114

32-
public static void OnDisable(Editor editor, ref TriPropertyTreeForSerializedObject inspector)
15+
private void OnDisable()
3316
{
34-
if (inspector != null)
35-
{
36-
UiElementsRoots.Remove(inspector);
37-
38-
inspector.Dispose();
39-
}
40-
41-
inspector = null;
17+
_core.Dispose();
4218
}
4319

44-
public static VisualElement CreateInspector(Action<VisualElement> onGui)
45-
{
46-
var container = new VisualElement();
47-
var root = new VisualElement()
48-
{
49-
style =
50-
{
51-
position = Position.Absolute,
52-
},
53-
};
54-
55-
container.Add(new IMGUIContainer(() =>
56-
{
57-
const float labelExtraPadding = 2;
58-
const float labelWidthRatio = 0.45f;
59-
const float labelMinWidth = 120;
60-
61-
var space = container.resolvedStyle.left + container.resolvedStyle.right + labelExtraPadding;
62-
63-
EditorGUIUtility.wideMode = true;
64-
EditorGUIUtility.hierarchyMode = false;
65-
EditorGUIUtility.labelWidth = Mathf.Max(labelMinWidth,
66-
container.resolvedStyle.width * labelWidthRatio - space);
67-
68-
onGui?.Invoke(root);
69-
}));
70-
71-
container.Add(root);
72-
73-
return container;
74-
}
75-
76-
public static void OnInspectorGUI(Editor editor,
77-
ref TriPropertyTreeForSerializedObject inspector, VisualElement visualRoot = null)
20+
public override VisualElement CreateInspectorGUI()
7821
{
79-
var serializedObject = editor.serializedObject;
80-
81-
if (serializedObject.targetObjects.Length == 0)
82-
{
83-
return;
84-
}
85-
86-
if (serializedObject.targetObject == null)
87-
{
88-
EditorGUILayout.HelpBox("Script is missing", MessageType.Warning);
89-
return;
90-
}
91-
92-
foreach (var targetObject in serializedObject.targetObjects)
93-
{
94-
if (TriGuiHelper.IsEditorTargetPushed(targetObject))
95-
{
96-
GUILayout.Label("Recursive inline editors not supported");
97-
return;
98-
}
99-
}
100-
101-
if (inspector == null)
102-
{
103-
inspector = new TriPropertyTreeForSerializedObject(serializedObject);
104-
}
105-
106-
if (visualRoot != null)
107-
{
108-
UiElementsRoots[inspector] = visualRoot;
109-
}
110-
111-
serializedObject.UpdateIfRequiredOrScript();
112-
113-
inspector.Update();
114-
inspector.RunValidationIfRequired();
115-
116-
EditorGUIUtility.hierarchyMode = false;
117-
118-
using (TriGuiHelper.PushEditorTarget(serializedObject.targetObject))
119-
{
120-
inspector.Draw();
121-
}
122-
123-
if (serializedObject.ApplyModifiedProperties())
124-
{
125-
inspector.RequestValidation();
126-
}
127-
128-
if (inspector.RepaintRequired)
129-
{
130-
editor.Repaint();
131-
}
22+
return _core.CreateVisualElement();
13223
}
13324
}
13425
}

Editor/Editors/TriEditorCore.cs

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
using System.Collections.Generic;
2+
using TriInspector.Utilities;
3+
using UnityEditor;
4+
using UnityEngine;
5+
using UnityEngine.UIElements;
6+
7+
namespace TriInspector.Editors
8+
{
9+
public class TriEditorCore
10+
{
11+
internal static readonly Dictionary<TriPropertyTree, VisualElement> UiElementsRoots
12+
= new Dictionary<TriPropertyTree, VisualElement>();
13+
14+
private readonly Editor _editor;
15+
16+
private TriPropertyTreeForSerializedObject _inspector;
17+
18+
public TriEditorCore(Editor editor)
19+
{
20+
_editor = editor;
21+
}
22+
23+
public void Dispose()
24+
{
25+
if (_inspector != null)
26+
{
27+
UiElementsRoots.Remove(_inspector);
28+
29+
_inspector.Dispose();
30+
}
31+
32+
_inspector = null;
33+
}
34+
35+
public void OnInspectorGUI(VisualElement visualRoot = null)
36+
{
37+
var serializedObject = _editor.serializedObject;
38+
39+
if (serializedObject.targetObjects.Length == 0)
40+
{
41+
return;
42+
}
43+
44+
if (serializedObject.targetObject == null)
45+
{
46+
EditorGUILayout.HelpBox("Script is missing", MessageType.Warning);
47+
return;
48+
}
49+
50+
foreach (var targetObject in serializedObject.targetObjects)
51+
{
52+
if (TriGuiHelper.IsEditorTargetPushed(targetObject))
53+
{
54+
GUILayout.Label("Recursive inline editors not supported");
55+
return;
56+
}
57+
}
58+
59+
if (_inspector == null)
60+
{
61+
_inspector = new TriPropertyTreeForSerializedObject(serializedObject);
62+
}
63+
64+
if (visualRoot != null)
65+
{
66+
UiElementsRoots[_inspector] = visualRoot;
67+
}
68+
69+
serializedObject.UpdateIfRequiredOrScript();
70+
71+
_inspector.Update();
72+
_inspector.RunValidationIfRequired();
73+
74+
EditorGUIUtility.hierarchyMode = false;
75+
76+
using (TriGuiHelper.PushEditorTarget(serializedObject.targetObject))
77+
{
78+
_inspector.Draw();
79+
}
80+
81+
if (serializedObject.ApplyModifiedProperties())
82+
{
83+
_inspector.RequestValidation();
84+
}
85+
86+
if (_inspector.RepaintRequired)
87+
{
88+
_editor.Repaint();
89+
}
90+
}
91+
92+
public VisualElement CreateVisualElement()
93+
{
94+
var container = new VisualElement();
95+
var root = new VisualElement()
96+
{
97+
style =
98+
{
99+
position = Position.Absolute,
100+
},
101+
};
102+
103+
container.Add(new IMGUIContainer(() =>
104+
{
105+
const float labelExtraPadding = 2;
106+
const float labelWidthRatio = 0.45f;
107+
const float labelMinWidth = 120;
108+
109+
var space = container.resolvedStyle.left + container.resolvedStyle.right + labelExtraPadding;
110+
111+
EditorGUIUtility.wideMode = true;
112+
EditorGUIUtility.hierarchyMode = false;
113+
EditorGUIUtility.labelWidth = Mathf.Max(labelMinWidth,
114+
container.resolvedStyle.width * labelWidthRatio - space);
115+
116+
OnInspectorGUI(root);
117+
}));
118+
119+
container.Add(root);
120+
121+
return container;
122+
}
123+
}
124+
}

Editor/Editors/TriEditorCore.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/Editors/TriScriptedImporterEditor.cs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,34 @@ namespace TriInspector.Editors
99
[CustomEditor(typeof(ScriptedImporter), editorForChildClasses: true)]
1010
public sealed class TriScriptedImporterEditor : ScriptedImporterEditor
1111
{
12-
private TriPropertyTreeForSerializedObject _inspector;
12+
private TriEditorCore _core;
13+
14+
public override void OnEnable()
15+
{
16+
base.OnEnable();
17+
18+
_core = new TriEditorCore(this);
19+
}
1320

1421
public override void OnDisable()
1522
{
16-
TriEditor.OnDisable(this, ref _inspector);
23+
_core.Dispose();
1724

1825
base.OnDisable();
1926
}
2027

2128
public override VisualElement CreateInspectorGUI()
2229
{
23-
return TriEditor.CreateInspector(root => OnInspectorGUI(root));
24-
}
30+
var root = new VisualElement();
2531

26-
public override void OnInspectorGUI()
27-
{
28-
OnInspectorGUI(null);
32+
root.Add(_core.CreateVisualElement());
33+
root.Add(new IMGUIContainer(() => DoImporterDefaultGUI()));
34+
35+
return root;
2936
}
3037

31-
private void OnInspectorGUI(VisualElement root)
38+
private void DoImporterDefaultGUI()
3239
{
33-
TriEditor.OnInspectorGUI(this, ref _inspector, root);
34-
3540
if (extraDataType != null)
3641
{
3742
EditorProxy.DoDrawDefaultInspector(extraDataSerializedObject);

0 commit comments

Comments
 (0)