|
|
|
@ -1,14 +1,16 @@
|
|
|
|
|
// This code is part of the Fungus library (https://github.com/snozbot/fungus) |
|
|
|
|
// It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE) |
|
|
|
|
|
|
|
|
|
using UnityEngine; |
|
|
|
|
using System.Collections.Generic; |
|
|
|
|
|
|
|
|
|
using UnityEngine; |
|
|
|
|
|
|
|
|
|
namespace Fungus |
|
|
|
|
{ |
|
|
|
|
/// <summary> |
|
|
|
|
/// The block will execute when the player drags an object and successfully drops it on a target object. |
|
|
|
|
/// |
|
|
|
|
/// ExecuteAlways used to get the Compatibility that we need, use of ISerializationCallbackReceiver is error prone |
|
|
|
|
/// when used on Unity controlled objects as it runs on threads other than main thread. |
|
|
|
|
/// </summary> |
|
|
|
|
[EventHandlerInfo("Sprite", |
|
|
|
|
"Drag Completed", |
|
|
|
@ -20,18 +22,19 @@ namespace Fungus
|
|
|
|
|
public class DragCompletedEvent |
|
|
|
|
{ |
|
|
|
|
public Draggable2D DraggableObject; |
|
|
|
|
|
|
|
|
|
public DragCompletedEvent(Draggable2D draggableObject) |
|
|
|
|
{ |
|
|
|
|
DraggableObject = draggableObject; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
[VariableProperty(typeof(GameObjectVariable))] |
|
|
|
|
|
|
|
|
|
[VariableProperty(typeof(GameObjectVariable))] |
|
|
|
|
[SerializeField] protected GameObjectVariable draggableRef; |
|
|
|
|
|
|
|
|
|
[VariableProperty(typeof(GameObjectVariable))] |
|
|
|
|
[SerializeField] protected GameObjectVariable targetRef; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Tooltip("Draggable object to listen for drag events on")] |
|
|
|
|
[HideInInspector] |
|
|
|
|
[SerializeField] protected Draggable2D draggableObject; |
|
|
|
@ -41,12 +44,8 @@ namespace Fungus
|
|
|
|
|
[Tooltip("Drag target object to listen for drag events on")] |
|
|
|
|
[HideInInspector] |
|
|
|
|
[SerializeField] protected Collider2D targetObject; |
|
|
|
|
[SerializeField] protected List<Collider2D> targetObjects; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[SerializeField] protected List<Collider2D> targetObjects; |
|
|
|
|
|
|
|
|
|
// 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. |
|
|
|
@ -56,59 +55,32 @@ namespace Fungus
|
|
|
|
|
|
|
|
|
|
protected EventDispatcher eventDispatcher; |
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
/// Awake is called when the script instance is being loaded. |
|
|
|
|
/// </summary> |
|
|
|
|
void Awake() |
|
|
|
|
{ |
|
|
|
|
//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); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
draggableObject = null; |
|
|
|
|
targetObject = null; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected virtual void OnEnable() |
|
|
|
|
{ |
|
|
|
|
if(Application.IsPlaying(this)){ |
|
|
|
|
if (Application.IsPlaying(this)) |
|
|
|
|
{ |
|
|
|
|
eventDispatcher = FungusManager.Instance.EventDispatcher; |
|
|
|
|
|
|
|
|
|
eventDispatcher.AddListener<DragCompletedEvent>(OnDragCompletedEvent); |
|
|
|
|
eventDispatcher.AddListener<DragEntered.DragEnteredEvent>(OnDragEnteredEvent); |
|
|
|
|
eventDispatcher.AddListener<DragExited.DragExitedEvent>(OnDragExitedEvent); |
|
|
|
|
|
|
|
|
|
foreach(Draggable2D dragObj in draggableObjects) |
|
|
|
|
foreach (Draggable2D dragObj in draggableObjects) |
|
|
|
|
{ |
|
|
|
|
dragObj.RegisterHandler(this); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected virtual void OnDisable() |
|
|
|
|
{ |
|
|
|
|
if(Application.IsPlaying(this)) |
|
|
|
|
if (Application.IsPlaying(this)) |
|
|
|
|
{ |
|
|
|
|
eventDispatcher.RemoveListener<DragCompletedEvent>(OnDragCompletedEvent); |
|
|
|
|
eventDispatcher.RemoveListener<DragEntered.DragEnteredEvent>(OnDragEnteredEvent); |
|
|
|
|
eventDispatcher.RemoveListener<DragExited.DragExitedEvent>(OnDragExitedEvent); |
|
|
|
|
|
|
|
|
|
foreach(Draggable2D dragObj in draggableObjects) |
|
|
|
|
foreach (Draggable2D dragObj in draggableObjects) |
|
|
|
|
{ |
|
|
|
|
dragObj.UnregisterHandler(this); |
|
|
|
|
} |
|
|
|
@ -117,30 +89,56 @@ namespace Fungus
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void OnDragCompletedEvent(DragCompletedEvent evt) |
|
|
|
|
private void OnDragCompletedEvent(DragCompletedEvent evt) |
|
|
|
|
{ |
|
|
|
|
OnDragCompleted(evt.DraggableObject); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void OnDragEnteredEvent(DragEntered.DragEnteredEvent evt) |
|
|
|
|
private void OnDragEnteredEvent(DragEntered.DragEnteredEvent evt) |
|
|
|
|
{ |
|
|
|
|
OnDragEntered(evt.DraggableObject, evt.TargetCollider); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void OnDragExitedEvent(DragExited.DragExitedEvent evt) |
|
|
|
|
private void OnDragExitedEvent(DragExited.DragExitedEvent evt) |
|
|
|
|
{ |
|
|
|
|
OnDragExited(evt.DraggableObject, evt.TargetCollider); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#region Compatibility |
|
|
|
|
|
|
|
|
|
void ISerializationCallbackReceiver.OnAfterDeserialize() |
|
|
|
|
{ |
|
|
|
|
//presentl using awake due to errors on non main thread access of targetCollider |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void ISerializationCallbackReceiver.OnBeforeSerialize() |
|
|
|
|
{ |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void Awake() |
|
|
|
|
{ |
|
|
|
|
//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); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
draggableObject = null; |
|
|
|
|
targetObject = null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#endregion Compatibility |
|
|
|
|
|
|
|
|
|
#region Public members |
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
@ -161,7 +159,7 @@ namespace Fungus
|
|
|
|
|
/// </summary> |
|
|
|
|
public virtual void OnDragEntered(Draggable2D draggableObject, Collider2D targetObject) |
|
|
|
|
{ |
|
|
|
|
if (this.targetObjects != null && this.draggableObjects !=null && |
|
|
|
|
if (this.targetObjects != null && this.draggableObjects != null && |
|
|
|
|
this.draggableObjects.Contains(draggableObject) && |
|
|
|
|
this.targetObjects.Contains(targetObject)) |
|
|
|
|
{ |
|
|
|
@ -175,13 +173,12 @@ namespace Fungus
|
|
|
|
|
/// </summary> |
|
|
|
|
public virtual void OnDragExited(Draggable2D draggableObject, Collider2D targetObject) |
|
|
|
|
{ |
|
|
|
|
if (this.targetObjects != null && this.draggableObjects !=null && |
|
|
|
|
if (this.targetObjects != null && this.draggableObjects != null && |
|
|
|
|
this.draggableObjects.Contains(draggableObject) && |
|
|
|
|
this.targetObjects.Contains(targetObject)) |
|
|
|
|
{ |
|
|
|
|
overTarget = false; |
|
|
|
|
targetCollider = null; |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -196,11 +193,11 @@ namespace Fungus
|
|
|
|
|
// 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 |
|
|
|
|
// draggable object is set to be inactive. |
|
|
|
|
if(draggableRef!=null) |
|
|
|
|
if (draggableRef != null) |
|
|
|
|
{ |
|
|
|
|
draggableRef.Value = draggableObject.gameObject; |
|
|
|
|
} |
|
|
|
|
if(targetRef!=null) |
|
|
|
|
if (targetRef != null) |
|
|
|
|
{ |
|
|
|
|
targetRef.Value = targetCollider.gameObject; |
|
|
|
|
} |
|
|
|
@ -212,7 +209,6 @@ namespace Fungus
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override string GetSummary() |
|
|
|
|
{ |
|
|
|
|
string summary = "Draggable: "; |
|
|
|
@ -247,6 +243,6 @@ namespace Fungus
|
|
|
|
|
return summary; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#endregion |
|
|
|
|
#endregion Public members |
|
|
|
|
} |
|
|
|
|
} |