Skip to content

Commit b22c52e

Browse files
committed
Add InlineEditor modes
1 parent cc22282 commit b22c52e

File tree

5 files changed

+103
-7
lines changed

5 files changed

+103
-7
lines changed

Editor.Extras/Drawers/InlineEditorDrawer.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ public override TriElement CreateElement(TriProperty property, TriElement next)
3030
titleMode = TriBoxGroupElement.TitleMode.Hidden,
3131
});
3232
element.AddChild(new ObjectReferenceFoldoutDrawerElement(property));
33-
element.AddChild(new InlineEditorElement(property));
33+
element.AddChild(new InlineEditorElement(property, new InlineEditorElement.Props
34+
{
35+
mode = Attribute.Mode,
36+
previewHeight = Attribute.PreviewHeight,
37+
previewWidth = Attribute.PreviewWidth,
38+
}));
3439
return element;
3540
}
3641

Editor/Elements/InlineEditorElement.cs

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using TriInspector.Utilities;
2-
using TriInspectorUnityInternalBridge;
1+
using TriInspectorUnityInternalBridge;
32
using UnityEditor;
43
using UnityEngine;
54

@@ -8,13 +7,27 @@ namespace TriInspector.Elements
87
public class InlineEditorElement : TriElement
98
{
109
private readonly TriProperty _property;
10+
private readonly Props _props;
1111
private Editor _editor;
1212
private Rect _editorPosition;
1313
private bool _dirty;
1414

15-
public InlineEditorElement(TriProperty property)
15+
[System.Serializable]
16+
public struct Props
17+
{
18+
public InlineEditorModes mode;
19+
public float previewWidth;
20+
public float previewHeight;
21+
22+
public bool DrawGUI => (mode & InlineEditorModes.GUIOnly) != 0;
23+
public bool DrawHeader => (mode & InlineEditorModes.Header) != 0;
24+
public bool DrawPreview => (mode & InlineEditorModes.Preview) != 0;
25+
}
26+
27+
public InlineEditorElement(TriProperty property, Props props = default)
1628
{
1729
_property = property;
30+
_props = props;
1831
_editorPosition = Rect.zero;
1932
}
2033

@@ -82,9 +95,55 @@ public override void OnGUI(Rect position)
8295
if (_editor != null && shouldDrawEditor)
8396
{
8497
GUILayout.BeginArea(_editorPosition);
85-
GUILayout.BeginVertical();
86-
_editor.OnInspectorGUI();
87-
GUILayout.EndVertical();
98+
GUILayout.BeginHorizontal();
99+
100+
if (_props.DrawHeader || _props.DrawGUI)
101+
{
102+
GUILayout.BeginVertical();
103+
104+
if (_props.DrawHeader)
105+
{
106+
GUILayout.BeginVertical();
107+
_editor.DrawHeader();
108+
GUILayout.EndVertical();
109+
}
110+
111+
if (_props.DrawGUI)
112+
{
113+
GUILayout.BeginVertical();
114+
_editor.OnInspectorGUI();
115+
GUILayout.EndVertical();
116+
}
117+
118+
GUILayout.EndVertical();
119+
}
120+
121+
if (_props.DrawPreview && _editor.HasPreviewGUI())
122+
{
123+
GUILayout.BeginVertical();
124+
125+
var horizontal = _props.DrawHeader || _props.DrawGUI;
126+
127+
var previewOpts = horizontal
128+
? new[] {GUILayout.Width(_props.previewWidth), GUILayout.ExpandHeight(true),}
129+
: new[] {GUILayout.ExpandWidth(true), GUILayout.Height(_props.previewHeight),};
130+
131+
var previewRect = GUILayoutUtility.GetRect(GUIContent.none, GUIStyle.none, previewOpts);
132+
133+
previewRect.width = Mathf.Max(previewRect.width, 10);
134+
previewRect.height = Mathf.Max(previewRect.height, 10);
135+
136+
var guiEnabled = GUI.enabled;
137+
GUI.enabled = true;
138+
139+
_editor.DrawPreview(previewRect);
140+
141+
GUI.enabled = guiEnabled;
142+
143+
GUILayout.EndVertical();
144+
}
145+
146+
GUILayout.EndHorizontal();
88147
lastEditorRect = GUILayoutUtility.GetLastRect();
89148
GUILayout.EndArea();
90149
}

Runtime/Attributes/InlineEditorAttribute.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,18 @@ namespace TriInspector
88
[Conditional("UNITY_EDITOR")]
99
public class InlineEditorAttribute : Attribute
1010
{
11+
public InlineEditorModes Mode { get; set; } = InlineEditorModes.GUIOnly;
12+
13+
public float PreviewWidth { get; set; } = 100;
14+
public float PreviewHeight { get; set; } = 50;
15+
16+
public InlineEditorAttribute()
17+
{
18+
}
19+
20+
public InlineEditorAttribute(InlineEditorModes mode)
21+
{
22+
Mode = mode;
23+
}
1124
}
1225
}

Runtime/InlineEditorModes.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
3+
namespace TriInspector
4+
{
5+
[Flags]
6+
public enum InlineEditorModes
7+
{
8+
GUIOnly = 1 << 0,
9+
Header = 1 << 1,
10+
Preview = 1 << 2,
11+
12+
GUIAndPreview = GUIOnly | Preview,
13+
GUIAndHeader = GUIOnly | Header,
14+
FullEditor = GUIOnly | Header | Preview,
15+
}
16+
}

Runtime/InlineEditorModes.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.

0 commit comments

Comments
 (0)