From 7bbc03cdbf5ddc783a5f42046dcc2bf767e26f09 Mon Sep 17 00:00:00 2001 From: Hubert Mattusch Date: Fri, 27 Jan 2023 15:31:25 +0100 Subject: [PATCH] more functions --- .../CanvasScalerDefault.cs | 30 ++++++++++++++ .../CanvasScalerDefault.cs.meta | 11 +++++ NEG/UI/Area/IArea.cs | 19 +-------- NEG/UI/PriorityQueue.cs | 6 +-- NEG/UI/UnityUi/Area/AutoWindowOpen.cs | 14 +++++++ NEG/UI/UnityUi/Area/AutoWindowOpen.cs.meta | 3 ++ NEG/UI/UnityUi/Area/MonoArea.cs | 11 ++++- NEG/UI/UnityUi/Buttons/ChangeSceneButton.cs | 26 ++++++++++++ .../UnityUi/Buttons/ChangeSceneButton.cs.meta | 11 +++++ .../Buttons/CloseWindowOnImageTouch.cs | 14 +++++++ .../Buttons/CloseWindowOnImageTouch.cs.meta | 3 ++ NEG/UI/UnityUi/NEG.UI.UnityUi.asmdef | 3 +- NEG/UI/UnityUi/Window/MonoWindow.cs | 4 ++ NEG/UI/UnityUi/WindowSlot/MonoWindowSlot.cs | 8 +++- NEG/UI/Window/IWindow.cs | 41 ++++++++++++------- NEG/UI/WindowSlot/ISlotsHolder.cs | 25 +++++++++++ NEG/UI/WindowSlot/ISlotsHolder.cs.meta | 3 ++ NEG/UI/WindowSlot/IWindowSlot.cs | 3 +- 18 files changed, 196 insertions(+), 39 deletions(-) create mode 100644 Editor/ComponentsAdditionalItems/CanvasScalerDefault.cs create mode 100644 Editor/ComponentsAdditionalItems/CanvasScalerDefault.cs.meta create mode 100644 NEG/UI/UnityUi/Area/AutoWindowOpen.cs create mode 100644 NEG/UI/UnityUi/Area/AutoWindowOpen.cs.meta create mode 100644 NEG/UI/UnityUi/Buttons/ChangeSceneButton.cs create mode 100644 NEG/UI/UnityUi/Buttons/ChangeSceneButton.cs.meta create mode 100644 NEG/UI/UnityUi/Buttons/CloseWindowOnImageTouch.cs create mode 100644 NEG/UI/UnityUi/Buttons/CloseWindowOnImageTouch.cs.meta create mode 100644 NEG/UI/WindowSlot/ISlotsHolder.cs create mode 100644 NEG/UI/WindowSlot/ISlotsHolder.cs.meta diff --git a/Editor/ComponentsAdditionalItems/CanvasScalerDefault.cs b/Editor/ComponentsAdditionalItems/CanvasScalerDefault.cs new file mode 100644 index 0000000..a6cb782 --- /dev/null +++ b/Editor/ComponentsAdditionalItems/CanvasScalerDefault.cs @@ -0,0 +1,30 @@ +using UnityEditor; +using UnityEngine; +using UnityEngine.UI; + +namespace NEG.Utils.Editor.ComponentsAdditionalItems +{ + public static class CanvasScalerDefault + { + [MenuItem("CONTEXT/CanvasScaler/Full HD horizontal", false, 2000)] + public static void SetFullHdHorizontal(MenuCommand command) => SetComponent(command, 1920, 1080); + [MenuItem("CONTEXT/CanvasScaler/Full HD vertical", false, 2000)] + public static void SetFullHdVertical(MenuCommand command) => SetComponent(command, 1080, 1920); + [MenuItem("CONTEXT/CanvasScaler/Full 2k horizontal", false, 2000)] + public static void Set2KHorizontal(MenuCommand command) => SetComponent(command, 2560, 1440 ); + [MenuItem("CONTEXT/CanvasScaler/Full 2k vertical", false, 2000)] + public static void Set2KVertical(MenuCommand command) => SetComponent(command, 1440, 2560); + [MenuItem("CONTEXT/CanvasScaler/Full 4k horizontal", false, 2000)] + public static void Set4KHorizontal(MenuCommand command) => SetComponent(command, 3840, 2160); + [MenuItem("CONTEXT/CanvasScaler/Full 4k vertical", false, 2000)] + public static void Set4KVertical(MenuCommand command) => SetComponent(command, 2160, 3840); + + private static void SetComponent(MenuCommand command, int width, int height) + { + var scaler = (CanvasScaler)command.context; + scaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize; + scaler.matchWidthOrHeight = width > height ? 1f : 0f; + scaler.referenceResolution = new Vector2(width, height); + } + } +} \ No newline at end of file diff --git a/Editor/ComponentsAdditionalItems/CanvasScalerDefault.cs.meta b/Editor/ComponentsAdditionalItems/CanvasScalerDefault.cs.meta new file mode 100644 index 0000000..781e85f --- /dev/null +++ b/Editor/ComponentsAdditionalItems/CanvasScalerDefault.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c7ddebddb1a1c1947be05ac9e96aceb8 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/NEG/UI/Area/IArea.cs b/NEG/UI/Area/IArea.cs index dd56cbf..2c7df06 100644 --- a/NEG/UI/Area/IArea.cs +++ b/NEG/UI/Area/IArea.cs @@ -5,23 +5,8 @@ using NEG.UI.WindowSlot; namespace NEG.UI.Area { - public interface IArea : IUiElement + public interface IArea : ISlotsHolder, IUiElement { - IEnumerable AvailableSlots { get; } - - /// - /// Open window - /// - /// - /// - void OpenWindow(IWindow window, object data = null); - - void CloseAllWindows() - { - foreach (var slot in AvailableSlots) - { - slot.CloseAllWindows(); - } - } + void OpenChildWindow(IWindow window, object data = null); } } \ No newline at end of file diff --git a/NEG/UI/PriorityQueue.cs b/NEG/UI/PriorityQueue.cs index 38ca145..74e3e1b 100644 --- a/NEG/UI/PriorityQueue.cs +++ b/NEG/UI/PriorityQueue.cs @@ -742,7 +742,7 @@ namespace System.Collections.Generic int parentIndex = GetParentIndex(nodeIndex); var parent = nodes[parentIndex]; - if (comparer.Compare(node.Priority, parent.Priority) < 0) + if (comparer != null && comparer.Compare(node.Priority, parent.Priority) < 0) { nodes[nodeIndex] = parent; nodeIndex = parentIndex; @@ -828,7 +828,7 @@ namespace System.Collections.Generic while (++i < childIndexUpperBound) { var nextChild = nodes[i]; - if (comparer.Compare(nextChild.Priority, minChild.Priority) < 0) + if (comparer != null && comparer.Compare(nextChild.Priority, minChild.Priority) < 0) { minChild = nextChild; minChildIndex = i; @@ -836,7 +836,7 @@ namespace System.Collections.Generic } // Heap property is satisfied; insert node in this location. - if (comparer.Compare(node.Priority, minChild.Priority) <= 0) break; + if (comparer != null && comparer.Compare(node.Priority, minChild.Priority) <= 0) break; // Move the minimal child up by one node and continue recursively from its location. nodes[nodeIndex] = minChild; diff --git a/NEG/UI/UnityUi/Area/AutoWindowOpen.cs b/NEG/UI/UnityUi/Area/AutoWindowOpen.cs new file mode 100644 index 0000000..1f87e17 --- /dev/null +++ b/NEG/UI/UnityUi/Area/AutoWindowOpen.cs @@ -0,0 +1,14 @@ +using NEG.UI.UnityUi.Window; +using NEG.UI.Window; +using System; +using UnityEngine; + +namespace NEG.UI.Area +{ + public class AutoWindowOpen : MonoBehaviour + { + [SerializeField] private MonoWindow window; + + private void Start() => window.Open(); + } +} \ No newline at end of file diff --git a/NEG/UI/UnityUi/Area/AutoWindowOpen.cs.meta b/NEG/UI/UnityUi/Area/AutoWindowOpen.cs.meta new file mode 100644 index 0000000..b3ed13c --- /dev/null +++ b/NEG/UI/UnityUi/Area/AutoWindowOpen.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: e439a77332dc4e3a881348c9506556c6 +timeCreated: 1673791915 \ No newline at end of file diff --git a/NEG/UI/UnityUi/Area/MonoArea.cs b/NEG/UI/UnityUi/Area/MonoArea.cs index 0a2ade2..1baa112 100644 --- a/NEG/UI/UnityUi/Area/MonoArea.cs +++ b/NEG/UI/UnityUi/Area/MonoArea.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using UnityEngine; using NEG.UI.Popup; +using NEG.UI.UnityUi.Window; using NEG.UI.UnityUi.WindowSlot; using NEG.UI.Window; using NEG.UI.WindowSlot; @@ -17,10 +18,18 @@ namespace NEG.UI.Area [SerializeField] private List windowSlots; - [SerializeField] private Queue currentPopups = new(); + [SerializeField] private Queue currentPopups = new(); public virtual void SetEnabled(bool setEnabled) => gameObject.SetActive(setEnabled); + public void OpenChildWindow(IWindow window, object data = null) + { + foreach (var slot in AvailableSlots) + { + + } + } + public virtual void OpenWindow(IWindow window, object data = null) { DefaultWindowSlot.AttachWindow(window); diff --git a/NEG/UI/UnityUi/Buttons/ChangeSceneButton.cs b/NEG/UI/UnityUi/Buttons/ChangeSceneButton.cs new file mode 100644 index 0000000..d6ec44a --- /dev/null +++ b/NEG/UI/UnityUi/Buttons/ChangeSceneButton.cs @@ -0,0 +1,26 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.SceneManagement; + +namespace NEG.UI.UnityUi.Buttons +{ + [RequireComponent(typeof(BaseButton))] + public class ChangeSceneButton : MonoBehaviour + { + [Header("Leave empty to use int value")] + [SerializeField] private string sceneName; + + [SerializeField] private int sceneIndex; + + private void Awake() => GetComponent().OnButtonPressed += OnClicked; + + private void OnClicked() + { + if (string.IsNullOrEmpty(sceneName)) + SceneManager.LoadScene(sceneIndex); + else + SceneManager.LoadScene(sceneName); + } + } +} diff --git a/NEG/UI/UnityUi/Buttons/ChangeSceneButton.cs.meta b/NEG/UI/UnityUi/Buttons/ChangeSceneButton.cs.meta new file mode 100644 index 0000000..4823740 --- /dev/null +++ b/NEG/UI/UnityUi/Buttons/ChangeSceneButton.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 15537f041c9c1204ebf05a5fb7ff6a3d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/NEG/UI/UnityUi/Buttons/CloseWindowOnImageTouch.cs b/NEG/UI/UnityUi/Buttons/CloseWindowOnImageTouch.cs new file mode 100644 index 0000000..4ea19c3 --- /dev/null +++ b/NEG/UI/UnityUi/Buttons/CloseWindowOnImageTouch.cs @@ -0,0 +1,14 @@ +using NEG.UI.UnityUi.Window; +using NEG.UI.Window; +using UnityEngine; +using UnityEngine.EventSystems; + +namespace NEG.UI.UnityUi.Buttons +{ + public class CloseWindowOnImageTouch : MonoBehaviour, IPointerDownHandler + { + [SerializeField] private MonoWindow windowToClose; + + public void OnPointerDown(PointerEventData eventData) => windowToClose.Close(); + } +} \ No newline at end of file diff --git a/NEG/UI/UnityUi/Buttons/CloseWindowOnImageTouch.cs.meta b/NEG/UI/UnityUi/Buttons/CloseWindowOnImageTouch.cs.meta new file mode 100644 index 0000000..7a8a485 --- /dev/null +++ b/NEG/UI/UnityUi/Buttons/CloseWindowOnImageTouch.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: dc1aab636014456da7f768a05d0f143e +timeCreated: 1673909580 \ No newline at end of file diff --git a/NEG/UI/UnityUi/NEG.UI.UnityUi.asmdef b/NEG/UI/UnityUi/NEG.UI.UnityUi.asmdef index 833fde9..e83fade 100644 --- a/NEG/UI/UnityUi/NEG.UI.UnityUi.asmdef +++ b/NEG/UI/UnityUi/NEG.UI.UnityUi.asmdef @@ -7,7 +7,8 @@ "GUID:6055be8ebefd69e48b49212b09b47b2f", "GUID:0c752da273b17c547ae705acf0f2adf2", "GUID:9e24947de15b9834991c9d8411ea37cf", - "GUID:84651a3751eca9349aac36a66bba901b" + "GUID:84651a3751eca9349aac36a66bba901b", + "GUID:23eed6c2401dca1419d1ebd180e58c5a" ], "includePlatforms": [], "excludePlatforms": [], diff --git a/NEG/UI/UnityUi/Window/MonoWindow.cs b/NEG/UI/UnityUi/Window/MonoWindow.cs index 36f5d7e..95db2a4 100644 --- a/NEG/UI/UnityUi/Window/MonoWindow.cs +++ b/NEG/UI/UnityUi/Window/MonoWindow.cs @@ -3,6 +3,7 @@ using NEG.UI.UnityUi.WindowSlot; using NEG.UI.Window; using NEG.UI.WindowSlot; using System; +using System.Collections.Generic; using UnityEngine; namespace NEG.UI.UnityUi.Window @@ -44,5 +45,8 @@ namespace NEG.UI.UnityUi.Window if (controller == null) controller = GetComponent(); } + + public IEnumerable AvailableSlots { get; } + public void OpenWindow(IWindow window, object data = null) => throw new NotImplementedException(); } } \ No newline at end of file diff --git a/NEG/UI/UnityUi/WindowSlot/MonoWindowSlot.cs b/NEG/UI/UnityUi/WindowSlot/MonoWindowSlot.cs index 5d454e4..6525e6b 100644 --- a/NEG/UI/UnityUi/WindowSlot/MonoWindowSlot.cs +++ b/NEG/UI/UnityUi/WindowSlot/MonoWindowSlot.cs @@ -2,14 +2,20 @@ using NEG.UI.Window; using NEG.UI.WindowSlot; using UnityEngine; +using TNRD; namespace NEG.UI.UnityUi.WindowSlot { public abstract class MonoWindowSlot : MonoBehaviour, IWindowSlot { - public IArea ParentArea { get; private set; } + [field: SerializeField] public bool OpenWindowAsMain { get; private set; } + + public ISlotsHolder ParentHolder => slotsHolder.Value; public abstract void AttachWindow(IWindow window); public abstract void DetachWindow(IWindow window); public abstract void CloseAllWindows(); + + [SerializeField] private SerializableInterface slotsHolder; + } } \ No newline at end of file diff --git a/NEG/UI/Window/IWindow.cs b/NEG/UI/Window/IWindow.cs index 02961b4..f3e7262 100644 --- a/NEG/UI/Window/IWindow.cs +++ b/NEG/UI/Window/IWindow.cs @@ -5,11 +5,10 @@ using UnityEngine; namespace NEG.UI.Window { - public interface IWindow + public interface IWindow : ISlotsHolder { IWindowSlot Parent { get; } - IWindowSlot ChildWindowSlot { get; } - + /// /// Called internally by slot to open window. /// @@ -33,20 +32,32 @@ namespace NEG.UI.Window { slot.AttachWindow(window); window.SetData(data); - } - else - UiManager.Instance.CurrentArea.OpenWindow(window, data); - } - public static void Open(this IWindow window, IArea area, object data = null) => area.OpenWindow(window, data); - public static void OpenChildWindow(this IWindow window, IWindow windowToOpen, object data = null) - { - if (window.ChildWindowSlot == null) - { - Debug.LogError("This window doesn't contain area for child windows"); return; } - //TODO: DO it - //window.ChildWindowArea.OpenWindow(windowToOpen, slot); + + UiManager.Instance.CurrentArea.OpenWindow(window, data); + } + + public static void Open(this IWindow window, IArea area, object data = null) => area.OpenWindow(window, data); + public static void OpenChild(this IWindow window, IWindow windowToOpen, object data = null) + { + if (windowToOpen == null) + { + Debug.LogError($"Window to open cannot be null"); + return; + } + window.OpenWindow(windowToOpen, data); + } + + public static void OpenAsChild(this IWindow window, IWindow parentWindow = null, object data = null) + { + if (parentWindow != null) + { + parentWindow.OpenWindow(window); + return; + } + + UiManager.Instance.CurrentArea.OpenChildWindow(window, data); } public static void Close(this IWindow window) => window.Parent.DetachWindow(window); diff --git a/NEG/UI/WindowSlot/ISlotsHolder.cs b/NEG/UI/WindowSlot/ISlotsHolder.cs new file mode 100644 index 0000000..f3740a9 --- /dev/null +++ b/NEG/UI/WindowSlot/ISlotsHolder.cs @@ -0,0 +1,25 @@ +using NEG.UI.Window; +using System.Collections.Generic; + +namespace NEG.UI.WindowSlot +{ + public interface ISlotsHolder + { + IEnumerable AvailableSlots { get; } + + /// + /// Open window + /// + /// + /// + void OpenWindow(IWindow window, object data = null); + + void CloseAllWindows() + { + foreach (var slot in AvailableSlots) + { + slot.CloseAllWindows(); + } + } + } +} \ No newline at end of file diff --git a/NEG/UI/WindowSlot/ISlotsHolder.cs.meta b/NEG/UI/WindowSlot/ISlotsHolder.cs.meta new file mode 100644 index 0000000..9aad87f --- /dev/null +++ b/NEG/UI/WindowSlot/ISlotsHolder.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: ace3899aabb0491d80f8b17bd827854b +timeCreated: 1674408955 \ No newline at end of file diff --git a/NEG/UI/WindowSlot/IWindowSlot.cs b/NEG/UI/WindowSlot/IWindowSlot.cs index 8d58681..16f2189 100644 --- a/NEG/UI/WindowSlot/IWindowSlot.cs +++ b/NEG/UI/WindowSlot/IWindowSlot.cs @@ -5,7 +5,8 @@ namespace NEG.UI.WindowSlot { public interface IWindowSlot { - IArea ParentArea { get; } + bool OpenWindowAsMain { get; } + ISlotsHolder ParentHolder { get; } void AttachWindow(IWindow window); void DetachWindow(IWindow window); void CloseAllWindows();