using MalbersAnimations.Scriptables; using System.Collections.Generic; using UnityEngine; #if UNITY_EDITOR using UnityEditor; #endif namespace MalbersAnimations.Events { /// /// The list of listeners that this event will notify if it is Invoked. /// Based on the Talk - Game Architecture with Scriptable Objects by Ryan Hipple /// [CreateAssetMenu(menuName = "Malbers Animations/Event", fileName = "New Event Asset", order = 3000)] public class MEvent : ScriptableObject { /// The list of listeners that this event will notify if it is raised. private readonly List eventListeners = new List(); #if UNITY_EDITOR [TextArea(3, 10)] public string Description; #endif public bool debug; public virtual void Invoke() { for (int i = eventListeners.Count - 1; i >= 0; i--) eventListeners[i].OnEventInvoked(); #if UNITY_EDITOR if (debug) Debug.Log($"{name} - Invoke()", this); #endif } public virtual void Invoke(float value) { for (int i = eventListeners.Count - 1; i >= 0; i--) eventListeners[i].OnEventInvoked(value); DebugEvent(value); } public virtual void Invoke(FloatVar value) { float val = value; for (int i = eventListeners.Count - 1; i >= 0; i--) eventListeners[i].OnEventInvoked(val); DebugEvent(value); } public virtual void Invoke(bool value) { DebugEvent(value); for (int i = eventListeners.Count - 1; i >= 0; i--) eventListeners[i].OnEventInvoked(value); } public virtual void Invoke(BoolVar value) { DebugEvent(value.Value); for (int i = eventListeners.Count - 1; i >= 0; i--) eventListeners[i].OnEventInvoked(value.Value); } public virtual void Invoke(string value) { DebugEvent(value); for (int i = eventListeners.Count - 1; i >= 0; i--) eventListeners[i].OnEventInvoked(value); } public virtual void Invoke(StringVar value) { DebugEvent(value.Value); for (int i = eventListeners.Count - 1; i >= 0; i--) eventListeners[i].OnEventInvoked(value.Value); } public virtual void Invoke(int value) { DebugEvent(value); for (int i = eventListeners.Count - 1; i >= 0; i--) eventListeners[i].OnEventInvoked(value); } public virtual void Invoke(IntVar value) { DebugEvent(value.Value); for (int i = eventListeners.Count - 1; i >= 0; i--) eventListeners[i].OnEventInvoked(value.Value); } public virtual void Invoke(IDs value) { #if UNITY_EDITOR if (debug) Debug.Log($"{name} Invoke({value.name} - {value.ID})"); #endif for (int i = eventListeners.Count - 1; i >= 0; i--) eventListeners[i].OnEventInvoked(value.ID); } public virtual void Invoke(GameObject value) { DebugEvent(value); for (int i = eventListeners.Count - 1; i >= 0; i--) eventListeners[i].OnEventInvoked(value); } public virtual void Invoke(Transform value) { DebugEvent(value); for (int i = eventListeners.Count - 1; i >= 0; i--) eventListeners[i].OnEventInvoked(value); } public virtual void Invoke(Vector3 value) { DebugEvent(value); for (int i = eventListeners.Count - 1; i >= 0; i--) eventListeners[i].OnEventInvoked(value); } public virtual void Invoke(Vector2 value) { DebugEvent(value); for (int i = eventListeners.Count - 1; i >= 0; i--) eventListeners[i].OnEventInvoked(value); } public virtual void Invoke(Component value) { DebugEvent(value); for (int i = eventListeners.Count - 1; i >= 0; i--) eventListeners[i].OnEventInvoked(value); } public virtual void Invoke(Sprite value) { DebugEvent(value); for (int i = eventListeners.Count - 1; i >= 0; i--) eventListeners[i].OnEventInvoked(value); } public virtual void RegisterListener(MEventItemListener listener) { if (!eventListeners.Contains(listener)) eventListeners.Add(listener); } public virtual void UnregisterListener(MEventItemListener listener) { if (eventListeners.Contains(listener)) eventListeners.Remove(listener); } public virtual void InvokeAsGameObject(Transform value) => Invoke(value.gameObject); public virtual void InvokeAsGameObject(Component value) => Invoke(value.gameObject); public virtual void InvokeAsTransform(GameObject value) => Invoke(value.transform); public virtual void InvokeAsTransform(Component value) => Invoke(value.transform); private void DebugEvent(object value) { #if UNITY_EDITOR if (debug) Debug.Log($"{name} - Invoke({value})",this); #endif } ////This is for Debugin porpuses #region Debuging Methods public virtual void LogDeb(string text) => Debug.Log(text); public virtual void Pause() => Debug.Break(); public virtual void LogDeb(bool value) => Debug.Log(name + ": " + value); public virtual void LogDeb(Vector3 value) => Debug.Log(name + ": " + value); public virtual void LogDeb(int value) => Debug.Log(name + ": " + value); public virtual void LogDeb(float value) => Debug.Log(name + ": " + value); public virtual void LogDeb(object value) => Debug.Log(name + ": " + value); public virtual void LogDeb(Component value) => Debug.Log(name + ": " + value); #endregion #if UNITY_EDITOR [HideInInspector] public IntReference m_int; [HideInInspector] public FloatReference m_float; [HideInInspector] public StringReference m_string; [HideInInspector] public BoolReference m_bool; [HideInInspector] public Vector2Reference m_V2; [HideInInspector] public Vector3Reference m_V3; [HideInInspector] public GameObjectReference m_go; [HideInInspector] public TransformReference m_transform; [HideInInspector] public Sprite m_Sprite; #endif } #if UNITY_EDITOR [CustomEditor(typeof(MEvent))] public class MEventEditor : Editor { public static GUIStyle Style_ => MTools.StyleGreen; SerializedProperty Description, debug, m_int, m_float, m_string, m_bool, m_V2, m_V3, m_go, m_transform, m_Sprite; MEvent ev; private void OnEnable() { ev = (MEvent)target; Description = serializedObject.FindProperty("Description"); debug = serializedObject.FindProperty("debug"); m_int = serializedObject.FindProperty("m_int"); m_float = serializedObject.FindProperty("m_float"); m_string = serializedObject.FindProperty("m_string"); m_bool = serializedObject.FindProperty("m_bool"); m_V2 = serializedObject.FindProperty("m_V2"); m_V3 = serializedObject.FindProperty("m_V3"); m_go = serializedObject.FindProperty("m_go"); m_transform = serializedObject.FindProperty("m_transform"); m_Sprite = serializedObject.FindProperty("m_Sprite"); } GUIStyle style; public override void OnInspectorGUI() { serializedObject.Update(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Description", EditorStyles.boldLabel); MalbersEditor.DrawDebugIcon(debug); //debug.boolValue = GUILayout.Toggle(debug.boolValue, "Debug", EditorStyles.miniButton); EditorGUILayout.EndHorizontal(); if (style == null) style = new GUIStyle(Style_) { fontSize = 12, fontStyle = FontStyle.Bold, alignment = TextAnchor.MiddleLeft, stretchWidth = true }; style.normal.textColor = EditorStyles.label.normal.textColor; Description.stringValue = UnityEditor.EditorGUILayout.TextArea(Description.stringValue, style); //EditorGUILayout.PropertyField(Description, GUIContent.none); if (debug.boolValue && Application.isPlaying) { EditorGUILayout.BeginVertical(EditorStyles.helpBox); var w = 60; if (GUILayout.Button("Invoke Void")) { ev.Invoke(); } EditorGUILayout.BeginHorizontal(); EditorGUILayout.PropertyField(m_bool); if (GUILayout.Button("Invoke", GUILayout.Width(w))) { ev.Invoke(ev.m_bool); } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.PropertyField(m_int); if (GUILayout.Button("Invoke", GUILayout.Width(w))) { ev.Invoke(ev.m_int); } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.PropertyField(m_float); if (GUILayout.Button("Invoke", GUILayout.Width(w))) { ev.Invoke(ev.m_float); } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.PropertyField(m_string); if (GUILayout.Button("Invoke", GUILayout.Width(w))) { ev.Invoke(ev.m_string); } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.PropertyField(m_V2); if (GUILayout.Button("Invoke", GUILayout.Width(w))) { ev.Invoke(ev.m_V2.Value); } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.PropertyField(m_V3); if (GUILayout.Button("Invoke", GUILayout.Width(w))) { ev.Invoke(ev.m_V3.Value); } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.PropertyField(m_go); if (GUILayout.Button("Invoke", GUILayout.Width(w))) { ev.Invoke(ev.m_go.Value); } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.PropertyField(m_transform); if (GUILayout.Button("Invoke", GUILayout.Width(w))) { ev.Invoke(ev.m_transform.Value); } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.PropertyField(m_Sprite); if (GUILayout.Button("Invoke", GUILayout.Width(w))) { ev.Invoke(ev.m_Sprite); } EditorGUILayout.EndHorizontal(); EditorGUILayout.EndVertical(); } serializedObject.ApplyModifiedProperties(); } } #endif }