From 8fe292818e3375ee1781b1065818b1f62c52943d Mon Sep 17 00:00:00 2001 From: Hubert Mattusch Date: Sat, 27 May 2023 21:39:28 +0200 Subject: [PATCH] Add custom editor --- Editor/SerializationExtentions.cs | 4 +- NEG/UI/UiManager.cs | 11 ++- .../Editor/NEG.UI.UnityUi.Editor.asmdef | 3 +- .../Editor/OverridableNavigationDrawer.cs | 94 +++++++++++++++++++ .../OverridableNavigationDrawer.cs.meta | 3 + NEG/UI/UnityUi/MonoUiManager.cs | 9 +- VectorSwizzle.cs | 2 + 7 files changed, 117 insertions(+), 9 deletions(-) create mode 100644 NEG/UI/UnityUi/Editor/OverridableNavigationDrawer.cs create mode 100644 NEG/UI/UnityUi/Editor/OverridableNavigationDrawer.cs.meta diff --git a/Editor/SerializationExtentions.cs b/Editor/SerializationExtentions.cs index e3ba2c3..2e37ad4 100644 --- a/Editor/SerializationExtentions.cs +++ b/Editor/SerializationExtentions.cs @@ -14,8 +14,8 @@ 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) => diff --git a/NEG/UI/UiManager.cs b/NEG/UI/UiManager.cs index 7348c9e..a935581 100644 --- a/NEG/UI/UiManager.cs +++ b/NEG/UI/UiManager.cs @@ -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; } /// /// Current area shown on screen. @@ -131,7 +131,9 @@ namespace NEG.UI return; UpdatePopupsState(false); } - + + public virtual void Dispose() => Instance = null; + protected void PopupClosed(PopupData data) { if (currentShownPopup.data != data) @@ -141,9 +143,10 @@ namespace NEG.UI } UpdatePopupsState(false); } - + protected void SetDefaultPopup(IDefaultPopup popup) => currentDefaultPopup = popup; + private void UpdatePopupsState(bool forceShow, int priority = 0, PopupData data = null) { if (forceShow) diff --git a/NEG/UI/UnityUi/Editor/NEG.UI.UnityUi.Editor.asmdef b/NEG/UI/UnityUi/Editor/NEG.UI.UnityUi.Editor.asmdef index 538cc41..f943876 100644 --- a/NEG/UI/UnityUi/Editor/NEG.UI.UnityUi.Editor.asmdef +++ b/NEG/UI/UnityUi/Editor/NEG.UI.UnityUi.Editor.asmdef @@ -2,7 +2,8 @@ "name": "NEG.UI.UnityUi.Editor", "rootNamespace": "", "references": [ - "GUID:e2aaf8effe1c9634d87b2edda6988a6a" + "GUID:e2aaf8effe1c9634d87b2edda6988a6a", + "GUID:5928dc8d9173fd348aa77d4593ca3fd8" ], "includePlatforms": [ "Editor" diff --git a/NEG/UI/UnityUi/Editor/OverridableNavigationDrawer.cs b/NEG/UI/UnityUi/Editor/OverridableNavigationDrawer.cs new file mode 100644 index 0000000..b667f49 --- /dev/null +++ b/NEG/UI/UnityUi/Editor/OverridableNavigationDrawer.cs @@ -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("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(_ => + { + 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; + } + } +} \ No newline at end of file diff --git a/NEG/UI/UnityUi/Editor/OverridableNavigationDrawer.cs.meta b/NEG/UI/UnityUi/Editor/OverridableNavigationDrawer.cs.meta new file mode 100644 index 0000000..4762951 --- /dev/null +++ b/NEG/UI/UnityUi/Editor/OverridableNavigationDrawer.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 63ba4de7f1a04763bf035b4a88317a7d +timeCreated: 1685212390 \ No newline at end of file diff --git a/NEG/UI/UnityUi/MonoUiManager.cs b/NEG/UI/UnityUi/MonoUiManager.cs index 13bf73b..041325c 100644 --- a/NEG/UI/UnityUi/MonoUiManager.cs +++ b/NEG/UI/UnityUi/MonoUiManager.cs @@ -19,7 +19,7 @@ namespace NEG.UI.UnityUi /// - UI/PopupCanvas - prefab with canvas to create popups (will be created on every scene) /// - UI/DefaultPopupPrefab - prefab of default popup with 2 options (has to have component) /// - 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); } - } } \ No newline at end of file diff --git a/VectorSwizzle.cs b/VectorSwizzle.cs index 00eb443..d1a7049 100644 --- a/VectorSwizzle.cs +++ b/VectorSwizzle.cs @@ -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); }