Skip to content

Commit

Permalink
fix: general improvements to CustomEditor type cache and ScriptedEditor
Browse files Browse the repository at this point in the history
  • Loading branch information
sinnwrig committed Oct 4, 2024
1 parent a4935be commit 329c29c
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 92 deletions.
7 changes: 5 additions & 2 deletions Prowl.Editor/Assets/Importers/MaterialImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,18 @@ public override void Import(SerializedAsset ctx, FileInfo assetPath)
public class MaterialImporterEditor : ScriptedEditor
{
private Material? _editingMaterial;
private MaterialEditor? _editor;
private ScriptedEditor? _editor;


public override void OnEnable()
{
SerializedProperty tag = StringTagConverter.ReadFromFile((target as MetaFile).AssetPath);
_editingMaterial = Serializer.Deserialize<Material>(tag);

_editor = new MaterialEditor(_editingMaterial, OnChange);
_editor = CreateEditor(_editingMaterial);

if (_editor is MaterialEditor matEditor)
matEditor.onChange = OnChange;
}


Expand Down
29 changes: 14 additions & 15 deletions Prowl.Editor/Assets/Importers/ModelImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -658,11 +658,10 @@ public MeshMaterialBinding(string meshName, Assimp.Mesh aMesh, AssetRef<Mesh> me
[CustomEditor(typeof(ModelImporter))]
public class ModelEditor : ScriptedEditor
{
private int _selectedAnim;
private int _selectedAnimBone;

int selectedAnim;
int selectedAnimBone;

int selectedTab;
private int _selectedTab;

public override void OnInspectorGUI()
{
Expand All @@ -677,19 +676,19 @@ public override void OnInspectorGUI()
using (gui.Node("Tabs").Width(Size.Percentage(1f)).MaxHeight(ItemSize).Layout(LayoutType.Row).ScaleChildren().Enter())
{
if (EditorGUI.StyledButton("Meshes"))
selectedTab = 0;
_selectedTab = 0;
if (EditorGUI.StyledButton("Materials"))
selectedTab = 1;
_selectedTab = 1;
if (EditorGUI.StyledButton("Scene"))
selectedTab = 2;
_selectedTab = 2;
if (EditorGUI.StyledButton("Animations"))
selectedTab = 3;
_selectedTab = 3;
}


using (gui.Node("Content").Width(Size.Percentage(1f)).MarginTop(5).Layout(LayoutType.Column).Scroll().Enter())
{
switch (selectedTab)
switch (_selectedTab)
{
case 0:
Meshes(importer, serialized);
Expand Down Expand Up @@ -802,14 +801,14 @@ private void Animations(ModelImporter importer, SerializedAsset? serialized)
{
if (EditorGUI.StyledButton(i + ": " + animations.ElementAt(i).Name))
{
selectedAnim = i + 1;
_selectedAnim = i + 1;
}
}
}

if (selectedAnim > 0 && selectedAnim <= animations.Count())
if (_selectedAnim > 0 && _selectedAnim <= animations.Count())
{
var anim = animations.ElementAt(selectedAnim - 1) as AnimationClip;
var anim = animations.ElementAt(_selectedAnim - 1) as AnimationClip;
gui.TextNode("aName", $"Name: {anim.Name}").ExpandWidth().Height(ItemSize);
gui.TextNode("aDuration", $"Duration: {anim.Duration}").ExpandWidth().Height(ItemSize);
gui.TextNode("aTPS", $"Ticks Per Second: {anim.TicksPerSecond}").ExpandWidth().Height(ItemSize);
Expand All @@ -825,14 +824,14 @@ private void Animations(ModelImporter importer, SerializedAsset? serialized)
{
if (EditorGUI.StyledButton(i + ": " + anim.Bones[i].BoneName))
{
selectedAnimBone = i;
_selectedAnimBone = i;
}
}
}

if (selectedAnimBone > 0 && selectedAnimBone <= anim.Bones.Count)
if (_selectedAnimBone > 0 && _selectedAnimBone <= anim.Bones.Count)
{
var bone = anim.Bones[selectedAnimBone - 1];
var bone = anim.Bones[_selectedAnimBone - 1];
gui.TextNode("bName", $"Bone Name: {bone.BoneName}").ExpandWidth().Height(ItemSize);
gui.TextNode("bPosKeys", $"Position Keys: {bone.PosX.Keys.Count}").ExpandWidth().Height(ItemSize);
gui.TextNode("bRotKeys", $"Rotation Keys: {bone.RotX.Keys.Count}").ExpandWidth().Height(ItemSize);
Expand Down
2 changes: 2 additions & 0 deletions Prowl.Editor/Editor/AssetsTreeWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,11 @@ private static bool DrawCreateContextMenu(DirectoryInfo? directory, bool fromAss
EditorGuiManager.Directory = directory;
EditorGuiManager.fromAssetBrowser = fromAssetBrowser;
var assetsPath = MenuItem.GetMenuPath("Assets");

foreach (var child in assetsPath.Children)
if (child.Path.Equals("Create", StringComparison.OrdinalIgnoreCase))
return MenuItem.DrawMenu(child, false, 1);

return false;
}

Expand Down
2 changes: 1 addition & 1 deletion Prowl.Editor/Editor/EditorGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -434,4 +434,4 @@ internal static WidgetStyle GetInputStyle()

#endregion

}
}
32 changes: 7 additions & 25 deletions Prowl.Editor/Editor/InspectorWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,15 @@ protected override void Draw()
if (relativeAssetPath != null)
{
// The selected object is a path in our asset database, load its meta data and display a custom editor for the Importer if ones found
if (AssetDatabase.TryGetGuid(path, out var id))
if (AssetDatabase.TryGetGuid(path, out Guid id))
{
var meta = MetaFile.Load(path);
if (meta != null)
{
Type? editorType = ScriptedEditor.GetEditorType(meta.importer.GetType());
if (editorType != null)
ScriptedEditor? editor = ScriptedEditor.CreateEditor(meta, meta.importer.GetType(), false);
if (editor != null)
{
customEditor = (path, (ScriptedEditor)Activator.CreateInstance(editorType));
customEditor.Value.Item2.target = meta;
customEditor.Value.Item2.OnEnable();
customEditor = (path, editor);
destroyCustomEditor = false;
}
else
Expand Down Expand Up @@ -151,28 +149,12 @@ protected override void Draw()
{
if (customEditor == null)
{
// Just selected a new object create the editor
Type? editorType = ScriptedEditor.GetEditorType(Selected.GetType());
if (editorType != null)
ScriptedEditor? editor = ScriptedEditor.CreateEditor(Selected);
if (editor != null)
{
customEditor = (Selected, (ScriptedEditor)Activator.CreateInstance(editorType));
customEditor.Value.Item2.target = Selected;
customEditor.Value.Item2.OnEnable();
customEditor = (Selected, editor);
destroyCustomEditor = false;
}
else
{
// No Editor, Just display Property Grid
if (EditorGUI.PropertyGrid("Default Drawer", ref Selected, EditorGUI.TargetFields.Serializable, EditorGUI.PropertyGridConfig.NoHeader))
{
// Search for a function named "OnValidate()"
var method = Selected.GetType().GetMethod("OnValidate", BindingFlags.Public | BindingFlags.Instance);
if (method != null)
{
method.Invoke(Selected, null);
}
}
}
}
else if (customEditor.Value.Item1 == Selected)
{
Expand Down
32 changes: 32 additions & 0 deletions Prowl.Editor/Editor/ScriptedEditors/ComponentEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// This file is part of the Prowl Game Engine
// Licensed under the MIT License. See the LICENSE file in the project root for details.

using Prowl.Runtime;
using Prowl.Editor.Assets;

using static Prowl.Editor.EditorGUI;

namespace Prowl.Editor;

[CustomEditor(typeof(MonoBehaviour))]
public class ComponentEditor : ScriptedEditor
{
private MonoBehaviour? _component;


public override void OnEnable()
{
_component = target as MonoBehaviour;
}


public override void OnInspectorGUI()
{
if (_component == null)
return;

object componentRef = _component;
if (PropertyGrid("ComponentPropertyGrid", ref componentRef, TargetFields.Serializable | TargetFields.Properties, PropertyGridConfig.NoHeader | PropertyGridConfig.NoBorder | PropertyGridConfig.NoBackground))
_component.OnValidate();
}
}
41 changes: 17 additions & 24 deletions Prowl.Editor/Editor/ScriptedEditors/GameObjectEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ public override void OnInspectorGUI()
// Draw Components
HashSet<int> editorsNeeded = [];
List<MonoBehaviour> toDelete = [];

foreach (var comp in go.GetComponents<MonoBehaviour>())
{
if (comp == null) continue;
Expand Down Expand Up @@ -207,17 +208,18 @@ public override void OnInspectorGUI()

DragnDrop.Drag(comp, comp!.GetType());

var rect = gui.CurrentNode.LayoutData.InnerRect;
Rect rect = gui.CurrentNode.LayoutData.InnerRect;
string cname = GetComponentDisplayName(cType);
var textSizeY = Font.DefaultFont.CalcTextSize(cname, 20).y;
var centerY = (rect.height / 2) - (textSizeY / 2);
gui.Draw2D.DrawText((compOpened ? FontAwesome6.ChevronDown : FontAwesome6.ChevronRight), gui.CurrentNode.LayoutData.GlobalContentPosition + new Vector2(8, centerY + 3));
double textSizeY = Font.DefaultFont.CalcTextSize(cname, 20).y;
double centerY = (rect.height / 2) - (textSizeY / 2);
gui.Draw2D.DrawText(compOpened ? FontAwesome6.ChevronDown : FontAwesome6.ChevronRight, gui.CurrentNode.LayoutData.GlobalContentPosition + new Vector2(8, centerY + 3));
gui.Draw2D.DrawText(cname, 23, gui.CurrentNode.LayoutData.GlobalContentPosition + new Vector2(29, centerY + 3), Color.black * 0.8f);
gui.Draw2D.DrawText(cname, 23, gui.CurrentNode.LayoutData.GlobalContentPosition + new Vector2(28, centerY + 2));

isEnabled = comp.Enabled;
if (gui.Checkbox("IsEnabledChk", ref isEnabled, Offset.Percentage(1f, -30), 0, out var chkNode, GetInputStyle()))
if (gui.Checkbox("IsEnabledChk", ref isEnabled, Offset.Percentage(1f, -30), 0, out LayoutNode? chkNode, GetInputStyle()))
comp.Enabled = isEnabled;

gui.Tooltip("Is Component Enabled?");


Expand Down Expand Up @@ -248,37 +250,28 @@ public override void OnInspectorGUI()
gui.Draw2D.DrawRectFilled(gui.CurrentNode.LayoutData.Rect, EditorStylePrefs.Instance.WindowBGOne * 0.6f, btnRoundness, 12);

// Handle Editors for this type if we have any
if (compEditors.TryGetValue(comp.InstanceID, out var editor))
if (compEditors.TryGetValue(comp.InstanceID, out ScriptedEditor? editor))
{
editor.OnInspectorGUI();
goto EndComponent;
}
else
{
var editorType = GetEditorType(cType);
if (editorType != null)
editor = CreateEditor(comp);
if (editor != null)
{
editor = Activator.CreateInstance(editorType) as ScriptedEditor;
if (editor != null)
{
compEditors[comp.InstanceID] = editor;
editor.target = comp;
editor.OnEnable();
editor.OnInspectorGUI();
goto EndComponent;
}
compEditors[comp.InstanceID] = editor;
editor.OnInspectorGUI();
}
}

// ScriptedEditor.CreateEditor should provide a fallback default instead of providing
// No Editor, Fallback to default Inspector
object compRef = comp;
if (PropertyGrid("CompPropertyGrid", ref compRef, TargetFields.Serializable | TargetFields.Properties, PropertyGridConfig.NoHeader | PropertyGridConfig.NoBorder | PropertyGridConfig.NoBackground))
comp.OnValidate();
// object compRef = comp;
// if (PropertyGrid("CompPropertyGrid", ref compRef, TargetFields.Serializable | TargetFields.Properties, PropertyGridConfig.NoHeader | PropertyGridConfig.NoBorder | PropertyGridConfig.NoBackground))
// comp.OnValidate();

// Draw any Buttons - these should be in PropertyGrid probably
//EditorGui.HandleAttributeButtons(comp);

EndComponent:;
}
}

Expand All @@ -288,7 +281,7 @@ public override void OnInspectorGUI()
}

// Handle Deletion
foreach (var comp in toDelete)
foreach (MonoBehaviour comp in toDelete)
go.RemoveComponent(comp);

// Remove any editors that are no longer needed
Expand Down
11 changes: 1 addition & 10 deletions Prowl.Editor/Editor/ScriptedEditors/MaterialEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,7 @@ namespace Prowl.Editor.ScriptedEditors;
[CustomEditor(typeof(Material))]
public class MaterialEditor : ScriptedEditor
{
private Action onChange;

private MaterialEditor() { }

public MaterialEditor(Material mat, Action onChange)
{
target = mat;
this.onChange = onChange;
}

public Action onChange;

public override void OnInspectorGUI()
{
Expand Down
Loading

0 comments on commit 329c29c

Please sign in to comment.