File size: 2,244 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
namespace Unity.VisualScripting
{
    [TypeIcon(typeof(StateGraph))]
    [UnitCategory("Nesting")]
    public sealed class StateUnit : NesterUnit<StateGraph, StateGraphAsset>
    {
        public StateUnit() : base() { }

        public StateUnit(StateGraphAsset macro) : base(macro) { }

        /// <summary>
        /// The entry point to start the state graph.
        /// </summary>
        [DoNotSerialize]
        public ControlInput start { get; private set; }

        /// <summary>
        /// The entry point to stop the state graph.
        /// </summary>
        [DoNotSerialize]
        public ControlInput stop { get; private set; }

        /// <summary>
        /// The action to execute after the state graph has been started.
        /// </summary>
        [DoNotSerialize]
        public ControlOutput started { get; private set; }

        /// <summary>
        /// The action to execute after the state graph has been stopped.
        /// </summary>
        [DoNotSerialize]
        public ControlOutput stopped { get; private set; }

        public static StateUnit WithStart()
        {
            var stateUnit = new StateUnit();
            stateUnit.nest.source = GraphSource.Embed;
            stateUnit.nest.embed = StateGraph.WithStart();
            return stateUnit;
        }

        protected override void Definition()
        {
            start = ControlInput(nameof(start), Start);
            stop = ControlInput(nameof(stop), Stop);

            started = ControlOutput(nameof(started));
            stopped = ControlOutput(nameof(stopped));

            Succession(start, started);
            Succession(stop, stopped);
        }

        private ControlOutput Start(Flow flow)
        {
            flow.stack.EnterParentElement(this);
            nest.graph.Start(flow);
            flow.stack.ExitParentElement();
            return started;
        }

        private ControlOutput Stop(Flow flow)
        {
            flow.stack.EnterParentElement(this);
            nest.graph.Stop(flow);
            flow.stack.ExitParentElement();
            return stopped;
        }

        public override StateGraph DefaultGraph()
        {
            return StateGraph.WithStart();
        }
    }
}