Adam Ramberg 70f8130797
Debug events by displaying stack traces for events (#159)
* Add stack trace toggled via user prefs

* Add docs regarding preferences

* Rename color getter functions

* Fix minior order of execution bug

* Use GUID + improved styling of detailed stack view

* - Changed the AddStackTrace method to be conditional (from one of your initial suggestions).
- Removed the implicit conversion operator in StackTraceEntry and is instead using ToString explicitly when needed.
- Improved implementation of GetFirstLine
- Simplified Equals implementation of the StackTraceEntry class
2020-06-06 22:19:07 +02:00

76 lines
2.2 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
namespace UnityAtoms
{
/// <summary>
/// None generic base class for Events. Inherits from `BaseAtom` and `ISerializationCallbackReceiver`.
/// </summary>
[EditorIcon("atom-icon-cherry")]
public abstract class AtomEventBase : BaseAtom, ISerializationCallbackReceiver
{
/// <summary>
/// Event without value.
/// </summary>
public event Action OnEventNoValue;
public virtual void Raise()
{
StackTraces.AddStackTrace(GetInstanceID(), StackTraceEntry.Create());
OnEventNoValue?.Invoke();
}
/// <summary>
/// Register handler to be called when the Event triggers.
/// </summary>
/// <param name="del">The handler.</param>
public void Register(Action del)
{
OnEventNoValue += del;
}
/// <summary>
/// Unregister handler that was registered using the `Register` method.
/// </summary>
/// <param name="del">The handler.</param>
public void Unregister(Action del)
{
OnEventNoValue -= del;
}
/// <summary>
/// Register a Listener that in turn trigger all its associated handlers when the Event triggers.
/// </summary>
/// <param name="listener">The Listener to register.</param>
public void RegisterListener(IAtomListener listener)
{
OnEventNoValue += listener.OnEventRaised;
}
/// <summary>
/// Unregister a listener that was registered using the `RegisterListener` method.
/// </summary>
/// <param name="listener">The Listener to unregister.</param>
public void UnregisterListener(IAtomListener listener)
{
OnEventNoValue -= listener.OnEventRaised;
}
public void OnBeforeSerialize() { }
public virtual void OnAfterDeserialize()
{
// Clear all delegates when exiting play mode
if (OnEventNoValue != null)
{
foreach (var d in OnEventNoValue.GetInvocationList())
{
OnEventNoValue -= (Action)d;
}
}
}
}
}