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.

Comparison.cs 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using UnityEngine;
  2. namespace Unity.VisualScripting
  3. {
  4. /// <summary>
  5. /// Compares two inputs.
  6. /// </summary>
  7. [UnitCategory("Logic")]
  8. [UnitTitle("Comparison")]
  9. [UnitShortTitle("Comparison")]
  10. [UnitOrder(99)]
  11. public sealed class Comparison : Unit
  12. {
  13. /// <summary>
  14. /// The first input.
  15. /// </summary>
  16. [DoNotSerialize]
  17. public ValueInput a { get; private set; }
  18. /// <summary>
  19. /// The second input.
  20. /// </summary>
  21. [DoNotSerialize]
  22. public ValueInput b { get; private set; }
  23. /// <summary>
  24. /// Whether the compared inputs are numbers.
  25. /// </summary>
  26. [Serialize]
  27. [Inspectable]
  28. public bool numeric { get; set; } = true;
  29. /// <summary>
  30. /// Whether A is less than B.
  31. /// </summary>
  32. [DoNotSerialize]
  33. [PortLabel("A < B")]
  34. public ValueOutput aLessThanB { get; private set; }
  35. /// <summary>
  36. /// Whether A is less than or equal to B.
  37. /// </summary>
  38. [DoNotSerialize]
  39. [PortLabel("A \u2264 B")]
  40. public ValueOutput aLessThanOrEqualToB { get; private set; }
  41. /// <summary>
  42. /// Whether A is equal to B.
  43. /// </summary>
  44. [DoNotSerialize]
  45. [PortLabel("A = B")]
  46. public ValueOutput aEqualToB { get; private set; }
  47. /// <summary>
  48. /// Whether A is not equal to B.
  49. /// </summary>
  50. [DoNotSerialize]
  51. [PortLabel("A \u2260 B")]
  52. public ValueOutput aNotEqualToB { get; private set; }
  53. /// <summary>
  54. /// Whether A is greater than or equal to B.
  55. /// </summary>
  56. [DoNotSerialize]
  57. [PortLabel("A \u2265 B")]
  58. public ValueOutput aGreaterThanOrEqualToB { get; private set; }
  59. /// <summary>
  60. /// Whether A is greater than B.
  61. /// </summary>
  62. [DoNotSerialize]
  63. [PortLabel("A > B")]
  64. public ValueOutput aGreatherThanB { get; private set; }
  65. protected override void Definition()
  66. {
  67. if (numeric)
  68. {
  69. a = ValueInput<float>(nameof(a));
  70. b = ValueInput<float>(nameof(b), 0);
  71. aLessThanB = ValueOutput(nameof(aLessThanB), (flow) => NumericLess(flow.GetValue<float>(a), flow.GetValue<float>(b))).Predictable();
  72. aLessThanOrEqualToB = ValueOutput(nameof(aLessThanOrEqualToB), (flow) => NumericLessOrEqual(flow.GetValue<float>(a), flow.GetValue<float>(b))).Predictable();
  73. aEqualToB = ValueOutput(nameof(aEqualToB), (flow) => NumericEqual(flow.GetValue<float>(a), flow.GetValue<float>(b))).Predictable();
  74. aNotEqualToB = ValueOutput(nameof(aNotEqualToB), (flow) => NumericNotEqual(flow.GetValue<float>(a), flow.GetValue<float>(b))).Predictable();
  75. aGreaterThanOrEqualToB = ValueOutput(nameof(aGreaterThanOrEqualToB), (flow) => NumericGreaterOrEqual(flow.GetValue<float>(a), flow.GetValue<float>(b))).Predictable();
  76. aGreatherThanB = ValueOutput(nameof(aGreatherThanB), (flow) => NumericGreater(flow.GetValue<float>(a), flow.GetValue<float>(b))).Predictable();
  77. }
  78. else
  79. {
  80. a = ValueInput<object>(nameof(a)).AllowsNull();
  81. b = ValueInput<object>(nameof(b)).AllowsNull();
  82. aLessThanB = ValueOutput(nameof(aLessThanB), (flow) => GenericLess(flow.GetValue(a), flow.GetValue(b)));
  83. aLessThanOrEqualToB = ValueOutput(nameof(aLessThanOrEqualToB), (flow) => GenericLessOrEqual(flow.GetValue(a), flow.GetValue(b)));
  84. aEqualToB = ValueOutput(nameof(aEqualToB), (flow) => GenericEqual(flow.GetValue(a), flow.GetValue(b)));
  85. aNotEqualToB = ValueOutput(nameof(aNotEqualToB), (flow) => GenericNotEqual(flow.GetValue(a), flow.GetValue(b)));
  86. aGreaterThanOrEqualToB = ValueOutput(nameof(aGreaterThanOrEqualToB), (flow) => GenericGreaterOrEqual(flow.GetValue(a), flow.GetValue(b)));
  87. aGreatherThanB = ValueOutput(nameof(aGreatherThanB), (flow) => GenericGreater(flow.GetValue(a), flow.GetValue(b)));
  88. }
  89. Requirement(a, aLessThanB);
  90. Requirement(b, aLessThanB);
  91. Requirement(a, aLessThanOrEqualToB);
  92. Requirement(b, aLessThanOrEqualToB);
  93. Requirement(a, aEqualToB);
  94. Requirement(b, aEqualToB);
  95. Requirement(a, aNotEqualToB);
  96. Requirement(b, aNotEqualToB);
  97. Requirement(a, aGreaterThanOrEqualToB);
  98. Requirement(b, aGreaterThanOrEqualToB);
  99. Requirement(a, aGreatherThanB);
  100. Requirement(b, aGreatherThanB);
  101. }
  102. private bool NumericLess(float a, float b)
  103. {
  104. return a < b;
  105. }
  106. private bool NumericLessOrEqual(float a, float b)
  107. {
  108. return a < b || Mathf.Approximately(a, b);
  109. }
  110. private bool NumericEqual(float a, float b)
  111. {
  112. return Mathf.Approximately(a, b);
  113. }
  114. private bool NumericNotEqual(float a, float b)
  115. {
  116. return !Mathf.Approximately(a, b);
  117. }
  118. private bool NumericGreaterOrEqual(float a, float b)
  119. {
  120. return a > b || Mathf.Approximately(a, b);
  121. }
  122. private bool NumericGreater(float a, float b)
  123. {
  124. return a > b;
  125. }
  126. private bool GenericLess(object a, object b)
  127. {
  128. return OperatorUtility.LessThan(a, b);
  129. }
  130. private bool GenericLessOrEqual(object a, object b)
  131. {
  132. return OperatorUtility.LessThanOrEqual(a, b);
  133. }
  134. private bool GenericEqual(object a, object b)
  135. {
  136. return OperatorUtility.Equal(a, b);
  137. }
  138. private bool GenericNotEqual(object a, object b)
  139. {
  140. return OperatorUtility.NotEqual(a, b);
  141. }
  142. private bool GenericGreaterOrEqual(object a, object b)
  143. {
  144. return OperatorUtility.GreaterThanOrEqual(a, b);
  145. }
  146. private bool GenericGreater(object a, object b)
  147. {
  148. return OperatorUtility.GreaterThan(a, b);
  149. }
  150. }
  151. }