File size: 4,427 Bytes
18a519f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
namespace Unity.VisualScripting
{
    /// <summary>
    /// Toggles between two values with on and off triggers.
    /// </summary>
    [UnitCategory("Control")]
    [UnitOrder(19)]
    [UnitFooterPorts(ControlInputs = true, ControlOutputs = true)]
    public sealed class ToggleValue : Unit, IGraphElementWithData
    {
        public class Data : IGraphElementData
        {
            public bool isOn;
        }

        /// <summary>
        /// Whether the toggle should start in the on state.
        /// </summary>
        [Serialize]
        [Inspectable]
        [UnitHeaderInspectable("Start On")]
        [InspectorToggleLeft]
        public bool startOn { get; set; } = true;

        /// <summary>
        /// Trigger to turn on the toggle.
        /// </summary>
        [DoNotSerialize]
        [PortLabel("On")]
        public ControlInput turnOn { get; private set; }

        /// <summary>
        /// Trigger to turn off the toggle.
        /// </summary>
        [DoNotSerialize]
        [PortLabel("Off")]
        public ControlInput turnOff { get; private set; }

        /// <summary>
        /// Trigger to toggle the state of the toggle.
        /// </summary>
        [DoNotSerialize]
        public ControlInput toggle { get; private set; }

        /// <summary>
        /// The value to return if the toggle is on.
        /// </summary>
        [DoNotSerialize]
        public ValueInput onValue { get; private set; }

        /// <summary>
        /// The value to return if the toggle is off.
        /// </summary>
        [DoNotSerialize]
        public ValueInput offValue { get; private set; }

        /// <summary>
        /// Triggered when the flow gets turned on.
        /// </summary>
        [DoNotSerialize]
        public ControlOutput turnedOn { get; private set; }

        /// <summary>
        /// Triggered when the flow gets turned off.
        /// </summary>
        [DoNotSerialize]
        public ControlOutput turnedOff { get; private set; }

        /// <summary>
        /// Whether the flow is currently on.
        /// </summary>
        [DoNotSerialize]
        public ValueOutput isOn { get; private set; }

        /// <summary>
        /// The value of the toggle selected depending on the state.
        /// </summary>
        [DoNotSerialize]
        public ValueOutput value { get; private set; }

        protected override void Definition()
        {
            turnOn = ControlInput(nameof(turnOn), TurnOn);
            turnOff = ControlInput(nameof(turnOff), TurnOff);
            toggle = ControlInput(nameof(toggle), Toggle);

            onValue = ValueInput<object>(nameof(onValue));
            offValue = ValueInput<object>(nameof(offValue));

            turnedOn = ControlOutput(nameof(turnedOn));
            turnedOff = ControlOutput(nameof(turnedOff));

            isOn = ValueOutput(nameof(isOn), IsOn);
            value = ValueOutput(nameof(value), Value);

            Requirement(onValue, value);
            Requirement(offValue, value);
            Succession(turnOn, turnedOn);
            Succession(turnOff, turnedOff);
            Succession(toggle, turnedOn);
            Succession(toggle, turnedOff);
        }

        public IGraphElementData CreateData()
        {
            return new Data() { isOn = startOn };
        }

        private bool IsOn(Flow flow)
        {
            return flow.stack.GetElementData<Data>(this).isOn;
        }

        private ControlOutput TurnOn(Flow flow)
        {
            var data = flow.stack.GetElementData<Data>(this);

            if (data.isOn)
            {
                return null;
            }

            data.isOn = true;

            return turnedOn;
        }

        private ControlOutput TurnOff(Flow flow)
        {
            var data = flow.stack.GetElementData<Data>(this);

            if (!data.isOn)
            {
                return null;
            }

            data.isOn = false;

            return turnedOff;
        }

        private ControlOutput Toggle(Flow flow)
        {
            var data = flow.stack.GetElementData<Data>(this);

            data.isOn = !data.isOn;

            return data.isOn ? turnedOn : turnedOff;
        }

        private object Value(Flow flow)
        {
            var data = flow.stack.GetElementData<Data>(this);

            return flow.GetValue(data.isOn ? onValue : offValue);
        }
    }
}