Browse Source

#228 added list support in drag event handlers

master
vjs22334 5 years ago
parent
commit
1175913375
  1. 2
      Assets/Fungus/Scripts/Components/Draggable2D.cs
  2. 27
      Assets/Fungus/Scripts/EventHandlers/DragCancelled.cs
  3. 89
      Assets/Fungus/Scripts/EventHandlers/DragCompleted.cs
  4. 55
      Assets/Fungus/Scripts/EventHandlers/DragEntered.cs
  5. 57
      Assets/Fungus/Scripts/EventHandlers/DragExited.cs
  6. 37
      Assets/Fungus/Scripts/EventHandlers/DragStarted.cs
  7. 1349
      Assets/dragable_test.unity
  8. 7
      Assets/dragable_test.unity.meta

2
Assets/Fungus/Scripts/Components/Draggable2D.cs

@ -137,7 +137,7 @@ namespace Fungus
for (int i = 0; i < dragCompletedHandlers.Count; i++) for (int i = 0; i < dragCompletedHandlers.Count; i++)
{ {
var handler = dragCompletedHandlers[i]; var handler = dragCompletedHandlers[i];
if (handler != null && handler.DraggableObject == this) if (handler != null && handler.DraggableObjects.Contains(this))
{ {
if (handler.IsOverTarget()) if (handler.IsOverTarget())
{ {

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

@ -24,10 +24,24 @@ 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")]
[SerializeField] protected List<Draggable2D> draggableObjects; [SerializeField] protected List<Draggable2D> draggableObjects;
[HideInInspector]
[SerializeField] protected Draggable2D draggableObject;
void OnValidate()
{
//add any dragableobject already present to list for backwards compatability
if(draggableObject!=null){
if(!draggableObjects.Contains(draggableObject)){
draggableObjects.Add(draggableObject);
}
}
}
protected EventDispatcher eventDispatcher; protected EventDispatcher eventDispatcher;
protected virtual void OnEnable() protected virtual void OnEnable()
@ -53,19 +67,16 @@ namespace Fungus
public virtual void OnDragCancelled(Draggable2D draggableObject) public virtual void OnDragCancelled(Draggable2D draggableObject)
{ {
for (int i = 0; i < this.draggableObjects.Count; i++) if (draggableObjects.Contains(draggableObject))
{ {
if (draggableObject == this.draggableObjects[i]) draggableRef.Set<GameObject>(draggableObject.gameObject);
{ ExecuteBlock();
ExecuteBlock();
}
} }
} }
public override string GetSummary() public override string GetSummary()
{ {
string summary = "Dragable: "; string summary = "Draggable: ";
if (this.draggableObjects != null && this.draggableObjects.Count != 0) if (this.draggableObjects != null && this.draggableObjects.Count != 0)
{ {
for (int i = 0; i < this.draggableObjects.Count; i++) for (int i = 0; i < this.draggableObjects.Count; i++)

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

@ -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,13 +160,17 @@ 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();
} }
@ -137,14 +178,28 @@ namespace Fungus
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)

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

@ -27,11 +27,35 @@ namespace Fungus
TargetCollider = targetCollider; TargetCollider = targetCollider;
} }
} }
[SerializeField] protected VariableReference draggableRef;
[SerializeField] protected VariableReference targetRef;
[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;
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);
}
}
}
protected EventDispatcher eventDispatcher; protected EventDispatcher eventDispatcher;
@ -60,23 +84,40 @@ namespace Fungus
/// </summary> /// </summary>
public virtual void OnDragEntered(Draggable2D draggableObject, Collider2D targetObject) public virtual void OnDragEntered(Draggable2D draggableObject, Collider2D targetObject)
{ {
if (draggableObject == this.draggableObject && if (this.targetObjects != null && this.draggableObjects !=null &&
targetObject == this.targetObject) this.draggableObjects.Contains(draggableObject) &&
this.targetObjects.Contains(targetObject))
{ {
draggableRef.Set<GameObject>(draggableObject.gameObject);
targetRef.Set<GameObject>(targetObject.gameObject);
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)

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

@ -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
{ {
@ -25,11 +27,35 @@ namespace Fungus
} }
} }
[SerializeField] protected VariableReference draggableRef;
[SerializeField] protected VariableReference targetRef;
[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;
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);
}
}
}
protected EventDispatcher eventDispatcher; protected EventDispatcher eventDispatcher;
@ -59,23 +85,40 @@ namespace Fungus
/// </summary> /// </summary>
public virtual void OnDragExited(Draggable2D draggableObject, Collider2D targetObject) public virtual void OnDragExited(Draggable2D draggableObject, Collider2D targetObject)
{ {
if (draggableObject == this.draggableObject && if (this.targetObjects != null && this.draggableObjects !=null &&
targetObject == this.targetObject) this.draggableObjects.Contains(draggableObject) &&
this.targetObjects.Contains(targetObject))
{ {
draggableRef.Set<GameObject>(draggableObject.gameObject);
targetRef.Set<GameObject>(targetObject.gameObject);
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)

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

@ -23,9 +23,13 @@ namespace Fungus
DraggableObject = draggableObject; DraggableObject = draggableObject;
} }
} }
[SerializeField] protected VariableReference draggableRef;
[SerializeField] protected List<Draggable2D> draggableObjects; [SerializeField] protected List<Draggable2D> draggableObjects;
[HideInInspector]
[SerializeField] protected Draggable2D draggableObject;
protected EventDispatcher eventDispatcher; protected EventDispatcher eventDispatcher;
@ -36,6 +40,20 @@ namespace Fungus
eventDispatcher.AddListener<DragStartedEvent>(OnDragStartedEvent); eventDispatcher.AddListener<DragStartedEvent>(OnDragStartedEvent);
} }
/// <summary>
/// Called when the script is loaded or a value is changed in the
/// inspector (Called in the editor only).
/// </summary>
void OnValidate()
{
//add any dragableobject already present to list for backwards compatability
if(draggableObject!=null){
if(!draggableObjects.Contains(draggableObject)){
draggableObjects.Add(draggableObject);
}
}
}
protected virtual void OnDisable() protected virtual void OnDisable()
{ {
eventDispatcher.RemoveListener<DragStartedEvent>(OnDragStartedEvent); eventDispatcher.RemoveListener<DragStartedEvent>(OnDragStartedEvent);
@ -55,20 +73,16 @@ namespace Fungus
/// </summary> /// </summary>
public virtual void OnDragStarted(Draggable2D draggableObject) public virtual void OnDragStarted(Draggable2D draggableObject)
{ {
for (int i = 0; i < this.draggableObjects.Count; i++) if (draggableObjects.Contains(draggableObject))
{ {
if (draggableObject == this.draggableObjects[i]) draggableRef.Set<GameObject>(draggableObject.gameObject);
{ ExecuteBlock();
ExecuteBlock();
}
} }
} }
public override string GetSummary() public override string GetSummary()
{ {
string summary = "Dragable: "; string summary = "Draggable: ";
if (this.draggableObjects != null && this.draggableObjects.Count != 0) if (this.draggableObjects != null && this.draggableObjects.Count != 0)
{ {
for (int i = 0; i < this.draggableObjects.Count; i++) for (int i = 0; i < this.draggableObjects.Count; i++)
@ -78,13 +92,14 @@ namespace Fungus
summary += draggableObjects[i].name + ","; summary += draggableObjects[i].name + ",";
} }
} }
return summary;
} }
else
if (summary.Length == 0)
{ {
return "None"; return "None";
} }
return summary;
} }
#endregion #endregion

1349
Assets/dragable_test.unity

File diff suppressed because it is too large Load Diff

7
Assets/dragable_test.unity.meta

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: a8833e6d8e2df434a83b8d9a29907351
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
Loading…
Cancel
Save