File size: 1,941 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
namespace Unity.VisualScripting
{
    [UnitCategory("Logic")]
    public abstract class BinaryComparisonUnit : 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; }

        [DoNotSerialize]
        public virtual ValueOutput comparison { get; private set; }

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

        // Backwards compatibility
        protected virtual string outputKey => nameof(comparison);

        protected override void Definition()
        {
            if (numeric)
            {
                a = ValueInput<float>(nameof(a));
                b = ValueInput<float>(nameof(b), 0);
                comparison = ValueOutput(outputKey, NumericComparison).Predictable();
            }
            else
            {
                a = ValueInput<object>(nameof(a)).AllowsNull();
                b = ValueInput<object>(nameof(b)).AllowsNull();
                comparison = ValueOutput(outputKey, GenericComparison).Predictable();
            }

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

        private bool NumericComparison(Flow flow)
        {
            return NumericComparison(flow.GetValue<float>(a), flow.GetValue<float>(b));
        }

        private bool GenericComparison(Flow flow)
        {
            return GenericComparison(flow.GetValue(a), flow.GetValue(b));
        }

        protected abstract bool NumericComparison(float a, float b);

        protected abstract bool GenericComparison(object a, object b);
    }
}