From 569389e80710e616908ec2df08b1d18c8f4dfd55 Mon Sep 17 00:00:00 2001 From: fjruizpo Date: Sat, 7 Jan 2017 08:48:30 +0100 Subject: [PATCH] Add support to the EventDispatcher in drag and click event handlers. --- .../Scripts/EventHandlers/DragCancelled.cs | 25 ++++++++++++ .../Scripts/EventHandlers/DragCompleted.cs | 39 +++++++++++++++++++ .../Scripts/EventHandlers/DragEntered.cs | 26 +++++++++++++ .../Scripts/EventHandlers/DragExited.cs | 28 +++++++++++++ .../Scripts/EventHandlers/DragStarted.cs | 26 +++++++++++++ .../Scripts/EventHandlers/ObjectClicked.cs | 27 +++++++++++++ 6 files changed, 171 insertions(+) diff --git a/Assets/Fungus/Scripts/EventHandlers/DragCancelled.cs b/Assets/Fungus/Scripts/EventHandlers/DragCancelled.cs index 5a369111..4bf4b01a 100644 --- a/Assets/Fungus/Scripts/EventHandlers/DragCancelled.cs +++ b/Assets/Fungus/Scripts/EventHandlers/DragCancelled.cs @@ -14,9 +14,34 @@ namespace Fungus [AddComponentMenu("")] public class DragCancelled : EventHandler { + public class DragCancelledEvent + { + public Draggable2D DraggableObject; + public DragCancelledEvent(Draggable2D draggableObject) + { + DraggableObject = draggableObject; + } + } + [Tooltip("Draggable object to listen for drag events on")] [SerializeField] protected Draggable2D draggableObject; + protected override void UnityOnEnable() + { + base.UnityOnEnable(); + EventDispatcher.AddListener(OnDragCancelledEvent); + } + + protected override void UnityOnDisable() + { + base.UnityOnDisable(); + EventDispatcher.RemoveListener(OnDragCancelledEvent); + } + + void OnDragCancelledEvent(DragCancelledEvent evt) + { + OnDragCancelled(evt.DraggableObject); + } #region Public members public virtual void OnDragCancelled(Draggable2D draggableObject) diff --git a/Assets/Fungus/Scripts/EventHandlers/DragCompleted.cs b/Assets/Fungus/Scripts/EventHandlers/DragCompleted.cs index 63240d76..ecdd0e2f 100644 --- a/Assets/Fungus/Scripts/EventHandlers/DragCompleted.cs +++ b/Assets/Fungus/Scripts/EventHandlers/DragCompleted.cs @@ -14,6 +14,14 @@ namespace Fungus [AddComponentMenu("")] public class DragCompleted : EventHandler { + public class DragCompletedEvent + { + public Draggable2D DraggableObject; + public DragCompletedEvent(Draggable2D draggableObject) + { + DraggableObject = draggableObject; + } + } [Tooltip("Draggable object to listen for drag events on")] [SerializeField] protected Draggable2D draggableObject; @@ -24,6 +32,37 @@ namespace Fungus // we have to listen to the callbacks and track the touching state ourselves. protected bool overTarget = false; + protected override void UnityOnEnable() + { + base.UnityOnEnable(); + EventDispatcher.AddListener(OnDragCompletedEvent); + EventDispatcher.AddListener(OnDragEnteredEvent); + EventDispatcher.AddListener(OnDragExitedEvent); + } + + protected override void UnityOnDisable() + { + base.UnityOnDisable(); + EventDispatcher.RemoveListener(OnDragCompletedEvent); + EventDispatcher.RemoveListener(OnDragEnteredEvent); + EventDispatcher.RemoveListener(OnDragExitedEvent); + } + + void OnDragCompletedEvent(DragCompletedEvent evt) + { + OnDragCompleted(evt.DraggableObject); + } + + void OnDragEnteredEvent(DragEntered.DragEnteredEvent evt) + { + OnDragEntered(evt.DraggableObject, evt.TargetCollider); + } + + void OnDragExitedEvent(DragExited.DragExitedEvent evt) + { + OnDragExited(evt.DraggableObject, evt.TargetCollider); + } + #region Public members /// diff --git a/Assets/Fungus/Scripts/EventHandlers/DragEntered.cs b/Assets/Fungus/Scripts/EventHandlers/DragEntered.cs index 3b9674f0..c55cf46c 100644 --- a/Assets/Fungus/Scripts/EventHandlers/DragEntered.cs +++ b/Assets/Fungus/Scripts/EventHandlers/DragEntered.cs @@ -17,12 +17,38 @@ namespace Fungus [AddComponentMenu("")] public class DragEntered : EventHandler { + public class DragEnteredEvent + { + public Draggable2D DraggableObject; + public Collider2D TargetCollider; + public DragEnteredEvent(Draggable2D draggableObject, Collider2D targetCollider) + { + DraggableObject = draggableObject; + TargetCollider = targetCollider; + } + } [Tooltip("Draggable object to listen for drag events on")] [SerializeField] protected Draggable2D draggableObject; [Tooltip("Drag target object to listen for drag events on")] [SerializeField] protected Collider2D targetObject; + protected override void UnityOnEnable() + { + base.UnityOnEnable(); + EventDispatcher.AddListener(OnDragEnteredEvent); + } + + protected override void UnityOnDisable() + { + base.UnityOnDisable(); + EventDispatcher.RemoveListener(OnDragEnteredEvent); + } + + void OnDragEnteredEvent(DragEnteredEvent evt) + { + OnDragEntered(evt.DraggableObject, evt.TargetCollider); + } #region Public members /// diff --git a/Assets/Fungus/Scripts/EventHandlers/DragExited.cs b/Assets/Fungus/Scripts/EventHandlers/DragExited.cs index 9381a7d6..711a9505 100644 --- a/Assets/Fungus/Scripts/EventHandlers/DragExited.cs +++ b/Assets/Fungus/Scripts/EventHandlers/DragExited.cs @@ -17,12 +17,40 @@ namespace Fungus [AddComponentMenu("")] public class DragExited : EventHandler { + public class DragExitedEvent + { + public Draggable2D DraggableObject; + public Collider2D TargetCollider; + public DragExitedEvent(Draggable2D draggableObject, Collider2D targetCollider) + { + DraggableObject = draggableObject; + TargetCollider = targetCollider; + } + } + [Tooltip("Draggable object to listen for drag events on")] [SerializeField] protected Draggable2D draggableObject; [Tooltip("Drag target object to listen for drag events on")] [SerializeField] protected Collider2D targetObject; + protected override void UnityOnEnable() + { + base.UnityOnEnable(); + EventDispatcher.AddListener(OnDragEnteredEvent); + } + + protected override void UnityOnDisable() + { + base.UnityOnDisable(); + EventDispatcher.RemoveListener(OnDragEnteredEvent); + } + + void OnDragEnteredEvent(DragExitedEvent evt) + { + OnDragExited(evt.DraggableObject, evt.TargetCollider); + } + #region Public members /// diff --git a/Assets/Fungus/Scripts/EventHandlers/DragStarted.cs b/Assets/Fungus/Scripts/EventHandlers/DragStarted.cs index 90d00a81..95f14570 100644 --- a/Assets/Fungus/Scripts/EventHandlers/DragStarted.cs +++ b/Assets/Fungus/Scripts/EventHandlers/DragStarted.cs @@ -14,8 +14,34 @@ namespace Fungus [AddComponentMenu("")] public class DragStarted : EventHandler { + public class DragStartedEvent + { + public Draggable2D DraggableObject; + public DragStartedEvent(Draggable2D draggableObject) + { + DraggableObject = draggableObject; + } + } + [SerializeField] protected Draggable2D draggableObject; + protected override void UnityOnEnable() + { + base.UnityOnEnable(); + EventDispatcher.AddListener(OnDragStartedEvent); + } + + protected override void UnityOnDisable() + { + base.UnityOnDisable(); + EventDispatcher.RemoveListener(OnDragStartedEvent); + } + + void OnDragStartedEvent(DragStartedEvent evt) + { + OnDragStarted(evt.DraggableObject); + } + #region Public members /// diff --git a/Assets/Fungus/Scripts/EventHandlers/ObjectClicked.cs b/Assets/Fungus/Scripts/EventHandlers/ObjectClicked.cs index a103a1ef..e326e6b0 100644 --- a/Assets/Fungus/Scripts/EventHandlers/ObjectClicked.cs +++ b/Assets/Fungus/Scripts/EventHandlers/ObjectClicked.cs @@ -15,12 +15,39 @@ namespace Fungus [AddComponentMenu("")] public class ObjectClicked : EventHandler { + public class ObjectClickedEvent + { + public Clickable2D ClickableObject; + public ObjectClickedEvent(Clickable2D clickableObject) + { + ClickableObject = clickableObject; + } + } + [Tooltip("Object that the user can click or tap on")] [SerializeField] protected Clickable2D clickableObject; [Tooltip("Wait for a number of frames before executing the block.")] [SerializeField] protected int waitFrames = 1; + + protected override void UnityOnEnable() + { + base.UnityOnEnable(); + EventDispatcher.AddListener(OnObjectClickedEvent); + } + + protected override void UnityOnDisable() + { + base.UnityOnDisable(); + EventDispatcher.RemoveListener(OnObjectClickedEvent); + } + + void OnObjectClickedEvent(ObjectClickedEvent evt) + { + OnObjectClicked(evt.ClickableObject); + } + /// /// Executing a block on the same frame that the object is clicked can cause /// input problems (e.g. auto completing Say Dialog text). A single frame delay