Skip to content

Commit

Permalink
Add InlineEditor modes
Browse files Browse the repository at this point in the history
  • Loading branch information
vanifatovvlad committed Apr 6, 2024
1 parent cc22282 commit b22c52e
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 7 deletions.
7 changes: 6 additions & 1 deletion Editor.Extras/Drawers/InlineEditorDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ public override TriElement CreateElement(TriProperty property, TriElement next)
titleMode = TriBoxGroupElement.TitleMode.Hidden,
});
element.AddChild(new ObjectReferenceFoldoutDrawerElement(property));
element.AddChild(new InlineEditorElement(property));
element.AddChild(new InlineEditorElement(property, new InlineEditorElement.Props
{
mode = Attribute.Mode,
previewHeight = Attribute.PreviewHeight,
previewWidth = Attribute.PreviewWidth,
}));
return element;
}

Expand Down
71 changes: 65 additions & 6 deletions Editor/Elements/InlineEditorElement.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using TriInspector.Utilities;
using TriInspectorUnityInternalBridge;
using TriInspectorUnityInternalBridge;
using UnityEditor;
using UnityEngine;

Expand All @@ -8,13 +7,27 @@ namespace TriInspector.Elements
public class InlineEditorElement : TriElement
{
private readonly TriProperty _property;
private readonly Props _props;
private Editor _editor;
private Rect _editorPosition;
private bool _dirty;

public InlineEditorElement(TriProperty property)
[System.Serializable]
public struct Props
{
public InlineEditorModes mode;
public float previewWidth;
public float previewHeight;

public bool DrawGUI => (mode & InlineEditorModes.GUIOnly) != 0;
public bool DrawHeader => (mode & InlineEditorModes.Header) != 0;
public bool DrawPreview => (mode & InlineEditorModes.Preview) != 0;
}

public InlineEditorElement(TriProperty property, Props props = default)
{
_property = property;
_props = props;
_editorPosition = Rect.zero;
}

Expand Down Expand Up @@ -82,9 +95,55 @@ public override void OnGUI(Rect position)
if (_editor != null && shouldDrawEditor)
{
GUILayout.BeginArea(_editorPosition);
GUILayout.BeginVertical();
_editor.OnInspectorGUI();
GUILayout.EndVertical();
GUILayout.BeginHorizontal();

if (_props.DrawHeader || _props.DrawGUI)
{
GUILayout.BeginVertical();

if (_props.DrawHeader)
{
GUILayout.BeginVertical();
_editor.DrawHeader();
GUILayout.EndVertical();
}

if (_props.DrawGUI)
{
GUILayout.BeginVertical();
_editor.OnInspectorGUI();
GUILayout.EndVertical();
}

GUILayout.EndVertical();
}

if (_props.DrawPreview && _editor.HasPreviewGUI())
{
GUILayout.BeginVertical();

var horizontal = _props.DrawHeader || _props.DrawGUI;

var previewOpts = horizontal
? new[] {GUILayout.Width(_props.previewWidth), GUILayout.ExpandHeight(true),}
: new[] {GUILayout.ExpandWidth(true), GUILayout.Height(_props.previewHeight),};

var previewRect = GUILayoutUtility.GetRect(GUIContent.none, GUIStyle.none, previewOpts);

previewRect.width = Mathf.Max(previewRect.width, 10);
previewRect.height = Mathf.Max(previewRect.height, 10);

var guiEnabled = GUI.enabled;
GUI.enabled = true;

_editor.DrawPreview(previewRect);

GUI.enabled = guiEnabled;

GUILayout.EndVertical();
}

GUILayout.EndHorizontal();
lastEditorRect = GUILayoutUtility.GetLastRect();
GUILayout.EndArea();
}
Expand Down
13 changes: 13 additions & 0 deletions Runtime/Attributes/InlineEditorAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,18 @@ namespace TriInspector
[Conditional("UNITY_EDITOR")]
public class InlineEditorAttribute : Attribute
{
public InlineEditorModes Mode { get; set; } = InlineEditorModes.GUIOnly;

public float PreviewWidth { get; set; } = 100;
public float PreviewHeight { get; set; } = 50;

public InlineEditorAttribute()
{
}

public InlineEditorAttribute(InlineEditorModes mode)
{
Mode = mode;
}
}
}
16 changes: 16 additions & 0 deletions Runtime/InlineEditorModes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;

namespace TriInspector
{
[Flags]
public enum InlineEditorModes
{
GUIOnly = 1 << 0,
Header = 1 << 1,
Preview = 1 << 2,

GUIAndPreview = GUIOnly | Preview,
GUIAndHeader = GUIOnly | Header,
FullEditor = GUIOnly | Header | Preview,
}
}
3 changes: 3 additions & 0 deletions Runtime/InlineEditorModes.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b22c52e

Please sign in to comment.