Browse Source

Merge pull request #791 from SarveshBajaj/dragable_events_array

Draggable events array
master
Steve Halliwell 5 years ago committed by GitHub
parent
commit
573620bcac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      .gitignore
  2. 2
      Assets/Fungus/Scripts/Components/Draggable2D.cs
  3. 58
      Assets/Fungus/Scripts/EventHandlers/DragCancelled.cs
  4. 152
      Assets/Fungus/Scripts/EventHandlers/DragCompleted.cs
  5. 101
      Assets/Fungus/Scripts/EventHandlers/DragEntered.cs
  6. 110
      Assets/Fungus/Scripts/EventHandlers/DragExited.cs
  7. 58
      Assets/Fungus/Scripts/EventHandlers/DragStarted.cs
  8. 1634
      Assets/FungusExamples/DragAndDrop/DragandDrop(DraggableObjectLists).unity
  9. 7
      Assets/FungusExamples/DragAndDrop/DragandDrop(DraggableObjectLists).unity.meta
  10. 2298
      Assets/FungusExamples/TheHunter/TheHunter.unity
  11. 4
      ProjectSettings/ProjectVersion.txt

1
.gitignore vendored

@ -17,3 +17,4 @@ UWP/*
Assets/Plugins/
Assets/Plugins.meta
Logs/*
.vscode/launch.json

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

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

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

@ -2,6 +2,8 @@
// 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;
namespace Fungus
{
@ -12,7 +14,7 @@ namespace Fungus
"Drag Cancelled",
"The block will execute when the player drags an object and releases it without dropping it on a target object.")]
[AddComponentMenu("")]
public class DragCancelled : EventHandler
public class DragCancelled : EventHandler, ISerializationCallbackReceiver
{
public class DragCancelledEvent
{
@ -22,10 +24,16 @@ namespace Fungus
DraggableObject = draggableObject;
}
}
[VariableProperty(typeof(GameObjectVariable))]
[SerializeField] protected GameObjectVariable draggableRef;
[Tooltip("Draggable object to listen for drag events on")]
[SerializeField] protected List<Draggable2D> draggableObjects;
[HideInInspector]
[SerializeField] protected Draggable2D draggableObject;
protected EventDispatcher eventDispatcher;
protected virtual void OnEnable()
@ -45,26 +53,62 @@ namespace Fungus
protected virtual void OnDragCancelledEvent(DragCancelledEvent evt)
{
OnDragCancelled(evt.DraggableObject);
}
void ISerializationCallbackReceiver.OnAfterDeserialize()
{
//add any dragableobject already present to list for backwards compatability
if (draggableObject != null)
{
if (!draggableObjects.Contains(draggableObject))
{
draggableObjects.Add(draggableObject);
}
draggableObject = null;
}
}
void ISerializationCallbackReceiver.OnBeforeSerialize()
{
}
#region Public members
public virtual void OnDragCancelled(Draggable2D draggableObject)
{
if (draggableObject == this.draggableObject)
if (draggableObjects.Contains(draggableObject))
{
if(draggableRef!=null)
{
draggableRef.Value = draggableObject.gameObject;
}
ExecuteBlock();
}
}
}
public override string GetSummary()
{
if (draggableObject != null)
string summary = "Draggable: ";
if (this.draggableObjects != null && this.draggableObjects.Count != 0)
{
for (int i = 0; i < this.draggableObjects.Count; i++)
{
if (draggableObjects[i] != null)
{
summary += draggableObjects[i].name + ",";
}
}
return summary;
}
else
{
return draggableObject.name;
return "None";
}
return "None";
}
#endregion

152
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)
using UnityEngine;
using System.Collections.Generic;
namespace Fungus
{
@ -12,7 +14,8 @@ namespace Fungus
"Drag Completed",
"The block will execute when the player drags an object and successfully drops it on a target object.")]
[AddComponentMenu("")]
public class DragCompleted : EventHandler
[ExecuteAlways]
public class DragCompleted : EventHandler, ISerializationCallbackReceiver
{
public class DragCompletedEvent
{
@ -22,44 +25,96 @@ namespace Fungus
DraggableObject = draggableObject;
}
}
[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;
[SerializeField] protected List<Draggable2D> draggableObjects;
[Tooltip("Drag target object to listen for drag events on")]
[HideInInspector]
[SerializeField] protected Collider2D targetObject;
[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.
protected bool overTarget = false;
protected Collider2D targetCollider = null;
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()
{
eventDispatcher = FungusManager.Instance.EventDispatcher;
if(Application.IsPlaying(this)){
eventDispatcher = FungusManager.Instance.EventDispatcher;
eventDispatcher.AddListener<DragCompletedEvent>(OnDragCompletedEvent);
eventDispatcher.AddListener<DragEntered.DragEnteredEvent>(OnDragEnteredEvent);
eventDispatcher.AddListener<DragExited.DragExitedEvent>(OnDragExitedEvent);
eventDispatcher.AddListener<DragCompletedEvent>(OnDragCompletedEvent);
eventDispatcher.AddListener<DragEntered.DragEnteredEvent>(OnDragEnteredEvent);
eventDispatcher.AddListener<DragExited.DragExitedEvent>(OnDragExitedEvent);
if(draggableObject != null)
{
draggableObject.RegisterHandler(this);
foreach(Draggable2D dragObj in draggableObjects)
{
dragObj.RegisterHandler(this);
}
}
}
protected virtual void OnDisable()
{
eventDispatcher.RemoveListener<DragCompletedEvent>(OnDragCompletedEvent);
eventDispatcher.RemoveListener<DragEntered.DragEnteredEvent>(OnDragEnteredEvent);
eventDispatcher.RemoveListener<DragExited.DragExitedEvent>(OnDragExitedEvent);
if(draggableObject != null)
if(Application.IsPlaying(this))
{
draggableObject.UnregisterHandler(this);
}
eventDispatcher.RemoveListener<DragCompletedEvent>(OnDragCompletedEvent);
eventDispatcher.RemoveListener<DragEntered.DragEnteredEvent>(OnDragEnteredEvent);
eventDispatcher.RemoveListener<DragExited.DragExitedEvent>(OnDragExitedEvent);
foreach(Draggable2D dragObj in draggableObjects)
{
dragObj.UnregisterHandler(this);
}
eventDispatcher = null;
eventDispatcher = null;
}
}
void OnDragCompletedEvent(DragCompletedEvent evt)
@ -77,12 +132,21 @@ namespace Fungus
OnDragExited(evt.DraggableObject, evt.TargetCollider);
}
void ISerializationCallbackReceiver.OnAfterDeserialize()
{
}
void ISerializationCallbackReceiver.OnBeforeSerialize()
{
}
#region Public members
/// <summary>
/// Gets the draggable object.
/// </summary>
public virtual Draggable2D DraggableObject { get { return draggableObject; } }
public virtual List<Draggable2D> DraggableObjects { get { return draggableObjects; } }
/// <summary>
/// Returns true if the draggable object is over the drag target object.
@ -97,11 +161,12 @@ namespace Fungus
/// </summary>
public virtual void OnDragEntered(Draggable2D draggableObject, Collider2D targetObject)
{
if (this.targetObject != null &&
draggableObject == this.draggableObject &&
targetObject == this.targetObject)
if (this.targetObjects != null && this.draggableObjects !=null &&
this.draggableObjects.Contains(draggableObject) &&
this.targetObjects.Contains(targetObject))
{
overTarget = true;
targetCollider = targetObject;
}
}
@ -110,11 +175,13 @@ namespace Fungus
/// </summary>
public virtual void OnDragExited(Draggable2D draggableObject, Collider2D targetObject)
{
if (this.targetObject != null &&
draggableObject == this.draggableObject &&
targetObject == this.targetObject)
if (this.targetObjects != null && this.draggableObjects !=null &&
this.draggableObjects.Contains(draggableObject) &&
this.targetObjects.Contains(targetObject))
{
overTarget = false;
targetCollider = null;
}
}
@ -123,28 +190,53 @@ namespace Fungus
/// </summary>
public virtual void OnDragCompleted(Draggable2D draggableObject)
{
if (draggableObject == this.draggableObject &&
if (this.draggableObjects.Contains(draggableObject) &&
overTarget)
{
// 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)
{
draggableRef.Value = draggableObject.gameObject;
}
if(targetRef!=null)
{
targetRef.Value = targetCollider.gameObject;
}
overTarget = false;
targetCollider = null;
ExecuteBlock();
}
}
public override string GetSummary()
{
string summary = "";
if (draggableObject != null)
string summary = "Draggable: ";
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)

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

@ -15,7 +15,9 @@ namespace Fungus
"Drag Entered",
"The block will execute when the player is dragging an object which starts touching the target object.")]
[AddComponentMenu("")]
public class DragEntered : EventHandler
[ExecuteAlways]
public class DragEntered : EventHandler, ISerializationCallbackReceiver
{
public class DragEnteredEvent
{
@ -27,26 +29,64 @@ namespace Fungus
TargetCollider = targetCollider;
}
}
[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;
[SerializeField] protected List<Draggable2D> draggableObjects;
[Tooltip("Drag target object to listen for drag events on")]
[HideInInspector]
[SerializeField] protected Collider2D targetObject;
[SerializeField] protected List<Collider2D> targetObjects;
protected EventDispatcher eventDispatcher;
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()
{
eventDispatcher = FungusManager.Instance.EventDispatcher;
if(Application.IsPlaying(this)){
eventDispatcher = FungusManager.Instance.EventDispatcher;
eventDispatcher.AddListener<DragEnteredEvent>(OnDragEnteredEvent);
eventDispatcher.AddListener<DragEnteredEvent>(OnDragEnteredEvent);
}
}
protected virtual void OnDisable()
{
eventDispatcher.RemoveListener<DragEnteredEvent>(OnDragEnteredEvent);
if(Application.IsPlaying(this)){
eventDispatcher.RemoveListener<DragEnteredEvent>(OnDragEnteredEvent);
eventDispatcher = null;
eventDispatcher = null;
}
}
void OnDragEnteredEvent(DragEnteredEvent evt)
@ -60,30 +100,63 @@ namespace Fungus
/// </summary>
public virtual void OnDragEntered(Draggable2D draggableObject, Collider2D targetObject)
{
if (draggableObject == this.draggableObject &&
targetObject == this.targetObject)
if (this.targetObjects != null && this.draggableObjects !=null &&
this.draggableObjects.Contains(draggableObject) &&
this.targetObjects.Contains(targetObject))
{
if(draggableRef!=null)
{
draggableRef.Value = draggableObject.gameObject;
}
if(targetRef!=null)
{
targetRef.Value = targetObject.gameObject;
}
ExecuteBlock();
}
}
void ISerializationCallbackReceiver.OnAfterDeserialize()
{
}
void ISerializationCallbackReceiver.OnBeforeSerialize()
{
}
public override string GetSummary()
{
string summary = "";
if (draggableObject != null)
string summary = "Draggable: ";
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)
{
return "None";
}
return summary;
}

110
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)
using UnityEngine;
using System.Collections.Generic;
namespace Fungus
{
@ -12,7 +14,9 @@ namespace Fungus
"Drag Exited",
"The block will execute when the player is dragging an object which stops touching the target object.")]
[AddComponentMenu("")]
public class DragExited : EventHandler
[ExecuteAlways]
public class DragExited : EventHandler, ISerializationCallbackReceiver
{
public class DragExitedEvent
{
@ -25,26 +29,70 @@ namespace Fungus
}
}
[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;
[SerializeField] protected List<Draggable2D> draggableObjects;
[Tooltip("Drag target object to listen for drag events on")]
[HideInInspector]
[SerializeField] protected Collider2D targetObject;
[SerializeField] protected List<Collider2D> targetObjects;
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()
{
eventDispatcher = FungusManager.Instance.EventDispatcher;
if(Application.IsPlaying(this)){
eventDispatcher = FungusManager.Instance.EventDispatcher;
eventDispatcher.AddListener<DragExitedEvent>(OnDragEnteredEvent);
eventDispatcher.AddListener<DragExitedEvent>(OnDragEnteredEvent);
}
}
protected virtual void OnDisable()
{
eventDispatcher.RemoveListener<DragExitedEvent>(OnDragEnteredEvent);
if(Application.IsPlaying(this)){
eventDispatcher.RemoveListener<DragExitedEvent>(OnDragEnteredEvent);
eventDispatcher = null;
eventDispatcher = null;
}
}
void OnDragEnteredEvent(DragExitedEvent evt)
@ -52,6 +100,16 @@ namespace Fungus
OnDragExited(evt.DraggableObject, evt.TargetCollider);
}
void ISerializationCallbackReceiver.OnAfterDeserialize()
{
}
void ISerializationCallbackReceiver.OnBeforeSerialize()
{
}
#region Public members
/// <summary>
@ -59,30 +117,54 @@ namespace Fungus
/// </summary>
public virtual void OnDragExited(Draggable2D draggableObject, Collider2D targetObject)
{
if (draggableObject == this.draggableObject &&
targetObject == this.targetObject)
if (this.targetObjects != null && this.draggableObjects !=null &&
this.draggableObjects.Contains(draggableObject) &&
this.targetObjects.Contains(targetObject))
{
if(draggableRef!=null)
{
draggableRef.Value = draggableObject.gameObject;
}
if(targetRef!=null)
{
targetRef.Value = targetObject.gameObject;
}
ExecuteBlock();
}
}
public override string GetSummary()
{
string summary = "";
if (draggableObject != null)
string summary = "Draggable: ";
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)
{
return "None";
}
return summary;
}

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

@ -2,6 +2,7 @@
// 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;
namespace Fungus
{
@ -12,7 +13,7 @@ namespace Fungus
"Drag Started",
"The block will execute when the player starts dragging an object.")]
[AddComponentMenu("")]
public class DragStarted : EventHandler
public class DragStarted : EventHandler, ISerializationCallbackReceiver
{
public class DragStartedEvent
{
@ -22,11 +23,17 @@ namespace Fungus
DraggableObject = draggableObject;
}
}
[VariableProperty(typeof(GameObjectVariable))]
[SerializeField] protected GameObjectVariable draggableRef;
[SerializeField] protected List<Draggable2D> draggableObjects;
[HideInInspector]
[SerializeField] protected Draggable2D draggableObject;
protected EventDispatcher eventDispatcher;
protected virtual void OnEnable()
{
eventDispatcher = FungusManager.Instance.EventDispatcher;
@ -34,6 +41,8 @@ namespace Fungus
eventDispatcher.AddListener<DragStartedEvent>(OnDragStartedEvent);
}
protected virtual void OnDisable()
{
eventDispatcher.RemoveListener<DragStartedEvent>(OnDragStartedEvent);
@ -46,6 +55,25 @@ namespace Fungus
OnDragStarted(evt.DraggableObject);
}
void ISerializationCallbackReceiver.OnAfterDeserialize()
{
//add any dragableobject already present to list for backwards compatability
if (draggableObject != null)
{
if (!draggableObjects.Contains(draggableObject))
{
draggableObjects.Add(draggableObject);
}
draggableObject = null;
}
}
void ISerializationCallbackReceiver.OnBeforeSerialize()
{
}
#region Public members
/// <summary>
@ -53,20 +81,36 @@ namespace Fungus
/// </summary>
public virtual void OnDragStarted(Draggable2D draggableObject)
{
if (draggableObject == this.draggableObject)
if (draggableObjects.Contains(draggableObject))
{
if(draggableRef!=null)
{
draggableRef.Value = draggableObject.gameObject;
}
ExecuteBlock();
}
}
}
public override string GetSummary()
{
if (draggableObject != null)
string summary = "Draggable: ";
if (this.draggableObjects != null && this.draggableObjects.Count != 0)
{
return draggableObject.name;
for (int i = 0; i < this.draggableObjects.Count; i++)
{
if (draggableObjects[i] != null)
{
summary += draggableObjects[i].name + ",";
}
}
}
return "None";
if (summary.Length == 0)
{
return "None";
}
return summary;
}
#endregion

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

File diff suppressed because it is too large Load Diff

7
Assets/FungusExamples/DragAndDrop/DragandDrop(DraggableObjectLists).unity.meta

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

2298
Assets/FungusExamples/TheHunter/TheHunter.unity

File diff suppressed because it is too large Load Diff

4
ProjectSettings/ProjectVersion.txt

@ -1,2 +1,2 @@
m_EditorVersion: 2019.2.15f1
m_EditorVersionWithRevision: 2019.2.15f1 (dcb72c2e9334)
m_EditorVersion: 2019.2.11f1
m_EditorVersionWithRevision: 2019.2.11f1 (5f859a4cfee5)

Loading…
Cancel
Save