-
-
Save lazlo-bonin/ee2dd7f48e7562e65c383fb10f9c49f6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Bolt; | |
using Ludiq; | |
using UnityEngine; | |
public class TestComponent : MonoBehaviour | |
{ | |
public event Action<TestArgs> testEvent; | |
private void Update() | |
{ | |
if (Input.GetKeyDown(KeyCode.Alpha1)) | |
{ | |
Debug.Log("Triggering Test.SuperEvent"); | |
if (testEvent != null) | |
{ | |
testEvent(new TestArgs { testArg = true }); | |
} | |
} | |
} | |
} | |
public class TestArgs | |
{ | |
public bool testArg; | |
} | |
[UnitCategory("Events")] | |
public sealed class TestEventUnit : EventUnit<TestArgs> | |
{ | |
public new class Data : EventUnit<TestArgs>.Data | |
{ | |
public TestComponent testComponent; | |
} | |
[DoNotSerialize] | |
public ValueInput testComponent { get; private set; } | |
[DoNotSerialize] | |
public ValueOutput testArg { get; private set; } | |
protected override bool register => false; | |
protected override void Definition() | |
{ | |
base.Definition(); | |
testComponent = ValueInput<TestComponent>(nameof(testComponent)); | |
testArg = ValueOutput<bool>(nameof(testArg)); | |
} | |
public override void StartListening(GraphStack stack) | |
{ | |
var data = stack.GetElementData<Data>(this); | |
if (data.isListening) | |
{ | |
return; | |
} | |
var reference = stack.ToReference(); | |
var testComponent = Flow.FetchValue<TestComponent>(this.testComponent, reference); | |
if (testComponent == null) | |
{ | |
return; | |
} | |
Action<TestArgs> handler = args => Trigger(reference, args); | |
testComponent.testEvent += handler; | |
data.testComponent = testComponent; | |
data.handler = handler; | |
data.isListening = true; | |
} | |
public override void StopListening(GraphStack stack) | |
{ | |
var data = stack.GetElementData<Data>(this); | |
if (!data.isListening) | |
{ | |
return; | |
} | |
if (data.testComponent != null) | |
{ | |
data.testComponent.testEvent -= (Action<TestArgs>)data.handler; | |
} | |
data.isListening = false; | |
} | |
protected override void AssignArguments(Flow flow, TestArgs args) | |
{ | |
base.AssignArguments(flow, args); | |
flow.SetValue(testArg, args.testArg); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment