暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ToggleFlow.cs 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. namespace Unity.VisualScripting
  2. {
  3. /// <summary>
  4. /// Toggles the control flow with on and off triggers.
  5. /// </summary>
  6. [UnitCategory("Control")]
  7. [UnitOrder(18)]
  8. [UnitFooterPorts(ControlInputs = true, ControlOutputs = true)]
  9. public sealed class ToggleFlow : Unit, IGraphElementWithData
  10. {
  11. public class Data : IGraphElementData
  12. {
  13. public bool isOn;
  14. }
  15. /// <summary>
  16. /// Whether the toggle should start in the on state.
  17. /// </summary>
  18. [Serialize]
  19. [Inspectable]
  20. [UnitHeaderInspectable("Start On")]
  21. [InspectorToggleLeft]
  22. public bool startOn { get; set; } = true;
  23. /// <summary>
  24. /// Entry point to the toggle.
  25. /// </summary>
  26. [DoNotSerialize]
  27. [PortLabelHidden]
  28. public ControlInput enter { get; private set; }
  29. /// <summary>
  30. /// Trigger to turn on the flow through the toggle.
  31. /// </summary>
  32. [DoNotSerialize]
  33. [PortLabel("On")]
  34. public ControlInput turnOn { get; private set; }
  35. /// <summary>
  36. /// Trigger to turn off the flow through the toggle.
  37. /// </summary>
  38. [DoNotSerialize]
  39. [PortLabel("Off")]
  40. public ControlInput turnOff { get; private set; }
  41. /// <summary>
  42. /// Trigger to toggle the flow through the toggle.
  43. /// </summary>
  44. [DoNotSerialize]
  45. public ControlInput toggle { get; private set; }
  46. /// <summary>
  47. /// Triggered on entry if the flow is on.
  48. /// </summary>
  49. [DoNotSerialize]
  50. [PortLabel("On")]
  51. public ControlOutput exitOn { get; private set; }
  52. /// <summary>
  53. /// Triggered on entry if the flow is off.
  54. /// </summary>
  55. [DoNotSerialize]
  56. [PortLabel("Off")]
  57. public ControlOutput exitOff { get; private set; }
  58. /// <summary>
  59. /// Triggered when the flow gets turned on.
  60. /// </summary>
  61. [DoNotSerialize]
  62. public ControlOutput turnedOn { get; private set; }
  63. /// <summary>
  64. /// Triggered when the flow gets turned off.
  65. /// </summary>
  66. [DoNotSerialize]
  67. public ControlOutput turnedOff { get; private set; }
  68. /// <summary>
  69. /// Whether the flow is currently on.
  70. /// </summary>
  71. [DoNotSerialize]
  72. public ValueOutput isOn { get; private set; }
  73. protected override void Definition()
  74. {
  75. enter = ControlInput(nameof(enter), Enter);
  76. turnOn = ControlInput(nameof(turnOn), TurnOn);
  77. turnOff = ControlInput(nameof(turnOff), TurnOff);
  78. toggle = ControlInput(nameof(toggle), Toggle);
  79. exitOn = ControlOutput(nameof(exitOn));
  80. exitOff = ControlOutput(nameof(exitOff));
  81. turnedOn = ControlOutput(nameof(turnedOn));
  82. turnedOff = ControlOutput(nameof(turnedOff));
  83. isOn = ValueOutput(nameof(isOn), IsOn);
  84. Succession(enter, exitOn);
  85. Succession(enter, exitOff);
  86. Succession(turnOn, turnedOn);
  87. Succession(turnOff, turnedOff);
  88. Succession(toggle, turnedOn);
  89. Succession(toggle, turnedOff);
  90. }
  91. public IGraphElementData CreateData()
  92. {
  93. return new Data() { isOn = startOn };
  94. }
  95. private bool IsOn(Flow flow)
  96. {
  97. return flow.stack.GetElementData<Data>(this).isOn;
  98. }
  99. private ControlOutput Enter(Flow flow)
  100. {
  101. return IsOn(flow) ? exitOn : exitOff;
  102. }
  103. private ControlOutput TurnOn(Flow flow)
  104. {
  105. var data = flow.stack.GetElementData<Data>(this);
  106. if (data.isOn)
  107. {
  108. return null;
  109. }
  110. data.isOn = true;
  111. return turnedOn;
  112. }
  113. private ControlOutput TurnOff(Flow flow)
  114. {
  115. var data = flow.stack.GetElementData<Data>(this);
  116. if (!data.isOn)
  117. {
  118. return null;
  119. }
  120. data.isOn = false;
  121. return turnedOff;
  122. }
  123. private ControlOutput Toggle(Flow flow)
  124. {
  125. var data = flow.stack.GetElementData<Data>(this);
  126. data.isOn = !data.isOn;
  127. return data.isOn ? turnedOn : turnedOff;
  128. }
  129. }
  130. }