namespace Unity.VisualScripting { [UnitOrder(202)] public abstract class Round : Unit { public enum Rounding { Floor = 0, Ceiling = 1, AwayFromZero = 2, } /// /// The rounding mode. /// [Inspectable, UnitHeaderInspectable, Serialize] public Rounding rounding { get; set; } = Rounding.AwayFromZero; /// /// The value to round. /// [DoNotSerialize] [PortLabelHidden] public ValueInput input { get; private set; } /// /// The rounded value. /// [DoNotSerialize] [PortLabelHidden] public ValueOutput output { get; private set; } protected override void Definition() { input = ValueInput(nameof(input)); output = ValueOutput(nameof(output), Operation).Predictable(); Requirement(input, output); } protected abstract TOutput Floor(TInput input); protected abstract TOutput AwayFromZero(TInput input); protected abstract TOutput Ceiling(TInput input); public TOutput Operation(Flow flow) { switch (rounding) { case Rounding.Floor: return Floor(flow.GetValue(input)); case Rounding.AwayFromZero: return AwayFromZero(flow.GetValue(input)); case Rounding.Ceiling: return Ceiling(flow.GetValue(input)); default: throw new UnexpectedEnumValueException(rounding); } } } }