Browse Source

Refactored recent EventDispatcher and drag handler changes to match Fungus coding standard.

master
Christopher 8 years ago
parent
commit
418545f706
  1. 4
      Assets/Fungus/Scripts/Components/Clickable2D.cs
  2. 20
      Assets/Fungus/Scripts/Components/Draggable2D.cs
  3. 32
      Assets/Fungus/Scripts/Components/EventHandler.cs
  4. 7
      Assets/Fungus/Scripts/Components/FungusManager.cs
  5. 19
      Assets/Fungus/Scripts/EventHandlers/DragCancelled.cs
  6. 24
      Assets/Fungus/Scripts/EventHandlers/DragCompleted.cs
  7. 16
      Assets/Fungus/Scripts/EventHandlers/DragEntered.cs
  8. 19
      Assets/Fungus/Scripts/EventHandlers/DragExited.cs
  9. 16
      Assets/Fungus/Scripts/EventHandlers/DragStarted.cs
  10. 174
      Assets/Fungus/Scripts/EventHandlers/EventDispatcher.cs
  11. 3
      Assets/Fungus/Scripts/EventHandlers/FlowchartEnabled.cs
  12. 3
      Assets/Fungus/Scripts/EventHandlers/GameStarted.cs
  13. 15
      Assets/Fungus/Scripts/EventHandlers/ObjectClicked.cs
  14. 150
      Assets/Fungus/Scripts/Utils/EventDispatcher.cs
  15. 0
      Assets/Fungus/Scripts/Utils/EventDispatcher.cs.meta

4
Assets/Fungus/Scripts/Components/Clickable2D.cs

@ -39,7 +39,9 @@ namespace Fungus
return; return;
} }
EventDispatcher.Raise(new ObjectClicked.ObjectClickedEvent(this)); var eventDispatcher = FungusManager.Instance.EventDispatcher;
eventDispatcher.Raise(new ObjectClicked.ObjectClickedEvent(this));
} }
protected virtual void DoPointerEnter() protected virtual void DoPointerEnter()

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

@ -75,7 +75,10 @@ namespace Fungus
{ {
return; return;
} }
EventDispatcher.Raise(new DragEntered.DragEnteredEvent(this, other));
var eventDispatcher = FungusManager.Instance.EventDispatcher;
eventDispatcher.Raise(new DragEntered.DragEnteredEvent(this, other));
} }
protected virtual void OnTriggerExit2D(Collider2D other) protected virtual void OnTriggerExit2D(Collider2D other)
@ -84,7 +87,10 @@ namespace Fungus
{ {
return; return;
} }
EventDispatcher.Raise(new DragExited.DragExitedEvent(this, other));
var eventDispatcher = FungusManager.Instance.EventDispatcher;
eventDispatcher.Raise(new DragExited.DragExitedEvent(this, other));
} }
protected virtual void DoBeginDrag() protected virtual void DoBeginDrag()
@ -97,7 +103,9 @@ namespace Fungus
startingPosition = transform.position; startingPosition = transform.position;
EventDispatcher.Raise(new DragStarted.DragStartedEvent(this)); var eventDispatcher = FungusManager.Instance.EventDispatcher;
eventDispatcher.Raise(new DragStarted.DragStartedEvent(this));
} }
protected virtual void DoDrag() protected virtual void DoDrag()
@ -123,6 +131,7 @@ namespace Fungus
return; return;
} }
var eventDispatcher = FungusManager.Instance.EventDispatcher;
bool dragCompleted = false; bool dragCompleted = false;
for (int i = 0; i < dragCompletedHandlers.Count; i++) for (int i = 0; i < dragCompletedHandlers.Count; i++)
@ -133,14 +142,15 @@ namespace Fungus
if (handler.IsOverTarget()) if (handler.IsOverTarget())
{ {
dragCompleted = true; dragCompleted = true;
EventDispatcher.Raise(new DragCompleted.DragCompletedEvent(this));
eventDispatcher.Raise(new DragCompleted.DragCompletedEvent(this));
} }
} }
} }
if (!dragCompleted) if (!dragCompleted)
{ {
EventDispatcher.Raise(new DragCancelled.DragCancelledEvent(this)); eventDispatcher.Raise(new DragCancelled.DragCancelledEvent(this));
if (returnOnCancelled) if (returnOnCancelled)
{ {

32
Assets/Fungus/Scripts/Components/EventHandler.cs

@ -41,38 +41,6 @@ namespace Fungus
[FormerlySerializedAs("parentSequence")] [FormerlySerializedAs("parentSequence")]
[SerializeField] protected Block parentBlock; [SerializeField] protected Block parentBlock;
#region Unity Messages Propagation
protected virtual void UnityOnEnable()
{
}
protected virtual void UnityOnDisable()
{
}
protected virtual void UnityStart()
{
}
void OnEnable()
{
UnityOnEnable();
}
void OnDisable()
{
UnityOnDisable();
}
void Start()
{
UnityStart();
}
#endregion
#region Public members #region Public members
/// <summary> /// <summary>

7
Assets/Fungus/Scripts/Components/FungusManager.cs

@ -11,6 +11,7 @@ namespace Fungus
/// </summary> /// </summary>
[RequireComponent(typeof(CameraManager))] [RequireComponent(typeof(CameraManager))]
[RequireComponent(typeof(MusicManager))] [RequireComponent(typeof(MusicManager))]
[RequireComponent(typeof(EventDispatcher))]
#if UNITY_5_3_OR_NEWER #if UNITY_5_3_OR_NEWER
[RequireComponent(typeof(SaveManager))] [RequireComponent(typeof(SaveManager))]
#endif #endif
@ -24,6 +25,7 @@ namespace Fungus
{ {
CameraManager = GetComponent<CameraManager>(); CameraManager = GetComponent<CameraManager>();
MusicManager = GetComponent<MusicManager>(); MusicManager = GetComponent<MusicManager>();
EventDispatcher = GetComponent<EventDispatcher>();
#if UNITY_5_3_OR_NEWER #if UNITY_5_3_OR_NEWER
SaveManager = GetComponent<SaveManager>(); SaveManager = GetComponent<SaveManager>();
#endif #endif
@ -54,6 +56,11 @@ namespace Fungus
/// </summary> /// </summary>
public MusicManager MusicManager { get; private set; } public MusicManager MusicManager { get; private set; }
/// <summary>
/// Gets the event dispatcher singleton instance.
/// </summary>
public EventDispatcher EventDispatcher { get; private set; }
#if UNITY_5_3_OR_NEWER #if UNITY_5_3_OR_NEWER
/// <summary> /// <summary>
/// Gets the save manager singleton instance. /// Gets the save manager singleton instance.

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

@ -26,22 +26,27 @@ namespace Fungus
[Tooltip("Draggable object to listen for drag events on")] [Tooltip("Draggable object to listen for drag events on")]
[SerializeField] protected Draggable2D draggableObject; [SerializeField] protected Draggable2D draggableObject;
protected override void UnityOnEnable() protected EventDispatcher eventDispatcher;
protected virtual void OnEnable()
{ {
base.UnityOnEnable(); eventDispatcher = FungusManager.Instance.EventDispatcher;
EventDispatcher.AddListener<DragCancelledEvent>(OnDragCancelledEvent);
eventDispatcher.AddListener<DragCancelledEvent>(OnDragCancelledEvent);
} }
protected override void UnityOnDisable() protected virtual void OnDisable()
{ {
base.UnityOnDisable(); eventDispatcher.RemoveListener<DragCancelledEvent>(OnDragCancelledEvent);
EventDispatcher.RemoveListener<DragCancelledEvent>(OnDragCancelledEvent);
eventDispatcher = null;
} }
void OnDragCancelledEvent(DragCancelledEvent evt) protected virtual void OnDragCancelledEvent(DragCancelledEvent evt)
{ {
OnDragCancelled(evt.DraggableObject); OnDragCancelled(evt.DraggableObject);
} }
#region Public members #region Public members
public virtual void OnDragCancelled(Draggable2D draggableObject) public virtual void OnDragCancelled(Draggable2D draggableObject)

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

@ -32,12 +32,15 @@ namespace Fungus
// 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 override void UnityOnEnable() protected EventDispatcher eventDispatcher;
protected virtual void OnEnable()
{ {
base.UnityOnEnable(); eventDispatcher = FungusManager.Instance.EventDispatcher;
EventDispatcher.AddListener<DragCompletedEvent>(OnDragCompletedEvent);
EventDispatcher.AddListener<DragEntered.DragEnteredEvent>(OnDragEnteredEvent); eventDispatcher.AddListener<DragCompletedEvent>(OnDragCompletedEvent);
EventDispatcher.AddListener<DragExited.DragExitedEvent>(OnDragExitedEvent); eventDispatcher.AddListener<DragEntered.DragEnteredEvent>(OnDragEnteredEvent);
eventDispatcher.AddListener<DragExited.DragExitedEvent>(OnDragExitedEvent);
if(draggableObject != null) if(draggableObject != null)
{ {
@ -45,17 +48,18 @@ namespace Fungus
} }
} }
protected override void UnityOnDisable() protected virtual void OnDisable()
{ {
base.UnityOnDisable(); 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);
if(draggableObject != null) if(draggableObject != null)
{ {
draggableObject.UnregisterHandler(this); draggableObject.UnregisterHandler(this);
} }
eventDispatcher = null;
} }
void OnDragCompletedEvent(DragCompletedEvent evt) void OnDragCompletedEvent(DragCompletedEvent evt)

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

@ -33,16 +33,20 @@ namespace Fungus
[Tooltip("Drag target object to listen for drag events on")] [Tooltip("Drag target object to listen for drag events on")]
[SerializeField] protected Collider2D targetObject; [SerializeField] protected Collider2D targetObject;
protected override void UnityOnEnable() protected EventDispatcher eventDispatcher;
protected virtual void OnEnable()
{ {
base.UnityOnEnable(); eventDispatcher = FungusManager.Instance.EventDispatcher;
EventDispatcher.AddListener<DragEnteredEvent>(OnDragEnteredEvent);
eventDispatcher.AddListener<DragEnteredEvent>(OnDragEnteredEvent);
} }
protected override void UnityOnDisable() protected virtual void OnDisable()
{ {
base.UnityOnDisable(); eventDispatcher.RemoveListener<DragEnteredEvent>(OnDragEnteredEvent);
EventDispatcher.RemoveListener<DragEnteredEvent>(OnDragEnteredEvent);
eventDispatcher = null;
} }
void OnDragEnteredEvent(DragEnteredEvent evt) void OnDragEnteredEvent(DragEnteredEvent evt)

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

@ -2,9 +2,6 @@
// 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;
using System.Collections;
using System.Collections.Generic;
namespace Fungus namespace Fungus
{ {
@ -34,16 +31,20 @@ namespace Fungus
[Tooltip("Drag target object to listen for drag events on")] [Tooltip("Drag target object to listen for drag events on")]
[SerializeField] protected Collider2D targetObject; [SerializeField] protected Collider2D targetObject;
protected override void UnityOnEnable() protected EventDispatcher eventDispatcher;
protected virtual void OnEnable()
{ {
base.UnityOnEnable(); eventDispatcher = FungusManager.Instance.EventDispatcher;
EventDispatcher.AddListener<DragExitedEvent>(OnDragEnteredEvent);
eventDispatcher.AddListener<DragExitedEvent>(OnDragEnteredEvent);
} }
protected override void UnityOnDisable() protected virtual void OnDisable()
{ {
base.UnityOnDisable(); eventDispatcher.RemoveListener<DragExitedEvent>(OnDragEnteredEvent);
EventDispatcher.RemoveListener<DragExitedEvent>(OnDragEnteredEvent);
eventDispatcher = null;
} }
void OnDragEnteredEvent(DragExitedEvent evt) void OnDragEnteredEvent(DragExitedEvent evt)

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

@ -25,16 +25,20 @@ namespace Fungus
[SerializeField] protected Draggable2D draggableObject; [SerializeField] protected Draggable2D draggableObject;
protected override void UnityOnEnable() protected EventDispatcher eventDispatcher;
protected virtual void OnEnable()
{ {
base.UnityOnEnable(); eventDispatcher = FungusManager.Instance.EventDispatcher;
EventDispatcher.AddListener<DragStartedEvent>(OnDragStartedEvent);
eventDispatcher.AddListener<DragStartedEvent>(OnDragStartedEvent);
} }
protected override void UnityOnDisable() protected virtual void OnDisable()
{ {
base.UnityOnDisable(); eventDispatcher.RemoveListener<DragStartedEvent>(OnDragStartedEvent);
EventDispatcher.RemoveListener<DragStartedEvent>(OnDragStartedEvent);
eventDispatcher = null;
} }
void OnDragStartedEvent(DragStartedEvent evt) void OnDragStartedEvent(DragStartedEvent evt)

174
Assets/Fungus/Scripts/EventHandlers/EventDispatcher.cs

@ -1,174 +0,0 @@
using System.Collections.Generic;
using System;
namespace Fungus
{
public class EventDispatcher
{
public delegate void TypedDelegate<T>(T e) where T : class;
#region Statics
static EventDispatcher _instance;
public static EventDispatcher Instance
{
get
{
if(_instance == null)
{
_instance = new EventDispatcher();
}
return _instance;
}
}
public static void AddLog(Action<string> log)
{
Instance.addLog(log);
}
public static void RemoveLog(Action<string> log)
{
Instance.removeLog(log);
}
public static void AddListener<T>(TypedDelegate<T> listener) where T : class
{
Instance.addListener(listener);
}
public static void RemoveListener<T>(TypedDelegate<T> listener) where T : class
{
Instance.removeListener(listener);
}
public static void Raise<T>(T evt) where T : class
{
Instance.raise(evt);
}
public static void Raise<T>() where T : class, new()
{
Instance.raise<T>(new T());
}
public static void UnregisterAll()
{
Instance.unregisterAll();
}
#endregion
#region Private Members
readonly Dictionary<Type, List<Delegate>> _delegates;
event Action<string> _onLog;
#endregion
#region Private Functions
private EventDispatcher()
{
_delegates = new Dictionary<Type, List<Delegate>>();
}
/// <summary>
/// Gets the delegate list copy.
/// </summary>
/// <remarks>
/// As listener can modify the list while iterating it, it is better to iterate a copy of the delegates list instead of a reference.
/// </remarks>
/// <returns>A copy of the delegates list if found. Null if the dictionary does not contain a delegate list for this event.</returns>
/// <param name="evt">Event instance.</param>
/// <typeparam name="T">Type of the received event.</typeparam>
List<Delegate> getDelegateListCopy<T>(T evt)
{
var type = typeof(T);
return _delegates.ContainsKey(type) ? new List<Delegate>(_delegates[type]) : null;
}
void log(string message)
{
if(_onLog != null)
{
_onLog(message);
}
}
#endregion
#region Public Functions
public void addLog(Action<string> log)
{
_onLog += log;
}
public void removeLog(Action<string> log)
{
_onLog -= log;
}
public void addListener<T>(TypedDelegate<T> listener) where T : class
{
var type = typeof(T);
if(!_delegates.ContainsKey(type))
{
_delegates.Add(type, new List<Delegate>());
}
var list = _delegates[type];
if(!list.Contains(listener))
{
list.Add(listener);
}
}
public void removeListener<T>(TypedDelegate<T> listener) where T : class
{
var type = typeof(T);
if(_delegates.ContainsKey(type))
{
_delegates[type].Remove(listener);
return;
}
}
public void raise<T>(T evt) where T : class
{
if(evt == null)
{
log("Raised a null event");
return;
}
var list = getDelegateListCopy(evt);
if(list == null || list.Count < 1)
{
log("Raised an event with no listeners");
return;
}
for(int i = 0; i < list.Count; ++i)
{
var callback = list[i] as TypedDelegate<T>;
if(callback != null)
{
try
{
callback(evt);
}
catch(Exception gotcha)
{
log(gotcha.Message);
}
}
}
}
public void raise<T>() where T : class, new()
{
raise<T>(new T());
}
public void unregisterAll()
{
_delegates.Clear();
}
#endregion
}
}

3
Assets/Fungus/Scripts/EventHandlers/FlowchartEnabled.cs

@ -14,9 +14,8 @@ namespace Fungus
[AddComponentMenu("")] [AddComponentMenu("")]
public class FlowchartEnabled : EventHandler public class FlowchartEnabled : EventHandler
{ {
protected override void UnityOnEnable() protected virtual void OnEnable()
{ {
base.UnityOnEnable();
// Blocks use coroutines to schedule command execution, but Unity's coroutines are // Blocks use coroutines to schedule command execution, but Unity's coroutines are
// sometimes unreliable when enabling / disabling objects. // sometimes unreliable when enabling / disabling objects.
// To workaround this we execute the block on the next frame. // To workaround this we execute the block on the next frame.

3
Assets/Fungus/Scripts/EventHandlers/GameStarted.cs

@ -18,9 +18,8 @@ namespace Fungus
[Tooltip("Wait for a number of frames after startup before executing the Block. Can help fix startup order issues.")] [Tooltip("Wait for a number of frames after startup before executing the Block. Can help fix startup order issues.")]
[SerializeField] protected int waitForFrames = 1; [SerializeField] protected int waitForFrames = 1;
protected override void UnityStart() protected virtual void Start()
{ {
base.UnityStart();
StartCoroutine(GameStartCoroutine()); StartCoroutine(GameStartCoroutine());
} }

15
Assets/Fungus/Scripts/EventHandlers/ObjectClicked.cs

@ -30,17 +30,20 @@ namespace Fungus
[Tooltip("Wait for a number of frames before executing the block.")] [Tooltip("Wait for a number of frames before executing the block.")]
[SerializeField] protected int waitFrames = 1; [SerializeField] protected int waitFrames = 1;
protected EventDispatcher eventDispatcher;
protected override void UnityOnEnable() protected virtual void OnEnable()
{ {
base.UnityOnEnable(); eventDispatcher = FungusManager.Instance.EventDispatcher;
EventDispatcher.AddListener<ObjectClickedEvent>(OnObjectClickedEvent);
eventDispatcher.AddListener<ObjectClickedEvent>(OnObjectClickedEvent);
} }
protected override void UnityOnDisable() protected virtual void OnDisable()
{ {
base.UnityOnDisable(); eventDispatcher.RemoveListener<ObjectClickedEvent>(OnObjectClickedEvent);
EventDispatcher.RemoveListener<ObjectClickedEvent>(OnObjectClickedEvent);
eventDispatcher = null;
} }
void OnObjectClickedEvent(ObjectClickedEvent evt) void OnObjectClickedEvent(ObjectClickedEvent evt)

150
Assets/Fungus/Scripts/Utils/EventDispatcher.cs

@ -0,0 +1,150 @@
// This code is part of the Fungus library (http://fungusgames.com) maintained by Chris Gregan (http://twitter.com/gofungus).
// 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;
namespace Fungus
{
/// <summary>
/// A simple efficient event dispatcher with logging support.
/// </summary>
public class EventDispatcher : MonoBehaviour
{
protected readonly Dictionary<Type, List<Delegate>> delegates = new Dictionary<Type, List<Delegate>>();
protected virtual event Action<string> onLog;
/// <summary>
/// Gets the delegate list copy.
/// </summary>
/// <remarks>
/// As listener can modify the list while iterating it, it is better to iterate a copy of the delegates list instead of a reference.
/// </remarks>
/// <returns>A copy of the delegates list if found. Null if the dictionary does not contain a delegate list for this event.</returns>
/// <param name="evt">Event instance.</param>
/// <typeparam name="T">Type of the received event.</typeparam>
protected virtual List<Delegate> GetDelegateListCopy<T>(T evt)
{
var type = typeof(T);
return delegates.ContainsKey(type) ? new List<Delegate>(delegates[type]) : null;
}
protected virtual void Log(string message)
{
if(onLog != null)
{
onLog(message);
}
}
#region Public members
/// <summary>
/// A typed delegate which contains information about the event.
/// </summary>
public delegate void TypedDelegate<T>(T e) where T : class;
/// <summary>
/// Adds a log callback action.
/// </summary>
public virtual void AddLog(Action<string> log)
{
onLog += log;
}
/// <summary>
/// Removes a log callback action.
/// </summary>
public virtual void RemoveLog(Action<string> log)
{
onLog -= log;
}
/// <summary>
/// Adds a listener for a specified event type.
/// </summary>
public virtual void AddListener<T>(TypedDelegate<T> listener) where T : class
{
var type = typeof(T);
if(!delegates.ContainsKey(type))
{
delegates.Add(type, new List<Delegate>());
}
var list = delegates[type];
if(!list.Contains(listener))
{
list.Add(listener);
}
}
/// <summary>
/// Removes a listener for a specified event type.
/// </summary>
public virtual void RemoveListener<T>(TypedDelegate<T> listener) where T : class
{
var type = typeof(T);
if(delegates.ContainsKey(type))
{
delegates[type].Remove(listener);
return;
}
}
/// <summary>
/// Raise an event of a specified type.
/// </summary>
public virtual void Raise<T>(T evt) where T : class
{
if(evt == null)
{
Log("Raised a null event");
return;
}
var list = GetDelegateListCopy(evt);
if(list == null || list.Count < 1)
{
Log("Raised an event with no listeners");
return;
}
for(int i = 0; i < list.Count; ++i)
{
var callback = list[i] as TypedDelegate<T>;
if(callback != null)
{
try
{
callback(evt);
}
catch(Exception gotcha)
{
Log(gotcha.Message);
}
}
}
}
/// <summary>
/// Raise an event of a specified type, creates an instance of the type automatically.
/// </summary>
public virtual void Raise<T>() where T : class, new()
{
Raise<T>(new T());
}
/// <summary>
/// Unregisters all event listeners.
/// </summary>
public virtual void UnregisterAll()
{
delegates.Clear();
}
#endregion
}
}

0
Assets/Fungus/Scripts/EventHandlers/EventDispatcher.cs.meta → Assets/Fungus/Scripts/Utils/EventDispatcher.cs.meta

Loading…
Cancel
Save