From a5fc6179fb686b2da69f8d941bc23a810cbb7ee6 Mon Sep 17 00:00:00 2001 From: Hubert Mattusch Date: Fri, 16 Dec 2022 02:30:06 +0100 Subject: [PATCH 1/8] basic functionality --- Editor/NEG.Utils.Editor.asmdef | 18 ++++++ Editor/NEG.Utils.Editor.asmdef.meta | 7 +++ NEG.meta | 3 + NEG/UI.meta | 3 + NEG/UI/Area.meta | 3 + NEG/UI/Area/IArea.cs | 32 ++++++++++ NEG/UI/Area/IArea.cs.meta | 3 + NEG/UI/Area/MonoArea.cs | 58 ++++++++++++++++++ NEG/UI/Area/MonoArea.cs.meta | 3 + NEG/UI/IUiElement.cs | 7 +++ NEG/UI/IUiElement.cs.meta | 3 + NEG/UI/NEG.UI.asmdef | 14 +++++ NEG/UI/NEG.UI.asmdef.meta | 7 +++ NEG/UI/Popup.meta | 3 + NEG/UI/Popup/IPopup.cs | 45 ++++++++++++++ NEG/UI/Popup/IPopup.cs.meta | 3 + NEG/UI/Popup/MonoPopup.cs | 24 ++++++++ NEG/UI/Popup/MonoPopup.cs.meta | 3 + NEG/UI/UiManager.cs | 50 ++++++++++++++++ NEG/UI/UiManager.cs.meta | 11 ++++ NEG/UI/UnityUi.meta | 8 +++ NEG/UI/UnityUi/Buttons.meta | 3 + NEG/UI/UnityUi/Buttons/BaseButton.cs | 59 +++++++++++++++++++ NEG/UI/UnityUi/Buttons/BaseButton.cs.meta | 3 + .../UnityUi/Buttons/ButtonSerializeFields.cs | 33 +++++++++++ .../Buttons/ButtonSerializeFields.cs.meta | 3 + NEG/UI/UnityUi/Buttons/CloseWindow.cs | 23 ++++++++ NEG/UI/UnityUi/Buttons/CloseWindow.cs.meta | 3 + NEG/UI/UnityUi/Buttons/OpenWindow.cs | 19 ++++++ NEG/UI/UnityUi/Buttons/OpenWindow.cs.meta | 3 + NEG/UI/UnityUi/NEG.UI.UnityUi.asmdef | 21 +++++++ NEG/UI/UnityUi/NEG.UI.UnityUi.asmdef.meta | 7 +++ NEG/UI/Window.meta | 3 + NEG/UI/Window/IWindow.cs | 48 +++++++++++++++ NEG/UI/Window/IWindow.cs.meta | 3 + NEG/UI/Window/MonoWindow.cs | 30 ++++++++++ NEG/UI/Window/MonoWindow.cs.meta | 3 + NEG/UI/WindowSlot.meta | 3 + NEG/UI/WindowSlot/IWindowSlot.cs | 13 ++++ NEG/UI/WindowSlot/IWindowSlot.cs.meta | 3 + NEG/UI/WindowSlot/MonoWindowSlot.cs | 14 +++++ NEG/UI/WindowSlot/MonoWindowSlot.cs.meta | 3 + NEG/UI/WindowSlot/SingleWindowSlot.cs | 25 ++++++++ NEG/UI/WindowSlot/SingleWindowSlot.cs.meta | 3 + 44 files changed, 636 insertions(+) create mode 100644 Editor/NEG.Utils.Editor.asmdef create mode 100644 Editor/NEG.Utils.Editor.asmdef.meta create mode 100644 NEG.meta create mode 100644 NEG/UI.meta create mode 100644 NEG/UI/Area.meta create mode 100644 NEG/UI/Area/IArea.cs create mode 100644 NEG/UI/Area/IArea.cs.meta create mode 100644 NEG/UI/Area/MonoArea.cs create mode 100644 NEG/UI/Area/MonoArea.cs.meta create mode 100644 NEG/UI/IUiElement.cs create mode 100644 NEG/UI/IUiElement.cs.meta create mode 100644 NEG/UI/NEG.UI.asmdef create mode 100644 NEG/UI/NEG.UI.asmdef.meta create mode 100644 NEG/UI/Popup.meta create mode 100644 NEG/UI/Popup/IPopup.cs create mode 100644 NEG/UI/Popup/IPopup.cs.meta create mode 100644 NEG/UI/Popup/MonoPopup.cs create mode 100644 NEG/UI/Popup/MonoPopup.cs.meta create mode 100644 NEG/UI/UiManager.cs create mode 100644 NEG/UI/UiManager.cs.meta create mode 100644 NEG/UI/UnityUi.meta create mode 100644 NEG/UI/UnityUi/Buttons.meta create mode 100644 NEG/UI/UnityUi/Buttons/BaseButton.cs create mode 100644 NEG/UI/UnityUi/Buttons/BaseButton.cs.meta create mode 100644 NEG/UI/UnityUi/Buttons/ButtonSerializeFields.cs create mode 100644 NEG/UI/UnityUi/Buttons/ButtonSerializeFields.cs.meta create mode 100644 NEG/UI/UnityUi/Buttons/CloseWindow.cs create mode 100644 NEG/UI/UnityUi/Buttons/CloseWindow.cs.meta create mode 100644 NEG/UI/UnityUi/Buttons/OpenWindow.cs create mode 100644 NEG/UI/UnityUi/Buttons/OpenWindow.cs.meta create mode 100644 NEG/UI/UnityUi/NEG.UI.UnityUi.asmdef create mode 100644 NEG/UI/UnityUi/NEG.UI.UnityUi.asmdef.meta create mode 100644 NEG/UI/Window.meta create mode 100644 NEG/UI/Window/IWindow.cs create mode 100644 NEG/UI/Window/IWindow.cs.meta create mode 100644 NEG/UI/Window/MonoWindow.cs create mode 100644 NEG/UI/Window/MonoWindow.cs.meta create mode 100644 NEG/UI/WindowSlot.meta create mode 100644 NEG/UI/WindowSlot/IWindowSlot.cs create mode 100644 NEG/UI/WindowSlot/IWindowSlot.cs.meta create mode 100644 NEG/UI/WindowSlot/MonoWindowSlot.cs create mode 100644 NEG/UI/WindowSlot/MonoWindowSlot.cs.meta create mode 100644 NEG/UI/WindowSlot/SingleWindowSlot.cs create mode 100644 NEG/UI/WindowSlot/SingleWindowSlot.cs.meta diff --git a/Editor/NEG.Utils.Editor.asmdef b/Editor/NEG.Utils.Editor.asmdef new file mode 100644 index 0000000..60746f0 --- /dev/null +++ b/Editor/NEG.Utils.Editor.asmdef @@ -0,0 +1,18 @@ +{ + "name": "NEG.Utils.Editor", + "rootNamespace": "", + "references": [ + "GUID:3c4294719a93e3c4e831a9ff0c261e8a" + ], + "includePlatforms": [ + "Editor" + ], + "excludePlatforms": [], + "allowUnsafeCode": false, + "overrideReferences": false, + "precompiledReferences": [], + "autoReferenced": true, + "defineConstraints": [], + "versionDefines": [], + "noEngineReferences": false +} \ No newline at end of file diff --git a/Editor/NEG.Utils.Editor.asmdef.meta b/Editor/NEG.Utils.Editor.asmdef.meta new file mode 100644 index 0000000..e1a6a84 --- /dev/null +++ b/Editor/NEG.Utils.Editor.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 5928dc8d9173fd348aa77d4593ca3fd8 +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/NEG.meta b/NEG.meta new file mode 100644 index 0000000..9b90a19 --- /dev/null +++ b/NEG.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 61eeed9106e147d8a76d7f0140d3571d +timeCreated: 1670708950 \ No newline at end of file diff --git a/NEG/UI.meta b/NEG/UI.meta new file mode 100644 index 0000000..3557a60 --- /dev/null +++ b/NEG/UI.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: bbdebf147b9a40b0bc94b6f710c7aa6b +timeCreated: 1670690396 \ No newline at end of file diff --git a/NEG/UI/Area.meta b/NEG/UI/Area.meta new file mode 100644 index 0000000..605a7f9 --- /dev/null +++ b/NEG/UI/Area.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 853f6a7d0f224278afbb5457d0fb8bde +timeCreated: 1670706939 \ No newline at end of file diff --git a/NEG/UI/Area/IArea.cs b/NEG/UI/Area/IArea.cs new file mode 100644 index 0000000..cc04b03 --- /dev/null +++ b/NEG/UI/Area/IArea.cs @@ -0,0 +1,32 @@ +using System.Collections.Generic; +using NEG.UI.Popup; +using NEG.UI.Window; +using NEG.UI.WindowSlot; + +namespace NEG.UI.Area +{ + public interface IArea : IUiElement + { + IEnumerable AvailableSlots { get; } + IEnumerable CurrentPopups { get; } + + /// + /// Open window + /// + /// + void OpenWindow(IWindow window); + /// + /// Open popup + /// + /// + void OpenPopup(IPopup popup); + + void CloseAllWindows() + { + foreach (var slot in AvailableSlots) + { + slot.CloseAllWindows(); + } + } + } +} \ No newline at end of file diff --git a/NEG/UI/Area/IArea.cs.meta b/NEG/UI/Area/IArea.cs.meta new file mode 100644 index 0000000..5d555b9 --- /dev/null +++ b/NEG/UI/Area/IArea.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: ffa2155176774ab691d499979707a1bb +timeCreated: 1670690282 \ No newline at end of file diff --git a/NEG/UI/Area/MonoArea.cs b/NEG/UI/Area/MonoArea.cs new file mode 100644 index 0000000..9f13ef3 --- /dev/null +++ b/NEG/UI/Area/MonoArea.cs @@ -0,0 +1,58 @@ +using System.Collections.Generic; +using UnityEngine; +using NEG.UI.Popup; +using NEG.UI.Window; +using NEG.UI.WindowSlot; + +namespace NEG.UI.Area +{ + public class MonoArea : MonoBehaviour, IArea + { + public IEnumerable AvailableSlots => windowSlots; + public IWindowSlot DefaultWindowSlot => windowSlots[0]; + public IEnumerable CurrentWindows { get; } + public IEnumerable CurrentPopups => currentPopups; + + [SerializeField] private List windowSlots; + + [SerializeField] private Queue currentPopups = new(); + + public void SetEnabled(bool setEnabled) => gameObject.SetActive(setEnabled); + + public void OpenWindow(IWindow window) => DefaultWindowSlot.AttachWindow(window); + + public void OpenPopup(IPopup popup) + { + currentPopups.Enqueue(popup); + popup.OnPopupClosed += OnPopupClosed; + popup.Show(); + UpdatePopupStates(); + } + + private void UpdatePopupStates() + { + if(currentPopups.Count == 0) + return; + + while (currentPopups.TryPeek(out var popup)) + { + if(popup.IsValid) + popup.SetEnabled(true); + + currentPopups.Dequeue(); + } + } + + private void OnPopupClosed(IPopup popup) + { + if (!currentPopups.Contains(popup)) + return; + + if(currentPopups.Peek() != popup) + return; + + currentPopups.Dequeue(); + UpdatePopupStates(); + } + } +} \ No newline at end of file diff --git a/NEG/UI/Area/MonoArea.cs.meta b/NEG/UI/Area/MonoArea.cs.meta new file mode 100644 index 0000000..805b8e3 --- /dev/null +++ b/NEG/UI/Area/MonoArea.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: bbfe2293348b4c9096a3763479ec00c7 +timeCreated: 1670707479 \ No newline at end of file diff --git a/NEG/UI/IUiElement.cs b/NEG/UI/IUiElement.cs new file mode 100644 index 0000000..f41a14e --- /dev/null +++ b/NEG/UI/IUiElement.cs @@ -0,0 +1,7 @@ +namespace NEG.UI +{ + public interface IUiElement + { + void SetEnabled(bool setEnabled); + } +} \ No newline at end of file diff --git a/NEG/UI/IUiElement.cs.meta b/NEG/UI/IUiElement.cs.meta new file mode 100644 index 0000000..765fb83 --- /dev/null +++ b/NEG/UI/IUiElement.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 296bf6969a6347f8aea788a7bdd086af +timeCreated: 1670693177 \ No newline at end of file diff --git a/NEG/UI/NEG.UI.asmdef b/NEG/UI/NEG.UI.asmdef new file mode 100644 index 0000000..df5cf87 --- /dev/null +++ b/NEG/UI/NEG.UI.asmdef @@ -0,0 +1,14 @@ +{ + "name": "NegUi", + "rootNamespace": "", + "references": [], + "includePlatforms": [], + "excludePlatforms": [], + "allowUnsafeCode": false, + "overrideReferences": false, + "precompiledReferences": [], + "autoReferenced": true, + "defineConstraints": [], + "versionDefines": [], + "noEngineReferences": false +} \ No newline at end of file diff --git a/NEG/UI/NEG.UI.asmdef.meta b/NEG/UI/NEG.UI.asmdef.meta new file mode 100644 index 0000000..b5859a4 --- /dev/null +++ b/NEG/UI/NEG.UI.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 7361f1d9c43da6649923760766194746 +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/NEG/UI/Popup.meta b/NEG/UI/Popup.meta new file mode 100644 index 0000000..0b36d76 --- /dev/null +++ b/NEG/UI/Popup.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 76fc21263637443ca8268859d4cb5378 +timeCreated: 1670707809 \ No newline at end of file diff --git a/NEG/UI/Popup/IPopup.cs b/NEG/UI/Popup/IPopup.cs new file mode 100644 index 0000000..25b2025 --- /dev/null +++ b/NEG/UI/Popup/IPopup.cs @@ -0,0 +1,45 @@ +using JetBrains.Annotations; +using NEG.UI.Area; +using System; + +namespace NEG.UI.Popup +{ + [PublicAPI] + public interface IPopup : IUiElement + { + /// + /// Call when popup is not forcly closed + /// + event Action OnPopupClosed; + + /// + /// Is popup still valid to show + /// + bool IsValid { get; } + + /// + /// Mark popup as ready to show. + /// + internal void Show(); + + /// + /// Close popup or mark as closed if not visible + /// + /// Is closing by forced by system, ex. close area + void Close(bool isForced = false); + } + + public static class PopupExtensions + { + public static void Show(this IPopup popup, IArea area = null) + { + if (area != null) + { + area.OpenPopup(popup); + return; + } + + UiManager.Instance.CurrentArea.OpenPopup(popup); + } + } +} \ No newline at end of file diff --git a/NEG/UI/Popup/IPopup.cs.meta b/NEG/UI/Popup/IPopup.cs.meta new file mode 100644 index 0000000..54795d7 --- /dev/null +++ b/NEG/UI/Popup/IPopup.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 79767831e4324605974f3bb0bb5026fb +timeCreated: 1670692499 \ No newline at end of file diff --git a/NEG/UI/Popup/MonoPopup.cs b/NEG/UI/Popup/MonoPopup.cs new file mode 100644 index 0000000..8277bf3 --- /dev/null +++ b/NEG/UI/Popup/MonoPopup.cs @@ -0,0 +1,24 @@ +using NEG.UI.Area; +using System; +using UnityEngine; + +namespace NEG.UI.Popup +{ + public class MonoPopup : MonoBehaviour, IPopup + { + public event Action OnPopupClosed; + public bool IsValid { get; private set; } + + public void Close(bool isForced = false) + { + IsValid = false; + gameObject.SetActive(false); + if(!isForced) + OnPopupClosed?.Invoke(this); + } + + public void SetEnabled(bool setEnabled) => gameObject.SetActive(setEnabled); + + void IPopup.Show() => IsValid = true; + } +} \ No newline at end of file diff --git a/NEG/UI/Popup/MonoPopup.cs.meta b/NEG/UI/Popup/MonoPopup.cs.meta new file mode 100644 index 0000000..292cb60 --- /dev/null +++ b/NEG/UI/Popup/MonoPopup.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: d3057d16be014bd58847b118a24c21dd +timeCreated: 1670707831 \ No newline at end of file diff --git a/NEG/UI/UiManager.cs b/NEG/UI/UiManager.cs new file mode 100644 index 0000000..435fd28 --- /dev/null +++ b/NEG/UI/UiManager.cs @@ -0,0 +1,50 @@ +using NEG.UI.Area; +using System.Collections.Generic; +using UnityEngine; + +namespace NEG.UI +{ + public class UiManager + { + //TODO: use default unity selection + //TODO: window snaping to slots + + public static UiManager Instance { get; private set; } + + public IArea CurrentArea + { + get => currentArea; + set + { + if (value == null) + { + Debug.LogError("CurrentArea can't be null"); + return; + } + + currentArea?.SetEnabled(false); + + currentArea = value; + + currentArea.SetEnabled(true); + } + } + + private IArea currentArea; + + public UiManager(IArea startArea) + { + if (Instance != null) + { + Debug.LogError("Only one instance od UiManager is allowed"); + return; + } + + Instance = this; + CurrentArea = startArea; + } + + } +} + + diff --git a/NEG/UI/UiManager.cs.meta b/NEG/UI/UiManager.cs.meta new file mode 100644 index 0000000..b87a94a --- /dev/null +++ b/NEG/UI/UiManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c322beedd2ec8f844903c18b1ef74b15 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/NEG/UI/UnityUi.meta b/NEG/UI/UnityUi.meta new file mode 100644 index 0000000..beaa12a --- /dev/null +++ b/NEG/UI/UnityUi.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: a0be272f427b40848b8aeff5b04770b4 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/NEG/UI/UnityUi/Buttons.meta b/NEG/UI/UnityUi/Buttons.meta new file mode 100644 index 0000000..edba027 --- /dev/null +++ b/NEG/UI/UnityUi/Buttons.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: dcaa262d2bcd4b19aa25512a19830555 +timeCreated: 1670777190 \ No newline at end of file diff --git a/NEG/UI/UnityUi/Buttons/BaseButton.cs b/NEG/UI/UnityUi/Buttons/BaseButton.cs new file mode 100644 index 0000000..13c2361 --- /dev/null +++ b/NEG/UI/UnityUi/Buttons/BaseButton.cs @@ -0,0 +1,59 @@ +using FMOD.Studio; +using FMODUnity; +using System; +using UnityEngine; +using UnityEngine.EventSystems; +using UnityEngine.UI; + +namespace NEG.UI.UnityUi.Buttons +{ + [RequireComponent(typeof(ButtonSerializeFields))] + public class BaseButton : MonoBehaviour, ISelectHandler, IDeselectHandler, IPointerEnterHandler, IPointerExitHandler + { + public event Action OnButtonPressed; + + protected ButtonSerializeFields serializeFields; + + private bool isHovered; + + public virtual void OnSelect(BaseEventData eventData) + { + if (serializeFields.Text) + serializeFields.Text.color = serializeFields.SelectedTextColor; + } + + public void OnDeselect(BaseEventData eventData) + { + if (serializeFields.Text) + serializeFields.Text.color = serializeFields.DeselectedTextColor; + } + + public void OnPointerEnter(PointerEventData eventData) + { + isHovered = true; + if (serializeFields.Text) + serializeFields.Text.color = serializeFields.SelectedTextColor; + } + + public void OnPointerExit(PointerEventData eventData) + { + isHovered = false; + if (serializeFields.Text) + serializeFields.Text.color = serializeFields.DeselectedTextColor; + } + + protected virtual void Awake() + { + serializeFields = GetComponent(); + serializeFields.Button.onClick.AddListener(OnClicked); + OnDeselect(null); + } + + protected virtual void OnClicked() + { + OnDeselect(null); + isHovered = false; + OnButtonPressed?.Invoke(); + } + } +} diff --git a/NEG/UI/UnityUi/Buttons/BaseButton.cs.meta b/NEG/UI/UnityUi/Buttons/BaseButton.cs.meta new file mode 100644 index 0000000..ea42d55 --- /dev/null +++ b/NEG/UI/UnityUi/Buttons/BaseButton.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 3a250ef2d0c34e7396a16fc5eddbdb01 +timeCreated: 1670777213 \ No newline at end of file diff --git a/NEG/UI/UnityUi/Buttons/ButtonSerializeFields.cs b/NEG/UI/UnityUi/Buttons/ButtonSerializeFields.cs new file mode 100644 index 0000000..60e93a9 --- /dev/null +++ b/NEG/UI/UnityUi/Buttons/ButtonSerializeFields.cs @@ -0,0 +1,33 @@ +using FMODUnity; +using System; +using TMPro; +using UnityEngine; +using UnityEngine.UI; + +namespace NEG.UI.UnityUi.Buttons +{ + public class ButtonSerializeFields : MonoBehaviour + { + [field: SerializeField] + public Button Button { get; private set; } + [field: SerializeField] + public TMP_Text Text { get; private set; } + [field: SerializeField] + public Color SelectedTextColor { get; private set; } + [field: SerializeField] + public Color DeselectedTextColor { get; private set; } + [field: SerializeField] + public EventReference HoverEventRef { get; private set; } + [field: SerializeField] + public EventReference ClickEventRef { get; private set; } + + private void OnValidate() + { + if(Button == null) + Button = GetComponent