Browse Source

Add note to Drag handlers re compatibility

master
Steve Halliwell 5 years ago
parent
commit
0fb320f364
  1. 33
      Assets/Fungus/Scripts/EventHandlers/DragCancelled.cs
  2. 110
      Assets/Fungus/Scripts/EventHandlers/DragCompleted.cs
  3. 103
      Assets/Fungus/Scripts/EventHandlers/DragEntered.cs
  4. 95
      Assets/Fungus/Scripts/EventHandlers/DragExited.cs
  5. 31
      Assets/Fungus/Scripts/EventHandlers/DragStarted.cs
  6. 37
      Assets/FungusExamples/DragAndDrop/DragandDrop(DraggableObjectLists).unity

33
Assets/Fungus/Scripts/EventHandlers/DragCancelled.cs

@ -1,9 +1,8 @@
// This code is part of the Fungus library (https://github.com/snozbot/fungus) // 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) // 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 System.Collections.Generic;
using UnityEngine;
namespace Fungus namespace Fungus
{ {
@ -15,25 +14,26 @@ namespace Fungus
"The block will execute when the player drags an object and releases it without dropping it on a target object.")] "The block will execute when the player drags an object and releases it without dropping it on a target object.")]
[AddComponentMenu("")] [AddComponentMenu("")]
public class DragCancelled : EventHandler, ISerializationCallbackReceiver public class DragCancelled : EventHandler, ISerializationCallbackReceiver
{ {
public class DragCancelledEvent public class DragCancelledEvent
{ {
public Draggable2D DraggableObject; public Draggable2D DraggableObject;
public DragCancelledEvent(Draggable2D draggableObject) public DragCancelledEvent(Draggable2D draggableObject)
{ {
DraggableObject = draggableObject; DraggableObject = draggableObject;
} }
} }
[VariableProperty(typeof(GameObjectVariable))] [VariableProperty(typeof(GameObjectVariable))]
[SerializeField] protected GameObjectVariable draggableRef; [SerializeField] protected GameObjectVariable draggableRef;
[Tooltip("Draggable object to listen for drag events on")] [Tooltip("Draggable object to listen for drag events on")]
[SerializeField] protected List<Draggable2D> draggableObjects; [SerializeField] protected List<Draggable2D> draggableObjects;
[HideInInspector] [HideInInspector]
[SerializeField] protected Draggable2D draggableObject; [SerializeField] protected Draggable2D draggableObject;
protected EventDispatcher eventDispatcher; protected EventDispatcher eventDispatcher;
protected virtual void OnEnable() protected virtual void OnEnable()
@ -55,7 +55,9 @@ namespace Fungus
OnDragCancelled(evt.DraggableObject); OnDragCancelled(evt.DraggableObject);
} }
void ISerializationCallbackReceiver.OnAfterDeserialize() #region Compatibility
void ISerializationCallbackReceiver.OnAfterDeserialize()
{ {
//add any dragableobject already present to list for backwards compatability //add any dragableobject already present to list for backwards compatability
if (draggableObject != null) if (draggableObject != null)
@ -63,7 +65,6 @@ namespace Fungus
if (!draggableObjects.Contains(draggableObject)) if (!draggableObjects.Contains(draggableObject))
{ {
draggableObjects.Add(draggableObject); draggableObjects.Add(draggableObject);
} }
draggableObject = null; draggableObject = null;
} }
@ -71,25 +72,24 @@ namespace Fungus
void ISerializationCallbackReceiver.OnBeforeSerialize() void ISerializationCallbackReceiver.OnBeforeSerialize()
{ {
} }
#endregion Compatibility
#region Public members #region Public members
public virtual void OnDragCancelled(Draggable2D draggableObject) public virtual void OnDragCancelled(Draggable2D draggableObject)
{ {
if (draggableObjects.Contains(draggableObject)) if (draggableObjects.Contains(draggableObject))
{ {
if(draggableRef!=null) if (draggableRef != null)
{ {
draggableRef.Value = draggableObject.gameObject; draggableRef.Value = draggableObject.gameObject;
} }
ExecuteBlock(); ExecuteBlock();
} }
} }
public override string GetSummary() public override string GetSummary()
{ {
string summary = "Draggable: "; string summary = "Draggable: ";
@ -100,7 +100,7 @@ namespace Fungus
if (draggableObjects[i] != null) if (draggableObjects[i] != null)
{ {
summary += draggableObjects[i].name + ","; summary += draggableObjects[i].name + ",";
} }
} }
return summary; return summary;
} }
@ -108,9 +108,8 @@ namespace Fungus
{ {
return "None"; return "None";
} }
} }
#endregion #endregion Public members
} }
} }

110
Assets/Fungus/Scripts/EventHandlers/DragCompleted.cs

@ -1,14 +1,16 @@
// This code is part of the Fungus library (https://github.com/snozbot/fungus) // 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) // 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 System.Collections.Generic;
using UnityEngine;
namespace Fungus namespace Fungus
{ {
/// <summary> /// <summary>
/// The block will execute when the player drags an object and successfully drops it on a target object. /// 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> /// </summary>
[EventHandlerInfo("Sprite", [EventHandlerInfo("Sprite",
"Drag Completed", "Drag Completed",
@ -16,38 +18,35 @@ namespace Fungus
[AddComponentMenu("")] [AddComponentMenu("")]
[ExecuteAlways] [ExecuteAlways]
public class DragCompleted : EventHandler, ISerializationCallbackReceiver public class DragCompleted : EventHandler, ISerializationCallbackReceiver
{ {
public class DragCompletedEvent public class DragCompletedEvent
{ {
public Draggable2D DraggableObject; public Draggable2D DraggableObject;
public DragCompletedEvent(Draggable2D draggableObject) public DragCompletedEvent(Draggable2D draggableObject)
{ {
DraggableObject = draggableObject; DraggableObject = draggableObject;
} }
} }
[VariableProperty(typeof(GameObjectVariable))]
[VariableProperty(typeof(GameObjectVariable))]
[SerializeField] protected GameObjectVariable draggableRef; [SerializeField] protected GameObjectVariable draggableRef;
[VariableProperty(typeof(GameObjectVariable))] [VariableProperty(typeof(GameObjectVariable))]
[SerializeField] protected GameObjectVariable targetRef; [SerializeField] protected GameObjectVariable targetRef;
[Tooltip("Draggable object to listen for drag events on")] [Tooltip("Draggable object to listen for drag events on")]
[HideInInspector] [HideInInspector]
[SerializeField] protected Draggable2D draggableObject; [SerializeField] protected Draggable2D draggableObject;
[SerializeField] protected List<Draggable2D> draggableObjects; [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] [HideInInspector]
[SerializeField] protected Collider2D targetObject; [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 // 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;
@ -56,59 +55,32 @@ namespace Fungus
protected EventDispatcher eventDispatcher; 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() protected virtual void OnEnable()
{ {
if(Application.IsPlaying(this)){ if (Application.IsPlaying(this))
{
eventDispatcher = FungusManager.Instance.EventDispatcher; eventDispatcher = FungusManager.Instance.EventDispatcher;
eventDispatcher.AddListener<DragCompletedEvent>(OnDragCompletedEvent); eventDispatcher.AddListener<DragCompletedEvent>(OnDragCompletedEvent);
eventDispatcher.AddListener<DragEntered.DragEnteredEvent>(OnDragEnteredEvent); eventDispatcher.AddListener<DragEntered.DragEnteredEvent>(OnDragEnteredEvent);
eventDispatcher.AddListener<DragExited.DragExitedEvent>(OnDragExitedEvent); eventDispatcher.AddListener<DragExited.DragExitedEvent>(OnDragExitedEvent);
foreach(Draggable2D dragObj in draggableObjects) foreach (Draggable2D dragObj in draggableObjects)
{ {
dragObj.RegisterHandler(this); dragObj.RegisterHandler(this);
} }
} }
} }
protected virtual void OnDisable() protected virtual void OnDisable()
{ {
if(Application.IsPlaying(this)) if (Application.IsPlaying(this))
{ {
eventDispatcher.RemoveListener<DragCompletedEvent>(OnDragCompletedEvent); eventDispatcher.RemoveListener<DragCompletedEvent>(OnDragCompletedEvent);
eventDispatcher.RemoveListener<DragEntered.DragEnteredEvent>(OnDragEnteredEvent); eventDispatcher.RemoveListener<DragEntered.DragEnteredEvent>(OnDragEnteredEvent);
eventDispatcher.RemoveListener<DragExited.DragExitedEvent>(OnDragExitedEvent); eventDispatcher.RemoveListener<DragExited.DragExitedEvent>(OnDragExitedEvent);
foreach(Draggable2D dragObj in draggableObjects) foreach (Draggable2D dragObj in draggableObjects)
{ {
dragObj.UnregisterHandler(this); dragObj.UnregisterHandler(this);
} }
@ -117,30 +89,56 @@ namespace Fungus
} }
} }
void OnDragCompletedEvent(DragCompletedEvent evt) private void OnDragCompletedEvent(DragCompletedEvent evt)
{ {
OnDragCompleted(evt.DraggableObject); OnDragCompleted(evt.DraggableObject);
} }
void OnDragEnteredEvent(DragEntered.DragEnteredEvent evt) private void OnDragEnteredEvent(DragEntered.DragEnteredEvent evt)
{ {
OnDragEntered(evt.DraggableObject, evt.TargetCollider); OnDragEntered(evt.DraggableObject, evt.TargetCollider);
} }
void OnDragExitedEvent(DragExited.DragExitedEvent evt) private void OnDragExitedEvent(DragExited.DragExitedEvent evt)
{ {
OnDragExited(evt.DraggableObject, evt.TargetCollider); OnDragExited(evt.DraggableObject, evt.TargetCollider);
} }
#region Compatibility
void ISerializationCallbackReceiver.OnAfterDeserialize() void ISerializationCallbackReceiver.OnAfterDeserialize()
{ {
//presentl using awake due to errors on non main thread access of targetCollider
} }
void ISerializationCallbackReceiver.OnBeforeSerialize() 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 #region Public members
/// <summary> /// <summary>
@ -161,7 +159,7 @@ namespace Fungus
/// </summary> /// </summary>
public virtual void OnDragEntered(Draggable2D draggableObject, Collider2D targetObject) 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.draggableObjects.Contains(draggableObject) &&
this.targetObjects.Contains(targetObject)) this.targetObjects.Contains(targetObject))
{ {
@ -169,22 +167,21 @@ namespace Fungus
targetCollider = targetObject; targetCollider = targetObject;
} }
} }
/// <summary> /// <summary>
/// Called by the Draggable2D object when the it exits the drag target. /// Called by the Draggable2D object when the it exits the drag target.
/// </summary> /// </summary>
public virtual void OnDragExited(Draggable2D draggableObject, Collider2D targetObject) 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.draggableObjects.Contains(draggableObject) &&
this.targetObjects.Contains(targetObject)) this.targetObjects.Contains(targetObject))
{ {
overTarget = false; overTarget = false;
targetCollider = null; targetCollider = null;
} }
} }
/// <summary> /// <summary>
/// Called by the Draggable2D object when the the drag ends over the drag target. /// Called by the Draggable2D object when the the drag ends over the drag target.
/// </summary> /// </summary>
@ -196,11 +193,11 @@ namespace Fungus
// 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.
if(draggableRef!=null) if (draggableRef != null)
{ {
draggableRef.Value = draggableObject.gameObject; draggableRef.Value = draggableObject.gameObject;
} }
if(targetRef!=null) if (targetRef != null)
{ {
targetRef.Value = targetCollider.gameObject; targetRef.Value = targetCollider.gameObject;
} }
@ -212,7 +209,6 @@ namespace Fungus
} }
} }
public override string GetSummary() public override string GetSummary()
{ {
string summary = "Draggable: "; string summary = "Draggable: ";
@ -223,7 +219,7 @@ namespace Fungus
if (draggableObjects[i] != null) if (draggableObjects[i] != null)
{ {
summary += draggableObjects[i].name + ","; summary += draggableObjects[i].name + ",";
} }
} }
} }
@ -235,7 +231,7 @@ namespace Fungus
if (targetObjects[i] != null) if (targetObjects[i] != null)
{ {
summary += targetObjects[i].name + ","; summary += targetObjects[i].name + ",";
} }
} }
} }
@ -247,6 +243,6 @@ namespace Fungus
return summary; return summary;
} }
#endregion #endregion Public members
} }
} }

103
Assets/Fungus/Scripts/EventHandlers/DragEntered.cs

@ -1,53 +1,92 @@
// This code is part of the Fungus library (https://github.com/snozbot/fungus) // 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) // It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE)
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine;
namespace Fungus namespace Fungus
{ {
/// <summary> /// <summary>
/// The block will execute when the player is dragging an object which starts touching the target object. /// The block will execute when the player is dragging an object which starts touching the 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> /// </summary>
[EventHandlerInfo("Sprite", [EventHandlerInfo("Sprite",
"Drag Entered", "Drag Entered",
"The block will execute when the player is dragging an object which starts touching the target object.")] "The block will execute when the player is dragging an object which starts touching the target object.")]
[AddComponentMenu("")] [AddComponentMenu("")]
[ExecuteAlways] [ExecuteAlways]
public class DragEntered : EventHandler, ISerializationCallbackReceiver public class DragEntered : EventHandler, ISerializationCallbackReceiver
{ {
public class DragEnteredEvent public class DragEnteredEvent
{ {
public Draggable2D DraggableObject; public Draggable2D DraggableObject;
public Collider2D TargetCollider; public Collider2D TargetCollider;
public DragEnteredEvent(Draggable2D draggableObject, Collider2D targetCollider) public DragEnteredEvent(Draggable2D draggableObject, Collider2D targetCollider)
{ {
DraggableObject = draggableObject; DraggableObject = draggableObject;
TargetCollider = targetCollider; TargetCollider = targetCollider;
} }
} }
[VariableProperty(typeof(GameObjectVariable))] [VariableProperty(typeof(GameObjectVariable))]
[SerializeField] protected GameObjectVariable draggableRef; [SerializeField] protected GameObjectVariable draggableRef;
[VariableProperty(typeof(GameObjectVariable))] [VariableProperty(typeof(GameObjectVariable))]
[SerializeField] protected GameObjectVariable targetRef; [SerializeField] protected GameObjectVariable targetRef;
[Tooltip("Draggable object to listen for drag events on")] [Tooltip("Draggable object to listen for drag events on")]
[HideInInspector] [HideInInspector]
[SerializeField] protected Draggable2D draggableObject; [SerializeField] protected Draggable2D draggableObject;
[SerializeField] protected List<Draggable2D> draggableObjects; [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] [HideInInspector]
[SerializeField] protected Collider2D targetObject; [SerializeField] protected Collider2D targetObject;
[SerializeField] protected List<Collider2D> targetObjects;
[SerializeField] protected List<Collider2D> targetObjects;
protected EventDispatcher eventDispatcher; protected EventDispatcher eventDispatcher;
void Awake() protected virtual void OnEnable()
{
if (Application.IsPlaying(this))
{
eventDispatcher = FungusManager.Instance.EventDispatcher;
eventDispatcher.AddListener<DragEnteredEvent>(OnDragEnteredEvent);
}
}
protected virtual void OnDisable()
{
if (Application.IsPlaying(this))
{
eventDispatcher.RemoveListener<DragEnteredEvent>(OnDragEnteredEvent);
eventDispatcher = null;
}
}
private void OnDragEnteredEvent(DragEnteredEvent evt)
{
OnDragEntered(evt.DraggableObject, evt.TargetCollider);
}
#region Compatibility
void ISerializationCallbackReceiver.OnAfterDeserialize()
{
}
void ISerializationCallbackReceiver.OnBeforeSerialize()
{
}
private void Awake()
{ {
//add any dragableobject already present to list for backwards compatability //add any dragableobject already present to list for backwards compatability
if (draggableObject != null) if (draggableObject != null)
@ -67,32 +106,10 @@ namespace Fungus
} }
draggableObject = null; draggableObject = null;
targetObject = null; targetObject = null;
} }
protected virtual void OnEnable() #endregion Compatibility
{
if(Application.IsPlaying(this)){
eventDispatcher = FungusManager.Instance.EventDispatcher;
eventDispatcher.AddListener<DragEnteredEvent>(OnDragEnteredEvent);
}
}
protected virtual void OnDisable()
{
if(Application.IsPlaying(this)){
eventDispatcher.RemoveListener<DragEnteredEvent>(OnDragEnteredEvent);
eventDispatcher = null;
}
}
void OnDragEnteredEvent(DragEnteredEvent evt)
{
OnDragEntered(evt.DraggableObject, evt.TargetCollider);
}
#region Public members #region Public members
/// <summary> /// <summary>
@ -100,15 +117,15 @@ namespace Fungus
/// </summary> /// </summary>
public virtual void OnDragEntered(Draggable2D draggableObject, Collider2D targetObject) 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.draggableObjects.Contains(draggableObject) &&
this.targetObjects.Contains(targetObject)) this.targetObjects.Contains(targetObject))
{ {
if(draggableRef!=null) if (draggableRef != null)
{ {
draggableRef.Value = draggableObject.gameObject; draggableRef.Value = draggableObject.gameObject;
} }
if(targetRef!=null) if (targetRef != null)
{ {
targetRef.Value = targetObject.gameObject; targetRef.Value = targetObject.gameObject;
} }
@ -116,16 +133,6 @@ namespace Fungus
} }
} }
void ISerializationCallbackReceiver.OnAfterDeserialize()
{
}
void ISerializationCallbackReceiver.OnBeforeSerialize()
{
}
public override string GetSummary() public override string GetSummary()
{ {
string summary = "Draggable: "; string summary = "Draggable: ";
@ -136,7 +143,7 @@ namespace Fungus
if (draggableObjects[i] != null) if (draggableObjects[i] != null)
{ {
summary += draggableObjects[i].name + ","; summary += draggableObjects[i].name + ",";
} }
} }
} }
@ -148,7 +155,7 @@ namespace Fungus
if (targetObjects[i] != null) if (targetObjects[i] != null)
{ {
summary += targetObjects[i].name + ","; summary += targetObjects[i].name + ",";
} }
} }
} }
@ -160,6 +167,6 @@ namespace Fungus
return summary; return summary;
} }
#endregion #endregion Public members
} }
} }

95
Assets/Fungus/Scripts/EventHandlers/DragExited.cs

@ -1,27 +1,29 @@
// This code is part of the Fungus library (https://github.com/snozbot/fungus) // 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) // 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 System.Collections.Generic;
using UnityEngine;
namespace Fungus namespace Fungus
{ {
/// <summary> /// <summary>
/// The block will execute when the player is dragging an object which stops touching the target object. /// The block will execute when the player is dragging an object which stops touching the 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> /// </summary>
[EventHandlerInfo("Sprite", [EventHandlerInfo("Sprite",
"Drag Exited", "Drag Exited",
"The block will execute when the player is dragging an object which stops touching the target object.")] "The block will execute when the player is dragging an object which stops touching the target object.")]
[AddComponentMenu("")] [AddComponentMenu("")]
[ExecuteAlways] [ExecuteAlways]
public class DragExited : EventHandler, ISerializationCallbackReceiver public class DragExited : EventHandler, ISerializationCallbackReceiver
{ {
public class DragExitedEvent public class DragExitedEvent
{ {
public Draggable2D DraggableObject; public Draggable2D DraggableObject;
public Collider2D TargetCollider; public Collider2D TargetCollider;
public DragExitedEvent(Draggable2D draggableObject, Collider2D targetCollider) public DragExitedEvent(Draggable2D draggableObject, Collider2D targetCollider)
{ {
DraggableObject = draggableObject; DraggableObject = draggableObject;
@ -31,85 +33,83 @@ namespace Fungus
[VariableProperty(typeof(GameObjectVariable))] [VariableProperty(typeof(GameObjectVariable))]
[SerializeField] protected GameObjectVariable draggableRef; [SerializeField] protected GameObjectVariable draggableRef;
[VariableProperty(typeof(GameObjectVariable))] [VariableProperty(typeof(GameObjectVariable))]
[SerializeField] protected GameObjectVariable targetRef; [SerializeField] protected GameObjectVariable targetRef;
[Tooltip("Draggable object to listen for drag events on")] [Tooltip("Draggable object to listen for drag events on")]
[HideInInspector] [HideInInspector]
[SerializeField] protected Draggable2D draggableObject; [SerializeField] protected Draggable2D draggableObject;
[SerializeField] protected List<Draggable2D> draggableObjects; [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] [HideInInspector]
[SerializeField] protected Collider2D targetObject; [SerializeField] protected Collider2D targetObject;
[SerializeField] protected List<Collider2D> targetObjects;
[SerializeField] protected List<Collider2D> targetObjects;
protected EventDispatcher eventDispatcher; 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() protected virtual void OnEnable()
{ {
if(Application.IsPlaying(this)){ if (Application.IsPlaying(this))
{
eventDispatcher = FungusManager.Instance.EventDispatcher; eventDispatcher = FungusManager.Instance.EventDispatcher;
eventDispatcher.AddListener<DragExitedEvent>(OnDragEnteredEvent); eventDispatcher.AddListener<DragExitedEvent>(OnDragEnteredEvent);
} }
} }
protected virtual void OnDisable() protected virtual void OnDisable()
{ {
if(Application.IsPlaying(this)){ if (Application.IsPlaying(this))
eventDispatcher.RemoveListener<DragExitedEvent>(OnDragEnteredEvent); {
eventDispatcher.RemoveListener<DragExitedEvent>(OnDragEnteredEvent);
eventDispatcher = null; eventDispatcher = null;
} }
} }
void OnDragEnteredEvent(DragExitedEvent evt) private void OnDragEnteredEvent(DragExitedEvent evt)
{ {
OnDragExited(evt.DraggableObject, evt.TargetCollider); OnDragExited(evt.DraggableObject, evt.TargetCollider);
} }
#region Compatibility
void ISerializationCallbackReceiver.OnAfterDeserialize() void ISerializationCallbackReceiver.OnAfterDeserialize()
{ {
} }
void ISerializationCallbackReceiver.OnBeforeSerialize() 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 #region Public members
/// <summary> /// <summary>
@ -117,15 +117,15 @@ namespace Fungus
/// </summary> /// </summary>
public virtual void OnDragExited(Draggable2D draggableObject, Collider2D targetObject) 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.draggableObjects.Contains(draggableObject) &&
this.targetObjects.Contains(targetObject)) this.targetObjects.Contains(targetObject))
{ {
if(draggableRef!=null) if (draggableRef != null)
{ {
draggableRef.Value = draggableObject.gameObject; draggableRef.Value = draggableObject.gameObject;
} }
if(targetRef!=null) if (targetRef != null)
{ {
targetRef.Value = targetObject.gameObject; targetRef.Value = targetObject.gameObject;
} }
@ -133,7 +133,6 @@ namespace Fungus
} }
} }
public override string GetSummary() public override string GetSummary()
{ {
string summary = "Draggable: "; string summary = "Draggable: ";
@ -144,7 +143,7 @@ namespace Fungus
if (draggableObjects[i] != null) if (draggableObjects[i] != null)
{ {
summary += draggableObjects[i].name + ","; summary += draggableObjects[i].name + ",";
} }
} }
} }
@ -156,7 +155,7 @@ namespace Fungus
if (targetObjects[i] != null) if (targetObjects[i] != null)
{ {
summary += targetObjects[i].name + ","; summary += targetObjects[i].name + ",";
} }
} }
} }
@ -168,6 +167,6 @@ namespace Fungus
return summary; return summary;
} }
#endregion #endregion Public members
} }
} }

31
Assets/Fungus/Scripts/EventHandlers/DragStarted.cs

@ -1,8 +1,8 @@
// This code is part of the Fungus library (https://github.com/snozbot/fungus) // 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) // 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 System.Collections.Generic;
using UnityEngine;
namespace Fungus namespace Fungus
{ {
@ -14,26 +14,27 @@ namespace Fungus
"The block will execute when the player starts dragging an object.")] "The block will execute when the player starts dragging an object.")]
[AddComponentMenu("")] [AddComponentMenu("")]
public class DragStarted : EventHandler, ISerializationCallbackReceiver public class DragStarted : EventHandler, ISerializationCallbackReceiver
{ {
public class DragStartedEvent public class DragStartedEvent
{ {
public Draggable2D DraggableObject; public Draggable2D DraggableObject;
public DragStartedEvent(Draggable2D draggableObject) public DragStartedEvent(Draggable2D draggableObject)
{ {
DraggableObject = draggableObject; DraggableObject = draggableObject;
} }
} }
[VariableProperty(typeof(GameObjectVariable))] [VariableProperty(typeof(GameObjectVariable))]
[SerializeField] protected GameObjectVariable draggableRef; [SerializeField] protected GameObjectVariable draggableRef;
[SerializeField] protected List<Draggable2D> draggableObjects; [SerializeField] protected List<Draggable2D> draggableObjects;
[HideInInspector] [HideInInspector]
[SerializeField] protected Draggable2D draggableObject; [SerializeField] protected Draggable2D draggableObject;
protected EventDispatcher eventDispatcher; protected EventDispatcher eventDispatcher;
protected virtual void OnEnable() protected virtual void OnEnable()
{ {
eventDispatcher = FungusManager.Instance.EventDispatcher; eventDispatcher = FungusManager.Instance.EventDispatcher;
@ -41,8 +42,6 @@ namespace Fungus
eventDispatcher.AddListener<DragStartedEvent>(OnDragStartedEvent); eventDispatcher.AddListener<DragStartedEvent>(OnDragStartedEvent);
} }
protected virtual void OnDisable() protected virtual void OnDisable()
{ {
eventDispatcher.RemoveListener<DragStartedEvent>(OnDragStartedEvent); eventDispatcher.RemoveListener<DragStartedEvent>(OnDragStartedEvent);
@ -50,11 +49,13 @@ namespace Fungus
eventDispatcher = null; eventDispatcher = null;
} }
void OnDragStartedEvent(DragStartedEvent evt) private void OnDragStartedEvent(DragStartedEvent evt)
{ {
OnDragStarted(evt.DraggableObject); OnDragStarted(evt.DraggableObject);
} }
#region Compatibility
void ISerializationCallbackReceiver.OnAfterDeserialize() void ISerializationCallbackReceiver.OnAfterDeserialize()
{ {
//add any dragableobject already present to list for backwards compatability //add any dragableobject already present to list for backwards compatability
@ -63,7 +64,6 @@ namespace Fungus
if (!draggableObjects.Contains(draggableObject)) if (!draggableObjects.Contains(draggableObject))
{ {
draggableObjects.Add(draggableObject); draggableObjects.Add(draggableObject);
} }
draggableObject = null; draggableObject = null;
} }
@ -71,9 +71,10 @@ namespace Fungus
void ISerializationCallbackReceiver.OnBeforeSerialize() void ISerializationCallbackReceiver.OnBeforeSerialize()
{ {
} }
#endregion Compatibility
#region Public members #region Public members
/// <summary> /// <summary>
@ -83,12 +84,12 @@ namespace Fungus
{ {
if (draggableObjects.Contains(draggableObject)) if (draggableObjects.Contains(draggableObject))
{ {
if(draggableRef!=null) if (draggableRef != null)
{ {
draggableRef.Value = draggableObject.gameObject; draggableRef.Value = draggableObject.gameObject;
} }
ExecuteBlock(); ExecuteBlock();
} }
} }
public override string GetSummary() public override string GetSummary()
@ -101,7 +102,7 @@ namespace Fungus
if (draggableObjects[i] != null) if (draggableObjects[i] != null)
{ {
summary += draggableObjects[i].name + ","; summary += draggableObjects[i].name + ",";
} }
} }
} }
@ -110,9 +111,9 @@ namespace Fungus
return "None"; return "None";
} }
return summary; return summary;
} }
#endregion #endregion Public members
} }
} }

37
Assets/FungusExamples/DragAndDrop/DragandDrop(DraggableObjectLists).unity

@ -993,9 +993,9 @@ MonoBehaviour:
m_EditorClassIdentifier: m_EditorClassIdentifier:
nodeRect: nodeRect:
serializedVersion: 2 serializedVersion: 2
x: 26.67772 x: 140.13919
y: 43.14051 y: 14.294373
width: 402.06586 width: 405
height: 40 height: 40
tint: {r: 1, g: 1, b: 1, a: 1} tint: {r: 1, g: 1, b: 1, a: 1}
useCustomTint: 0 useCustomTint: 0
@ -1019,18 +1019,19 @@ MonoBehaviour:
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
version: 1 version: 1
scrollPos: {x: 550.1149, y: 176.97496} scrollPos: {x: 22.411636, y: 147.07748}
variablesScrollPos: {x: 0, y: 0} variablesScrollPos: {x: 0, y: 0}
variablesExpanded: 1 variablesExpanded: 1
blockViewHeight: 501 blockViewHeight: 501
zoom: 0.48400036 zoom: 1
scrollViewRect: scrollViewRect:
serializedVersion: 2 serializedVersion: 2
x: -343 x: -343
y: -340 y: -340
width: 1114 width: 1114
height: 859 height: 859
selectedBlocks: [] selectedBlocks:
- {fileID: 1077498089}
selectedCommands: [] selectedCommands: []
variables: variables:
- {fileID: 1077498086} - {fileID: 1077498086}
@ -1157,9 +1158,9 @@ MonoBehaviour:
m_EditorClassIdentifier: m_EditorClassIdentifier:
nodeRect: nodeRect:
serializedVersion: 2 serializedVersion: 2
x: 386.49557 x: 126.88034
y: 195.74385 y: 230.35919
width: 402.06586 width: 405
height: 40 height: 40
tint: {r: 1, g: 1, b: 1, a: 1} tint: {r: 1, g: 1, b: 1, a: 1}
useCustomTint: 0 useCustomTint: 0
@ -1286,9 +1287,9 @@ MonoBehaviour:
m_EditorClassIdentifier: m_EditorClassIdentifier:
nodeRect: nodeRect:
serializedVersion: 2 serializedVersion: 2
x: 609.26447 x: 116.95709
y: 92.36371 y: 148.13286
width: 402.06586 width: 405
height: 40 height: 40
tint: {r: 1, g: 1, b: 1, a: 1} tint: {r: 1, g: 1, b: 1, a: 1}
useCustomTint: 0 useCustomTint: 0
@ -1313,9 +1314,9 @@ MonoBehaviour:
m_EditorClassIdentifier: m_EditorClassIdentifier:
nodeRect: nodeRect:
serializedVersion: 2 serializedVersion: 2
x: -68.13205 x: 58.790962
y: 152.53719 y: 94.84494
width: 402.06586 width: 405
height: 40 height: 40
tint: {r: 1, g: 1, b: 1, a: 1} tint: {r: 1, g: 1, b: 1, a: 1}
useCustomTint: 0 useCustomTint: 0
@ -1388,9 +1389,9 @@ MonoBehaviour:
m_EditorClassIdentifier: m_EditorClassIdentifier:
nodeRect: nodeRect:
serializedVersion: 2 serializedVersion: 2
x: 434.23154 x: 122.69321
y: -11.975143 y: -67.74434
width: 402.06586 width: 405
height: 40 height: 40
tint: {r: 1, g: 1, b: 1, a: 1} tint: {r: 1, g: 1, b: 1, a: 1}
useCustomTint: 0 useCustomTint: 0

Loading…
Cancel
Save