diff --git a/KeyBasedFactory.cs b/KeyBasedFactory.cs new file mode 100644 index 0000000..c4d4d5f --- /dev/null +++ b/KeyBasedFactory.cs @@ -0,0 +1,54 @@ +using JetBrains.Annotations; +using System; +using System.Collections.Generic; +using System.Reflection; + +namespace NEG.Utils +{ + public class KeyBasedFactory + { + [PublicAPI] + protected Dictionary data; + + public KeyBasedFactory() + { + data = new Dictionary(); + } + + public void FireRegistration() + { + ScanAssembly(typeof(T2).Assembly); + + if(typeof(T2).Assembly.GetType().Assembly == typeof(T2).Assembly) + return; + + ScanAssembly(typeof(T2).Assembly.GetType().Assembly); + } + + private static void ScanAssembly(Assembly assembly) + { + foreach (var type in assembly.GetTypes()) + { + var methodFields = + type.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); + for (int i = 0; i < methodFields.Length; i++) + { + if (Attribute.GetCustomAttribute(methodFields[i], typeof(FactoryRegistration)) != null) + { + methodFields[i].Invoke(null, Array.Empty()); + } + } + } + } + + public void Register(T1 key, Type type) => data.Add(key, type); + + public T2 CreateInstance(T1 key, params object[] args) => (T2)Activator.CreateInstance(data[key], args); + } + + [AttributeUsage(AttributeTargets.Method)] + public class FactoryRegistration : Attribute + { + public FactoryRegistration() { } + } +} \ No newline at end of file diff --git a/KeyBasedFactory.cs.meta b/KeyBasedFactory.cs.meta new file mode 100644 index 0000000..1c03d8f --- /dev/null +++ b/KeyBasedFactory.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 2e0d3df1ddd34209bfb7262b4e51abfe +timeCreated: 1683917310 \ No newline at end of file diff --git a/NEG/UI/NEG.UI.asmdef b/NEG/UI/NEG.UI.asmdef index db95a9f..62f697c 100644 --- a/NEG/UI/NEG.UI.asmdef +++ b/NEG/UI/NEG.UI.asmdef @@ -1,7 +1,7 @@ { "name": "NEG.UI", "rootNamespace": "", - "references": [], + "references": ["GUID:3c4294719a93e3c4e831a9ff0c261e8a"], "includePlatforms": [], "excludePlatforms": [], "allowUnsafeCode": false, diff --git a/NEG/UI/UiManager.cs b/NEG/UI/UiManager.cs index 028a212..f145012 100644 --- a/NEG/UI/UiManager.cs +++ b/NEG/UI/UiManager.cs @@ -2,6 +2,7 @@ using JetBrains.Annotations; using NEG.UI.Area; using NEG.UI.Popup; using NEG.UI.Window; +using NEG.Utils; using System; using System.Collections.Generic; using UnityEngine; diff --git a/NEG/UI/UnityUi/Buttons/BaseButton.cs b/NEG/UI/UnityUi/Buttons/BaseButton.cs index 35dfbe9..2d5234f 100644 --- a/NEG/UI/UnityUi/Buttons/BaseButton.cs +++ b/NEG/UI/UnityUi/Buttons/BaseButton.cs @@ -1,72 +1,92 @@ -using FMOD.Studio; -using FMODUnity; +using KBCore.Refs; +using NEG.UI.UnityUi.Buttons.Reaction; +using NEG.UI.UnityUi.Buttons.Settings; using System; +using System.Collections.Generic; +using TMPro; using UnityEngine; using UnityEngine.EventSystems; +using UnityEngine.Serialization; using UnityEngine.UI; namespace NEG.UI.UnityUi.Buttons { - [RequireComponent(typeof(ButtonSerializeFields))] + [DefaultExecutionOrder(-1)] + [RequireComponent(typeof(Button))] public class BaseButton : MonoBehaviour, ISelectHandler, IDeselectHandler, IPointerEnterHandler, IPointerExitHandler { + public delegate void SelectionHandler(bool isSilent); + /// + /// is silent + /// + public event SelectionHandler OnSelected; + public event SelectionHandler OnDeselected; public event Action OnButtonPressed; - - public bool Interactable { get => serializeFields.Button.interactable; set => serializeFields.Button.interactable = value; } - - [SerializeField] - protected ButtonSerializeFields serializeFields; + + public bool Interactable { get => button.interactable; set => button.interactable = value; } + + public TMP_Text Text => text; private bool isHovered; - public virtual void OnSelect(BaseEventData eventData) - { - if (serializeFields.Text) - serializeFields.Text.color = serializeFields.SelectedTextColor; - } + [SerializeField, Self] private Button button; + [SerializeField, Child(Flag.Optional)] private TMP_Text text; + [SerializeField, Child(Flag.Optional)] private Image icon; - public void OnDeselect(BaseEventData eventData) - { - if (serializeFields.Text) - serializeFields.Text.color = serializeFields.DeselectedTextColor; - } + [SerializeField] private ButtonSettings groupButtonSettings; + //[SerializeField, Self(Flag.Optional)] private ButtonSettingOverride overrideSettings; - public void OnPointerEnter(PointerEventData eventData) - { - isHovered = true; - if (serializeFields.Text) - serializeFields.Text.color = serializeFields.SelectedTextColor; - } + private Dictionary behaviours = new Dictionary(); + + public virtual void OnSelect(BaseEventData eventData) => OnSelected?.Invoke(eventData is SilentEventData); + + public void OnDeselect(BaseEventData eventData) => OnDeselected?.Invoke(eventData is SilentEventData); + + public void OnPointerEnter(PointerEventData eventData) => EventSystem.current.SetSelectedGameObject(gameObject); public void OnPointerExit(PointerEventData eventData) { - isHovered = false; - if (serializeFields.Text) - serializeFields.Text.color = serializeFields.DeselectedTextColor; + if(EventSystem.current.currentSelectedGameObject == gameObject) + EventSystem.current.SetSelectedGameObject(null); } - public void SetText(string text) + public void SetText(string txt) { - if(serializeFields == null) + if(text == null) return; - if(serializeFields.Text == null) + text.text = txt; + } + + public void AddOrOverrideSetting(SettingData data) + { + if (behaviours.TryGetValue(data.Key, out var setting)) + { + setting.ChangeData(data); return; - serializeFields.Text.text = text; + } + behaviours.Add("ChangeTextColor", MonoUiManager.Instance.BehavioursFactory.CreateInstance(data.Key, this, data)); + } + + public void RemoveSetting(string key) + { + if (!behaviours.TryGetValue(key, out var setting)) + { + Debug.LogError($"Behaviour with key {key} was not found"); + return; + } + setting.Dispose(); + behaviours.Remove(key); } protected virtual void Awake() { - if(serializeFields == null) - serializeFields = GetComponent(); - serializeFields.Button.onClick.AddListener(OnClicked); - OnDeselect(null); + button.onClick.AddListener(OnClicked); + groupButtonSettings.Apply(this); } - private void OnValidate() - { - if(serializeFields == null) - serializeFields = GetComponent(); - } + private void Start() => OnDeselect(null); + + private void OnValidate() => this.ValidateRefs(); protected virtual void OnClicked() { diff --git a/NEG/UI/UnityUi/Buttons/BaseButton.cs.meta b/NEG/UI/UnityUi/Buttons/BaseButton.cs.meta index ea42d55..5a38446 100644 --- a/NEG/UI/UnityUi/Buttons/BaseButton.cs.meta +++ b/NEG/UI/UnityUi/Buttons/BaseButton.cs.meta @@ -1,3 +1,11 @@ fileFormatVersion: 2 guid: 3a250ef2d0c34e7396a16fc5eddbdb01 -timeCreated: 1670777213 \ No newline at end of file +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: -1 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/NEG/UI/UnityUi/Buttons/ButtonSerializeFields.cs b/NEG/UI/UnityUi/Buttons/ButtonSerializeFields.cs deleted file mode 100644 index 60e93a9..0000000 --- a/NEG/UI/UnityUi/Buttons/ButtonSerializeFields.cs +++ /dev/null @@ -1,33 +0,0 @@ -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