diff options
Diffstat (limited to 'Runtime/Graphs/UnityEngine.Graphs/MonoBehaviourEventDummies')
5 files changed, 200 insertions, 0 deletions
diff --git a/Runtime/Graphs/UnityEngine.Graphs/MonoBehaviourEventDummies/ColliderDummyBase.cs b/Runtime/Graphs/UnityEngine.Graphs/MonoBehaviourEventDummies/ColliderDummyBase.cs new file mode 100644 index 0000000..2d7efca --- /dev/null +++ b/Runtime/Graphs/UnityEngine.Graphs/MonoBehaviourEventDummies/ColliderDummyBase.cs @@ -0,0 +1,25 @@ +using System; +using UnityEngine; + +namespace UnityEngine.Graphs.LogicGraph +{ + // For now we do triggers by attaching this MonoBehaviour to needed gameobjects. Class then sends events to logic graph nodes. + public abstract class ColliderDummyBase : MonoBehaviour + { + protected static Component AttachToCollider(Collider self, Type dummyType) + { + var attached = GetAndAddComponentIfNeeded(self.gameObject, dummyType); + + if (attached == null) + throw new ArgumentException("Failed to attach Logic Graph Collider Event handler to a game object of component '" + self + "'."); + + return attached; + } + + private static Component GetAndAddComponentIfNeeded(GameObject go, Type type) + { + return go.GetComponent(type) ?? go.AddComponent(type); + } + } +} + diff --git a/Runtime/Graphs/UnityEngine.Graphs/MonoBehaviourEventDummies/OnAnimationEventDummy.cs b/Runtime/Graphs/UnityEngine.Graphs/MonoBehaviourEventDummies/OnAnimationEventDummy.cs new file mode 100644 index 0000000..66bf531 --- /dev/null +++ b/Runtime/Graphs/UnityEngine.Graphs/MonoBehaviourEventDummies/OnAnimationEventDummy.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using UnityEngine; + +namespace UnityEngine.Graphs.LogicGraph +{ + // This component gets attached to a GameObject with Animation component and handles special LogicGraphEvent. + public class OnAnimationEventDummy : MonoBehaviour + { + private Dictionary<string, Action> m_Events = new Dictionary<string, Action> (); + + public static void AttachToGameObject(Animation component, string eventName, Action delegateToCall) + { + var animEventDummy = component.gameObject.GetComponent (typeof (OnAnimationEventDummy)) as OnAnimationEventDummy ?? + component.gameObject.AddComponent(typeof(OnAnimationEventDummy)) as OnAnimationEventDummy; + + if (animEventDummy == null) + throw new ArgumentException("Failed to attach Logic Graph Animation Event handler to a game object of component '" + component + "'."); + + animEventDummy.AddNewEvent(eventName, delegateToCall); + } + + private void AddNewEvent(string eventName, Action delegateToCall) + { + if (!m_Events.ContainsKey(eventName)) + m_Events.Add(eventName, delegateToCall); + else + m_Events[eventName] += delegateToCall; + } + + public void LogicGraphEvent(string eventName) + { + Action delegateToCall; + + if (!m_Events.TryGetValue(eventName, out delegateToCall)) + { + Debug.LogError("Logic Graph failed to handle Animation Event '" + eventName + "'. Receiver was not found."); + return; + } + + if (delegateToCall == null) + { + Debug.LogError("Logic Graph failed to handle Animation Event '" + eventName + "'. Receiver was null."); + return; + } + + delegateToCall (); + } + } +} + diff --git a/Runtime/Graphs/UnityEngine.Graphs/MonoBehaviourEventDummies/OnCollisionEventDummy.cs b/Runtime/Graphs/UnityEngine.Graphs/MonoBehaviourEventDummies/OnCollisionEventDummy.cs new file mode 100644 index 0000000..09ccc8a --- /dev/null +++ b/Runtime/Graphs/UnityEngine.Graphs/MonoBehaviourEventDummies/OnCollisionEventDummy.cs @@ -0,0 +1,43 @@ +using System; +using UnityEngine; + +namespace UnityEngine.Graphs.LogicGraph +{ + // For now we do triggers by attaching this MonoBehaviour to needed gameobjects. Class then sends events to logic graph nodes. + public class OnCollisionEventDummy : ColliderDummyBase + { + public delegate void CollisionOutDelegate(Collision other); + + private CollisionOutDelegate m_OnEnter; + private CollisionOutDelegate m_OnExit; + private CollisionOutDelegate m_OnStay; + + public static void AttachToCollider(ColliderNodes.OnCollisionEvent node) + { + var attached = AttachToCollider(node.self, typeof(OnCollisionEventDummy)) as OnCollisionEventDummy; + attached.m_OnEnter += node.EnterDummy; + attached.m_OnExit += node.ExitDummy; + attached.m_OnStay += node.StayDummy; + } + + public void OnCollisionEnter(Collision collision) + { + if (m_OnEnter == null) + return; + m_OnEnter (collision); + } + public void OnCollisionExit(Collision collision) + { + if (m_OnExit == null) + return; + m_OnExit(collision); + } + public void OnCollisionStay(Collision collision) + { + if (m_OnStay == null) + return; + m_OnStay(collision); + } + } +} + diff --git a/Runtime/Graphs/UnityEngine.Graphs/MonoBehaviourEventDummies/OnMouseEventDummy.cs b/Runtime/Graphs/UnityEngine.Graphs/MonoBehaviourEventDummies/OnMouseEventDummy.cs new file mode 100644 index 0000000..c359edf --- /dev/null +++ b/Runtime/Graphs/UnityEngine.Graphs/MonoBehaviourEventDummies/OnMouseEventDummy.cs @@ -0,0 +1,41 @@ +using System; +using UnityEngine; + +namespace UnityEngine.Graphs.LogicGraph +{ + // For now we do triggers by attaching this MonoBehaviour to needed gameobjects. Class then sends events to logic graph nodes. + public class OnMouseEventDummy : ColliderDummyBase + { + private Action m_OnEnter; + private Action m_OnOver; + private Action m_OnExit; + private Action m_OnDown; + private Action m_OnUp; + private Action m_OnDrag; + + public static void AttachToCollider (ColliderNodes.OnMouseEvent node) + { + var attached = AttachToCollider(node.self, typeof(OnMouseEventDummy)) as OnMouseEventDummy; + attached.m_OnEnter += node.enter; + attached.m_OnOver += node.over; + attached.m_OnExit += node.exit; + attached.m_OnDown += node.down; + attached.m_OnUp += node.up; + attached.m_OnDrag += node.drag; + } + + public void OnMouseEnter () { CallEventDelegate (m_OnEnter); } + public void OnMouseOver () { CallEventDelegate (m_OnOver); } + public void OnMouseExit () { CallEventDelegate (m_OnExit); } + public void OnMouseDown () { CallEventDelegate (m_OnDown); } + public void OnMouseUp () { CallEventDelegate (m_OnUp); } + public void OnMouseDrag () { CallEventDelegate (m_OnDrag); } + + protected static void CallEventDelegate(Action eventDelegate) + { + if (eventDelegate != null) + eventDelegate(); + } + } +} + diff --git a/Runtime/Graphs/UnityEngine.Graphs/MonoBehaviourEventDummies/OnTriggerEventDummy.cs b/Runtime/Graphs/UnityEngine.Graphs/MonoBehaviourEventDummies/OnTriggerEventDummy.cs new file mode 100644 index 0000000..992f240 --- /dev/null +++ b/Runtime/Graphs/UnityEngine.Graphs/MonoBehaviourEventDummies/OnTriggerEventDummy.cs @@ -0,0 +1,40 @@ +using System; +using UnityEngine; +using System.Collections.Generic; + +namespace UnityEngine.Graphs.LogicGraph +{ + // For now we do triggers by attaching this MonoBehaviour to needed gameobjects. Class then sends events to logic graph nodes. + public class OnTriggerEventDummy : ColliderDummyBase + { + public delegate void TriggerOutDelegate (Collider other); + private TriggerOutDelegate m_OnEnter; + private TriggerOutDelegate m_OnExit; + private TriggerOutDelegate m_OnStay; + + public static void AttachToCollider(ColliderNodes.OnTriggerEvent node) + { + var attached = AttachToCollider(node.self, typeof(OnTriggerEventDummy)) as OnTriggerEventDummy; + attached.m_OnEnter += node.EnterDummy; + attached.m_OnExit += node.ExitDummy; + attached.m_OnStay += node.StayDummy; + } + + public void OnTriggerEnter(Collider other) + { + if (m_OnEnter != null) + m_OnEnter (other); + } + public void OnTriggerExit(Collider other) + { + if (m_OnExit != null) + m_OnExit(other); + } + public void OnTriggerStay(Collider other) + { + if (m_OnStay != null) + m_OnStay (other); + } + } +} + |