File size: 1,697 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 | using System.Collections.Generic;
using System.Linq;
namespace Unity.VisualScripting
{
[UnitOrder(303)]
[TypeIcon(typeof(Add<>))]
public abstract class Sum<T> : MultiInputUnit<T>
{
/// <summary>
/// The sum.
/// </summary>
[DoNotSerialize]
[PortLabelHidden]
public ValueOutput sum { get; private set; }
protected override void Definition()
{
if (this is IDefaultValue<T> defaultValueUnit)
{
var mi = new List<ValueInput>();
multiInputs = mi.AsReadOnly();
for (var i = 0; i < inputCount; i++)
{
if (i == 0)
{
mi.Add(ValueInput<T>(i.ToString()));
continue;
}
mi.Add(ValueInput(i.ToString(), defaultValueUnit.defaultValue));
}
}
else
{
base.Definition();
}
sum = ValueOutput(nameof(sum), Operation).Predictable();
foreach (var multiInput in multiInputs)
{
Requirement(multiInput, sum);
}
}
public abstract T Operation(T a, T b);
public abstract T Operation(IEnumerable<T> values);
public T Operation(Flow flow)
{
if (inputCount == 2)
{
return Operation(flow.GetValue<T>(multiInputs[0]), flow.GetValue<T>(multiInputs[1]));
}
else
{
return Operation(multiInputs.Select(flow.GetValue<T>));
}
}
}
}
|