Initial commit
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
namespace Unity.VisualScripting
|
||||
{
|
||||
public struct EmptyEventArgs { }
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6410b5472beb34ccca83003c8599382c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,94 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Unity.VisualScripting
|
||||
{
|
||||
public static class EventBus
|
||||
{
|
||||
static EventBus()
|
||||
{
|
||||
events = new Dictionary<EventHook, HashSet<Delegate>>(new EventHookComparer());
|
||||
}
|
||||
|
||||
private static readonly Dictionary<EventHook, HashSet<Delegate>> events;
|
||||
internal static Dictionary<EventHook, HashSet<Delegate>> testAccessEvents => events;
|
||||
|
||||
public static void Register<TArgs>(EventHook hook, Action<TArgs> handler)
|
||||
{
|
||||
if (!events.TryGetValue(hook, out var handlers))
|
||||
{
|
||||
handlers = new HashSet<Delegate>();
|
||||
events.Add(hook, handlers);
|
||||
}
|
||||
|
||||
handlers.Add(handler);
|
||||
}
|
||||
|
||||
public static void Unregister(EventHook hook, Delegate handler)
|
||||
{
|
||||
if (events.TryGetValue(hook, out var handlers))
|
||||
{
|
||||
if (handlers.Remove(handler))
|
||||
{
|
||||
// Free the key references for GC collection
|
||||
if (handlers.Count == 0)
|
||||
{
|
||||
events.Remove(hook);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void Trigger<TArgs>(EventHook hook, TArgs args)
|
||||
{
|
||||
HashSet<Action<TArgs>> handlers = null;
|
||||
|
||||
if (events.TryGetValue(hook, out var potentialHandlers))
|
||||
{
|
||||
foreach (var potentialHandler in potentialHandlers)
|
||||
{
|
||||
if (potentialHandler is Action<TArgs> handler)
|
||||
{
|
||||
if (handlers == null)
|
||||
{
|
||||
handlers = HashSetPool<Action<TArgs>>.New();
|
||||
}
|
||||
|
||||
handlers.Add(handler);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (handlers != null)
|
||||
{
|
||||
foreach (var handler in handlers)
|
||||
{
|
||||
if (!potentialHandlers.Contains(handler))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
handler.Invoke(args);
|
||||
}
|
||||
|
||||
handlers.Free();
|
||||
}
|
||||
}
|
||||
|
||||
public static void Trigger<TArgs>(string name, GameObject target, TArgs args)
|
||||
{
|
||||
Trigger(new EventHook(name, target), args);
|
||||
}
|
||||
|
||||
public static void Trigger(EventHook hook)
|
||||
{
|
||||
Trigger(hook, new EmptyEventArgs());
|
||||
}
|
||||
|
||||
public static void Trigger(string name, GameObject target)
|
||||
{
|
||||
Trigger(new EventHook(name, target));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2d134ee5ae68a4bd982d255c10af93cd
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,55 @@
|
||||
namespace Unity.VisualScripting
|
||||
{
|
||||
public struct EventHook
|
||||
{
|
||||
public readonly string name;
|
||||
|
||||
public readonly object target;
|
||||
|
||||
public readonly object tag;
|
||||
|
||||
public EventHook(string name, object target = null, object tag = null)
|
||||
{
|
||||
Ensure.That(nameof(name)).IsNotNull(name);
|
||||
|
||||
this.name = name;
|
||||
this.target = target;
|
||||
this.tag = tag;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (!(obj is EventHook other))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return Equals(other);
|
||||
}
|
||||
|
||||
public bool Equals(EventHook other)
|
||||
{
|
||||
return name == other.name && Equals(target, other.target) && Equals(tag, other.tag);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return HashUtility.GetHashCode(name, target, tag);
|
||||
}
|
||||
|
||||
public static bool operator ==(EventHook a, EventHook b)
|
||||
{
|
||||
return a.Equals(b);
|
||||
}
|
||||
|
||||
public static bool operator !=(EventHook a, EventHook b)
|
||||
{
|
||||
return !(a == b);
|
||||
}
|
||||
|
||||
public static implicit operator EventHook(string name)
|
||||
{
|
||||
return new EventHook(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6f2162496fe3847ca8389e55d36b6640
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,18 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Unity.VisualScripting
|
||||
{
|
||||
// Make sure the equality comparer doesn't use boxing
|
||||
public class EventHookComparer : IEqualityComparer<EventHook>
|
||||
{
|
||||
public bool Equals(EventHook x, EventHook y)
|
||||
{
|
||||
return x.Equals(y);
|
||||
}
|
||||
|
||||
public int GetHashCode(EventHook obj)
|
||||
{
|
||||
return obj.GetHashCode();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 38b235dd087ab4b8782eee9e958246fc
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,89 @@
|
||||
namespace Unity.VisualScripting
|
||||
{
|
||||
public static class EventHooks
|
||||
{
|
||||
// Bolt
|
||||
public const string Custom = nameof(Custom);
|
||||
|
||||
// Global
|
||||
public const string OnGUI = nameof(OnGUI);
|
||||
public const string OnApplicationFocus = nameof(OnApplicationFocus);
|
||||
public const string OnApplicationLostFocus = nameof(OnApplicationLostFocus);
|
||||
public const string OnApplicationPause = nameof(OnApplicationPause);
|
||||
public const string OnApplicationResume = nameof(OnApplicationResume);
|
||||
public const string OnApplicationQuit = nameof(OnApplicationQuit);
|
||||
|
||||
// Lifecycle
|
||||
public const string OnEnable = nameof(OnEnable);
|
||||
public const string Start = nameof(Start);
|
||||
public const string Update = nameof(Update);
|
||||
public const string FixedUpdate = nameof(FixedUpdate);
|
||||
public const string LateUpdate = nameof(LateUpdate);
|
||||
public const string OnDisable = nameof(OnDisable);
|
||||
public const string OnDestroy = nameof(OnDestroy);
|
||||
|
||||
// External
|
||||
public const string AnimationEvent = nameof(AnimationEvent);
|
||||
public const string UnityEvent = nameof(UnityEvent);
|
||||
|
||||
// Editor
|
||||
public const string OnDrawGizmos = nameof(OnDrawGizmos);
|
||||
public const string OnDrawGizmosSelected = nameof(OnDrawGizmosSelected);
|
||||
|
||||
// Game Object
|
||||
public const string OnPointerEnter = nameof(OnPointerEnter);
|
||||
public const string OnPointerExit = nameof(OnPointerExit);
|
||||
public const string OnPointerDown = nameof(OnPointerDown);
|
||||
public const string OnPointerUp = nameof(OnPointerUp);
|
||||
public const string OnPointerClick = nameof(OnPointerClick);
|
||||
public const string OnBeginDrag = nameof(OnBeginDrag);
|
||||
public const string OnDrag = nameof(OnDrag);
|
||||
public const string OnEndDrag = nameof(OnEndDrag);
|
||||
public const string OnDrop = nameof(OnDrop);
|
||||
public const string OnScroll = nameof(OnScroll);
|
||||
public const string OnSelect = nameof(OnSelect);
|
||||
public const string OnDeselect = nameof(OnDeselect);
|
||||
public const string OnSubmit = nameof(OnSubmit);
|
||||
public const string OnCancel = nameof(OnCancel);
|
||||
public const string OnMove = nameof(OnMove);
|
||||
public const string OnBecameInvisible = nameof(OnBecameInvisible);
|
||||
public const string OnBecameVisible = nameof(OnBecameVisible);
|
||||
public const string OnCollisionEnter = nameof(OnCollisionEnter);
|
||||
public const string OnCollisionExit = nameof(OnCollisionExit);
|
||||
public const string OnCollisionStay = nameof(OnCollisionStay);
|
||||
public const string OnCollisionEnter2D = nameof(OnCollisionEnter2D);
|
||||
public const string OnCollisionExit2D = nameof(OnCollisionExit2D);
|
||||
public const string OnCollisionStay2D = nameof(OnCollisionStay2D);
|
||||
public const string OnControllerColliderHit = nameof(OnControllerColliderHit);
|
||||
public const string OnJointBreak = nameof(OnJointBreak);
|
||||
public const string OnJointBreak2D = nameof(OnJointBreak2D);
|
||||
public const string OnMouseDown = nameof(OnMouseDown);
|
||||
public const string OnMouseDrag = nameof(OnMouseDrag);
|
||||
public const string OnMouseEnter = nameof(OnMouseEnter);
|
||||
public const string OnMouseExit = nameof(OnMouseExit);
|
||||
public const string OnMouseOver = nameof(OnMouseOver);
|
||||
public const string OnMouseUp = nameof(OnMouseUp);
|
||||
public const string OnMouseUpAsButton = nameof(OnMouseUpAsButton);
|
||||
public const string OnParticleCollision = nameof(OnParticleCollision);
|
||||
public const string OnTransformChildrenChanged = nameof(OnTransformChildrenChanged);
|
||||
public const string OnTransformParentChanged = nameof(OnTransformParentChanged);
|
||||
public const string OnTriggerEnter = nameof(OnTriggerEnter);
|
||||
public const string OnTriggerExit = nameof(OnTriggerExit);
|
||||
public const string OnTriggerStay = nameof(OnTriggerStay);
|
||||
public const string OnTriggerEnter2D = nameof(OnTriggerEnter2D);
|
||||
public const string OnTriggerExit2D = nameof(OnTriggerExit2D);
|
||||
public const string OnTriggerStay2D = nameof(OnTriggerStay2D);
|
||||
public const string OnAnimatorMove = nameof(OnAnimatorMove);
|
||||
public const string OnAnimatorIK = nameof(OnAnimatorIK);
|
||||
|
||||
// GUI
|
||||
public const string OnButtonClick = nameof(OnButtonClick);
|
||||
public const string OnToggleValueChanged = nameof(OnToggleValueChanged);
|
||||
public const string OnSliderValueChanged = nameof(OnSliderValueChanged);
|
||||
public const string OnScrollbarValueChanged = nameof(OnScrollbarValueChanged);
|
||||
public const string OnDropdownValueChanged = nameof(OnDropdownValueChanged);
|
||||
public const string OnInputFieldValueChanged = nameof(OnInputFieldValueChanged);
|
||||
public const string OnInputFieldEndEdit = nameof(OnInputFieldEndEdit);
|
||||
public const string OnScrollRectValueChanged = nameof(OnScrollRectValueChanged);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cb9374a04c97c4b48b123eff373bc7f9
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,137 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Unity.VisualScripting
|
||||
{
|
||||
public abstract class EventMachine<TGraph, TMacro> : Machine<TGraph, TMacro>, IEventMachine
|
||||
where TGraph : class, IGraph, new()
|
||||
where TMacro : Macro<TGraph>, new()
|
||||
{
|
||||
protected void TriggerEvent(string name)
|
||||
{
|
||||
if (hasGraph)
|
||||
{
|
||||
TriggerRegisteredEvent(new EventHook(name, this), new EmptyEventArgs());
|
||||
}
|
||||
}
|
||||
|
||||
protected void TriggerEvent<TArgs>(string name, TArgs args)
|
||||
{
|
||||
if (hasGraph)
|
||||
{
|
||||
TriggerRegisteredEvent(new EventHook(name, this), args);
|
||||
}
|
||||
}
|
||||
|
||||
protected void TriggerUnregisteredEvent(string name)
|
||||
{
|
||||
if (hasGraph)
|
||||
{
|
||||
TriggerUnregisteredEvent(name, new EmptyEventArgs());
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void TriggerRegisteredEvent<TArgs>(EventHook hook, TArgs args)
|
||||
{
|
||||
EventBus.Trigger(hook, args);
|
||||
}
|
||||
|
||||
protected virtual void TriggerUnregisteredEvent<TArgs>(EventHook hook, TArgs args)
|
||||
{
|
||||
using (var stack = reference.ToStackPooled())
|
||||
{
|
||||
stack.TriggerEventHandler(_hook => _hook == hook, args, parent => true, true);
|
||||
|
||||
stack.ClearReference();
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Awake()
|
||||
{
|
||||
base.Awake();
|
||||
|
||||
GlobalMessageListener.Require();
|
||||
}
|
||||
|
||||
protected override void OnEnable()
|
||||
{
|
||||
base.OnEnable();
|
||||
|
||||
TriggerEvent(EventHooks.OnEnable);
|
||||
}
|
||||
|
||||
protected virtual void Start()
|
||||
{
|
||||
TriggerEvent(EventHooks.Start);
|
||||
}
|
||||
|
||||
protected override void OnInstantiateWhileEnabled()
|
||||
{
|
||||
base.OnInstantiateWhileEnabled();
|
||||
|
||||
TriggerEvent(EventHooks.OnEnable);
|
||||
}
|
||||
|
||||
protected virtual void Update()
|
||||
{
|
||||
TriggerEvent(EventHooks.Update);
|
||||
}
|
||||
|
||||
protected virtual void FixedUpdate()
|
||||
{
|
||||
TriggerEvent(EventHooks.FixedUpdate);
|
||||
}
|
||||
|
||||
protected virtual void LateUpdate()
|
||||
{
|
||||
TriggerEvent(EventHooks.LateUpdate);
|
||||
}
|
||||
|
||||
protected override void OnUninstantiateWhileEnabled()
|
||||
{
|
||||
TriggerEvent(EventHooks.OnDisable);
|
||||
|
||||
base.OnUninstantiateWhileEnabled();
|
||||
}
|
||||
|
||||
protected override void OnDisable()
|
||||
{
|
||||
TriggerEvent(EventHooks.OnDisable);
|
||||
|
||||
base.OnDisable();
|
||||
}
|
||||
|
||||
protected override void OnDestroy()
|
||||
{
|
||||
try
|
||||
{
|
||||
TriggerEvent(EventHooks.OnDestroy);
|
||||
}
|
||||
finally
|
||||
{
|
||||
base.OnDestroy();
|
||||
}
|
||||
}
|
||||
|
||||
#if MODULE_ANIMATION_EXISTS
|
||||
public override void TriggerAnimationEvent(AnimationEvent animationEvent)
|
||||
{
|
||||
TriggerEvent(EventHooks.AnimationEvent, animationEvent);
|
||||
}
|
||||
#endif
|
||||
|
||||
public override void TriggerUnityEvent(string name)
|
||||
{
|
||||
TriggerEvent(EventHooks.UnityEvent, name);
|
||||
}
|
||||
|
||||
protected virtual void OnDrawGizmos()
|
||||
{
|
||||
TriggerUnregisteredEvent(EventHooks.OnDrawGizmos);
|
||||
}
|
||||
|
||||
protected virtual void OnDrawGizmosSelected()
|
||||
{
|
||||
TriggerUnregisteredEvent(EventHooks.OnDrawGizmosSelected);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: aec68ea079c1545968ca3678efd2a542
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,37 @@
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
|
||||
namespace Unity.VisualScripting
|
||||
{
|
||||
public class FrameDelayedCallback
|
||||
{
|
||||
private System.Action m_Callback;
|
||||
private int m_FrameDelay;
|
||||
public FrameDelayedCallback(System.Action function, int frameDelay)
|
||||
{
|
||||
m_Callback = function;
|
||||
m_FrameDelay = frameDelay;
|
||||
EditorApplication.update += Update;
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
EditorApplication.update -= Update;
|
||||
m_FrameDelay = 0;
|
||||
m_Callback = null;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (--m_FrameDelay == 0)
|
||||
{
|
||||
// Clear state before firing callback to ensure reset (callback could call ExitGUI)
|
||||
var callback = m_Callback;
|
||||
Clear();
|
||||
|
||||
callback?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c6ae16097a0d4e30bf4c1f3c16ba3805
|
||||
timeCreated: 1602684288
|
||||
@@ -0,0 +1,63 @@
|
||||
using System;
|
||||
|
||||
namespace Unity.VisualScripting
|
||||
{
|
||||
public static class XEventGraph
|
||||
{
|
||||
public static void TriggerEventHandler<TArgs>(this GraphReference reference, Func<EventHook, bool> predicate, TArgs args, Func<IGraphParentElement, bool> recurse, bool force)
|
||||
{
|
||||
Ensure.That(nameof(reference)).IsNotNull(reference);
|
||||
|
||||
foreach (var element in reference.graph.elements)
|
||||
{
|
||||
if (element is IGraphEventHandler<TArgs> handler && (predicate?.Invoke(handler.GetHook(reference)) ?? true))
|
||||
{
|
||||
if (force || handler.IsListening(reference))
|
||||
{
|
||||
handler.Trigger(reference, args);
|
||||
}
|
||||
}
|
||||
|
||||
if (element is IGraphParentElement parentElement && recurse(parentElement))
|
||||
{
|
||||
reference.ChildReference(parentElement, false, 0)?.TriggerEventHandler(predicate, args, recurse, force);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void TriggerEventHandler<TArgs>(this GraphStack stack, Func<EventHook, bool> predicate, TArgs args, Func<IGraphParentElement, bool> recurse, bool force)
|
||||
{
|
||||
Ensure.That(nameof(stack)).IsNotNull(stack);
|
||||
|
||||
GraphReference reference = null;
|
||||
|
||||
foreach (var element in stack.graph.elements)
|
||||
{
|
||||
if (element is IGraphEventHandler<TArgs> handler)
|
||||
{
|
||||
if (reference == null)
|
||||
{
|
||||
reference = stack.ToReference();
|
||||
}
|
||||
|
||||
if (predicate == null || predicate.Invoke(handler.GetHook(reference)))
|
||||
{
|
||||
if (force || handler.IsListening(reference))
|
||||
{
|
||||
handler.Trigger(reference, args);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (element is IGraphParentElement parentElement && recurse(parentElement))
|
||||
{
|
||||
if (stack.TryEnterParentElementUnsafe(parentElement))
|
||||
{
|
||||
stack.TriggerEventHandler(predicate, args, recurse, force);
|
||||
stack.ExitParentElement();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ed948b29f5fab40c2b0a902e8248cc20
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,13 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Unity.VisualScripting
|
||||
{
|
||||
public interface IEventMachine : IMachine
|
||||
{
|
||||
#if MODULE_ANIMATION_EXISTS
|
||||
void TriggerAnimationEvent(AnimationEvent animationEvent);
|
||||
#endif
|
||||
|
||||
void TriggerUnityEvent(string name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7ccb3a0de46a0487fa26d56aeec5f659
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace Unity.VisualScripting
|
||||
{
|
||||
public interface IGraphEventHandler<TArgs>
|
||||
{
|
||||
EventHook GetHook(GraphReference reference);
|
||||
|
||||
void Trigger(GraphReference reference, TArgs args);
|
||||
|
||||
bool IsListening(GraphPointer pointer);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 54b30d9ccafa84c9b804674810fe0244
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user