|
|
@ -2,6 +2,8 @@ |
|
|
|
// It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE) |
|
|
|
// It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE) |
|
|
|
|
|
|
|
|
|
|
|
using UnityEngine; |
|
|
|
using UnityEngine; |
|
|
|
|
|
|
|
using System.Collections.Generic; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace Fungus |
|
|
|
namespace Fungus |
|
|
|
{ |
|
|
|
{ |
|
|
@ -22,16 +24,48 @@ namespace Fungus |
|
|
|
DraggableObject = draggableObject; |
|
|
|
DraggableObject = draggableObject; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[SerializeField] protected VariableReference draggableRef; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Tooltip("Draggable object to listen for drag events on")] |
|
|
|
[Tooltip("Draggable object to listen for drag events on")] |
|
|
|
|
|
|
|
[HideInInspector] |
|
|
|
[SerializeField] protected Draggable2D draggableObject; |
|
|
|
[SerializeField] protected Draggable2D draggableObject; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[SerializeField] protected List<Draggable2D> draggableObjects; |
|
|
|
|
|
|
|
|
|
|
|
[Tooltip("Drag target object to listen for drag events on")] |
|
|
|
[Tooltip("Drag target object to listen for drag events on")] |
|
|
|
|
|
|
|
[HideInInspector] |
|
|
|
[SerializeField] protected Collider2D targetObject; |
|
|
|
[SerializeField] protected Collider2D targetObject; |
|
|
|
|
|
|
|
[SerializeField] protected List<Collider2D> targetObjects; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[SerializeField] protected VariableReference targetRef; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void OnValidate() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
//add any dragableobject already present to list for backwards compatability |
|
|
|
|
|
|
|
if(draggableObject!=null){ |
|
|
|
|
|
|
|
if(!draggableObjects.Contains(draggableObject)){ |
|
|
|
|
|
|
|
draggableObjects.Add(draggableObject); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(targetObject!=null){ |
|
|
|
|
|
|
|
if(!targetObjects.Contains(targetObject)){ |
|
|
|
|
|
|
|
targetObjects.Add(targetObject); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// There's no way to poll if an object is touching another object, so |
|
|
|
// There's no way to poll if an object is touching another object, so |
|
|
|
// we have to listen to the callbacks and track the touching state ourselves. |
|
|
|
// we have to listen to the callbacks and track the touching state ourselves. |
|
|
|
protected bool overTarget = false; |
|
|
|
protected bool overTarget = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected Collider2D targetCollider = null; |
|
|
|
|
|
|
|
|
|
|
|
protected EventDispatcher eventDispatcher; |
|
|
|
protected EventDispatcher eventDispatcher; |
|
|
|
|
|
|
|
|
|
|
|
protected virtual void OnEnable() |
|
|
|
protected virtual void OnEnable() |
|
|
@ -42,9 +76,9 @@ namespace Fungus |
|
|
|
eventDispatcher.AddListener<DragEntered.DragEnteredEvent>(OnDragEnteredEvent); |
|
|
|
eventDispatcher.AddListener<DragEntered.DragEnteredEvent>(OnDragEnteredEvent); |
|
|
|
eventDispatcher.AddListener<DragExited.DragExitedEvent>(OnDragExitedEvent); |
|
|
|
eventDispatcher.AddListener<DragExited.DragExitedEvent>(OnDragExitedEvent); |
|
|
|
|
|
|
|
|
|
|
|
if(draggableObject != null) |
|
|
|
foreach(Draggable2D dragObj in draggableObjects) |
|
|
|
{ |
|
|
|
{ |
|
|
|
draggableObject.RegisterHandler(this); |
|
|
|
dragObj.RegisterHandler(this); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -54,9 +88,9 @@ namespace Fungus |
|
|
|
eventDispatcher.RemoveListener<DragEntered.DragEnteredEvent>(OnDragEnteredEvent); |
|
|
|
eventDispatcher.RemoveListener<DragEntered.DragEnteredEvent>(OnDragEnteredEvent); |
|
|
|
eventDispatcher.RemoveListener<DragExited.DragExitedEvent>(OnDragExitedEvent); |
|
|
|
eventDispatcher.RemoveListener<DragExited.DragExitedEvent>(OnDragExitedEvent); |
|
|
|
|
|
|
|
|
|
|
|
if(draggableObject != null) |
|
|
|
foreach(Draggable2D dragObj in draggableObjects) |
|
|
|
{ |
|
|
|
{ |
|
|
|
draggableObject.UnregisterHandler(this); |
|
|
|
dragObj.UnregisterHandler(this); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
eventDispatcher = null; |
|
|
|
eventDispatcher = null; |
|
|
@ -82,7 +116,7 @@ namespace Fungus |
|
|
|
/// <summary> |
|
|
|
/// <summary> |
|
|
|
/// Gets the draggable object. |
|
|
|
/// Gets the draggable object. |
|
|
|
/// </summary> |
|
|
|
/// </summary> |
|
|
|
public virtual Draggable2D DraggableObject { get { return draggableObject; } } |
|
|
|
public virtual List<Draggable2D> DraggableObjects { get { return draggableObjects; } } |
|
|
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
/// <summary> |
|
|
|
/// Returns true if the draggable object is over the drag target object. |
|
|
|
/// Returns true if the draggable object is over the drag target object. |
|
|
@ -97,11 +131,12 @@ namespace Fungus |
|
|
|
/// </summary> |
|
|
|
/// </summary> |
|
|
|
public virtual void OnDragEntered(Draggable2D draggableObject, Collider2D targetObject) |
|
|
|
public virtual void OnDragEntered(Draggable2D draggableObject, Collider2D targetObject) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (this.targetObject != null && |
|
|
|
if (this.targetObjects != null && this.draggableObjects !=null && |
|
|
|
draggableObject == this.draggableObject && |
|
|
|
this.draggableObjects.Contains(draggableObject) && |
|
|
|
targetObject == this.targetObject) |
|
|
|
this.targetObjects.Contains(targetObject)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
overTarget = true; |
|
|
|
overTarget = true; |
|
|
|
|
|
|
|
targetCollider = targetObject; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -110,11 +145,13 @@ namespace Fungus |
|
|
|
/// </summary> |
|
|
|
/// </summary> |
|
|
|
public virtual void OnDragExited(Draggable2D draggableObject, Collider2D targetObject) |
|
|
|
public virtual void OnDragExited(Draggable2D draggableObject, Collider2D targetObject) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (this.targetObject != null && |
|
|
|
if (this.targetObjects != null && this.draggableObjects !=null && |
|
|
|
draggableObject == this.draggableObject && |
|
|
|
this.draggableObjects.Contains(draggableObject) && |
|
|
|
targetObject == this.targetObject) |
|
|
|
this.targetObjects.Contains(targetObject)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
overTarget = false; |
|
|
|
overTarget = false; |
|
|
|
|
|
|
|
targetCollider = null; |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -123,28 +160,46 @@ namespace Fungus |
|
|
|
/// </summary> |
|
|
|
/// </summary> |
|
|
|
public virtual void OnDragCompleted(Draggable2D draggableObject) |
|
|
|
public virtual void OnDragCompleted(Draggable2D draggableObject) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (draggableObject == this.draggableObject && |
|
|
|
if (this.draggableObjects.Contains(draggableObject) && |
|
|
|
overTarget) |
|
|
|
overTarget) |
|
|
|
{ |
|
|
|
{ |
|
|
|
// Assume that the player will have to do perform another drag and drop operation |
|
|
|
// Assume that the player will have to do perform another drag and drop operation |
|
|
|
// to complete the drag again. This is necessary because we don't get an OnDragExited if the |
|
|
|
// to complete the drag again. This is necessary because we don't get an OnDragExited if the |
|
|
|
// draggable object is set to be inactive. |
|
|
|
// draggable object is set to be inactive. |
|
|
|
|
|
|
|
draggableRef.Set<GameObject>(draggableObject.gameObject); |
|
|
|
|
|
|
|
targetRef.Set<GameObject>(targetCollider.gameObject); |
|
|
|
|
|
|
|
|
|
|
|
overTarget = false; |
|
|
|
overTarget = false; |
|
|
|
|
|
|
|
targetCollider = null; |
|
|
|
|
|
|
|
|
|
|
|
ExecuteBlock(); |
|
|
|
ExecuteBlock(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public override string GetSummary() |
|
|
|
public override string GetSummary() |
|
|
|
{ |
|
|
|
{ |
|
|
|
string summary = ""; |
|
|
|
string summary = "Draggable: "; |
|
|
|
if (draggableObject != null) |
|
|
|
if (this.draggableObjects != null && this.draggableObjects.Count != 0) |
|
|
|
{ |
|
|
|
{ |
|
|
|
summary += "\nDraggable: " + draggableObject.name; |
|
|
|
for (int i = 0; i < this.draggableObjects.Count; i++) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (draggableObjects[i] != null) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
summary += draggableObjects[i].name + ","; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if (targetObject != null) |
|
|
|
|
|
|
|
|
|
|
|
summary += "\nTarget: "; |
|
|
|
|
|
|
|
if (this.targetObjects != null && this.targetObjects.Count != 0) |
|
|
|
{ |
|
|
|
{ |
|
|
|
summary += "\nTarget: " + targetObject.name; |
|
|
|
for (int i = 0; i < this.targetObjects.Count; i++) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (targetObjects[i] != null) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
summary += targetObjects[i].name + ","; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (summary.Length == 0) |
|
|
|
if (summary.Length == 0) |
|
|
|