|
|
|
@ -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,90 +94,6 @@ namespace Fungus
|
|
|
|
|
return GameObject.FindObjectsOfType<T>(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#region Legacy OnMouseX methods |
|
|
|
|
protected virtual void OnMouseDown() |
|
|
|
|
{ |
|
|
|
|
if (!useEventSystem) |
|
|
|
|
{ |
|
|
|
|
DoBeginDrag(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected virtual void OnMouseDrag() |
|
|
|
|
{ |
|
|
|
|
if (!useEventSystem) |
|
|
|
|
{ |
|
|
|
|
DoDrag(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected virtual void OnMouseUp() |
|
|
|
|
{ |
|
|
|
|
if (!useEventSystem) |
|
|
|
|
{ |
|
|
|
|
DoEndDrag(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected virtual void OnMouseEnter() |
|
|
|
|
{ |
|
|
|
|
if (!useEventSystem) |
|
|
|
|
{ |
|
|
|
|
DoPointerEnter(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected virtual void OnMouseExit() |
|
|
|
|
{ |
|
|
|
|
if (!useEventSystem) |
|
|
|
|
{ |
|
|
|
|
DoPointerExit(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
#endregion |
|
|
|
|
|
|
|
|
|
#region Pointer and Drag handler implementations |
|
|
|
|
public void OnBeginDrag(PointerEventData eventData) |
|
|
|
|
{ |
|
|
|
|
if (useEventSystem) |
|
|
|
|
{ |
|
|
|
|
DoBeginDrag(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void OnDrag(PointerEventData eventData) |
|
|
|
|
{ |
|
|
|
|
if (useEventSystem) |
|
|
|
|
{ |
|
|
|
|
DoDrag(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void OnEndDrag(PointerEventData eventData) |
|
|
|
|
{ |
|
|
|
|
if (useEventSystem) |
|
|
|
|
{ |
|
|
|
|
DoEndDrag(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void OnPointerEnter(PointerEventData eventData) |
|
|
|
|
{ |
|
|
|
|
if (useEventSystem) |
|
|
|
|
{ |
|
|
|
|
DoPointerEnter(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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 |
|
|
|
@ -271,5 +186,115 @@ namespace Fungus
|
|
|
|
|
|
|
|
|
|
Cursor.SetCursor(cursorTexture, Vector2.zero, CursorMode.Auto); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#region Legacy OnMouseX methods |
|
|
|
|
|
|
|
|
|
protected virtual void OnMouseDown() |
|
|
|
|
{ |
|
|
|
|
if (!useEventSystem) |
|
|
|
|
{ |
|
|
|
|
DoBeginDrag(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected virtual void OnMouseDrag() |
|
|
|
|
{ |
|
|
|
|
if (!useEventSystem) |
|
|
|
|
{ |
|
|
|
|
DoDrag(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected virtual void OnMouseUp() |
|
|
|
|
{ |
|
|
|
|
if (!useEventSystem) |
|
|
|
|
{ |
|
|
|
|
DoEndDrag(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected virtual void OnMouseEnter() |
|
|
|
|
{ |
|
|
|
|
if (!useEventSystem) |
|
|
|
|
{ |
|
|
|
|
DoPointerEnter(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected virtual void OnMouseExit() |
|
|
|
|
{ |
|
|
|
|
if (!useEventSystem) |
|
|
|
|
{ |
|
|
|
|
DoPointerExit(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#endregion |
|
|
|
|
|
|
|
|
|
#region IDraggable2D implementation |
|
|
|
|
|
|
|
|
|
public virtual bool DragEnabled { get { return dragEnabled; } set { dragEnabled = value; } } |
|
|
|
|
|
|
|
|
|
#endregion |
|
|
|
|
|
|
|
|
|
#region IBeginDragHandler implementation |
|
|
|
|
|
|
|
|
|
public void OnBeginDrag(PointerEventData eventData) |
|
|
|
|
{ |
|
|
|
|
if (useEventSystem) |
|
|
|
|
{ |
|
|
|
|
DoBeginDrag(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#endregion |
|
|
|
|
|
|
|
|
|
#region IDragHandler implementation |
|
|
|
|
|
|
|
|
|
public void OnDrag(PointerEventData eventData) |
|
|
|
|
{ |
|
|
|
|
if (useEventSystem) |
|
|
|
|
{ |
|
|
|
|
DoDrag(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#endregion |
|
|
|
|
|
|
|
|
|
#region IEndDragHandler implementation |
|
|
|
|
|
|
|
|
|
public void OnEndDrag(PointerEventData eventData) |
|
|
|
|
{ |
|
|
|
|
if (useEventSystem) |
|
|
|
|
{ |
|
|
|
|
DoEndDrag(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#endregion |
|
|
|
|
|
|
|
|
|
#region IPointerEnterHandler implementation |
|
|
|
|
|
|
|
|
|
public void OnPointerEnter(PointerEventData eventData) |
|
|
|
|
{ |
|
|
|
|
if (useEventSystem) |
|
|
|
|
{ |
|
|
|
|
DoPointerEnter(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#endregion |
|
|
|
|
|
|
|
|
|
#region IPointerExitHandler implementation |
|
|
|
|
|
|
|
|
|
public void OnPointerExit(PointerEventData eventData) |
|
|
|
|
{ |
|
|
|
|
if (useEventSystem) |
|
|
|
|
{ |
|
|
|
|
DoPointerExit(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#endregion |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|