Browse Source

Refactored Draggable2D to use IDraggable2D interface

master
Christopher 9 years ago
parent
commit
b423d7f313
  1. 2
      Assets/Fungus/Sprite/Scripts/Clickable2D.cs
  2. 217
      Assets/Fungus/Sprite/Scripts/Draggable2D.cs
  3. 21
      Assets/Fungus/Sprite/Scripts/IDraggable2D.cs
  4. 12
      Assets/Fungus/Sprite/Scripts/IDraggable2D.cs.meta

2
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.
/// </summary>
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;

217
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.
/// </summary>
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<T>();
}
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<DragStarted>())
{
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<DragCompleted>();
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<DragCancelled>())
{
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<DragStarted>())
{
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<DragCompleted>();
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<DragCancelled>())
{
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
}
}

21
Assets/Fungus/Sprite/Scripts/IDraggable2D.cs

@ -0,0 +1,21 @@
using UnityEngine;
using System.Collections;
namespace Fungus
{
/// <summary>
/// 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.
/// </summary>
public interface IDraggable2D
{
/// <summary>
/// Is object drag and drop enabled.
/// </summary>
/// <value><c>true</c> if drag enabled; otherwise, <c>false</c>.</value>
bool DragEnabled { get; set; }
}
}

12
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:
Loading…
Cancel
Save