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. 197
      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;

197
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,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
}
}

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