Add custom editor

This commit is contained in:
Hubert Mattusch 2023-05-27 21:39:28 +02:00
parent c0dd3dfdc5
commit 8fe292818e
7 changed files with 117 additions and 9 deletions

View File

@ -15,7 +15,7 @@ namespace NEG.Utils.Serialization
return @this.FindPropertyRelative(GetBackingFieldName(name));
}
private static string GetBackingFieldName(string name)
public static string GetBackingFieldName(string name)
{
#if NET_STANDARD || NET_STANDARD_2_1
return string.Create(1/*<*/ + name.Length + 16/*>k__BackingField*/, name, static (span, name) =>

View File

@ -10,9 +10,9 @@ using UnityEngine;
namespace NEG.UI
{
[PublicAPI]
public abstract class UiManager
public abstract class UiManager : IDisposable
{
public static UiManager Instance { get; private set; }
public static UiManager Instance { get; protected set; }
/// <summary>
/// Current area shown on screen.
@ -132,6 +132,8 @@ namespace NEG.UI
UpdatePopupsState(false);
}
public virtual void Dispose() => Instance = null;
protected void PopupClosed(PopupData data)
{
if (currentShownPopup.data != data)
@ -144,6 +146,7 @@ namespace NEG.UI
protected void SetDefaultPopup(IDefaultPopup popup) => currentDefaultPopup = popup;
private void UpdatePopupsState(bool forceShow, int priority = 0, PopupData data = null)
{
if (forceShow)

View File

@ -2,7 +2,8 @@
"name": "NEG.UI.UnityUi.Editor",
"rootNamespace": "",
"references": [
"GUID:e2aaf8effe1c9634d87b2edda6988a6a"
"GUID:e2aaf8effe1c9634d87b2edda6988a6a",
"GUID:5928dc8d9173fd348aa77d4593ca3fd8"
],
"includePlatforms": [
"Editor"

View File

@ -0,0 +1,94 @@
using NEG.UI.UnityUi.Buttons;
using NEG.Utils.Serialization;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine.UI;
using UnityEngine.UIElements;
using ObjectField = UnityEditor.Search.ObjectField;
using Toggle = UnityEngine.UIElements.Toggle;
namespace NEG.UI.UnityUi.Editor
{
[CustomPropertyDrawer(typeof(OverridableNavigation))]
public class OverridableNavigationDrawer: PropertyDrawer
{
public override VisualElement CreatePropertyGUI(SerializedProperty property)
{
var container = new VisualElement()
{
style =
{
paddingLeft = 3,
paddingRight = -2,
flexDirection = FlexDirection.Row,
justifyContent = Justify.SpaceBetween
}
};
string name = property.name;
if (name.Length > 0)
{
name = $"{char.ToUpper(name[0])}{name[1..]}";
}
var innerContainer = new VisualElement()
{
style =
{
flexDirection = FlexDirection.Row,
justifyContent = Justify.SpaceBetween,
marginRight = 2,
flexGrow = 1
}
};
var label = new Label(name)
{
style =
{
//
}
};
var enabler = new Toggle();
enabler.BindProperty(property.FindPropertyRelative("<Override>k__BackingField"));
var field = new ObjectField()
{
style =
{
flexGrow = 100
}
};
var selectableField = property.FindAutoPropertyRelative(nameof(OverridableNavigation.Selectable));
field.BindProperty(selectableField);
field.objectType = typeof(Selectable);
innerContainer.Add(label);
innerContainer.Add(enabler);
container.Add(innerContainer);
container.Add(field);
container.RegisterCallback<GeometryChangedEvent>(_ =>
{
var panel = container.panel;
var size = panel.visualTree.contentRect.size;
// magic value measured with ruler, may change in the future!
field.style.maxWidth = size.x * 0.55f + 15;
});
field.SetEnabled(enabler.value);
enabler.RegisterValueChangedCallback(v =>
{
field.SetEnabled(v.newValue);
});
return container;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 63ba4de7f1a04763bf035b4a88317a7d
timeCreated: 1685212390

View File

@ -19,7 +19,7 @@ namespace NEG.UI.UnityUi
/// <para> - UI/PopupCanvas - prefab with canvas to create popups (will be created on every scene)</para>
/// <para> - UI/DefaultPopupPrefab - prefab of default popup with 2 options (has to have <see cref="MonoDefaultPopup"/> component)</para>
/// </summary>
public class MonoUiManager : UiManager
public class MonoUiManager : UiManager, IDisposable
{
//TODO: use default unity selection
//TODO: window snaping to slots
@ -60,6 +60,12 @@ namespace NEG.UI.UnityUi
inputModule = (UiInputModule)Activator.CreateInstance(inputModuleType);
}
public override void Dispose()
{
base.Dispose();
Instance = null;
}
private void SpawnDefaultPopup()
{
var canvas = Object.Instantiate(canvasPrefab);
@ -67,6 +73,5 @@ namespace NEG.UI.UnityUi
SetDefaultPopup(Object.Instantiate(defaultPopupPrefab, canvas.transform));
currentDefaultPopup.Close(true);
}
}
}

View File

@ -91,6 +91,8 @@ namespace NEG.Utils
public static Vector3 zzz(this Vector3 a) { return new Vector3(a.z, a.z, a.z); }
public static Vector3 x0y(this Vector2 a) { return new Vector3(a.x, 0f, a.y);}
//swizzles of size 4
public static Vector4 xxxx(this Vector3 a) { return new Vector4(a.x, a.x, a.x, a.x); }
public static Vector4 yxxx(this Vector3 a) { return new Vector4(a.y, a.x, a.x, a.x); }