From c55fba8ab2a1c9d3df65eda4a5a1e957f4aa1f78 Mon Sep 17 00:00:00 2001 From: Andrew Lee Date: Sun, 19 Apr 2020 17:19:32 -0400 Subject: Inital commit --- .../Events/Signals/CustomSignalEventDrawer.cs | 5 + .../Events/Signals/CustomSignalEventDrawer.cs.meta | 11 + .../Runtime/Events/Signals/SignalAsset.cs | 22 ++ .../Runtime/Events/Signals/SignalAsset.cs.meta | 11 + .../Runtime/Events/Signals/SignalEmitter.cs | 72 ++++++ .../Runtime/Events/Signals/SignalEmitter.cs.meta | 11 + .../Runtime/Events/Signals/SignalReceiver.cs | 248 +++++++++++++++++++++ .../Runtime/Events/Signals/SignalReceiver.cs.meta | 11 + 8 files changed, 391 insertions(+) create mode 100644 Library/PackageCache/com.unity.timeline@1.2.13/Runtime/Events/Signals/CustomSignalEventDrawer.cs create mode 100644 Library/PackageCache/com.unity.timeline@1.2.13/Runtime/Events/Signals/CustomSignalEventDrawer.cs.meta create mode 100644 Library/PackageCache/com.unity.timeline@1.2.13/Runtime/Events/Signals/SignalAsset.cs create mode 100644 Library/PackageCache/com.unity.timeline@1.2.13/Runtime/Events/Signals/SignalAsset.cs.meta create mode 100644 Library/PackageCache/com.unity.timeline@1.2.13/Runtime/Events/Signals/SignalEmitter.cs create mode 100644 Library/PackageCache/com.unity.timeline@1.2.13/Runtime/Events/Signals/SignalEmitter.cs.meta create mode 100644 Library/PackageCache/com.unity.timeline@1.2.13/Runtime/Events/Signals/SignalReceiver.cs create mode 100644 Library/PackageCache/com.unity.timeline@1.2.13/Runtime/Events/Signals/SignalReceiver.cs.meta (limited to 'Library/PackageCache/com.unity.timeline@1.2.13/Runtime/Events/Signals') diff --git a/Library/PackageCache/com.unity.timeline@1.2.13/Runtime/Events/Signals/CustomSignalEventDrawer.cs b/Library/PackageCache/com.unity.timeline@1.2.13/Runtime/Events/Signals/CustomSignalEventDrawer.cs new file mode 100644 index 0000000..49df674 --- /dev/null +++ b/Library/PackageCache/com.unity.timeline@1.2.13/Runtime/Events/Signals/CustomSignalEventDrawer.cs @@ -0,0 +1,5 @@ +namespace UnityEngine.Timeline +{ + //used to tell Signal Handler inspector to use a special drawer for UnityEvent + class CustomSignalEventDrawer : PropertyAttribute {} +} diff --git a/Library/PackageCache/com.unity.timeline@1.2.13/Runtime/Events/Signals/CustomSignalEventDrawer.cs.meta b/Library/PackageCache/com.unity.timeline@1.2.13/Runtime/Events/Signals/CustomSignalEventDrawer.cs.meta new file mode 100644 index 0000000..c7c813f --- /dev/null +++ b/Library/PackageCache/com.unity.timeline@1.2.13/Runtime/Events/Signals/CustomSignalEventDrawer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a7ebd1239373d5f41af65ef32d67f445 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Library/PackageCache/com.unity.timeline@1.2.13/Runtime/Events/Signals/SignalAsset.cs b/Library/PackageCache/com.unity.timeline@1.2.13/Runtime/Events/Signals/SignalAsset.cs new file mode 100644 index 0000000..d605588 --- /dev/null +++ b/Library/PackageCache/com.unity.timeline@1.2.13/Runtime/Events/Signals/SignalAsset.cs @@ -0,0 +1,22 @@ +using System; +using UnityEngine; + +namespace UnityEngine.Timeline +{ + /// + /// An asset representing an emitted signal. A SignalAsset connects a SignalEmitter with a SignalReceiver. + /// + /// + /// + [AssetFileNameExtension("signal")] + public class SignalAsset : ScriptableObject + { + internal static event Action OnEnableCallback; + + void OnEnable() + { + if (OnEnableCallback != null) + OnEnableCallback(this); + } + } +} diff --git a/Library/PackageCache/com.unity.timeline@1.2.13/Runtime/Events/Signals/SignalAsset.cs.meta b/Library/PackageCache/com.unity.timeline@1.2.13/Runtime/Events/Signals/SignalAsset.cs.meta new file mode 100644 index 0000000..437f4d3 --- /dev/null +++ b/Library/PackageCache/com.unity.timeline@1.2.13/Runtime/Events/Signals/SignalAsset.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: d6fa2d92fc1b3f34da284357edf89c3b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Library/PackageCache/com.unity.timeline@1.2.13/Runtime/Events/Signals/SignalEmitter.cs b/Library/PackageCache/com.unity.timeline@1.2.13/Runtime/Events/Signals/SignalEmitter.cs new file mode 100644 index 0000000..d4d4ca9 --- /dev/null +++ b/Library/PackageCache/com.unity.timeline@1.2.13/Runtime/Events/Signals/SignalEmitter.cs @@ -0,0 +1,72 @@ +using System; +using UnityEngine; +using UnityEngine.Playables; + +namespace UnityEngine.Timeline +{ + /// + /// + /// Marker that emits a signal to a SignalReceiver. + /// + /// A SignalEmitter emits a notification through the playable system. A SignalEmitter is used with a SignalReceiver and a SignalAsset. + /// + /// + [Serializable] + [CustomStyle("SignalEmitter")] + [ExcludeFromPreset] + public class SignalEmitter : Marker, INotification, INotificationOptionProvider + { + [SerializeField] bool m_Retroactive; + [SerializeField] bool m_EmitOnce; + [SerializeField] SignalAsset m_Asset; + + /// + /// Use retroactive to emit the signal if playback starts after the SignalEmitter time. + /// + public bool retroactive + { + get { return m_Retroactive; } + set { m_Retroactive = value; } + } + + /// + /// Use emitOnce to emit this signal once during loops. + /// + public bool emitOnce + { + get { return m_EmitOnce; } + set { m_EmitOnce = value; } + } + + /// + /// Asset representing the signal being emitted. + /// + public SignalAsset asset + { + get { return m_Asset; } + set { m_Asset = value; } + } + + PropertyName INotification.id + { + get + { + if (m_Asset != null) + { + return new PropertyName(m_Asset.name); + } + return new PropertyName(string.Empty); + } + } + + NotificationFlags INotificationOptionProvider.flags + { + get + { + return (retroactive ? NotificationFlags.Retroactive : default(NotificationFlags)) | + (emitOnce ? NotificationFlags.TriggerOnce : default(NotificationFlags)) | + NotificationFlags.TriggerInEditMode; + } + } + } +} diff --git a/Library/PackageCache/com.unity.timeline@1.2.13/Runtime/Events/Signals/SignalEmitter.cs.meta b/Library/PackageCache/com.unity.timeline@1.2.13/Runtime/Events/Signals/SignalEmitter.cs.meta new file mode 100644 index 0000000..f14c8a3 --- /dev/null +++ b/Library/PackageCache/com.unity.timeline@1.2.13/Runtime/Events/Signals/SignalEmitter.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 15c38f6fa1940124db1ab7f6fe7268d1 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Library/PackageCache/com.unity.timeline@1.2.13/Runtime/Events/Signals/SignalReceiver.cs b/Library/PackageCache/com.unity.timeline@1.2.13/Runtime/Events/Signals/SignalReceiver.cs new file mode 100644 index 0000000..4e7564a --- /dev/null +++ b/Library/PackageCache/com.unity.timeline@1.2.13/Runtime/Events/Signals/SignalReceiver.cs @@ -0,0 +1,248 @@ +using System; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.Events; +using UnityEngine.Playables; + +namespace UnityEngine.Timeline +{ + /// + /// Listens for emitted signals and reacts depending on its defined reactions. + /// + /// A SignalReceiver contains a list of reactions. Each reaction is bound to a SignalAsset. + /// When a SignalEmitter emits a signal, the SignalReceiver invokes the corresponding reaction. + /// + /// + public class SignalReceiver : MonoBehaviour, INotificationReceiver + { + [SerializeField] + EventKeyValue m_Events = new EventKeyValue(); + + /// + /// Called when a notification is sent. + /// + public void OnNotify(Playable origin, INotification notification, object context) + { + var signal = notification as SignalEmitter; + if (signal != null && signal.asset != null) + { + UnityEvent evt; + if (m_Events.TryGetValue(signal.asset, out evt) && evt != null) + { + evt.Invoke(); + } + } + } + + /// + /// Defines a new reaction for a SignalAsset. + /// + /// The SignalAsset for which the reaction is being defined. + /// The UnityEvent that describes the reaction. + /// Thrown when the asset is null. + /// Thrown when the SignalAsset is already registered with this receiver. + public void AddReaction(SignalAsset asset, UnityEvent reaction) + { + if (asset == null) + throw new ArgumentNullException("asset"); + + if (m_Events.signals.Contains(asset)) + throw new ArgumentException("SignalAsset already used."); + m_Events.Append(asset, reaction); + } + + /// + /// Appends a null SignalAsset with a reaction specified by the UnityEvent. + /// + /// The new reaction to be appended. + /// The index of the appended reaction. + /// Multiple null assets are valid. + public int AddEmptyReaction(UnityEvent reaction) + { + m_Events.Append(null, reaction); + return m_Events.events.Count - 1; + } + + /// + /// Removes the first occurrence of a SignalAsset. + /// + /// The SignalAsset to be removed. + public void Remove(SignalAsset asset) + { + if (!m_Events.signals.Contains(asset)) + { + throw new ArgumentException("The SignalAsset is not registered with this receiver."); + } + + m_Events.Remove(asset); + } + + /// + /// Gets a list of all registered SignalAssets. + /// + /// Returns a list of SignalAssets. + public IEnumerable GetRegisteredSignals() + { + return m_Events.signals; + } + + /// + /// Gets the first UnityEvent associated with a SignalAsset. + /// + /// A SignalAsset defining the signal. + /// Returns the reaction associated with a SignalAsset. Returns null if the signal asset does not exist. + public UnityEvent GetReaction(SignalAsset key) + { + UnityEvent ret; + if (m_Events.TryGetValue(key, out ret)) + { + return ret; + } + + return null; + } + + /// + /// Returns the count of registered SignalAssets. + /// + /// + public int Count() + { + return m_Events.signals.Count; + } + + /// + /// Replaces the SignalAsset associated with a reaction at a specific index. + /// + /// The index of the reaction. + /// The replacement SignalAsset. + /// Thrown when the replacement SignalAsset is already registered to this SignalReceiver. + /// The new SignalAsset can be null. + public void ChangeSignalAtIndex(int idx, SignalAsset newKey) + { + if (idx < 0 || idx > m_Events.signals.Count - 1) + throw new IndexOutOfRangeException(); + + if (m_Events.signals[idx] == newKey) + return; + var alreadyUsed = m_Events.signals.Contains(newKey); + if (newKey == null || m_Events.signals[idx] == null || !alreadyUsed) + m_Events.signals[idx] = newKey; + + if (newKey != null && alreadyUsed) + throw new ArgumentException("SignalAsset already used."); + } + + /// + /// Removes the SignalAsset and reaction at a specific index. + /// + /// The index of the SignalAsset to be removed. + public void RemoveAtIndex(int idx) + { + if (idx < 0 || idx > m_Events.signals.Count - 1) + throw new IndexOutOfRangeException(); + m_Events.Remove(idx); + } + + /// + /// Replaces the reaction at a specific index with a new UnityEvent. + /// + /// The index of the reaction to be replaced. + /// The replacement reaction. + /// Thrown when the replacement reaction is null. + public void ChangeReactionAtIndex(int idx, UnityEvent reaction) + { + if (idx < 0 || idx > m_Events.events.Count - 1) + throw new IndexOutOfRangeException(); + + m_Events.events[idx] = reaction; + } + + /// + /// Gets the reaction at a specific index. + /// + /// The index of the reaction. + /// Returns a reaction. + public UnityEvent GetReactionAtIndex(int idx) + { + if (idx < 0 || idx > m_Events.events.Count - 1) + throw new IndexOutOfRangeException(); + return m_Events.events[idx]; + } + + /// + /// Gets the SignalAsset at a specific index + /// + /// The index of the SignalAsset. + /// Returns a SignalAsset. + public SignalAsset GetSignalAssetAtIndex(int idx) + { + if (idx < 0 || idx > m_Events.signals.Count - 1) + throw new IndexOutOfRangeException(); + return m_Events.signals[idx]; + } + + // Required by Unity for the MonoBehaviour to have an enabled state + private void OnEnable() + { + } + + [Serializable] + class EventKeyValue + { + [SerializeField] + List m_Signals = new List(); + + [SerializeField, CustomSignalEventDrawer] + List m_Events = new List(); + + public bool TryGetValue(SignalAsset key, out UnityEvent value) + { + var index = m_Signals.IndexOf(key); + if (index != -1) + { + value = m_Events[index]; + return true; + } + + value = null; + return false; + } + + public void Append(SignalAsset key, UnityEvent value) + { + m_Signals.Add(key); + m_Events.Add(value); + } + + public void Remove(int idx) + { + if (idx != -1) + { + m_Signals.RemoveAt(idx); + m_Events.RemoveAt(idx); + } + } + + public void Remove(SignalAsset key) + { + var idx = m_Signals.IndexOf(key); + if (idx != -1) + { + m_Signals.RemoveAt(idx); + m_Events.RemoveAt(idx); + } + } + + public List signals + { + get { return m_Signals; } + } + + public List events + { + get { return m_Events; } + } + } + } +} diff --git a/Library/PackageCache/com.unity.timeline@1.2.13/Runtime/Events/Signals/SignalReceiver.cs.meta b/Library/PackageCache/com.unity.timeline@1.2.13/Runtime/Events/Signals/SignalReceiver.cs.meta new file mode 100644 index 0000000..8d08ff6 --- /dev/null +++ b/Library/PackageCache/com.unity.timeline@1.2.13/Runtime/Events/Signals/SignalReceiver.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e52de21a22b6dd44c9cc19f810c65059 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: -- cgit v1.2.3