No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Round.cs 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. namespace Unity.VisualScripting
  2. {
  3. [UnitOrder(202)]
  4. public abstract class Round<TInput, TOutput> : Unit
  5. {
  6. public enum Rounding
  7. {
  8. Floor = 0,
  9. Ceiling = 1,
  10. AwayFromZero = 2,
  11. }
  12. /// <summary>
  13. /// The rounding mode.
  14. /// </summary>
  15. [Inspectable, UnitHeaderInspectable, Serialize]
  16. public Rounding rounding { get; set; } = Rounding.AwayFromZero;
  17. /// <summary>
  18. /// The value to round.
  19. /// </summary>
  20. [DoNotSerialize]
  21. [PortLabelHidden]
  22. public ValueInput input { get; private set; }
  23. /// <summary>
  24. /// The rounded value.
  25. /// </summary>
  26. [DoNotSerialize]
  27. [PortLabelHidden]
  28. public ValueOutput output { get; private set; }
  29. protected override void Definition()
  30. {
  31. input = ValueInput<TInput>(nameof(input));
  32. output = ValueOutput(nameof(output), Operation).Predictable();
  33. Requirement(input, output);
  34. }
  35. protected abstract TOutput Floor(TInput input);
  36. protected abstract TOutput AwayFromZero(TInput input);
  37. protected abstract TOutput Ceiling(TInput input);
  38. public TOutput Operation(Flow flow)
  39. {
  40. switch (rounding)
  41. {
  42. case Rounding.Floor:
  43. return Floor(flow.GetValue<TInput>(input));
  44. case Rounding.AwayFromZero:
  45. return AwayFromZero(flow.GetValue<TInput>(input));
  46. case Rounding.Ceiling:
  47. return Ceiling(flow.GetValue<TInput>(input));
  48. default:
  49. throw new UnexpectedEnumValueException<Rounding>(rounding);
  50. }
  51. }
  52. }
  53. }