Skip to content

Commit 4e87dda

Browse files
committed
Functional Editor drawer & ToolboxScriptedImporterEditor
1 parent b3bb394 commit 4e87dda

9 files changed

Lines changed: 197 additions & 95 deletions

Assets/Editor Toolbox/Editor/BasicToolboxEditor.cs

Lines changed: 0 additions & 58 deletions
This file was deleted.
Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,36 @@
1-
using UnityEditor.AssetImporters;
1+
using UnityEditor;
2+
using UnityEditor.AssetImporters;
23

34
namespace Toolbox.Editor.Editors
45
{
5-
public class ToolboxScriptedImporterEditor : ScriptedImporterEditor
6+
using Editor = UnityEditor.Editor;
7+
8+
public class ToolboxScriptedImporterEditor : ScriptedImporterEditor, IToolboxEditor
69
{
7-
//TODO:
10+
public sealed override void OnInspectorGUI()
11+
{
12+
ToolboxEditorHandler.HandleToolboxEditor(this);
13+
}
14+
15+
public virtual void DrawCustomInspector()
16+
{
17+
Drawer.DrawToolboxInspector(serializedObject);
18+
if (extraDataType != null)
19+
{
20+
Drawer.DrawToolboxInspector(extraDataSerializedObject);
21+
}
22+
23+
ApplyRevertGUI();
24+
}
25+
26+
//TODO: ignore properties
27+
public void IgnoreProperty(SerializedProperty property)
28+
{ }
29+
30+
public void IgnoreProperty(string propertyPath)
31+
{ }
32+
33+
Editor IToolboxEditor.ContextEditor => this;
34+
public IToolboxEditorDrawer Drawer { get; } = new ToolboxEditorDrawer();
835
}
936
}

Assets/Editor Toolbox/Editor/IToolboxEditor.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ namespace Toolbox.Editor
66

77
public interface IToolboxEditor
88
{
9-
void DrawCustomProperty(SerializedProperty property);
109
void DrawCustomInspector();
11-
void DrawCustomInspector(SerializedObject serializedObject);
10+
void IgnoreProperty(SerializedProperty property);
11+
void IgnoreProperty(string propertyPath);
1212

1313
Editor ContextEditor { get; }
14+
IToolboxEditorDrawer Drawer { get; }
1415
}
1516
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using UnityEditor;
2+
3+
namespace Toolbox.Editor
4+
{
5+
public interface IToolboxEditorDrawer
6+
{
7+
/// <summary>
8+
/// Handles property display process using custom <see cref="Drawers.ToolboxDrawer"/>.
9+
/// </summary>
10+
/// <param name="property">Property to display.</param>
11+
void DrawToolboxProperty(SerializedProperty property);
12+
/// <summary>
13+
/// Handles property display process using the default (built-in) drawers.
14+
/// </summary>
15+
/// <param name="property">Property to display.</param>
16+
void DrawDefaultProperty(SerializedProperty property);
17+
/// <summary>
18+
/// Draws each available property using internally <see cref="Drawers.ToolboxDrawer"/>s.
19+
/// </summary>
20+
void DrawToolboxInspector(SerializedObject serializedObject);
21+
/// <summary>
22+
/// Draws <see cref="SerializedProperty"/>/ies in the default (native) way.
23+
/// </summary>
24+
/// <param name="serializedObject"></param>
25+
void DrawDefaultInspector(SerializedObject serializedObject);
26+
/// <summary>
27+
/// Forces provided <see cref="SerializedProperty"/> to be ignored in the drawing process.
28+
/// </summary>
29+
void IgnoreProperty(SerializedProperty property);
30+
/// <summary>
31+
/// Forces provided <see cref="SerializedProperty"/> to be ignored in the drawing process.
32+
/// </summary>
33+
void IgnoreProperty(string propertyPath);
34+
}
35+
}

Assets/Editor Toolbox/Editor/IToolboxEditorDrawer.cs.meta

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

Assets/Editor Toolbox/Editor/ToolboxEditor.cs

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,61 +14,41 @@ namespace Toolbox.Editor
1414
[CanEditMultipleObjects]
1515
public class ToolboxEditor : Editor, IToolboxEditor
1616
{
17-
private readonly HashSet<string> propertiesToIgnore = new HashSet<string>();
18-
19-
//TODO: better initialization
20-
private IToolboxEditor nestedEditor;
21-
2217
/// <summary>
2318
/// Inspector GUI re-draw call.
2419
/// </summary>
2520
public sealed override void OnInspectorGUI()
2621
{
27-
nestedEditor ??= new BasicToolboxEditor(this);
2822
ToolboxEditorHandler.HandleToolboxEditor(this);
2923
}
3024

31-
/// <summary>
32-
/// Handles property display process using custom <see cref="Drawers.ToolboxDrawer"/>.
33-
/// </summary>
34-
/// <param name="property">Property to display.</param>
25+
//TODO: how to handle this method, currently unused
26+
/// <inheritdoc />
3527
public virtual void DrawCustomProperty(SerializedProperty property)
3628
{
37-
nestedEditor.DrawCustomProperty(property);
29+
Drawer.DrawToolboxProperty(property);
3830
}
3931

40-
/// <summary>
41-
/// Draws each available property using internally <see cref="Drawers.ToolboxDrawer"/>s.
42-
/// </summary>
32+
/// <inheritdoc />
4333
public virtual void DrawCustomInspector()
4434
{
45-
nestedEditor.DrawCustomInspector();
46-
}
47-
48-
/// <summary>
49-
/// Draws each available property using internally <see cref="Drawers.ToolboxDrawer"/>s.
50-
/// </summary>
51-
public virtual void DrawCustomInspector(SerializedObject serializedObject)
52-
{
53-
nestedEditor.DrawCustomInspector(serializedObject);
35+
Drawer.DrawToolboxInspector(serializedObject);
5436
}
5537

56-
/// <summary>
57-
/// Forces provided <see cref="SerializedProperty"/> to be ignored in the drawing process.
58-
/// </summary>
38+
/// <inheritdoc />
5939
public void IgnoreProperty(SerializedProperty property)
6040
{
61-
IgnoreProperty(property.propertyPath);
41+
Drawer.IgnoreProperty(property);
6242
}
6343

64-
/// <summary>
65-
/// Forces associated <see cref="SerializedProperty"/> to be ignored in the drawing process.
66-
/// </summary>
44+
/// <inheritdoc />
6745
public void IgnoreProperty(string propertyPath)
6846
{
69-
propertiesToIgnore.Add(propertyPath);
47+
Drawer.IgnoreProperty(propertyPath);
7048
}
7149

50+
7251
Editor IToolboxEditor.ContextEditor => this;
52+
public IToolboxEditorDrawer Drawer { get; } = new ToolboxEditorDrawer();
7353
}
7454
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
using UnityEditor;
5+
6+
namespace Toolbox.Editor
7+
{
8+
public class ToolboxEditorDrawer : IToolboxEditorDrawer
9+
{
10+
private readonly HashSet<string> propertiesToIgnore = new HashSet<string>();
11+
12+
//TODO: refactor
13+
/// <inheritdoc />
14+
public void DrawToolboxProperty(SerializedProperty property)
15+
{
16+
var propertyPath = property.propertyPath;
17+
if (propertiesToIgnore.Contains(propertyPath))
18+
{
19+
return;
20+
}
21+
22+
ToolboxEditorGui.DrawToolboxProperty(property);
23+
}
24+
25+
/// <inheritdoc />
26+
public void DrawDefaultProperty(SerializedProperty property)
27+
{
28+
var propertyPath = property.propertyPath;
29+
if (propertiesToIgnore.Contains(propertyPath))
30+
{
31+
return;
32+
}
33+
34+
ToolboxEditorGui.DrawNativeProperty(property);
35+
}
36+
37+
/// <inheritdoc />
38+
public void DrawToolboxInspector(SerializedObject serializedObject)
39+
{
40+
if (!ToolboxDrawerModule.ToolboxDrawersAllowed)
41+
{
42+
DrawDefaultInspector(serializedObject);
43+
return;
44+
}
45+
46+
//TODO: how to handle default/toolbox drawers
47+
//Action<SerializedProperty> drawAction = ToolboxDrawerModule.ToolboxDrawersAllowed
48+
// ? (Action<SerializedProperty>)DrawToolboxProperty
49+
// : DrawDefaultProperty;
50+
serializedObject.UpdateIfRequiredOrScript();
51+
var property = serializedObject.GetIterator();
52+
if (property.NextVisible(true))
53+
{
54+
var isScript = PropertyUtility.IsDefaultScriptProperty(property);
55+
using (new EditorGUI.DisabledScope(isScript))
56+
{
57+
DrawToolboxProperty(property.Copy());
58+
}
59+
60+
while (property.NextVisible(false))
61+
{
62+
DrawToolboxProperty(property.Copy());
63+
}
64+
}
65+
66+
serializedObject.ApplyModifiedProperties();
67+
}
68+
69+
//TODO:
70+
/// <inheritdoc />
71+
public void DrawDefaultInspector(SerializedObject serializedObject)
72+
{
73+
serializedObject.UpdateIfRequiredOrScript();
74+
var property = serializedObject.GetIterator();
75+
if (property.NextVisible(true))
76+
{
77+
var isScript = PropertyUtility.IsDefaultScriptProperty(property);
78+
using (new EditorGUI.DisabledScope(isScript))
79+
{
80+
EditorGUILayout.PropertyField(property, true);
81+
}
82+
83+
while (property.NextVisible(false))
84+
{
85+
EditorGUILayout.PropertyField(property, true);
86+
}
87+
}
88+
89+
serializedObject.ApplyModifiedProperties();
90+
}
91+
92+
/// <inheritdoc />
93+
public void IgnoreProperty(SerializedProperty property)
94+
{
95+
IgnoreProperty(property.propertyPath);
96+
}
97+
98+
/// <inheritdoc />
99+
public void IgnoreProperty(string propertyPath)
100+
{
101+
propertiesToIgnore.Add(propertyPath);
102+
}
103+
}
104+
}

Assets/Editor Toolbox/Editor/BasicToolboxEditor.cs.meta renamed to Assets/Editor Toolbox/Editor/ToolboxEditorDrawer.cs.meta

File renamed without changes.

Assets/Editor Toolbox/Editor/ToolboxEditorHandler.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ namespace Toolbox.Editor
66

77
internal static class ToolboxEditorHandler
88
{
9-
//TODO: how to pass dditor and multiple serialized objects
109
public static void HandleToolboxEditor(IToolboxEditor editor)
1110
{
1211
try
@@ -34,6 +33,9 @@ public static void HandleToolboxEditor(IToolboxEditor editor)
3433
public static event Action<Editor> OnBreakToolboxEditor;
3534
public static event Action<Editor> OnCloseToolboxEditor;
3635

36+
/// <summary>
37+
/// Currently maintained <see cref="Editor"/>.
38+
/// </summary>
3739
public static Editor ContextEditor { get; private set; }
3840
}
3941
}

0 commit comments

Comments
 (0)