Replay subject (#114)

* Add constructor with value to Reference classes

* Implemented ReplayBehaviour for AtomEvents
This commit is contained in:
Adam Ramberg 2020-02-23 21:44:04 +01:00 committed by GitHub
parent 7c4f92ecd8
commit 6309943385
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
48 changed files with 574 additions and 230 deletions

View File

@ -1,55 +0,0 @@
#if UNITY_2019_1_OR_NEWER
using UnityEngine;
using UnityEngine.UIElements;
namespace UnityAtoms.Editor
{
/// <summary>
/// Custom editor for Events. Adds the possiblity to raise an Event from Unity's Inspector.
/// </summary>
/// <typeparam name="T">The type this Event is made of.</typeparam>
/// <typeparam name="E">Event of type T.</typeparam>
public abstract class AtomEventEditor<T, E> : UnityEditor.Editor
where E : AtomEvent<T>
{
protected T _raiseValue = default(T);
protected virtual VisualElement GetRaiseValueInput() { return null; }
public override VisualElement CreateInspectorGUI()
{
var root = new VisualElement();
var desc = serializedObject.FindProperty("_developerDescription");
var developerDescription = new TextField("Developer Description") { value = desc.stringValue, multiline = true };
developerDescription.RegisterCallback<ChangeEvent<string>>(evt =>
{
desc.stringValue = evt.newValue;
serializedObject.ApplyModifiedProperties();
});
root.Add(developerDescription);
var runtimeWrapper = new VisualElement();
runtimeWrapper.SetEnabled(Application.isPlaying);
var input = GetRaiseValueInput();
if (input != null)
{
runtimeWrapper.Add(input);
}
runtimeWrapper.Add(new Button(() =>
{
E e = target as E;
e.Raise(_raiseValue);
})
{
text = "Raise"
});
root.Add(runtimeWrapper);
return root;
}
}
}
#endif

View File

@ -8,14 +8,6 @@ namespace UnityAtoms.Editor
/// Event property drawer of type `AtomBaseVariable`. Inherits from `AtomEventEditor&lt;AtomBaseVariable, AtomBaseVariableEvent&gt;`. Only availble in `UNITY_2019_1_OR_NEWER`.
/// </summary>
[CustomEditor(typeof(AtomBaseVariableEvent))]
public sealed class AtomBaseVariableEventEditor : AtomEventEditor<AtomBaseVariable, AtomBaseVariableEvent>
{
protected override VisualElement GetRaiseValueInput()
{
var input = new Toggle() { label = "Raise value", name = "Raise value", viewDataKey = "Raise value" };
input.RegisterCallback<ChangeEvent<AtomBaseVariable>>((evt) => { _raiseValue = evt.newValue; });
return input;
}
}
public sealed class AtomBaseVariableEventEditor : AtomEventEditor<AtomBaseVariable, AtomBaseVariableEvent> { }
}
#endif

View File

@ -0,0 +1,78 @@
#if UNITY_2019_1_OR_NEWER
using UnityEngine;
using UnityEngine.UIElements;
namespace UnityAtoms.Editor
{
/// <summary>
/// Custom editor for Events. Adds the possiblity to raise an Event from Unity's Inspector.
/// </summary>
/// <typeparam name="T">The type of this event..</typeparam>
/// <typeparam name="E">Event of type T.</typeparam>
public abstract class AtomEventEditor<T, E> : UnityEditor.Editor
where E : AtomEvent<T>
{
static string RAISE_VALUE_PROPNAME = "_raiseValue";
public override VisualElement CreateInspectorGUI()
{
var root = new VisualElement();
IMGUIContainer defaultInspector = new IMGUIContainer(() => DrawDefaultInspector());
root.Add(defaultInspector);
var runtimeWrapper = new VisualElement();
runtimeWrapper.SetEnabled(Application.isPlaying);
runtimeWrapper.Add(new Button(() =>
{
E e = target as E;
var raiseValueProp = serializedObject.FindProperty(RAISE_VALUE_PROPNAME);
e.Raise((T)raiseValueProp.GetPropertyValue());
})
{
text = "Raise"
});
root.Add(runtimeWrapper);
return root;
}
}
/// <summary>
/// Custom editor for Events. Adds the possiblity to raise an Event from Unity's Inspector.
/// </summary>
/// <typeparam name="T1">The first type of this Event.</typeparam>
/// <typeparam name="T2">The second type of this Event.</typeparam>
/// <typeparam name="E">Event of type T1 and T2.</typeparam>
public abstract class AtomEventEditor<T1, T2, E> : UnityEditor.Editor
where E : AtomEvent<T1, T2>
{
static string RAISE_VALUE_1_PROPNAME = "_raiseValue1";
static string RAISE_VALUE_2_PROPNAME = "_raiseValue2";
public override VisualElement CreateInspectorGUI()
{
var root = new VisualElement();
IMGUIContainer defaultInspector = new IMGUIContainer(() => DrawDefaultInspector());
root.Add(defaultInspector);
var runtimeWrapper = new VisualElement();
runtimeWrapper.SetEnabled(Application.isPlaying);
runtimeWrapper.Add(new Button(() =>
{
E e = target as E;
var raiseValue1Prop = serializedObject.FindProperty(RAISE_VALUE_1_PROPNAME);
var raiseValue2Prop = serializedObject.FindProperty(RAISE_VALUE_2_PROPNAME);
e.Raise((T1)raiseValue1Prop.GetPropertyValue(), (T2)raiseValue2Prop.GetPropertyValue());
})
{
text = "Raise"
});
root.Add(runtimeWrapper);
return root;
}
}
}
#endif

View File

@ -0,0 +1,13 @@
#if UNITY_2019_1_OR_NEWER
using UnityEditor;
using UnityEngine.UIElements;
namespace UnityAtoms.Editor
{
/// <summary>
/// Event property drawer of type `&lt;bool, bool&gt;`. Inherits from `AtomEventEditor&lt;bool, bool, BoolEvent&gt;`. Only availble in `UNITY_2019_1_OR_NEWER`.
/// </summary>
[CustomEditor(typeof(BoolBoolEvent))]
public sealed class BoolBoolEventEditor : AtomEventEditor<bool, bool, BoolBoolEvent> { }
}
#endif

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f94c7416dd0104225ab6456e46e6176a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -8,14 +8,6 @@ namespace UnityAtoms.Editor
/// Event property drawer of type `bool`. Inherits from `AtomEventEditor&lt;bool, BoolEvent&gt;`. Only availble in `UNITY_2019_1_OR_NEWER`.
/// </summary>
[CustomEditor(typeof(BoolEvent))]
public sealed class BoolEventEditor : AtomEventEditor<bool, BoolEvent>
{
protected override VisualElement GetRaiseValueInput()
{
var input = new Toggle() { label = "Raise value", name = "Raise value", viewDataKey = "Raise value" };
input.RegisterCallback<ChangeEvent<bool>>((evt) => { _raiseValue = evt.newValue; });
return input;
}
}
public sealed class BoolEventEditor : AtomEventEditor<bool, BoolEvent> { }
}
#endif

View File

@ -0,0 +1,14 @@
#if UNITY_2019_1_OR_NEWER
using UnityEditor;
using UnityEngine.UIElements;
using UnityEngine;
namespace UnityAtoms.Editor
{
/// <summary>
/// Event property drawer of type `&lt;Collider2D, Collider2D&gt;`. Inherits from `AtomEventEditor&lt;Collider2D, Collider2D, Collider2DEvent&gt;`. Only availble in `UNITY_2019_1_OR_NEWER`.
/// </summary>
[CustomEditor(typeof(Collider2DCollider2DEvent))]
public sealed class Collider2DCollider2DEventEditor : AtomEventEditor<Collider2D, Collider2D, Collider2DCollider2DEvent> { }
}
#endif

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 472f18d4aa6d245c1974a60152ce813d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -9,14 +9,6 @@ namespace UnityAtoms.Editor
/// Event property drawer of type `Collider2D`. Inherits from `AtomEventEditor&lt;Collider2D, Collider2DEvent&gt;`. Only availble in `UNITY_2019_1_OR_NEWER`.
/// </summary>
[CustomEditor(typeof(Collider2DEvent))]
public sealed class Collider2DEventEditor : AtomEventEditor<Collider2D, Collider2DEvent>
{
protected override VisualElement GetRaiseValueInput()
{
var input = new Toggle() { label = "Raise value", name = "Raise value", viewDataKey = "Raise value" };
input.RegisterCallback<ChangeEvent<Collider2D>>((evt) => { _raiseValue = evt.newValue; });
return input;
}
}
public sealed class Collider2DEventEditor : AtomEventEditor<Collider2D, Collider2DEvent> { }
}
#endif

View File

@ -0,0 +1,14 @@
#if UNITY_2019_1_OR_NEWER
using UnityEditor;
using UnityEngine.UIElements;
using UnityEngine;
namespace UnityAtoms.Editor
{
/// <summary>
/// Event property drawer of type `&lt;Collider, Collider&gt;`. Inherits from `AtomEventEditor&lt;Collider, Collider, ColliderEvent&gt;`. Only availble in `UNITY_2019_1_OR_NEWER`.
/// </summary>
[CustomEditor(typeof(ColliderColliderEvent))]
public sealed class ColliderColliderEventEditor : AtomEventEditor<Collider, Collider, ColliderColliderEvent> { }
}
#endif

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f808989740b204845967e31f2ef23279
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -9,14 +9,6 @@ namespace UnityAtoms.Editor
/// Event property drawer of type `Collider`. Inherits from `AtomEventEditor&lt;Collider, ColliderEvent&gt;`. Only availble in `UNITY_2019_1_OR_NEWER`.
/// </summary>
[CustomEditor(typeof(ColliderEvent))]
public sealed class ColliderEventEditor : AtomEventEditor<Collider, ColliderEvent>
{
protected override VisualElement GetRaiseValueInput()
{
var input = new Toggle() { label = "Raise value", name = "Raise value", viewDataKey = "Raise value" };
input.RegisterCallback<ChangeEvent<Collider>>((evt) => { _raiseValue = evt.newValue; });
return input;
}
}
public sealed class ColliderEventEditor : AtomEventEditor<Collider, ColliderEvent> { }
}
#endif

View File

@ -0,0 +1,14 @@
#if UNITY_2019_1_OR_NEWER
using UnityEditor;
using UnityEngine.UIElements;
using UnityEngine;
namespace UnityAtoms.Editor
{
/// <summary>
/// Event property drawer of type `&lt;Color, Color&gt;`. Inherits from `AtomEventEditor&lt;Color, Color, ColorEvent&gt;`. Only availble in `UNITY_2019_1_OR_NEWER`.
/// </summary>
[CustomEditor(typeof(ColorColorEvent))]
public sealed class ColorColorEventEditor : AtomEventEditor<Color, Color, ColorColorEvent> { }
}
#endif

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4b54781409de140c8936eced665b0bc9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -9,14 +9,6 @@ namespace UnityAtoms.Editor
/// Event property drawer of type `Color`. Inherits from `AtomEventEditor&lt;Color, ColorEvent&gt;`. Only availble in `UNITY_2019_1_OR_NEWER`.
/// </summary>
[CustomEditor(typeof(ColorEvent))]
public sealed class ColorEventEditor : AtomEventEditor<Color, ColorEvent>
{
protected override VisualElement GetRaiseValueInput()
{
var input = new Toggle() { label = "Raise value", name = "Raise value", viewDataKey = "Raise value" };
input.RegisterCallback<ChangeEvent<Color>>((evt) => { _raiseValue = evt.newValue; });
return input;
}
}
public sealed class ColorEventEditor : AtomEventEditor<Color, ColorEvent> { }
}
#endif

View File

@ -8,14 +8,6 @@ namespace UnityAtoms.Editor
/// Event property drawer of type `float`. Inherits from `AtomEventEditor&lt;float, FloatEvent&gt;`. Only availble in `UNITY_2019_1_OR_NEWER`.
/// </summary>
[CustomEditor(typeof(FloatEvent))]
public sealed class FloatEventEditor : AtomEventEditor<float, FloatEvent>
{
protected override VisualElement GetRaiseValueInput()
{
var input = new Toggle() { label = "Raise value", name = "Raise value", viewDataKey = "Raise value" };
input.RegisterCallback<ChangeEvent<float>>((evt) => { _raiseValue = evt.newValue; });
return input;
}
}
public sealed class FloatEventEditor : AtomEventEditor<float, FloatEvent> { }
}
#endif

View File

@ -0,0 +1,13 @@
#if UNITY_2019_1_OR_NEWER
using UnityEditor;
using UnityEngine.UIElements;
namespace UnityAtoms.Editor
{
/// <summary>
/// Event property drawer of type `&lt;float, float&gt;`. Inherits from `AtomEventEditor&lt;float, float, FloatEvent&gt;`. Only availble in `UNITY_2019_1_OR_NEWER`.
/// </summary>
[CustomEditor(typeof(FloatFloatEvent))]
public sealed class FloatFloatEventEditor : AtomEventEditor<float, float, FloatFloatEvent> { }
}
#endif

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 7209e4da400f246219e5a8ee1fc6b2b4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -9,14 +9,6 @@ namespace UnityAtoms.Editor
/// Event property drawer of type `GameObject`. Inherits from `AtomEventEditor&lt;GameObject, GameObjectEvent&gt;`. Only availble in `UNITY_2019_1_OR_NEWER`.
/// </summary>
[CustomEditor(typeof(GameObjectEvent))]
public sealed class GameObjectEventEditor : AtomEventEditor<GameObject, GameObjectEvent>
{
protected override VisualElement GetRaiseValueInput()
{
var input = new Toggle() { label = "Raise value", name = "Raise value", viewDataKey = "Raise value" };
input.RegisterCallback<ChangeEvent<GameObject>>((evt) => { _raiseValue = evt.newValue; });
return input;
}
}
public sealed class GameObjectEventEditor : AtomEventEditor<GameObject, GameObjectEvent> { }
}
#endif

View File

@ -0,0 +1,14 @@
#if UNITY_2019_1_OR_NEWER
using UnityEditor;
using UnityEngine.UIElements;
using UnityEngine;
namespace UnityAtoms.Editor
{
/// <summary>
/// Event property drawer of type `&lt;GameObject, GameObject&gt;`. Inherits from `AtomEventEditor&lt;GameObject, GameObject, GameObjectEvent&gt;`. Only availble in `UNITY_2019_1_OR_NEWER`.
/// </summary>
[CustomEditor(typeof(GameObjectGameObjectEvent))]
public sealed class GameObjectGameObjectEventEditor : AtomEventEditor<GameObject, GameObject, GameObjectGameObjectEvent> { }
}
#endif

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3f19b3bab10ef4785900be8cf5a10e04
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -8,14 +8,6 @@ namespace UnityAtoms.Editor
/// Event property drawer of type `int`. Inherits from `AtomEventEditor&lt;int, IntEvent&gt;`. Only availble in `UNITY_2019_1_OR_NEWER`.
/// </summary>
[CustomEditor(typeof(IntEvent))]
public sealed class IntEventEditor : AtomEventEditor<int, IntEvent>
{
protected override VisualElement GetRaiseValueInput()
{
var input = new Toggle() { label = "Raise value", name = "Raise value", viewDataKey = "Raise value" };
input.RegisterCallback<ChangeEvent<int>>((evt) => { _raiseValue = evt.newValue; });
return input;
}
}
public sealed class IntEventEditor : AtomEventEditor<int, IntEvent> { }
}
#endif

View File

@ -0,0 +1,13 @@
#if UNITY_2019_1_OR_NEWER
using UnityEditor;
using UnityEngine.UIElements;
namespace UnityAtoms.Editor
{
/// <summary>
/// Event property drawer of type `&lt;int, int&gt;`. Inherits from `AtomEventEditor&lt;int, int, IntEvent&gt;`. Only availble in `UNITY_2019_1_OR_NEWER`.
/// </summary>
[CustomEditor(typeof(IntIntEvent))]
public sealed class IntIntEventEditor : AtomEventEditor<int, int, IntIntEvent> { }
}
#endif

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: afeddc83caf724ba9bcf8ccaedc0c775
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -8,14 +8,6 @@ namespace UnityAtoms.Editor
/// Event property drawer of type `string`. Inherits from `AtomEventEditor&lt;string, StringEvent&gt;`. Only availble in `UNITY_2019_1_OR_NEWER`.
/// </summary>
[CustomEditor(typeof(StringEvent))]
public sealed class StringEventEditor : AtomEventEditor<string, StringEvent>
{
protected override VisualElement GetRaiseValueInput()
{
var input = new Toggle() { label = "Raise value", name = "Raise value", viewDataKey = "Raise value" };
input.RegisterCallback<ChangeEvent<string>>((evt) => { _raiseValue = evt.newValue; });
return input;
}
}
public sealed class StringEventEditor : AtomEventEditor<string, StringEvent> { }
}
#endif

View File

@ -0,0 +1,13 @@
#if UNITY_2019_1_OR_NEWER
using UnityEditor;
using UnityEngine.UIElements;
namespace UnityAtoms.Editor
{
/// <summary>
/// Event property drawer of type `&lt;string, string&gt;`. Inherits from `AtomEventEditor&lt;string, string, StringEvent&gt;`. Only availble in `UNITY_2019_1_OR_NEWER`.
/// </summary>
[CustomEditor(typeof(StringStringEvent))]
public sealed class StringStringEventEditor : AtomEventEditor<string, string, StringStringEvent> { }
}
#endif

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1674d6d8f049a468ebc768baa32a590e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -9,14 +9,6 @@ namespace UnityAtoms.Editor
/// Event property drawer of type `Vector2`. Inherits from `AtomEventEditor&lt;Vector2, Vector2Event&gt;`. Only availble in `UNITY_2019_1_OR_NEWER`.
/// </summary>
[CustomEditor(typeof(Vector2Event))]
public sealed class Vector2EventEditor : AtomEventEditor<Vector2, Vector2Event>
{
protected override VisualElement GetRaiseValueInput()
{
var input = new Toggle() { label = "Raise value", name = "Raise value", viewDataKey = "Raise value" };
input.RegisterCallback<ChangeEvent<Vector2>>((evt) => { _raiseValue = evt.newValue; });
return input;
}
}
public sealed class Vector2EventEditor : AtomEventEditor<Vector2, Vector2Event> { }
}
#endif

View File

@ -0,0 +1,14 @@
#if UNITY_2019_1_OR_NEWER
using UnityEditor;
using UnityEngine.UIElements;
using UnityEngine;
namespace UnityAtoms.Editor
{
/// <summary>
/// Event property drawer of type `&lt;Vector2, Vector2&gt;`. Inherits from `AtomEventEditor&lt;Vector2, Vector2, Vector2Event&gt;`. Only availble in `UNITY_2019_1_OR_NEWER`.
/// </summary>
[CustomEditor(typeof(Vector2Vector2Event))]
public sealed class Vector2Vector2EventEditor : AtomEventEditor<Vector2, Vector2, Vector2Vector2Event> { }
}
#endif

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5a359d85f1a4d46c9834a5bcfd8e71c1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -9,14 +9,6 @@ namespace UnityAtoms.Editor
/// Event property drawer of type `Vector3`. Inherits from `AtomEventEditor&lt;Vector3, Vector3Event&gt;`. Only availble in `UNITY_2019_1_OR_NEWER`.
/// </summary>
[CustomEditor(typeof(Vector3Event))]
public sealed class Vector3EventEditor : AtomEventEditor<Vector3, Vector3Event>
{
protected override VisualElement GetRaiseValueInput()
{
var input = new Toggle() { label = "Raise value", name = "Raise value", viewDataKey = "Raise value" };
input.RegisterCallback<ChangeEvent<Vector3>>((evt) => { _raiseValue = evt.newValue; });
return input;
}
}
public sealed class Vector3EventEditor : AtomEventEditor<Vector3, Vector3Event> { }
}
#endif

View File

@ -0,0 +1,14 @@
#if UNITY_2019_1_OR_NEWER
using UnityEditor;
using UnityEngine.UIElements;
using UnityEngine;
namespace UnityAtoms.Editor
{
/// <summary>
/// Event property drawer of type `&lt;Vector3, Vector3&gt;`. Inherits from `AtomEventEditor&lt;Vector3, Vector3, Vector3Event&gt;`. Only availble in `UNITY_2019_1_OR_NEWER`.
/// </summary>
[CustomEditor(typeof(Vector3Vector3Event))]
public sealed class Vector3Vector3EventEditor : AtomEventEditor<Vector3, Vector3, Vector3Vector3Event> { }
}
#endif

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1426b9356ef364a7c827107405c32459
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -18,14 +18,6 @@ namespace UnityAtoms.Editor
/// Event property drawer of type `{TYPE}`. Inherits from `AtomEventEditor&lt;{TYPE}, {TYPE_NAME}Event&gt;`. Only availble in `UNITY_2019_1_OR_NEWER`.
/// </summary>
[CustomEditor(typeof({TYPE_NAME}Event))]
public sealed class {TYPE_NAME}EventEditor : AtomEventEditor<{TYPE}, {TYPE_NAME}Event>
{
protected override VisualElement GetRaiseValueInput()
{
var input = new Toggle() { label = "Raise value", name = "Raise value", viewDataKey = "Raise value" };
input.RegisterCallback<ChangeEvent<{TYPE}>>((evt) => { _raiseValue = evt.newValue; });
return input;
}
}
public sealed class {TYPE_NAME}EventEditor : AtomEventEditor<{TYPE}, {TYPE_NAME}Event> { }
}
#endif

View File

@ -0,0 +1,23 @@
#if UNITY_2019_1_OR_NEWER
using UnityEditor;
using UnityEngine.UIElements;
<%IF TYPE_HAS_NAMESPACE%>
using {TYPE_NAMESPACE};
<%ENDIF%>
<%IF HAS_SUB_UA_NAMESPACE%>
using UnityAtoms.Editor;
<%ENDIF%>
<%IF HAS_SUB_UA_NAMESPACE%>
namespace UnityAtoms.{SUB_UA_NAMESPACE}.Editor
<%ELSE%>
namespace UnityAtoms.Editor
<%ENDIF%>
{
/// <summary>
/// Event property drawer of type `&lt;{TYPE}, {TYPE}&gt;`. Inherits from `AtomEventEditor&lt;{TYPE}, {TYPE}, {TYPE_NAME}Event&gt;`. Only availble in `UNITY_2019_1_OR_NEWER`.
/// </summary>
[CustomEditor(typeof({TYPE_NAME}{TYPE_NAME}Event))]
public sealed class {TYPE_NAME}{TYPE_NAME}EventEditor : AtomEventEditor<{TYPE}, {TYPE}, {TYPE_NAME}{TYPE_NAME}Event> { }
}
#endif

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 70e48c2d353144e4bb0d1229ef6b7b60
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using UnityEngine;
namespace UnityAtoms
{
@ -9,10 +11,38 @@ namespace UnityAtoms
[EditorIcon("atom-icon-cherry")]
public abstract class AtomEvent<T> : AtomEventBase
{
[SerializeField]
private event Action<T> _onEvent;
/// <summary>
/// Actual event.
/// The event replays the specified number of old values to new subscribers. Works like a ReplaySubject in Rx.
/// </summary>
public event Action<T> OnEvent;
[SerializeField]
[Range(0, 10)]
[Tooltip("The number of old values (between 0-10) being replayed when someone subscribes to this Event.")]
private int _replayBufferSize = 1;
private Queue<T> _replayBuffer = new Queue<T>();
private void OnDisable()
{
// Clear all delegates when exiting play mode
if (_onEvent != null)
{
var invocationList = _onEvent.GetInvocationList();
foreach (var d in invocationList)
{
_onEvent -= (Action<T>)d;
}
}
}
/// <summary>
/// Used when raising values from the inspector for debugging purposes.
/// </summary>
[SerializeField]
[Tooltip("Value that will be used when using the Raise button in the editor inspector.")]
private T _raiseValue;
/// <summary>
/// Raise the Event.
@ -21,25 +51,27 @@ namespace UnityAtoms
public void Raise(T item)
{
base.RaiseNoValue();
OnEvent?.Invoke(item);
_onEvent?.Invoke(item);
AddToReplayBuffer(item);
}
/// <summary>
/// Register handler to be called when the Event triggers.
/// </summary>
/// <param name="del">The handler.</param>
public void Register(Action<T> del)
/// <param name="action">The handler.</param>
public void Register(Action<T> action)
{
OnEvent += del;
_onEvent += action;
ReplayBuffer(action);
}
/// <summary>
/// Unregister handler that was registered using the `Register` method.
/// </summary>
/// <param name="del">The handler.</param>
public void Unregister(Action<T> del)
/// <param name="action">The handler.</param>
public void Unregister(Action<T> action)
{
OnEvent -= del;
_onEvent -= action;
}
/// <summary>
@ -48,7 +80,8 @@ namespace UnityAtoms
/// <param name="listener">The Listener to register.</param>
public void RegisterListener(IAtomListener<T> listener)
{
OnEvent += listener.OnEventRaised;
_onEvent += listener.OnEventRaised;
ReplayBuffer(listener.OnEventRaised);
}
/// <summary>
@ -57,7 +90,7 @@ namespace UnityAtoms
/// <param name="listener">The Listener to unregister.</param>
public void UnregisterListener(IAtomListener<T> listener)
{
OnEvent -= listener.OnEventRaised;
_onEvent -= listener.OnEventRaised;
}
#region Observable
@ -71,15 +104,30 @@ namespace UnityAtoms
}
#endregion // Observable
public override void OnAfterDeserialize()
private void AddToReplayBuffer(T item)
{
base.OnAfterDeserialize();
// Clear all delegates when exiting play mode
if (OnEvent != null)
if (_replayBufferSize > 0)
{
foreach (var d in OnEvent.GetInvocationList())
while (_replayBuffer.Count >= _replayBufferSize) { _replayBuffer.Dequeue(); }
_replayBuffer.Enqueue(item);
}
}
private void ReplayBuffer(Action<T> action)
{
if (_replayBufferSize > 0 && _replayBuffer.Count > 0)
{
var enumerator = _replayBuffer.GetEnumerator();
try
{
OnEvent -= (Action<T>)d;
while (enumerator.MoveNext())
{
action(enumerator.Current);
}
}
finally
{
enumerator.Dispose();
}
}
}
@ -93,10 +141,47 @@ namespace UnityAtoms
[EditorIcon("atom-icon-cherry")]
public abstract class AtomEvent<T1, T2> : AtomEventBase
{
[SerializeField]
private event Action<T1, T2> _onEvent;
/// <summary>
/// Actual event.
/// The event replays the specified number of old values to new subscribers. Works like a ReplaySubject in Rx.
/// </summary>
public event Action<T1, T2> OnEvent;
[SerializeField]
[Range(0, 10)]
[Tooltip("The number of old values (between 0-10) being replayed when someone subscribes to this Event.")]
private int _replayBufferSize = 1;
private Queue<(T1, T2)> _replayBuffer = new Queue<(T1, T2)>();
/// <summary>
/// Used when raising values from the inspector for debugging purposes.
/// </summary>
[SerializeField]
[Tooltip("First value that will be used when using the Raise button in the editor inspector.")]
private T1 _raiseValue1;
/// <summary>
/// Used when raising values from the inspector for debugging purposes.
/// </summary>
[SerializeField]
[Tooltip("Second value that will be used when using the Raise button in the editor inspector.")]
private T2 _raiseValue2;
private void OnDisable()
{
// Clear all delegates when exiting play mode
if (_onEvent != null)
{
var invocationList = _onEvent.GetInvocationList();
foreach (var d in invocationList)
{
_onEvent -= (Action<T1, T2>)d;
}
}
}
/// <summary>
/// Raise the Event.
@ -106,25 +191,27 @@ namespace UnityAtoms
public void Raise(T1 item1, T2 item2)
{
base.RaiseNoValue();
OnEvent?.Invoke(item1, item2);
_onEvent?.Invoke(item1, item2);
AddToReplayBuffer(item1, item2);
}
/// <summary>
/// Register handler to be called when the Event triggers.
/// </summary>
/// <param name="del">The handler.</param>
public void Register(Action<T1, T2> del)
/// <param name="action">The handler.</param>
public void Register(Action<T1, T2> action)
{
OnEvent += del;
_onEvent += action;
ReplayBuffer(action);
}
/// <summary>
/// Unregister handler that was registered using the `Register` method.
/// </summary>
/// <param name="del">The handler.</param>
public void Unregister(Action<T1, T2> del)
/// <param name="action">The handler.</param>
public void Unregister(Action<T1, T2> action)
{
OnEvent -= del;
_onEvent -= action;
}
/// <summary>
@ -133,7 +220,8 @@ namespace UnityAtoms
/// <param name="listener">The Listener to register.</param>
public void RegisterListener(IAtomListener<T1, T2> listener)
{
OnEvent += listener.OnEventRaised;
_onEvent += listener.OnEventRaised;
ReplayBuffer(listener.OnEventRaised);
}
/// <summary>
@ -142,7 +230,7 @@ namespace UnityAtoms
/// <param name="listener">The Listener to unregister.</param>
public void UnregisterListener(IAtomListener<T1, T2> listener)
{
OnEvent -= listener.OnEventRaised;
_onEvent -= listener.OnEventRaised;
}
#region Observable
@ -159,15 +247,32 @@ namespace UnityAtoms
}
#endregion // Observable
public override void OnAfterDeserialize()
private void AddToReplayBuffer(T1 item1, T2 item2)
{
base.OnAfterDeserialize();
// Clear all delegates when exiting play mode
if (OnEvent != null)
foreach (var d in OnEvent.GetInvocationList())
if (_replayBufferSize > 0)
{
while (_replayBuffer.Count >= _replayBufferSize) { _replayBuffer.Dequeue(); }
_replayBuffer.Enqueue((item1, item2));
}
}
private void ReplayBuffer(Action<T1, T2> action)
{
if (_replayBufferSize > 0 && _replayBuffer.Count > 0)
{
var enumerator = _replayBuffer.GetEnumerator();
try
{
OnEvent -= (Action<T1, T2>)d;
while (enumerator.MoveNext())
{
action(enumerator.Current.Item1, enumerator.Current.Item2);
}
}
finally
{
enumerator.Dispose();
}
}
}
}
}

View File

@ -31,11 +31,11 @@ namespace UnityAtoms
/// <summary>
/// The Event we are listening for as a property.
/// </summary>
/// <value>The Event of type `E1`.</value>
public E1 Event { get => _eventReference.Event; set => _eventReference.Event = value; }
/// <value>The Event Reference of type `ER`.</value>
public ER EventReference { get => _eventReference; set => _eventReference = value; }
/// <summary>
/// The Event that we are listening to.
/// The Event Reference that we are listening to.
/// </summary>
[SerializeField]
private ER _eventReference = null;
@ -56,14 +56,14 @@ namespace UnityAtoms
private void OnEnable()
{
if (Event == null) return;
Event.RegisterListener(this);
if (EventReference.Event == null) return;
EventReference.Event.RegisterListener(this);
}
private void OnDisable()
{
if (Event == null) return;
Event.UnregisterListener(this);
if (EventReference.Event == null) return;
EventReference.Event.UnregisterListener(this);
}
/// <summary>

View File

@ -31,14 +31,14 @@ namespace UnityAtoms
/// <summary>
/// The Event we are listening for as a property.
/// </summary>
/// <value>The Event of type `E2`.</value>
public E2 Event { get => _eventReference.Event; set => _eventReference.Event = value; }
/// <value>The Event Reference of type `E2R`.</value>
public E2R EventReference { get => _eventReference; set => _eventReference = value; }
/// <summary>
/// The Event that we are listening to.
/// The Event Reference that we are listening to.
/// </summary>
[SerializeField]
private E2R _eventReference;
private E2R _eventReference = null;
/// <summary>
/// The Unity Event responses.
@ -56,14 +56,14 @@ namespace UnityAtoms
private void OnEnable()
{
if (_eventReference == null) return;
Event.RegisterListener(this);
if (EventReference.Event == null) return;
EventReference.Event.RegisterListener(this);
}
private void OnDisable()
{
if (_eventReference == null) return;
Event.UnregisterListener(this);
if (EventReference.Event == null) return;
EventReference.Event.UnregisterListener(this);
}
/// <summary>

View File

@ -10,14 +10,6 @@ namespace UnityAtoms.Mobile.Editor
/// Event property drawer of type `TouchUserInput`. Inherits from `AtomEventEditor&lt;TouchUserInput, TouchUserInputEvent&gt;`. Only availble in `UNITY_2019_1_OR_NEWER`.
/// </summary>
[CustomEditor(typeof(TouchUserInputEvent))]
public sealed class TouchUserInputEventEditor : AtomEventEditor<TouchUserInput, TouchUserInputEvent>
{
protected override VisualElement GetRaiseValueInput()
{
var input = new Toggle() { label = "Raise value", name = "Raise value", viewDataKey = "Raise value" };
input.RegisterCallback<ChangeEvent<TouchUserInput>>((evt) => { _raiseValue = evt.newValue; });
return input;
}
}
public sealed class TouchUserInputEventEditor : AtomEventEditor<TouchUserInput, TouchUserInputEvent> { }
}
#endif

View File

@ -0,0 +1,15 @@
#if UNITY_2019_1_OR_NEWER
using UnityEditor;
using UnityEngine.UIElements;
using UnityAtoms.Mobile;
using UnityAtoms.Editor;
namespace UnityAtoms.Mobile.Editor
{
/// <summary>
/// Event property drawer of type `&lt;TouchUserInput, TouchUserInput&gt;`. Inherits from `AtomEventEditor&lt;TouchUserInput, TouchUserInput, TouchUserInputEvent&gt;`. Only availble in `UNITY_2019_1_OR_NEWER`.
/// </summary>
[CustomEditor(typeof(TouchUserInputTouchUserInputEvent))]
public sealed class TouchUserInputTouchUserInputEventEditor : AtomEventEditor<TouchUserInput, TouchUserInput, TouchUserInputTouchUserInputEvent> { }
}
#endif

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: aaa2726c27f214ef59f5b24a13c094fe
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -10,14 +10,6 @@ namespace UnityAtoms.SceneMgmt.Editor
/// Event property drawer of type `SceneField`. Inherits from `AtomEventEditor&lt;SceneField, SceneFieldEvent&gt;`. Only availble in `UNITY_2019_1_OR_NEWER`.
/// </summary>
[CustomEditor(typeof(SceneFieldEvent))]
public sealed class SceneFieldEventEditor : AtomEventEditor<SceneField, SceneFieldEvent>
{
protected override VisualElement GetRaiseValueInput()
{
var input = new Toggle() { label = "Raise value", name = "Raise value", viewDataKey = "Raise value" };
input.RegisterCallback<ChangeEvent<SceneField>>((evt) => { _raiseValue = evt.newValue; });
return input;
}
}
public sealed class SceneFieldEventEditor : AtomEventEditor<SceneField, SceneFieldEvent> { }
}
#endif

View File

@ -0,0 +1,15 @@
#if UNITY_2019_1_OR_NEWER
using UnityEditor;
using UnityEngine.UIElements;
using UnityAtoms.SceneMgmt;
using UnityAtoms.Editor;
namespace UnityAtoms.SceneMgmt.Editor
{
/// <summary>
/// Event property drawer of type `&lt;SceneField, SceneField&gt;`. Inherits from `AtomEventEditor&lt;SceneField, SceneField, SceneFieldEvent&gt;`. Only availble in `UNITY_2019_1_OR_NEWER`.
/// </summary>
[CustomEditor(typeof(SceneFieldSceneFieldEvent))]
public sealed class SceneFieldSceneFieldEventEditor : AtomEventEditor<SceneField, SceneField, SceneFieldSceneFieldEvent> { }
}
#endif

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9cd4204f240d24e62861e78697439c9f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: