225 lines
7.2 KiB
C#
Raw Normal View History

2019-04-11 13:52:13 +02:00
using System;
2018-10-30 20:05:06 +01:00
using System.Collections.Generic;
using UnityEngine;
namespace UnityAtoms
{
2019-10-15 20:44:25 +02:00
/// <summary>
/// None generic base class for Events. Inherits from `BaseAtom` and `ISerializationCallbackReceiver`.
/// </summary>
2019-10-14 16:51:54 +02:00
[EditorIcon("atom-icon-cherry")]
2019-10-15 02:16:11 +02:00
public abstract class AtomEvent : BaseAtom, ISerializationCallbackReceiver
2019-09-30 01:24:02 +02:00
{
2019-10-15 20:44:25 +02:00
/// <summary>
/// Event without value.
/// </summary>
2019-09-30 01:24:02 +02:00
public event Action OnEventNoValue;
protected void RaiseNoValue()
{
OnEventNoValue?.Invoke();
}
2019-10-15 20:44:25 +02:00
/// <summary>
/// Register handler to be called when the Event triggers.
/// </summary>
/// <param name="del">The handler.</param>
2019-09-30 01:24:02 +02:00
public void Register(Action del)
{
OnEventNoValue += del;
}
2019-10-15 20:44:25 +02:00
/// <summary>
/// Unregister handler that was registered using the `Register` method.
/// </summary>
/// <param name="del">The handler.</param>
2019-09-30 01:24:02 +02:00
public void Unregister(Action del)
{
OnEventNoValue -= del;
}
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;
}
}
}
}
2019-10-15 20:44:25 +02:00
/// <summary>
/// Generic base class for Events. Inherits from `AtomEvent`.
/// </summary>
/// <typeparam name="T">The type for this Event.</typeparam>
2019-10-14 16:51:54 +02:00
[EditorIcon("atom-icon-cherry")]
2019-09-30 01:24:02 +02:00
public abstract class AtomEvent<T> : AtomEvent
2018-10-30 20:05:06 +01:00
{
2019-10-15 20:44:25 +02:00
/// <summary>
/// Actual event.
/// </summary>
2019-04-11 13:52:13 +02:00
public event Action<T> OnEvent;
2018-10-30 20:05:06 +01:00
2019-10-15 20:44:25 +02:00
/// <summary>
/// Raise the Event.
/// </summary>
/// <param name="item">The value associated with the Event.</param>
2018-10-30 20:05:06 +01:00
public void Raise(T item)
{
2019-09-30 01:24:02 +02:00
base.RaiseNoValue();
2019-04-11 13:52:13 +02:00
OnEvent?.Invoke(item);
2018-10-30 20:05:06 +01:00
}
2019-10-15 20:44:25 +02:00
/// <summary>
/// Register handler to be called when the Event triggers.
/// </summary>
/// <param name="del">The handler.</param>
public void Register(Action<T> del)
{
OnEvent += del;
}
2019-10-15 20:44:25 +02:00
/// <summary>
/// Unregister handler that was registered using the `Register` method.
/// </summary>
/// <param name="del">The handler.</param>
public void Unregister(Action<T> del)
{
OnEvent -= del;
}
2019-10-15 20:44:25 +02:00
/// <summary>
/// Register a Listener that in turn trigger all its associated handlers when the Event triggers.
/// </summary>
/// <param name="listener">The Listenr to register.</param>
public void RegisterListener(IAtomListener<T> listener)
2018-10-30 20:05:06 +01:00
{
2019-04-11 13:52:13 +02:00
OnEvent += listener.OnEventRaised;
2018-10-30 20:05:06 +01:00
}
2019-10-15 20:44:25 +02:00
/// <summary>
/// Unregister a listener that was registered using the `RegisterListener` method.
/// </summary>
/// <param name="listener">The Listenr to unregister.</param>
public void UnregisterListener(IAtomListener<T> listener)
2018-10-30 20:05:06 +01:00
{
2019-04-11 13:52:13 +02:00
OnEvent -= listener.OnEventRaised;
2018-10-30 20:05:06 +01:00
}
#region Observable
2019-10-15 20:44:25 +02:00
/// <summary>
/// Turn the Event into an `IObservable&lt;T&gt;`. Makes Events compatible with for example UniRx.
/// </summary>
/// <returns>The Event as an `IObservable&lt;T&gt;`.</returns>
public IObservable<T> Observe()
{
return new ObservableEvent<T>(Register, Unregister);
}
#endregion // Observable
2019-09-30 01:24:02 +02:00
public override void OnAfterDeserialize()
{
2019-09-30 01:24:02 +02:00
base.OnAfterDeserialize();
// Clear all delegates when exiting play mode
if (OnEvent != null)
{
foreach (var d in OnEvent.GetInvocationList())
{
OnEvent -= (Action<T>)d;
}
}
}
2018-10-30 20:05:06 +01:00
}
2019-10-15 20:44:25 +02:00
/// <summary>
/// Generic base class for Events. Inherits from `AtomEvent`.
/// </summary>
/// <typeparam name="T1">The first type for this Event.</typeparam>
/// <typeparam name="T2">The second type for this Event.</typeparam>
2019-10-14 16:51:54 +02:00
[EditorIcon("atom-icon-cherry")]
2019-09-30 01:24:02 +02:00
public abstract class AtomEvent<T1, T2> : AtomEvent
2018-10-30 20:05:06 +01:00
{
2019-10-15 20:44:25 +02:00
/// <summary>
/// Actual event.
/// </summary>
2019-04-11 13:52:13 +02:00
public event Action<T1, T2> OnEvent;
2018-10-30 20:05:06 +01:00
2019-10-15 20:44:25 +02:00
/// <summary>
/// Raise the Event.
/// </summary>
/// <param name="item1">The first value associated with the Event.</param>
/// <param name="item2">The second value associated with the Event.</param>
2018-10-30 20:05:06 +01:00
public void Raise(T1 item1, T2 item2)
{
2019-09-30 01:24:02 +02:00
base.RaiseNoValue();
2019-04-11 13:52:13 +02:00
OnEvent?.Invoke(item1, item2);
2018-10-30 20:05:06 +01:00
}
2019-10-15 20:44:25 +02:00
/// <summary>
/// Register handler to be called when the Event triggers.
/// </summary>
/// <param name="del">The handler.</param>
public void Register(Action<T1, T2> del)
{
OnEvent += del;
}
2019-10-15 20:44:25 +02:00
/// <summary>
/// Unregister handler that was registered using the `Register` method.
/// </summary>
/// <param name="del">The handler.</param>
public void Unregister(Action<T1, T2> del)
{
OnEvent -= del;
}
2019-10-15 20:44:25 +02:00
/// <summary>
/// Register a Listener that in turn trigger all its associated handlers when the Event triggers.
/// </summary>
/// <param name="listener">The Listenr to register.</param>
public void RegisterListener(IAtomListener<T1, T2> listener)
2018-10-30 20:05:06 +01:00
{
2019-04-11 13:52:13 +02:00
OnEvent += listener.OnEventRaised;
2018-10-30 20:05:06 +01:00
}
2019-10-15 20:44:25 +02:00
/// <summary>
/// Unregister a listener that was registered using the `RegisterListener` method.
/// </summary>
/// <param name="listener">The Listenr to unregister.</param>
public void UnregisterListener(IAtomListener<T1, T2> listener)
2018-10-30 20:05:06 +01:00
{
2019-04-11 13:52:13 +02:00
OnEvent -= listener.OnEventRaised;
2018-10-30 20:05:06 +01:00
}
#region Observable
2019-10-15 20:44:25 +02:00
/// <summary>
/// Turn the Event into an `IObservable&lt;M&gt;`. Makes Events compatible with for example UniRx.
/// </summary>
/// <param name="resultSelector">Takes `T1` and `T2` and returns a new type of type `M`.abstract Most of the time this is going to be combination of T1 and T2, eg. `ValueTuple&lt;T1, T2&gt;`</param>
/// <typeparam name="M">The result selector type.</typeparam>
/// <returns>The Event as an `IObservable&lt;M&gt;`.</returns>
public IObservable<M> Observe<M>(Func<T1, T2, M> resultSelector)
{
2019-10-15 20:44:25 +02:00
return new ObservableEvent<T1, T2, M>(Register, Unregister, resultSelector);
}
#endregion // Observable
2019-09-30 01:24:02 +02:00
public override void OnAfterDeserialize()
{
2019-09-30 01:24:02 +02:00
base.OnAfterDeserialize();
// Clear all delegates when exiting play mode
if (OnEvent != null)
foreach (var d in OnEvent.GetInvocationList())
{
OnEvent -= (Action<T1, T2>)d;
}
}
2018-10-30 20:05:06 +01:00
}
Squashed commit of the following: commit 847eff037204d841546c0da772d2f341f9cf1e25 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Sun Mar 17 22:09:38 2019 +0100 #17 - Serializable not needed for ScriptableObject commit 593b275e6394b9d589de8a7a20375145dfc4aa84 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Sun Mar 17 21:59:33 2019 +0100 18 - IGameEvent<T1, T2> RegisterListener commit 40443ce9bd4b1c339aaf19cfcb119f2336608dae Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:35:04 2019 +0100 Remove some more warnings commit bd453110ac51a6ebe3d54368fcb039bdbe52e278 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:33:30 2019 +0100 Update README commit 90977b853a047c84efb5311768a09f4e8a1165b2 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:31:45 2019 +0100 Initialize to null to get rid off warnings commit 38b7f5c4ede195aa7198f567801c9cbeedc9b6f6 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:15:53 2019 +0100 More fixes to enable local unity project commit 404e1cbf88ed4431c61d3ece074e838e74ac5141 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:12:37 2019 +0100 Remove duplicated asmdef commit 5734300684e8a16553f213157cad1b4722b7cb7f Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:09:46 2019 +0100 Change files to include commit d1e42b119a6bc1577b1792459fa298e063652337 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:05:56 2019 +0100 Added root package json commit 1709a0347147d74460f653182bbaf8d15eb6154e Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 15:30:44 2019 +0100 #16 - Add test and examples Unity project commit a3ea1a133bf6727e011ba85c64569db45302e487 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 13:12:02 2019 +0100 #13 - Make usage of UPM (package manager) commit 492a30e905f6cf3f5899cb7080ef2bda73110f00 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 11:28:56 2019 +0100 Added extensions + code formatting fixes commit 709949a1016c236cfd363cf25392fedfd8d083ca Author: Oliver Biwer <soraphis@users.noreply.github.com> Date: Fri Mar 8 10:16:45 2019 +0100 More AtomicTags changes (#15) * - added assembly defintions, and unit tests - improved AtomicTags in regards of #8, #9 and #10 * Fixes #11 - Added Equality Members (inclusive HashCode) for ScriptableObjectVariableBase * removed Rider Plugins from git * Further AtomicTag optimization commit ae6584c879f182e727fe0a8d0aff4b0715829914 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 10:08:36 2019 +0100 Editor config commit 197d7067608600e4e2d13dc42db909ee8f8c75df Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 09:23:12 2019 +0100 Added editor config file commit 53d6adc07bbc2967c12c17227a1c31d9f1cfba77 Author: Oliver Biwer <soraphis@users.noreply.github.com> Date: Tue Mar 5 22:57:47 2019 +0100 More efficient AtomicTags (#12) * - added assembly defintions, and unit tests - improved AtomicTags in regards of #8, #9 and #10 * Fixes #11 - Added Equality Members (inclusive HashCode) for ScriptableObjectVariableBase * removed Rider Plugins from git commit 81209d83b5195300d4c2d54411cff3c7983f0d97 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Wed Dec 12 20:54:17 2018 +0100 Added MonoHooks + ColliderType + bug fixes commit c6b240cebbdc410341fb05204235842df5da9d73 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Sat Dec 1 00:23:10 2018 +0100 Experimenting with adding UI state management commit dfd70a8944acbeabe5feba1cf6cff2be6802c470 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Nov 30 23:10:21 2018 +0100 Issue #6 - AtomicTags commit 8907763227f4d4c2a32c5684e7caa4d4a082eb16 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Nov 30 22:42:29 2018 +0100 First commit of v1.0.0
2019-03-17 23:43:20 +01:00
}