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(); } } } }
public void StartListening(GraphStack stack) { var data = stack.GetElementData <Data>(this); if (data.isListening) { return; } var reference = stack.ToReference(); var hook = new EventHook(EventHooks.Update, stack.machine); Action <EmptyEventArgs> update = args => TriggerUpdate(reference); EventBus.Register(hook, update); data.update = update; data.isListening = true; }
public virtual void StartListening(GraphStack stack) { var data = stack.GetElementData <Data>(this); if (data.isListening) { return; } if (register) { var reference = stack.ToReference(); var hook = GetHook(reference); Action <TArgs> handler = args => Trigger(reference, args); EventBus.Register(hook, handler); data.hook = hook; data.handler = handler; } data.isListening = true; }
private void UpdateTarget(GraphStack stack) { var data = stack.GetElementData <Data>(this); var wasListening = data.isListening; var newTarget = Flow.FetchValue <GameObject>(target, stack.ToReference()); if (newTarget != data.target) { if (wasListening) { StopListening(stack); } data.target = newTarget; if (wasListening) { StartListening(stack, false); } } }