Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Editor.Extras/Drawers/CustomBuiltInDrawer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using TriInspector;
using TriInspector.Drawers;
using TriInspector.Editors;
using TriInspector.Elements;
using TriInspector.Utilities;
using TriInspectorUnityInternalBridge;
Expand All @@ -24,6 +25,15 @@ public override TriElement CreateElement(TriValue<object> propertyValue, TriElem

if (drawWithHandler)
{
var visualElement = handler.CreatePropertyGUI(serializedProperty);

if (visualElement != null &&
TriEditor.UiElementsRoots.TryGetValue(property.PropertyTree, out var rootElement))
{
return new TriUiToolkitPropertyElement(property, serializedProperty,
visualElement, rootElement);
}

return new TriBuiltInPropertyElement(property, serializedProperty, handler);
}
}
Expand Down
57 changes: 55 additions & 2 deletions Editor/Editors/TriEditor.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,79 @@
using System;
using System.Collections.Generic;
using TriInspector.Utilities;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

namespace TriInspector.Editors
{
public abstract class TriEditor : Editor
{
internal static readonly Dictionary<TriPropertyTree, VisualElement> UiElementsRoots
= new Dictionary<TriPropertyTree, VisualElement>();

private TriPropertyTreeForSerializedObject _inspector;

private void OnDisable()
{
OnDisable(this, ref _inspector);
}

public override VisualElement CreateInspectorGUI()
{
return CreateInspector(root => OnInspectorGUI(this, ref _inspector, root));
}

public override void OnInspectorGUI()
{
OnInspectorGUI(this, ref _inspector);
}

public static void OnDisable(Editor editor, ref TriPropertyTreeForSerializedObject inspector)
{
inspector?.Dispose();
if (inspector != null)
{
UiElementsRoots.Remove(inspector);

inspector.Dispose();
}

inspector = null;
}

public static VisualElement CreateInspector(Action<VisualElement> onGui)
{
var container = new VisualElement();
var root = new VisualElement()
{
style =
{
position = Position.Absolute,
},
};

container.Add(new IMGUIContainer(() =>
{
const float labelExtraPadding = 2;
const float labelWidthRatio = 0.45f;
const float labelMinWidth = 120;

var space = container.resolvedStyle.left + container.resolvedStyle.right + labelExtraPadding;

EditorGUIUtility.hierarchyMode = false;
EditorGUIUtility.labelWidth = Mathf.Max(labelMinWidth,
container.resolvedStyle.width * labelWidthRatio - space);

onGui?.Invoke(root);
}));

container.Add(root);

return container;
}

public static void OnInspectorGUI(Editor editor,
ref TriPropertyTreeForSerializedObject inspector)
ref TriPropertyTreeForSerializedObject inspector, VisualElement visualRoot = null)
{
var serializedObject = editor.serializedObject;

Expand Down Expand Up @@ -54,6 +102,11 @@ public static void OnInspectorGUI(Editor editor,
inspector = new TriPropertyTreeForSerializedObject(serializedObject);
}

if (visualRoot != null)
{
UiElementsRoots[inspector] = visualRoot;
}

serializedObject.UpdateIfRequiredOrScript();

inspector.Update();
Expand Down
13 changes: 12 additions & 1 deletion Editor/Editors/TriScriptedImporterEditor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using TriInspectorUnityInternalBridge;
using UnityEditor;
using UnityEditor.AssetImporters;
using UnityEngine.UIElements;

namespace TriInspector.Editors
{
Expand All @@ -17,9 +18,19 @@ public override void OnDisable()
base.OnDisable();
}

public override VisualElement CreateInspectorGUI()
{
return TriEditor.CreateInspector(root => OnInspectorGUI(root));
}

public override void OnInspectorGUI()
{
TriEditor.OnInspectorGUI(this, ref _inspector);
OnInspectorGUI(null);
}

private void OnInspectorGUI(VisualElement root)
{
TriEditor.OnInspectorGUI(this, ref _inspector, root);

if (extraDataType != null)
{
Expand Down
91 changes: 91 additions & 0 deletions Editor/Elements/TriUiToolkitPropertyElemenet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using TriInspectorUnityInternalBridge;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;

namespace TriInspector.Elements
{
internal class TriUiToolkitPropertyElement : TriElement
{
private readonly SerializedProperty _serializedProperty;

private readonly VisualElement _rootElement;
private readonly VisualElement _selfElement;

private bool _heightDirty;

public TriUiToolkitPropertyElement(
TriProperty property,
SerializedProperty serializedProperty,
VisualElement selfElement,
VisualElement rootElement)
{
_serializedProperty = serializedProperty;
_selfElement = selfElement;
_rootElement = rootElement;

_selfElement.style.position = Position.Absolute;
}

protected override void OnAttachToPanel()
{
base.OnAttachToPanel();

_rootElement.schedule.Execute(() =>
{
_rootElement.Add(_selfElement);
_selfElement.Bind(_serializedProperty.serializedObject);
});
}

protected override void OnDetachFromPanel()
{
_rootElement.schedule.Execute(() =>
{
_selfElement.Unbind();
_rootElement.Remove(_selfElement);
});

base.OnDetachFromPanel();
}

public override bool Update()
{
var dirty = base.Update();

if (_heightDirty)
{
_heightDirty = false;
dirty = true;
}

return dirty;
}

public override float GetHeight(float width)
{
var height = _selfElement.resolvedStyle.height;

if (float.IsNaN(height))
{
_heightDirty = true;
return 0f;
}

return height;
}

public override void OnGUI(Rect position)
{
if (Event.current.type == EventType.Repaint)
{
var pos = GUIClipProxy.UnClip(position.position);

_selfElement.style.width = position.width;
_selfElement.style.left = pos.x;
_selfElement.style.top = pos.y;
}
}
}
}
3 changes: 3 additions & 0 deletions Editor/Elements/TriUiToolkitPropertyElemenet.cs.meta

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

26 changes: 26 additions & 0 deletions Unity.InternalAPIEditorBridge.012/GUIClipProxy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using UnityEditor;
using UnityEngine;

namespace TriInspectorUnityInternalBridge
{
internal static class GUIClipProxy
{
private static Func<Vector2, Vector2> _guiClipUnClipVector2;

[InitializeOnLoadMethod]
private static void Setup()
{
var imGuiModuleAssembly = typeof(GUI).Assembly;
var guiClipType = imGuiModuleAssembly.GetType("UnityEngine.GUIClip", throwOnError: true);

_guiClipUnClipVector2 = (Func<Vector2, Vector2>) Delegate.CreateDelegate(typeof(Func<Vector2, Vector2>),
guiClipType.GetMethod("Unclip", new[] {typeof(Vector2)}));
}

public static Vector2 UnClip(Vector2 pos)
{
return _guiClipUnClipVector2.Invoke(pos);
}
}
}
3 changes: 3 additions & 0 deletions Unity.InternalAPIEditorBridge.012/GUIClipProxy.cs.meta

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

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

namespace TriInspectorUnityInternalBridge
{
Expand All @@ -24,6 +25,11 @@ internal PropertyHandlerProxy(PropertyHandler handler)
// ReSharper disable once InconsistentNaming
public bool hasPropertyDrawer => _handler.hasPropertyDrawer;

public VisualElement CreatePropertyGUI(SerializedProperty property)
{
return _handler.propertyDrawer?.CreatePropertyGUI(property);
}

public float GetHeight(SerializedProperty property, GUIContent label, bool includeChildren)
{
return _handler.GetHeight(property, label, includeChildren);
Expand Down