Browse Source

Add note to Drag handlers re compatibility

master
Steve Halliwell 5 years ago
parent
commit
0fb320f364
  1. 23
      Assets/Fungus/Scripts/EventHandlers/DragCancelled.cs
  2. 98
      Assets/Fungus/Scripts/EventHandlers/DragCompleted.cs
  3. 93
      Assets/Fungus/Scripts/EventHandlers/DragEntered.cs
  4. 85
      Assets/Fungus/Scripts/EventHandlers/DragExited.cs
  5. 21
      Assets/Fungus/Scripts/EventHandlers/DragStarted.cs
  6. 37
      Assets/FungusExamples/DragAndDrop/DragandDrop(DraggableObjectLists).unity

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

@ -1,9 +1,8 @@
// 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
{
@ -19,21 +18,22 @@ namespace Fungus
public class DragCancelledEvent
{
public Draggable2D DraggableObject;
public DragCancelledEvent(Draggable2D draggableObject)
{
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()
@ -55,7 +55,9 @@ namespace Fungus
OnDragCancelled(evt.DraggableObject);
}
void ISerializationCallbackReceiver.OnAfterDeserialize()
#region Compatibility
void ISerializationCallbackReceiver.OnAfterDeserialize()
{
//add any dragableobject already present to list for backwards compatability
if (draggableObject != null)
@ -63,7 +65,6 @@ namespace Fungus
if (!draggableObjects.Contains(draggableObject))
{
draggableObjects.Add(draggableObject);
}
draggableObject = null;
}
@ -71,16 +72,17 @@ namespace Fungus
void ISerializationCallbackReceiver.OnBeforeSerialize()
{
}
#endregion Compatibility
#region Public members
public virtual void OnDragCancelled(Draggable2D draggableObject)
{
if (draggableObjects.Contains(draggableObject))
{
if(draggableRef!=null)
if (draggableRef != null)
{
draggableRef.Value = draggableObject.gameObject;
}
@ -88,8 +90,6 @@ namespace Fungus
}
}
public override string GetSummary()
{
string summary = "Draggable: ";
@ -108,9 +108,8 @@ namespace Fungus
{
return "None";
}
}
#endregion
#endregion Public members
}
}

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

@ -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
}
}

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

@ -1,21 +1,21 @@
// 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;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Fungus
{
/// <summary>
/// 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>
[EventHandlerInfo("Sprite",
"Drag Entered",
"The block will execute when the player is dragging an object which starts touching the target object.")]
[AddComponentMenu("")]
[ExecuteAlways]
public class DragEntered : EventHandler, ISerializationCallbackReceiver
{
@ -23,16 +23,20 @@ namespace Fungus
{
public Draggable2D DraggableObject;
public Collider2D TargetCollider;
public DragEnteredEvent(Draggable2D draggableObject, Collider2D targetCollider)
{
DraggableObject = draggableObject;
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;
@ -42,12 +46,47 @@ 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;
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
if (draggableObject != null)
@ -67,32 +106,10 @@ namespace Fungus
}
draggableObject = null;
targetObject = null;
}
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;
}
}
#endregion Compatibility
void OnDragEnteredEvent(DragEnteredEvent evt)
{
OnDragEntered(evt.DraggableObject, evt.TargetCollider);
}
#region Public members
/// <summary>
@ -100,15 +117,15 @@ 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))
{
if(draggableRef!=null)
if (draggableRef != null)
{
draggableRef.Value = draggableObject.gameObject;
}
if(targetRef!=null)
if (targetRef != null)
{
targetRef.Value = targetObject.gameObject;
}
@ -116,16 +133,6 @@ namespace Fungus
}
}
void ISerializationCallbackReceiver.OnAfterDeserialize()
{
}
void ISerializationCallbackReceiver.OnBeforeSerialize()
{
}
public override string GetSummary()
{
string summary = "Draggable: ";
@ -160,6 +167,6 @@ namespace Fungus
return summary;
}
#endregion
#endregion Public members
}
}

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

@ -1,20 +1,21 @@
// 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 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>
[EventHandlerInfo("Sprite",
"Drag Exited",
"The block will execute when the player is dragging an object which stops touching the target object.")]
[AddComponentMenu("")]
[ExecuteAlways]
public class DragExited : EventHandler, ISerializationCallbackReceiver
{
@ -22,6 +23,7 @@ namespace Fungus
{
public Draggable2D DraggableObject;
public Collider2D TargetCollider;
public DragExitedEvent(Draggable2D draggableObject, Collider2D targetCollider)
{
DraggableObject = draggableObject;
@ -31,6 +33,7 @@ namespace Fungus
[VariableProperty(typeof(GameObjectVariable))]
[SerializeField] protected GameObjectVariable draggableRef;
[VariableProperty(typeof(GameObjectVariable))]
[SerializeField] protected GameObjectVariable targetRef;
@ -43,73 +46,70 @@ 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;
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<DragExitedEvent>(OnDragEnteredEvent);
}
}
protected virtual void OnDisable()
{
if(Application.IsPlaying(this)){
eventDispatcher.RemoveListener<DragExitedEvent>(OnDragEnteredEvent);
if (Application.IsPlaying(this))
{
eventDispatcher.RemoveListener<DragExitedEvent>(OnDragEnteredEvent);
eventDispatcher = null;
}
}
void OnDragEnteredEvent(DragExitedEvent evt)
private void OnDragEnteredEvent(DragExitedEvent evt)
{
OnDragExited(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
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>
@ -117,15 +117,15 @@ 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))
{
if(draggableRef!=null)
if (draggableRef != null)
{
draggableRef.Value = draggableObject.gameObject;
}
if(targetRef!=null)
if (targetRef != null)
{
targetRef.Value = targetObject.gameObject;
}
@ -133,7 +133,6 @@ namespace Fungus
}
}
public override string GetSummary()
{
string summary = "Draggable: ";
@ -168,6 +167,6 @@ namespace Fungus
return summary;
}
#endregion
#endregion Public members
}
}

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

@ -1,8 +1,8 @@
// 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
{
@ -18,22 +18,23 @@ namespace Fungus
public class DragStartedEvent
{
public Draggable2D DraggableObject;
public DragStartedEvent(Draggable2D draggableObject)
{
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;
@ -41,8 +42,6 @@ namespace Fungus
eventDispatcher.AddListener<DragStartedEvent>(OnDragStartedEvent);
}
protected virtual void OnDisable()
{
eventDispatcher.RemoveListener<DragStartedEvent>(OnDragStartedEvent);
@ -50,11 +49,13 @@ namespace Fungus
eventDispatcher = null;
}
void OnDragStartedEvent(DragStartedEvent evt)
private void OnDragStartedEvent(DragStartedEvent evt)
{
OnDragStarted(evt.DraggableObject);
}
#region Compatibility
void ISerializationCallbackReceiver.OnAfterDeserialize()
{
//add any dragableobject already present to list for backwards compatability
@ -63,7 +64,6 @@ namespace Fungus
if (!draggableObjects.Contains(draggableObject))
{
draggableObjects.Add(draggableObject);
}
draggableObject = null;
}
@ -71,9 +71,10 @@ namespace Fungus
void ISerializationCallbackReceiver.OnBeforeSerialize()
{
}
#endregion Compatibility
#region Public members
/// <summary>
@ -83,7 +84,7 @@ namespace Fungus
{
if (draggableObjects.Contains(draggableObject))
{
if(draggableRef!=null)
if (draggableRef != null)
{
draggableRef.Value = draggableObject.gameObject;
}
@ -113,6 +114,6 @@ namespace Fungus
return summary;
}
#endregion
#endregion Public members
}
}

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

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

Loading…
Cancel
Save