better-ui-manager #2

Merged
BitterSmile merged 25 commits from better-ui-manager into main 2023-09-02 14:34:16 +02:00
14 changed files with 124 additions and 17 deletions
Showing only changes of commit f837aec845 - Show all commits

View File

@ -1,6 +1,7 @@
using NEG.UI.UnityUi.Window; using NEG.UI.UnityUi.Window;
using NEG.UI.Window; using NEG.UI.Window;
using System; using System;
using KBCore.Refs;
using UnityEngine; using UnityEngine;
namespace NEG.UI.Area namespace NEG.UI.Area

View File

@ -27,7 +27,7 @@ namespace NEG.UI.UnityUi.Buttons
public TMP_Text Text => text; 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 TMP_Text text;
[SerializeField, Child(Flag.Optional)] private Image icon; [SerializeField, Child(Flag.Optional)] private Image icon;
@ -62,7 +62,7 @@ namespace NEG.UI.UnityUi.Buttons
setting.ChangeData(data); setting.ChangeData(data);
return; 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) public void RemoveSetting(string key)
@ -79,7 +79,10 @@ namespace NEG.UI.UnityUi.Buttons
protected virtual void Awake() protected virtual void Awake()
{ {
button.onClick.AddListener(OnClicked); button.onClick.AddListener(OnClicked);
groupButtonSettings.Apply(this); if (groupButtonSettings == null)
MonoUiManager.Instance.DefaultUiSettings.Apply(this);
else
groupButtonSettings.Apply(this);
} }
private void Start() => OnDeselect(null); private void Start() => OnDeselect(null);

View File

@ -1,4 +1,5 @@
using System; using System;
using KBCore.Refs;
using UnityEngine; using UnityEngine;
namespace NEG.UI.UnityUi.Buttons namespace NEG.UI.UnityUi.Buttons
@ -6,9 +7,13 @@ namespace NEG.UI.UnityUi.Buttons
[RequireComponent(typeof(BaseButton))] [RequireComponent(typeof(BaseButton))]
public abstract class ButtonReaction : MonoBehaviour public abstract class ButtonReaction : MonoBehaviour
{ {
protected virtual void Awake() => GetComponent<BaseButton>().OnButtonPressed += OnClicked; [SerializeField, Self(Flag.Optional)] protected BaseButton button;
protected virtual void Awake() => button.OnButtonPressed += OnClicked;
protected virtual void OnDestroy() => GetComponent<BaseButton>().OnButtonPressed -= OnClicked; protected virtual void OnDestroy() => button.OnButtonPressed -= OnClicked;
private void OnValidate() => this.ValidateRefs();
protected abstract void OnClicked(); protected abstract void OnClicked();
} }

View File

@ -7,7 +7,7 @@ namespace NEG.UI.UnityUi.Buttons
{ {
public class CustomNavigationButton : Button public class CustomNavigationButton : Button
{ {
[SerializeField] private OverridableNavigation upOverride; [SerializeField] [HideInInspector] private OverridableNavigation upOverride;
[SerializeField] private OverridableNavigation downOverride; [SerializeField] private OverridableNavigation downOverride;
[SerializeField] private OverridableNavigation leftOverride; [SerializeField] private OverridableNavigation leftOverride;
[SerializeField] private OverridableNavigation rightOverride; [SerializeField] private OverridableNavigation rightOverride;

View File

@ -12,10 +12,7 @@ namespace NEG.UI.UnityUi.Buttons.Reaction
public ChangeTextColorBehaviour(BaseButton baseButton, ColorData data) : base(baseButton, data) public ChangeTextColorBehaviour(BaseButton baseButton, ColorData data) : base(baseButton, data)
{ {
if (baseButton.Text == null) if (baseButton.Text == null)
{
Debug.LogError("Button without text was provided");
return; return;
}
baseButton.OnSelected += OnButtonSelected; baseButton.OnSelected += OnButtonSelected;
baseButton.OnDeselected += OnButtonDeselected; baseButton.OnDeselected += OnButtonDeselected;

View File

@ -25,7 +25,6 @@ namespace NEG.UI.UnityUi.Editor
{ {
base.OnInspectorGUI(); base.OnInspectorGUI();
EditorGUILayout.Space(); EditorGUILayout.Space();
serializedObject.Update(); serializedObject.Update();
EditorGUILayout.PropertyField(upOverrideProperty); EditorGUILayout.PropertyField(upOverrideProperty);
EditorGUILayout.PropertyField(downOverrideProperty); EditorGUILayout.PropertyField(downOverrideProperty);

View File

@ -2,6 +2,7 @@
using NEG.Utils.Serialization; using NEG.Utils.Serialization;
using UnityEditor; using UnityEditor;
using UnityEditor.UIElements; using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UI; using UnityEngine.UI;
using UnityEngine.UIElements; using UnityEngine.UIElements;
using ObjectField = UnityEditor.Search.ObjectField; using ObjectField = UnityEditor.Search.ObjectField;
@ -13,6 +14,31 @@ namespace NEG.UI.UnityUi.Editor
[CustomPropertyDrawer(typeof(OverridableNavigation))] [CustomPropertyDrawer(typeof(OverridableNavigation))]
public class OverridableNavigationDrawer: PropertyDrawer 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) public override VisualElement CreatePropertyGUI(SerializedProperty property)
{ {
var container = new VisualElement() var container = new VisualElement()

View File

@ -24,7 +24,8 @@ namespace NEG.UI.UnityUi
//TODO: use default unity selection //TODO: use default unity selection
//TODO: window snaping to slots //TODO: window snaping to slots
public static new MonoUiManager Instance { get; private set; } public static new MonoUiManager Instance { get; private set; }
public ButtonSettings DefaultUiSettings { get; }
public KeyBasedFactory<string, ButtonElementBehaviour> BehavioursFactory { get; private set; } public KeyBasedFactory<string, ButtonElementBehaviour> BehavioursFactory { get; private set; }
//TODO: editor to auto add slots, buttons //TODO: editor to auto add slots, buttons
@ -34,7 +35,7 @@ namespace NEG.UI.UnityUi
private UiInputModule inputModule; private UiInputModule inputModule;
public MonoUiManager(IArea startArea, Type inputModuleType) : base(startArea) public MonoUiManager(IArea startArea, Type inputModuleType, ButtonSettings defaultUiSettings) : base(startArea)
{ {
Instance = this; Instance = this;
@ -58,6 +59,7 @@ namespace NEG.UI.UnityUi
BehavioursFactory.FireRegistration(); BehavioursFactory.FireRegistration();
inputModule = (UiInputModule)Activator.CreateInstance(inputModuleType); inputModule = (UiInputModule)Activator.CreateInstance(inputModuleType);
DefaultUiSettings = defaultUiSettings;
} }
public override void Dispose() public override void Dispose()

View File

@ -10,13 +10,13 @@ namespace NEG.UI.UnityUi.Popup
protected PopupData data; protected PopupData data;
public void Show(PopupData data) public virtual void Show(PopupData data)
{ {
this.data = data; this.data = data;
gameObject.SetActive(true); gameObject.SetActive(true);
} }
public void Close(bool silent = false) public virtual void Close(bool silent = false)
{ {
gameObject.SetActive(false); gameObject.SetActive(false);

View File

@ -48,14 +48,18 @@ namespace NEG.UI.UnityUi.Window
controller.OnClosed(); 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() private void OnValidate()
{ {
if (controller == null) if (controller == null)
controller = GetComponent<WindowController>(); controller = GetComponent<WindowController>();
if(defaultSelectedItem == null) 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) public void OpenWindow(IWindow window, object data = null)
@ -63,5 +67,7 @@ namespace NEG.UI.UnityUi.Window
DefaultWindowSlot.AttachWindow(window); DefaultWindowSlot.AttachWindow(window);
window.SetData(data); window.SetData(data);
} }
public void SetDefaultSelectedItem(GameObject item) => defaultSelectedItem = item;
} }
} }

View File

@ -1,4 +1,5 @@
using NEG.UI.Area; using KBCore.Refs;
using NEG.UI.Area;
using NEG.UI.Window; using NEG.UI.Window;
using NEG.UI.WindowSlot; using NEG.UI.WindowSlot;
using System; using System;

View File

@ -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<IWindow> windowsHistory = new List<IWindow>();
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();
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d98305dbca084b218b635682f06b934b
timeCreated: 1685995642

View File

@ -28,6 +28,16 @@ namespace NEG.UI.Window
/// Called internally to close window by slot. /// Called internally to close window by slot.
/// </summary> /// </summary>
void SetClosedState(); void SetClosedState();
/// <summary>
/// Called internally to hide window by slot.
/// </summary>
void SetHiddenState();
/// <summary>
/// Called internally to set window visible by slot.
/// </summary>
void SeVisibleState();
} }
public static class WindowInterfaceExtensions public static class WindowInterfaceExtensions