No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FlowState.cs 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System.ComponentModel;
  2. using UnityEngine;
  3. namespace Unity.VisualScripting
  4. {
  5. [SerializationVersion("A")]
  6. [TypeIcon(typeof(FlowGraph))]
  7. [DisplayName("Script State")]
  8. public sealed class FlowState : NesterState<FlowGraph, ScriptGraphAsset>, IGraphEventListener
  9. {
  10. public FlowState() { }
  11. public FlowState(ScriptGraphAsset macro) : base(macro) { }
  12. #region Lifecycle
  13. protected override void OnEnterImplementation(Flow flow)
  14. {
  15. if (flow.stack.TryEnterParentElement(this))
  16. {
  17. nest.graph.StartListening(flow.stack);
  18. flow.stack.TriggerEventHandler(hook => hook == StateEventHooks.OnEnterState, new EmptyEventArgs(), parent => parent is SubgraphUnit, false);
  19. flow.stack.ExitParentElement();
  20. }
  21. }
  22. protected override void OnExitImplementation(Flow flow)
  23. {
  24. if (flow.stack.TryEnterParentElement(this))
  25. {
  26. flow.stack.TriggerEventHandler(hook => hook == StateEventHooks.OnExitState, new EmptyEventArgs(), parent => parent is SubgraphUnit, false);
  27. nest.graph.StopListening(flow.stack);
  28. flow.stack.ExitParentElement();
  29. }
  30. }
  31. public void StartListening(GraphStack stack)
  32. {
  33. if (stack.TryEnterParentElement(this))
  34. {
  35. nest.graph.StartListening(stack);
  36. stack.ExitParentElement();
  37. }
  38. }
  39. public void StopListening(GraphStack stack)
  40. {
  41. if (stack.TryEnterParentElement(this))
  42. {
  43. nest.graph.StopListening(stack);
  44. stack.ExitParentElement();
  45. }
  46. }
  47. public bool IsListening(GraphPointer pointer)
  48. {
  49. return pointer.GetElementData<Data>(this).isActive;
  50. }
  51. #endregion
  52. #region Factory
  53. public override FlowGraph DefaultGraph()
  54. {
  55. return GraphWithEnterUpdateExit();
  56. }
  57. public static FlowState WithEnterUpdateExit()
  58. {
  59. var flowState = new FlowState();
  60. flowState.nest.source = GraphSource.Embed;
  61. flowState.nest.embed = GraphWithEnterUpdateExit();
  62. return flowState;
  63. }
  64. public static FlowGraph GraphWithEnterUpdateExit()
  65. {
  66. return new FlowGraph
  67. {
  68. units =
  69. {
  70. new OnEnterState { position = new Vector2(-205, -215) },
  71. new Update { position = new Vector2(-161, -38) },
  72. new OnExitState { position = new Vector2(-205, 145) }
  73. }
  74. };
  75. }
  76. #endregion
  77. }
  78. }