diff --git a/NEG/UI/UnityUi/Area/AutoWindowOpen.cs b/NEG/UI/UnityUi/Area/AutoWindowOpen.cs index b19f017..48e1f64 100644 --- a/NEG/UI/UnityUi/Area/AutoWindowOpen.cs +++ b/NEG/UI/UnityUi/Area/AutoWindowOpen.cs @@ -1,6 +1,7 @@ using NEG.UI.UnityUi.Window; using NEG.UI.Window; using System; +using KBCore.Refs; using UnityEngine; namespace NEG.UI.Area diff --git a/NEG/UI/UnityUi/Buttons/BaseButton.cs b/NEG/UI/UnityUi/Buttons/BaseButton.cs index 4d495de..03b8f9d 100644 --- a/NEG/UI/UnityUi/Buttons/BaseButton.cs +++ b/NEG/UI/UnityUi/Buttons/BaseButton.cs @@ -27,7 +27,7 @@ namespace NEG.UI.UnityUi.Buttons public TMP_Text Text => text; - [SerializeField, Self] private Button button; + [SerializeField, Self(Flag.Optional)] private Button button; [SerializeField, Child(Flag.Optional)] private TMP_Text text; [SerializeField, Child(Flag.Optional)] private Image icon; @@ -62,7 +62,7 @@ namespace NEG.UI.UnityUi.Buttons setting.ChangeData(data); return; } - behaviours.Add("ChangeTextColor", MonoUiManager.Instance.BehavioursFactory.CreateInstance(data.Key, this, data)); + behaviours.Add(data.Key, MonoUiManager.Instance.BehavioursFactory.CreateInstance(data.Key, this, data)); } public void RemoveSetting(string key) @@ -79,7 +79,10 @@ namespace NEG.UI.UnityUi.Buttons protected virtual void Awake() { button.onClick.AddListener(OnClicked); - groupButtonSettings.Apply(this); + if (groupButtonSettings == null) + MonoUiManager.Instance.DefaultUiSettings.Apply(this); + else + groupButtonSettings.Apply(this); } private void Start() => OnDeselect(null); diff --git a/NEG/UI/UnityUi/Buttons/ButtonReaction.cs b/NEG/UI/UnityUi/Buttons/ButtonReaction.cs index 1f30b1d..c2c2590 100644 --- a/NEG/UI/UnityUi/Buttons/ButtonReaction.cs +++ b/NEG/UI/UnityUi/Buttons/ButtonReaction.cs @@ -1,4 +1,5 @@ using System; +using KBCore.Refs; using UnityEngine; namespace NEG.UI.UnityUi.Buttons @@ -6,9 +7,13 @@ namespace NEG.UI.UnityUi.Buttons [RequireComponent(typeof(BaseButton))] public abstract class ButtonReaction : MonoBehaviour { - protected virtual void Awake() => GetComponent().OnButtonPressed += OnClicked; + [SerializeField, Self(Flag.Optional)] protected BaseButton button; + + protected virtual void Awake() => button.OnButtonPressed += OnClicked; - protected virtual void OnDestroy() => GetComponent().OnButtonPressed -= OnClicked; + protected virtual void OnDestroy() => button.OnButtonPressed -= OnClicked; + + private void OnValidate() => this.ValidateRefs(); protected abstract void OnClicked(); } diff --git a/NEG/UI/UnityUi/Buttons/CustomNavigationButton.cs b/NEG/UI/UnityUi/Buttons/CustomNavigationButton.cs index d32918e..e7e82eb 100644 --- a/NEG/UI/UnityUi/Buttons/CustomNavigationButton.cs +++ b/NEG/UI/UnityUi/Buttons/CustomNavigationButton.cs @@ -7,7 +7,7 @@ namespace NEG.UI.UnityUi.Buttons { public class CustomNavigationButton : Button { - [SerializeField] private OverridableNavigation upOverride; + [SerializeField] [HideInInspector] private OverridableNavigation upOverride; [SerializeField] private OverridableNavigation downOverride; [SerializeField] private OverridableNavigation leftOverride; [SerializeField] private OverridableNavigation rightOverride; diff --git a/NEG/UI/UnityUi/Buttons/Reaction/ChangeTextColorBehaviour.cs b/NEG/UI/UnityUi/Buttons/Reaction/ChangeTextColorBehaviour.cs index bd18235..f679698 100644 --- a/NEG/UI/UnityUi/Buttons/Reaction/ChangeTextColorBehaviour.cs +++ b/NEG/UI/UnityUi/Buttons/Reaction/ChangeTextColorBehaviour.cs @@ -12,10 +12,7 @@ namespace NEG.UI.UnityUi.Buttons.Reaction public ChangeTextColorBehaviour(BaseButton baseButton, ColorData data) : base(baseButton, data) { if (baseButton.Text == null) - { - Debug.LogError("Button without text was provided"); return; - } baseButton.OnSelected += OnButtonSelected; baseButton.OnDeselected += OnButtonDeselected; diff --git a/NEG/UI/UnityUi/Editor/CustomNavigationButtonEditor.cs b/NEG/UI/UnityUi/Editor/CustomNavigationButtonEditor.cs index 0557b31..2e92e00 100644 --- a/NEG/UI/UnityUi/Editor/CustomNavigationButtonEditor.cs +++ b/NEG/UI/UnityUi/Editor/CustomNavigationButtonEditor.cs @@ -25,7 +25,6 @@ namespace NEG.UI.UnityUi.Editor { base.OnInspectorGUI(); EditorGUILayout.Space(); - serializedObject.Update(); EditorGUILayout.PropertyField(upOverrideProperty); EditorGUILayout.PropertyField(downOverrideProperty); diff --git a/NEG/UI/UnityUi/Editor/OverridableNavigationDrawer.cs b/NEG/UI/UnityUi/Editor/OverridableNavigationDrawer.cs index b667f49..aa1b212 100644 --- a/NEG/UI/UnityUi/Editor/OverridableNavigationDrawer.cs +++ b/NEG/UI/UnityUi/Editor/OverridableNavigationDrawer.cs @@ -2,6 +2,7 @@ using NEG.Utils.Serialization; using UnityEditor; using UnityEditor.UIElements; +using UnityEngine; using UnityEngine.UI; using UnityEngine.UIElements; using ObjectField = UnityEditor.Search.ObjectField; @@ -13,6 +14,31 @@ namespace NEG.UI.UnityUi.Editor [CustomPropertyDrawer(typeof(OverridableNavigation))] public class OverridableNavigationDrawer: PropertyDrawer { + public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) + { + EditorGUI.BeginProperty(position, label, property); + + // Draw label + position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label); + + // Don't make child fields be indented + int indent = EditorGUI.indentLevel; + EditorGUI.indentLevel = 0; + + // Calculate rects + var amountRect = new Rect(position.x, position.y, 200, position.height); + var unitRect = new Rect(position.x + 35, position.y, 200, position.height); + + // Draw fields - pass GUIContent.none to each so they are drawn without labels + EditorGUI.PropertyField(amountRect, property.FindAutoPropertyRelative(nameof(OverridableNavigation.Override)), GUIContent.none); + EditorGUI.PropertyField(unitRect, property.FindAutoPropertyRelative(nameof(OverridableNavigation.Selectable)), GUIContent.none); + + // Set indent back to what it was + EditorGUI.indentLevel = indent; + + EditorGUI.EndProperty(); + } + public override VisualElement CreatePropertyGUI(SerializedProperty property) { var container = new VisualElement() diff --git a/NEG/UI/UnityUi/MonoUiManager.cs b/NEG/UI/UnityUi/MonoUiManager.cs index 041325c..4fd003d 100644 --- a/NEG/UI/UnityUi/MonoUiManager.cs +++ b/NEG/UI/UnityUi/MonoUiManager.cs @@ -24,7 +24,8 @@ namespace NEG.UI.UnityUi //TODO: use default unity selection //TODO: window snaping to slots public static new MonoUiManager Instance { get; private set; } - + + public ButtonSettings DefaultUiSettings { get; } public KeyBasedFactory BehavioursFactory { get; private set; } //TODO: editor to auto add slots, buttons @@ -34,7 +35,7 @@ namespace NEG.UI.UnityUi private UiInputModule inputModule; - public MonoUiManager(IArea startArea, Type inputModuleType) : base(startArea) + public MonoUiManager(IArea startArea, Type inputModuleType, ButtonSettings defaultUiSettings) : base(startArea) { Instance = this; @@ -58,6 +59,7 @@ namespace NEG.UI.UnityUi BehavioursFactory.FireRegistration(); inputModule = (UiInputModule)Activator.CreateInstance(inputModuleType); + DefaultUiSettings = defaultUiSettings; } public override void Dispose() diff --git a/NEG/UI/UnityUi/Popup/MonoPopup.cs b/NEG/UI/UnityUi/Popup/MonoPopup.cs index eb51396..2a7a2a3 100644 --- a/NEG/UI/UnityUi/Popup/MonoPopup.cs +++ b/NEG/UI/UnityUi/Popup/MonoPopup.cs @@ -10,13 +10,13 @@ namespace NEG.UI.UnityUi.Popup protected PopupData data; - public void Show(PopupData data) + public virtual void Show(PopupData data) { this.data = data; gameObject.SetActive(true); } - public void Close(bool silent = false) + public virtual void Close(bool silent = false) { gameObject.SetActive(false); diff --git a/NEG/UI/UnityUi/Window/MonoWindow.cs b/NEG/UI/UnityUi/Window/MonoWindow.cs index 147239a..d905ac4 100644 --- a/NEG/UI/UnityUi/Window/MonoWindow.cs +++ b/NEG/UI/UnityUi/Window/MonoWindow.cs @@ -48,14 +48,18 @@ namespace NEG.UI.UnityUi.Window controller.OnClosed(); } - private void Awake() => ((IWindow)this).SetClosedState(); + public void SetHiddenState() => gameObject.SetActive(false); + + public void SeVisibleState() => gameObject.SetActive(true); + + private void Awake() => ((IWindow)this).SetHiddenState(); private void OnValidate() { if (controller == null) controller = GetComponent(); if(defaultSelectedItem == null) - Debug.LogWarning("Window should have default selected item set"); + Debug.LogWarning($"Window {name} should have default selected item set"); } public void OpenWindow(IWindow window, object data = null) @@ -63,5 +67,7 @@ namespace NEG.UI.UnityUi.Window DefaultWindowSlot.AttachWindow(window); window.SetData(data); } + + public void SetDefaultSelectedItem(GameObject item) => defaultSelectedItem = item; } } \ No newline at end of file diff --git a/NEG/UI/UnityUi/WindowSlot/MonoWindowSlot.cs b/NEG/UI/UnityUi/WindowSlot/MonoWindowSlot.cs index 9c1a910..49c94da 100644 --- a/NEG/UI/UnityUi/WindowSlot/MonoWindowSlot.cs +++ b/NEG/UI/UnityUi/WindowSlot/MonoWindowSlot.cs @@ -1,4 +1,5 @@ -using NEG.UI.Area; +using KBCore.Refs; +using NEG.UI.Area; using NEG.UI.Window; using NEG.UI.WindowSlot; using System; diff --git a/NEG/UI/UnityUi/WindowSlot/SingleWindowSlotWithHistory.cs b/NEG/UI/UnityUi/WindowSlot/SingleWindowSlotWithHistory.cs new file mode 100644 index 0000000..facab85 --- /dev/null +++ b/NEG/UI/UnityUi/WindowSlot/SingleWindowSlotWithHistory.cs @@ -0,0 +1,54 @@ +using NEG.UI.UnityUi.WindowSlot; +using NEG.UI.Window; +using System.Collections.Generic; +using System.Linq; + +namespace NegUtils.NEG.UI.UnityUi.WindowSlot +{ + public class SingleWindowSlotWithHistory : MonoWindowSlot + { + public IWindow CurrentWindow + { + get => currentWindow; + set + { + if (value == null) + { + CloseAllWindows(); + return; + } + + currentWindow?.SetHiddenState(); + currentWindow = value; + windowsHistory.Add(currentWindow); + currentWindow?.SetOpenedState(this); + } + } + + private IWindow currentWindow; + + private readonly List windowsHistory = new List(); + + public override void AttachWindow(IWindow window) => CurrentWindow = window; + + public override void DetachWindow(IWindow window) + { + if(window == null) + return; + window.SetClosedState(); + windowsHistory.Remove(window); + if (window != currentWindow || windowsHistory.Count == 0) return; + windowsHistory[^1].SeVisibleState(); + currentWindow = windowsHistory[^1]; + } + public override void CloseAllWindows() + { + currentWindow = null; + foreach (var window in windowsHistory) + { + window.SetClosedState(); + } + windowsHistory.Clear(); + } + } +} \ No newline at end of file diff --git a/NEG/UI/UnityUi/WindowSlot/SingleWindowSlotWithHistory.cs.meta b/NEG/UI/UnityUi/WindowSlot/SingleWindowSlotWithHistory.cs.meta new file mode 100644 index 0000000..9ec293e --- /dev/null +++ b/NEG/UI/UnityUi/WindowSlot/SingleWindowSlotWithHistory.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: d98305dbca084b218b635682f06b934b +timeCreated: 1685995642 \ No newline at end of file diff --git a/NEG/UI/Window/IWindow.cs b/NEG/UI/Window/IWindow.cs index 567bc7f..9495701 100644 --- a/NEG/UI/Window/IWindow.cs +++ b/NEG/UI/Window/IWindow.cs @@ -28,6 +28,16 @@ namespace NEG.UI.Window /// Called internally to close window by slot. /// void SetClosedState(); + + /// + /// Called internally to hide window by slot. + /// + void SetHiddenState(); + + /// + /// Called internally to set window visible by slot. + /// + void SeVisibleState(); } public static class WindowInterfaceExtensions