File size: 3,928 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
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace Unity.VisualScripting
{
    public interface IUnit : IGraphElementWithDebugData
    {
        new FlowGraph graph { get; }

        #region Definition

        bool canDefine { get; }

        bool isDefined { get; }

        bool failedToDefine { get; }

        Exception definitionException { get; }

        void Define();

        void EnsureDefined();

        void RemoveUnconnectedInvalidPorts();

        #endregion

        #region Default Values

        Dictionary<string, object> defaultValues { get; }

        #endregion

        #region Ports

        IUnitPortCollection<ControlInput> controlInputs { get; }

        IUnitPortCollection<ControlOutput> controlOutputs { get; }

        IUnitPortCollection<ValueInput> valueInputs { get; }

        IUnitPortCollection<ValueOutput> valueOutputs { get; }

        IUnitPortCollection<InvalidInput> invalidInputs { get; }

        IUnitPortCollection<InvalidOutput> invalidOutputs { get; }

        IEnumerable<IUnitInputPort> inputs { get; }

        IEnumerable<IUnitOutputPort> outputs { get; }

        IEnumerable<IUnitInputPort> validInputs { get; }

        IEnumerable<IUnitOutputPort> validOutputs { get; }

        IEnumerable<IUnitPort> ports { get; }

        IEnumerable<IUnitPort> invalidPorts { get; }

        IEnumerable<IUnitPort> validPorts { get; }

        void PortsChanged();

        event Action onPortsChanged;

        #endregion

        #region Connections

        IConnectionCollection<IUnitRelation, IUnitPort, IUnitPort> relations { get; }

        IEnumerable<IUnitConnection> connections { get; }

        #endregion

        #region Analysis

        bool isControlRoot { get; }

        #endregion

        #region Widget

        Vector2 position { get; set; }

        #endregion
    }

    public static class XUnit
    {
        public static ValueInput CompatibleValueInput(this IUnit unit, Type outputType)
        {
            Ensure.That(nameof(outputType)).IsNotNull(outputType);

            return unit.valueInputs
                .Where(valueInput => ConversionUtility.CanConvert(outputType, valueInput.type, false))
                .OrderBy((valueInput) =>
                {
                    var exactType = outputType == valueInput.type;
                    var free = !valueInput.hasValidConnection;

                    if (free && exactType)
                    {
                        return 1;
                    }
                    else if (free)
                    {
                        return 2;
                    }
                    else if (exactType)
                    {
                        return 3;
                    }
                    else
                    {
                        return 4;
                    }
                }).FirstOrDefault();
        }

        public static ValueOutput CompatibleValueOutput(this IUnit unit, Type inputType)
        {
            Ensure.That(nameof(inputType)).IsNotNull(inputType);

            return unit.valueOutputs
                .Where(valueOutput => ConversionUtility.CanConvert(valueOutput.type, inputType, false))
                .OrderBy((valueOutput) =>
                {
                    var exactType = inputType == valueOutput.type;
                    var free = !valueOutput.hasValidConnection;

                    if (free && exactType)
                    {
                        return 1;
                    }
                    else if (free)
                    {
                        return 2;
                    }
                    else if (exactType)
                    {
                        return 3;
                    }
                    else
                    {
                        return 4;
                    }
                }).FirstOrDefault();
        }
    }
}