diff --git a/Assets/Fungus/Sprite/Scripts/Clickable2D.cs b/Assets/Fungus/Sprite/Scripts/Clickable2D.cs index a532be32..0cdca596 100644 --- a/Assets/Fungus/Sprite/Scripts/Clickable2D.cs +++ b/Assets/Fungus/Sprite/Scripts/Clickable2D.cs @@ -11,7 +11,7 @@ namespace Fungus /// The Game Object must have a Collider or Collider2D component attached. /// Use in conjunction with the ObjectClicked Flowchart event handler. /// - public class Clickable2D : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler + public class Clickable2D : MonoBehaviour, IClickable2D, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler { [Tooltip("Is object clicking enabled")] [SerializeField] protected bool clickEnabled = true; diff --git a/Assets/Fungus/Sprite/Scripts/Draggable2D.cs b/Assets/Fungus/Sprite/Scripts/Draggable2D.cs index f3456fb0..10c9ab61 100644 --- a/Assets/Fungus/Sprite/Scripts/Draggable2D.cs +++ b/Assets/Fungus/Sprite/Scripts/Draggable2D.cs @@ -15,11 +15,10 @@ namespace Fungus /// The RigidBody would typically have the Is Kinematic property set to true, unless you want the object to move around using physics. /// Use in conjunction with the Drag Started, Drag Completed, Drag Cancelled, Drag Entered & Drag Exited event handlers. /// - public class Draggable2D : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerEnterHandler, IPointerExitHandler + public class Draggable2D : MonoBehaviour, IDraggable2D, IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerEnterHandler, IPointerExitHandler { [Tooltip("Is object dragging enabled")] [SerializeField] protected bool dragEnabled = true; - public virtual bool DragEnabled { get { return dragEnabled; } set { dragEnabled = value; } } [Tooltip("Move object back to its starting position when drag is cancelled")] [FormerlySerializedAs("returnToStartPos")] @@ -95,7 +94,101 @@ namespace Fungus return GameObject.FindObjectsOfType(); } + protected virtual void DoBeginDrag() + { + // Offset the object so that the drag is anchored to the exact point where the user clicked it + float x = Input.mousePosition.x; + float y = Input.mousePosition.y; + delta = Camera.main.ScreenToWorldPoint(new Vector3(x, y, 10f)) - transform.position; + delta.z = 0f; + + startingPosition = transform.position; + + foreach (DragStarted handler in GetHandlers()) + { + handler.OnDragStarted(this); + } + } + + protected virtual void DoDrag() + { + if (!dragEnabled) + { + return; + } + + float x = Input.mousePosition.x; + float y = Input.mousePosition.y; + float z = transform.position.z; + + newPosition = Camera.main.ScreenToWorldPoint(new Vector3(x, y, 10f)) - delta; + newPosition.z = z; + updatePosition = true; + } + + protected virtual void DoEndDrag() + { + if (!dragEnabled) + { + return; + } + + bool dragCompleted = false; + + DragCompleted[] handlers = GetHandlers(); + foreach (DragCompleted handler in handlers) + { + if (handler.DraggableObject == this) + { + if (handler.IsOverTarget()) + { + handler.OnDragCompleted(this); + dragCompleted = true; + + if (returnOnCompleted) + { + LeanTween.move(gameObject, startingPosition, returnDuration).setEase(LeanTweenType.easeOutExpo); + } + } + } + } + + if (!dragCompleted) + { + foreach (DragCancelled handler in GetHandlers()) + { + handler.OnDragCancelled(this); + } + + if (returnOnCancelled) + { + LeanTween.move(gameObject, startingPosition, returnDuration).setEase(LeanTweenType.easeOutExpo); + } + } + } + + protected virtual void DoPointerEnter() + { + ChangeCursor(hoverCursor); + } + + protected virtual void DoPointerExit() + { + SetMouseCursor.ResetMouseCursor(); + } + + protected virtual void ChangeCursor(Texture2D cursorTexture) + { + if (!dragEnabled) + { + return; + } + + Cursor.SetCursor(cursorTexture, Vector2.zero, CursorMode.Auto); + } + #region Legacy OnMouseX methods + protected virtual void OnMouseDown() { if (!useEventSystem) @@ -135,9 +228,17 @@ namespace Fungus DoPointerExit(); } } + + #endregion + + #region IDraggable2D implementation + + public virtual bool DragEnabled { get { return dragEnabled; } set { dragEnabled = value; } } + #endregion - #region Pointer and Drag handler implementations + #region IBeginDragHandler implementation + public void OnBeginDrag(PointerEventData eventData) { if (useEventSystem) @@ -146,6 +247,10 @@ namespace Fungus } } + #endregion + + #region IDragHandler implementation + public void OnDrag(PointerEventData eventData) { if (useEventSystem) @@ -154,6 +259,10 @@ namespace Fungus } } + #endregion + + #region IEndDragHandler implementation + public void OnEndDrag(PointerEventData eventData) { if (useEventSystem) @@ -162,6 +271,10 @@ namespace Fungus } } + #endregion + + #region IPointerEnterHandler implementation + public void OnPointerEnter(PointerEventData eventData) { if (useEventSystem) @@ -170,106 +283,18 @@ namespace Fungus } } - public void OnPointerExit(PointerEventData eventData) - { - if (useEventSystem) - { - DoPointerExit(); - } - } #endregion - protected virtual void DoBeginDrag() - { - // Offset the object so that the drag is anchored to the exact point where the user clicked it - float x = Input.mousePosition.x; - float y = Input.mousePosition.y; - delta = Camera.main.ScreenToWorldPoint(new Vector3(x, y, 10f)) - transform.position; - delta.z = 0f; - - startingPosition = transform.position; - - foreach (DragStarted handler in GetHandlers()) - { - handler.OnDragStarted(this); - } - } - - protected virtual void DoDrag() - { - if (!dragEnabled) - { - return; - } - - float x = Input.mousePosition.x; - float y = Input.mousePosition.y; - float z = transform.position.z; - - newPosition = Camera.main.ScreenToWorldPoint(new Vector3(x, y, 10f)) - delta; - newPosition.z = z; - updatePosition = true; - } + #region IPointerExitHandler implementation - protected virtual void DoEndDrag() + public void OnPointerExit(PointerEventData eventData) { - if (!dragEnabled) - { - return; - } - - bool dragCompleted = false; - - DragCompleted[] handlers = GetHandlers(); - foreach (DragCompleted handler in handlers) - { - if (handler.DraggableObject == this) - { - if (handler.IsOverTarget()) - { - handler.OnDragCompleted(this); - dragCompleted = true; - - if (returnOnCompleted) - { - LeanTween.move(gameObject, startingPosition, returnDuration).setEase(LeanTweenType.easeOutExpo); - } - } - } - } - - if (!dragCompleted) + if (useEventSystem) { - foreach (DragCancelled handler in GetHandlers()) - { - handler.OnDragCancelled(this); - } - - if (returnOnCancelled) - { - LeanTween.move(gameObject, startingPosition, returnDuration).setEase(LeanTweenType.easeOutExpo); - } + DoPointerExit(); } } - protected virtual void DoPointerEnter() - { - ChangeCursor(hoverCursor); - } - - protected virtual void DoPointerExit() - { - SetMouseCursor.ResetMouseCursor(); - } - - protected virtual void ChangeCursor(Texture2D cursorTexture) - { - if (!dragEnabled) - { - return; - } - - Cursor.SetCursor(cursorTexture, Vector2.zero, CursorMode.Auto); - } + #endregion } } diff --git a/Assets/Fungus/Sprite/Scripts/IDraggable2D.cs b/Assets/Fungus/Sprite/Scripts/IDraggable2D.cs new file mode 100644 index 00000000..d01d6cb6 --- /dev/null +++ b/Assets/Fungus/Sprite/Scripts/IDraggable2D.cs @@ -0,0 +1,21 @@ +using UnityEngine; +using System.Collections; + +namespace Fungus +{ + /// + /// Detects drag and drop interactions on a Game Object, and sends events to all Flowchart event handlers in the scene. + /// The Game Object must have Collider2D & RigidBody components attached. + /// The Collider2D must have the Is Trigger property set to true. + /// The RigidBody would typically have the Is Kinematic property set to true, unless you want the object to move around using physics. + /// Use in conjunction with the Drag Started, Drag Completed, Drag Cancelled, Drag Entered & Drag Exited event handlers. + /// + public interface IDraggable2D + { + /// + /// Is object drag and drop enabled. + /// + /// true if drag enabled; otherwise, false. + bool DragEnabled { get; set; } + } +} diff --git a/Assets/Fungus/Sprite/Scripts/IDraggable2D.cs.meta b/Assets/Fungus/Sprite/Scripts/IDraggable2D.cs.meta new file mode 100644 index 00000000..8b127b00 --- /dev/null +++ b/Assets/Fungus/Sprite/Scripts/IDraggable2D.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: be9f3ca656cc14276b86e6f41cb87d9d +timeCreated: 1473691259 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: