File size: 2,974 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
using System;
using UnityEngine;

namespace Unity.VisualScripting
{
    [SerializationVersion("A")]
    public sealed class FlowStateTransition : NesterStateTransition<FlowGraph, ScriptGraphAsset>, IGraphEventListener
    {
        public FlowStateTransition() : base() { }

        public FlowStateTransition(IState source, IState destination) : base(source, destination)
        {
            if (!source.canBeSource)
            {
                throw new InvalidOperationException("Source state cannot emit transitions.");
            }

            if (!destination.canBeDestination)
            {
                throw new InvalidOperationException("Destination state cannot receive transitions.");
            }
        }

        public static FlowStateTransition WithDefaultTrigger(IState source, IState destination)
        {
            var flowStateTransition = new FlowStateTransition(source, destination);
            flowStateTransition.nest.source = GraphSource.Embed;
            flowStateTransition.nest.embed = GraphWithDefaultTrigger();
            return flowStateTransition;
        }

        public static FlowGraph GraphWithDefaultTrigger()
        {
            return new FlowGraph()
            {
                units =
                {
                    new TriggerStateTransition() { position = new Vector2(100, -50) }
                }
            };
        }

        #region Lifecycle

        public override void OnEnter(Flow flow)
        {
            if (flow.stack.TryEnterParentElement(this))
            {
                flow.stack.TriggerEventHandler(hook => hook == StateEventHooks.OnEnterState, new EmptyEventArgs(), parent => parent is SubgraphUnit, false);
                flow.stack.ExitParentElement();
            }
        }

        public override void OnExit(Flow flow)
        {
            if (flow.stack.TryEnterParentElement(this))
            {
                flow.stack.TriggerEventHandler(hook => hook == StateEventHooks.OnExitState, new EmptyEventArgs(), parent => parent is SubgraphUnit, false);
                nest.graph.StopListening(flow.stack);
                flow.stack.ExitParentElement();
            }
        }

        public void StartListening(GraphStack stack)
        {
            if (stack.TryEnterParentElement(this))
            {
                nest.graph.StartListening(stack);
                stack.ExitParentElement();
            }
        }

        public void StopListening(GraphStack stack)
        {
            if (stack.TryEnterParentElement(this))
            {
                nest.graph.StopListening(stack);
                stack.ExitParentElement();
            }
        }

        public bool IsListening(GraphPointer pointer)
        {
            return pointer.GetElementData<State.Data>(source).isActive;
        }

        #endregion


        public override FlowGraph DefaultGraph()
        {
            return GraphWithDefaultTrigger();
        }
    }
}