File size: 6,254 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using UnityEngine;

namespace Unity.VisualScripting
{
    /// <summary>
    /// Compares two inputs.
    /// </summary>
    [UnitCategory("Logic")]
    [UnitTitle("Comparison")]
    [UnitShortTitle("Comparison")]
    [UnitOrder(99)]
    public sealed class Comparison : Unit
    {
        /// <summary>
        /// The first input.
        /// </summary>
        [DoNotSerialize]
        public ValueInput a { get; private set; }

        /// <summary>
        /// The second input.
        /// </summary>
        [DoNotSerialize]
        public ValueInput b { get; private set; }

        /// <summary>
        /// Whether the compared inputs are numbers.
        /// </summary>
        [Serialize]
        [Inspectable]
        public bool numeric { get; set; } = true;

        /// <summary>
        /// Whether A is less than B.
        /// </summary>
        [DoNotSerialize]
        [PortLabel("A < B")]
        public ValueOutput aLessThanB { get; private set; }

        /// <summary>
        /// Whether A is less than or equal to B.
        /// </summary>
        [DoNotSerialize]
        [PortLabel("A \u2264 B")]
        public ValueOutput aLessThanOrEqualToB { get; private set; }

        /// <summary>
        /// Whether A is equal to B.
        /// </summary>
        [DoNotSerialize]
        [PortLabel("A = B")]
        public ValueOutput aEqualToB { get; private set; }

        /// <summary>
        /// Whether A is not equal to B.
        /// </summary>
        [DoNotSerialize]
        [PortLabel("A \u2260 B")]
        public ValueOutput aNotEqualToB { get; private set; }

        /// <summary>
        /// Whether A is greater than or equal to B.
        /// </summary>
        [DoNotSerialize]
        [PortLabel("A \u2265 B")]
        public ValueOutput aGreaterThanOrEqualToB { get; private set; }

        /// <summary>
        /// Whether A is greater than B.
        /// </summary>
        [DoNotSerialize]
        [PortLabel("A > B")]
        public ValueOutput aGreatherThanB { get; private set; }

        protected override void Definition()
        {
            if (numeric)
            {
                a = ValueInput<float>(nameof(a));
                b = ValueInput<float>(nameof(b), 0);

                aLessThanB = ValueOutput(nameof(aLessThanB), (flow) => NumericLess(flow.GetValue<float>(a), flow.GetValue<float>(b))).Predictable();
                aLessThanOrEqualToB = ValueOutput(nameof(aLessThanOrEqualToB), (flow) => NumericLessOrEqual(flow.GetValue<float>(a), flow.GetValue<float>(b))).Predictable();
                aEqualToB = ValueOutput(nameof(aEqualToB), (flow) => NumericEqual(flow.GetValue<float>(a), flow.GetValue<float>(b))).Predictable();
                aNotEqualToB = ValueOutput(nameof(aNotEqualToB), (flow) => NumericNotEqual(flow.GetValue<float>(a), flow.GetValue<float>(b))).Predictable();
                aGreaterThanOrEqualToB = ValueOutput(nameof(aGreaterThanOrEqualToB), (flow) => NumericGreaterOrEqual(flow.GetValue<float>(a), flow.GetValue<float>(b))).Predictable();
                aGreatherThanB = ValueOutput(nameof(aGreatherThanB), (flow) => NumericGreater(flow.GetValue<float>(a), flow.GetValue<float>(b))).Predictable();
            }
            else
            {
                a = ValueInput<object>(nameof(a)).AllowsNull();
                b = ValueInput<object>(nameof(b)).AllowsNull();

                aLessThanB = ValueOutput(nameof(aLessThanB), (flow) => GenericLess(flow.GetValue(a), flow.GetValue(b)));
                aLessThanOrEqualToB = ValueOutput(nameof(aLessThanOrEqualToB), (flow) => GenericLessOrEqual(flow.GetValue(a), flow.GetValue(b)));
                aEqualToB = ValueOutput(nameof(aEqualToB), (flow) => GenericEqual(flow.GetValue(a), flow.GetValue(b)));
                aNotEqualToB = ValueOutput(nameof(aNotEqualToB), (flow) => GenericNotEqual(flow.GetValue(a), flow.GetValue(b)));
                aGreaterThanOrEqualToB = ValueOutput(nameof(aGreaterThanOrEqualToB), (flow) => GenericGreaterOrEqual(flow.GetValue(a), flow.GetValue(b)));
                aGreatherThanB = ValueOutput(nameof(aGreatherThanB), (flow) => GenericGreater(flow.GetValue(a), flow.GetValue(b)));
            }

            Requirement(a, aLessThanB);
            Requirement(b, aLessThanB);

            Requirement(a, aLessThanOrEqualToB);
            Requirement(b, aLessThanOrEqualToB);

            Requirement(a, aEqualToB);
            Requirement(b, aEqualToB);

            Requirement(a, aNotEqualToB);
            Requirement(b, aNotEqualToB);

            Requirement(a, aGreaterThanOrEqualToB);
            Requirement(b, aGreaterThanOrEqualToB);

            Requirement(a, aGreatherThanB);
            Requirement(b, aGreatherThanB);
        }

        private bool NumericLess(float a, float b)
        {
            return a < b;
        }

        private bool NumericLessOrEqual(float a, float b)
        {
            return a < b || Mathf.Approximately(a, b);
        }

        private bool NumericEqual(float a, float b)
        {
            return Mathf.Approximately(a, b);
        }

        private bool NumericNotEqual(float a, float b)
        {
            return !Mathf.Approximately(a, b);
        }

        private bool NumericGreaterOrEqual(float a, float b)
        {
            return a > b || Mathf.Approximately(a, b);
        }

        private bool NumericGreater(float a, float b)
        {
            return a > b;
        }

        private bool GenericLess(object a, object b)
        {
            return OperatorUtility.LessThan(a, b);
        }

        private bool GenericLessOrEqual(object a, object b)
        {
            return OperatorUtility.LessThanOrEqual(a, b);
        }

        private bool GenericEqual(object a, object b)
        {
            return OperatorUtility.Equal(a, b);
        }

        private bool GenericNotEqual(object a, object b)
        {
            return OperatorUtility.NotEqual(a, b);
        }

        private bool GenericGreaterOrEqual(object a, object b)
        {
            return OperatorUtility.GreaterThanOrEqual(a, b);
        }

        private bool GenericGreater(object a, object b)
        {
            return OperatorUtility.GreaterThan(a, b);
        }
    }
}