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.

NumericComparison.cs 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using UnityEngine;
  3. namespace Unity.VisualScripting
  4. {
  5. /// <summary>
  6. /// Compares two numeric inputs.
  7. /// </summary>
  8. [UnitCategory("Logic")]
  9. [UnitTitle("Numeric Comparison")]
  10. [UnitSurtitle("Numeric")]
  11. [UnitShortTitle("Comparison")]
  12. [UnitOrder(99)]
  13. [Obsolete("Use the Comparison node with Numeric enabled instead.")]
  14. public sealed class NumericComparison : Unit
  15. {
  16. /// <summary>
  17. /// The first input.
  18. /// </summary>
  19. [DoNotSerialize]
  20. public ValueInput a { get; private set; }
  21. /// <summary>
  22. /// The second input.
  23. /// </summary>
  24. [DoNotSerialize]
  25. public ValueInput b { get; private set; }
  26. /// <summary>
  27. /// Whether A is less than B.
  28. /// </summary>
  29. [DoNotSerialize]
  30. [PortLabel("A < B")]
  31. public ValueOutput aLessThanB { get; private set; }
  32. /// <summary>
  33. /// Whether A is less than or equal to B.
  34. /// </summary>
  35. [DoNotSerialize]
  36. [PortLabel("A \u2264 B")]
  37. public ValueOutput aLessThanOrEqualToB { get; private set; }
  38. /// <summary>
  39. /// Whether A is equal to B.
  40. /// </summary>
  41. [DoNotSerialize]
  42. [PortLabel("A = B")]
  43. public ValueOutput aEqualToB { get; private set; }
  44. /// <summary>
  45. /// Whether A is greater than or equal to B.
  46. /// </summary>
  47. [DoNotSerialize]
  48. [PortLabel("A \u2265 B")]
  49. public ValueOutput aGreaterThanOrEqualToB { get; private set; }
  50. /// <summary>
  51. /// Whether A is greater than B.
  52. /// </summary>
  53. [DoNotSerialize]
  54. [PortLabel("A > B")]
  55. public ValueOutput aGreatherThanB { get; private set; }
  56. protected override void Definition()
  57. {
  58. a = ValueInput<float>(nameof(a));
  59. b = ValueInput<float>(nameof(b), 0);
  60. aLessThanB = ValueOutput(nameof(aLessThanB), Less).Predictable();
  61. aLessThanOrEqualToB = ValueOutput(nameof(aLessThanOrEqualToB), LessOrEqual).Predictable();
  62. aEqualToB = ValueOutput(nameof(aEqualToB), Equal).Predictable();
  63. aGreaterThanOrEqualToB = ValueOutput(nameof(aGreaterThanOrEqualToB), GreaterOrEqual).Predictable();
  64. aGreatherThanB = ValueOutput(nameof(aGreatherThanB), Greater).Predictable();
  65. Requirement(a, aLessThanB);
  66. Requirement(b, aLessThanB);
  67. Requirement(a, aLessThanOrEqualToB);
  68. Requirement(b, aLessThanOrEqualToB);
  69. Requirement(a, aEqualToB);
  70. Requirement(b, aEqualToB);
  71. Requirement(a, aGreaterThanOrEqualToB);
  72. Requirement(b, aGreaterThanOrEqualToB);
  73. Requirement(a, aGreatherThanB);
  74. Requirement(b, aGreatherThanB);
  75. }
  76. private bool Less(Flow flow)
  77. {
  78. return flow.GetValue<float>(a) < flow.GetValue<float>(b);
  79. }
  80. private bool LessOrEqual(Flow flow)
  81. {
  82. var a = flow.GetValue<float>(this.a);
  83. var b = flow.GetValue<float>(this.b);
  84. return a < b || Mathf.Approximately(a, b);
  85. }
  86. private bool Equal(Flow flow)
  87. {
  88. return Mathf.Approximately(flow.GetValue<float>(a), flow.GetValue<float>(b));
  89. }
  90. private bool GreaterOrEqual(Flow flow)
  91. {
  92. var a = flow.GetValue<float>(this.a);
  93. var b = flow.GetValue<float>(this.b);
  94. return a > b || Mathf.Approximately(a, b);
  95. }
  96. private bool Greater(Flow flow)
  97. {
  98. return flow.GetValue<float>(a) < flow.GetValue<float>(b);
  99. }
  100. }
  101. }