This commit is contained in:
2021-06-13 10:28:03 +02:00
parent eb70603c85
commit df2d24cbd3
7487 changed files with 943244 additions and 0 deletions

View File

@@ -0,0 +1,44 @@
namespace Unity.VisualScripting
{
/// <summary>
/// Returns true if both inputs are true.
/// </summary>
[UnitCategory("Logic")]
[UnitOrder(0)]
public sealed class And : Unit
{
/// <summary>
/// The first boolean.
/// </summary>
[DoNotSerialize]
public ValueInput a { get; private set; }
/// <summary>
/// The second boolean.
/// </summary>
[DoNotSerialize]
public ValueInput b { get; private set; }
/// <summary>
/// True if A and B are both true; false otherwise.
/// </summary>
[DoNotSerialize]
[PortLabel("A & B")]
public ValueOutput result { get; private set; }
protected override void Definition()
{
a = ValueInput<bool>(nameof(a));
b = ValueInput<bool>(nameof(b));
result = ValueOutput(nameof(result), Operation).Predictable();
Requirement(a, result);
Requirement(b, result);
}
public bool Operation(Flow flow)
{
return flow.GetValue<bool>(a) && flow.GetValue<bool>(b);
}
}
}

View File

@@ -0,0 +1,50 @@
using System;
using UnityEngine;
namespace Unity.VisualScripting
{
/// <summary>
/// Compares two numbers to determine if they are approximately equal (disregarding floating point precision errors).
/// </summary>
[UnitCategory("Logic")]
[UnitShortTitle("Equal")]
[UnitSubtitle("(Approximately)")]
[UnitOrder(7)]
[Obsolete("Use the Equal unit with Numeric enabled instead.")]
public sealed class ApproximatelyEqual : Unit
{
/// <summary>
/// The first number.
/// </summary>
[DoNotSerialize]
public ValueInput a { get; private set; }
/// <summary>
/// The second number.
/// </summary>
[DoNotSerialize]
public ValueInput b { get; private set; }
/// <summary>
/// Whether A is approximately equal to B.
/// </summary>
[DoNotSerialize]
[PortLabel("A \u2248 B")]
public ValueOutput equal { get; private set; }
protected override void Definition()
{
a = ValueInput<float>(nameof(a));
b = ValueInput<float>(nameof(b), 0);
equal = ValueOutput(nameof(equal), Comparison).Predictable();
Requirement(a, equal);
Requirement(b, equal);
}
public bool Comparison(Flow flow)
{
return Mathf.Approximately(flow.GetValue<float>(a), flow.GetValue<float>(b));
}
}
}

View File

@@ -0,0 +1,65 @@
namespace Unity.VisualScripting
{
[UnitCategory("Logic")]
public abstract class BinaryComparisonUnit : Unit
{
/// <summary>
/// The first input.
/// </summary>
[DoNotSerialize]
public ValueInput a { get; private set; }
/// <summary>
/// The second input.
/// </summary>
[DoNotSerialize]
public ValueInput b { get; private set; }
[DoNotSerialize]
public virtual ValueOutput comparison { get; private set; }
/// <summary>
/// Whether the compared inputs are numbers.
/// </summary>
[Serialize]
[Inspectable]
[InspectorToggleLeft]
public bool numeric { get; set; } = true;
// Backwards compatibility
protected virtual string outputKey => nameof(comparison);
protected override void Definition()
{
if (numeric)
{
a = ValueInput<float>(nameof(a));
b = ValueInput<float>(nameof(b), 0);
comparison = ValueOutput(outputKey, NumericComparison).Predictable();
}
else
{
a = ValueInput<object>(nameof(a)).AllowsNull();
b = ValueInput<object>(nameof(b)).AllowsNull();
comparison = ValueOutput(outputKey, GenericComparison).Predictable();
}
Requirement(a, comparison);
Requirement(b, comparison);
}
private bool NumericComparison(Flow flow)
{
return NumericComparison(flow.GetValue<float>(a), flow.GetValue<float>(b));
}
private bool GenericComparison(Flow flow)
{
return GenericComparison(flow.GetValue(a), flow.GetValue(b));
}
protected abstract bool NumericComparison(float a, float b);
protected abstract bool GenericComparison(object a, object b);
}
}

View File

@@ -0,0 +1,181 @@
using UnityEngine;
namespace Unity.VisualScripting
{
/// <summary>
/// Compares two inputs.
/// </summary>
[UnitCategory("Logic")]
[UnitTitle("Comparison")]
[UnitShortTitle("Comparison")]
[UnitOrder(99)]
public sealed class Comparison : Unit
{
/// <summary>
/// The first input.
/// </summary>
[DoNotSerialize]
public ValueInput a { get; private set; }
/// <summary>
/// The second input.
/// </summary>
[DoNotSerialize]
public ValueInput b { get; private set; }
/// <summary>
/// Whether the compared inputs are numbers.
/// </summary>
[Serialize]
[Inspectable]
public bool numeric { get; set; } = true;
/// <summary>
/// Whether A is less than B.
/// </summary>
[DoNotSerialize]
[PortLabel("A < B")]
public ValueOutput aLessThanB { get; private set; }
/// <summary>
/// Whether A is less than or equal to B.
/// </summary>
[DoNotSerialize]
[PortLabel("A \u2264 B")]
public ValueOutput aLessThanOrEqualToB { get; private set; }
/// <summary>
/// Whether A is equal to B.
/// </summary>
[DoNotSerialize]
[PortLabel("A = B")]
public ValueOutput aEqualToB { get; private set; }
/// <summary>
/// Whether A is not equal to B.
/// </summary>
[DoNotSerialize]
[PortLabel("A \u2260 B")]
public ValueOutput aNotEqualToB { get; private set; }
/// <summary>
/// Whether A is greater than or equal to B.
/// </summary>
[DoNotSerialize]
[PortLabel("A \u2265 B")]
public ValueOutput aGreaterThanOrEqualToB { get; private set; }
/// <summary>
/// Whether A is greater than B.
/// </summary>
[DoNotSerialize]
[PortLabel("A > B")]
public ValueOutput aGreatherThanB { get; private set; }
protected override void Definition()
{
if (numeric)
{
a = ValueInput<float>(nameof(a));
b = ValueInput<float>(nameof(b), 0);
aLessThanB = ValueOutput(nameof(aLessThanB), (flow) => NumericLess(flow.GetValue<float>(a), flow.GetValue<float>(b))).Predictable();
aLessThanOrEqualToB = ValueOutput(nameof(aLessThanOrEqualToB), (flow) => NumericLessOrEqual(flow.GetValue<float>(a), flow.GetValue<float>(b))).Predictable();
aEqualToB = ValueOutput(nameof(aEqualToB), (flow) => NumericEqual(flow.GetValue<float>(a), flow.GetValue<float>(b))).Predictable();
aNotEqualToB = ValueOutput(nameof(aNotEqualToB), (flow) => NumericNotEqual(flow.GetValue<float>(a), flow.GetValue<float>(b))).Predictable();
aGreaterThanOrEqualToB = ValueOutput(nameof(aGreaterThanOrEqualToB), (flow) => NumericGreaterOrEqual(flow.GetValue<float>(a), flow.GetValue<float>(b))).Predictable();
aGreatherThanB = ValueOutput(nameof(aGreatherThanB), (flow) => NumericGreater(flow.GetValue<float>(a), flow.GetValue<float>(b))).Predictable();
}
else
{
a = ValueInput<object>(nameof(a)).AllowsNull();
b = ValueInput<object>(nameof(b)).AllowsNull();
aLessThanB = ValueOutput(nameof(aLessThanB), (flow) => GenericLess(flow.GetValue(a), flow.GetValue(b)));
aLessThanOrEqualToB = ValueOutput(nameof(aLessThanOrEqualToB), (flow) => GenericLessOrEqual(flow.GetValue(a), flow.GetValue(b)));
aEqualToB = ValueOutput(nameof(aEqualToB), (flow) => GenericEqual(flow.GetValue(a), flow.GetValue(b)));
aNotEqualToB = ValueOutput(nameof(aNotEqualToB), (flow) => GenericNotEqual(flow.GetValue(a), flow.GetValue(b)));
aGreaterThanOrEqualToB = ValueOutput(nameof(aGreaterThanOrEqualToB), (flow) => GenericGreaterOrEqual(flow.GetValue(a), flow.GetValue(b)));
aGreatherThanB = ValueOutput(nameof(aGreatherThanB), (flow) => GenericGreater(flow.GetValue(a), flow.GetValue(b)));
}
Requirement(a, aLessThanB);
Requirement(b, aLessThanB);
Requirement(a, aLessThanOrEqualToB);
Requirement(b, aLessThanOrEqualToB);
Requirement(a, aEqualToB);
Requirement(b, aEqualToB);
Requirement(a, aNotEqualToB);
Requirement(b, aNotEqualToB);
Requirement(a, aGreaterThanOrEqualToB);
Requirement(b, aGreaterThanOrEqualToB);
Requirement(a, aGreatherThanB);
Requirement(b, aGreatherThanB);
}
private bool NumericLess(float a, float b)
{
return a < b;
}
private bool NumericLessOrEqual(float a, float b)
{
return a < b || Mathf.Approximately(a, b);
}
private bool NumericEqual(float a, float b)
{
return Mathf.Approximately(a, b);
}
private bool NumericNotEqual(float a, float b)
{
return !Mathf.Approximately(a, b);
}
private bool NumericGreaterOrEqual(float a, float b)
{
return a > b || Mathf.Approximately(a, b);
}
private bool NumericGreater(float a, float b)
{
return a > b;
}
private bool GenericLess(object a, object b)
{
return OperatorUtility.LessThan(a, b);
}
private bool GenericLessOrEqual(object a, object b)
{
return OperatorUtility.LessThanOrEqual(a, b);
}
private bool GenericEqual(object a, object b)
{
return OperatorUtility.Equal(a, b);
}
private bool GenericNotEqual(object a, object b)
{
return OperatorUtility.NotEqual(a, b);
}
private bool GenericGreaterOrEqual(object a, object b)
{
return OperatorUtility.GreaterThanOrEqual(a, b);
}
private bool GenericGreater(object a, object b)
{
return OperatorUtility.GreaterThan(a, b);
}
}
}

View File

@@ -0,0 +1,38 @@
using UnityEngine;
namespace Unity.VisualScripting
{
/// <summary>
/// Compares two inputs to determine whether they are equal.
/// </summary>
[UnitCategory("Logic")]
[UnitOrder(5)]
public sealed class Equal : BinaryComparisonUnit
{
public Equal() : base()
{
numeric = false;
}
// Backward compatibility
protected override string outputKey => "equal";
/// <summary>
/// Whether A is equal to B.
/// </summary>
[DoNotSerialize]
[PortLabel("A = B")]
[PortKey("equal")]
public override ValueOutput comparison => base.comparison;
protected override bool NumericComparison(float a, float b)
{
return Mathf.Approximately(a, b);
}
protected override bool GenericComparison(object a, object b)
{
return OperatorUtility.Equal(a, b);
}
}
}

View File

@@ -0,0 +1,66 @@
using System;
namespace Unity.VisualScripting
{
/// <summary>
/// Compares two inputs to determine if they are equal or not equal.
/// </summary>
[UnitCategory("Logic")]
[UnitTitle("Equality Comparison")]
[UnitSurtitle("Equality")]
[UnitShortTitle("Comparison")]
[UnitOrder(4)]
[Obsolete("Use the Comparison unit instead.")]
public sealed class EqualityComparison : Unit
{
/// <summary>
/// The first input.
/// </summary>
[DoNotSerialize]
public ValueInput a { get; private set; }
/// <summary>
/// The second input.
/// </summary>
[DoNotSerialize]
public ValueInput b { get; private set; }
/// <summary>
/// Whether A is equal to B.
/// </summary>
[DoNotSerialize]
[PortLabel("A = B")]
public ValueOutput equal { get; private set; }
/// <summary>
/// Whether A is different than B.
/// </summary>
[DoNotSerialize]
[PortLabel("A \u2260 B")]
public ValueOutput notEqual { get; private set; }
protected override void Definition()
{
a = ValueInput<object>(nameof(a)).AllowsNull();
b = ValueInput<object>(nameof(b)).AllowsNull();
equal = ValueOutput(nameof(equal), Equal).Predictable();
notEqual = ValueOutput(nameof(notEqual), NotEqual).Predictable();
Requirement(a, equal);
Requirement(b, equal);
Requirement(a, notEqual);
Requirement(b, notEqual);
}
private bool Equal(Flow flow)
{
return OperatorUtility.Equal(flow.GetValue(a), flow.GetValue(b));
}
private bool NotEqual(Flow flow)
{
return OperatorUtility.NotEqual(flow.GetValue(a), flow.GetValue(b));
}
}
}

View File

@@ -0,0 +1,44 @@
namespace Unity.VisualScripting
{
/// <summary>
/// Returns true if one input is true and the other is false.
/// </summary>
[UnitCategory("Logic")]
[UnitOrder(2)]
public sealed class ExclusiveOr : Unit
{
/// <summary>
/// The first boolean.
/// </summary>
[DoNotSerialize]
public ValueInput a { get; private set; }
/// <summary>
/// The second boolean.
/// </summary>
[DoNotSerialize]
public ValueInput b { get; private set; }
/// <summary>
/// True if either A or B is true but not the other; false otherwise.
/// </summary>
[DoNotSerialize]
[PortLabel("A \u2295 B")]
public ValueOutput result { get; private set; }
protected override void Definition()
{
a = ValueInput<bool>(nameof(a));
b = ValueInput<bool>(nameof(b));
result = ValueOutput(nameof(result), Operation).Predictable();
Requirement(a, result);
Requirement(b, result);
}
public bool Operation(Flow flow)
{
return flow.GetValue<bool>(a) ^ flow.GetValue<bool>(b);
}
}
}

View File

@@ -0,0 +1,26 @@
namespace Unity.VisualScripting
{
/// <summary>
/// Compares two inputs to determine whether the first is greater than the second.
/// </summary>
[UnitCategory("Logic")]
[UnitOrder(11)]
public sealed class Greater : BinaryComparisonUnit
{
/// <summary>
/// Whether A is greater than B.
/// </summary>
[PortLabel("A > B")]
public override ValueOutput comparison => base.comparison;
protected override bool NumericComparison(float a, float b)
{
return a > b;
}
protected override bool GenericComparison(object a, object b)
{
return OperatorUtility.GreaterThan(a, b);
}
}
}

View File

@@ -0,0 +1,28 @@
using UnityEngine;
namespace Unity.VisualScripting
{
/// <summary>
/// Compares two inputs to determine whether the first is greater than or equal to the second.
/// </summary>
[UnitCategory("Logic")]
[UnitOrder(12)]
public sealed class GreaterOrEqual : BinaryComparisonUnit
{
/// <summary>
/// Whether A is greater than or equal to B.
/// </summary>
[PortLabel("A \u2265 B")]
public override ValueOutput comparison => base.comparison;
protected override bool NumericComparison(float a, float b)
{
return a > b || Mathf.Approximately(a, b);
}
protected override bool GenericComparison(object a, object b)
{
return OperatorUtility.GreaterThanOrEqual(a, b);
}
}
}

View File

@@ -0,0 +1,26 @@
namespace Unity.VisualScripting
{
/// <summary>
/// Compares two inputs to determine whether the first is less than the second.
/// </summary>
[UnitCategory("Logic")]
[UnitOrder(9)]
public sealed class Less : BinaryComparisonUnit
{
/// <summary>
/// Whether A is less than B.
/// </summary>
[PortLabel("A < B")]
public override ValueOutput comparison => base.comparison;
protected override bool NumericComparison(float a, float b)
{
return a < b;
}
protected override bool GenericComparison(object a, object b)
{
return OperatorUtility.LessThan(a, b);
}
}
}

View File

@@ -0,0 +1,28 @@
using UnityEngine;
namespace Unity.VisualScripting
{
/// <summary>
/// Compares two inputs to determine whether the first is less than or equal to the second.
/// </summary>
[UnitCategory("Logic")]
[UnitOrder(10)]
public sealed class LessOrEqual : BinaryComparisonUnit
{
/// <summary>
/// Whether A is greater than or equal to B.
/// </summary>
[PortLabel("A \u2264 B")]
public override ValueOutput comparison => base.comparison;
protected override bool NumericComparison(float a, float b)
{
return a < b || Mathf.Approximately(a, b);
}
protected override bool GenericComparison(object a, object b)
{
return OperatorUtility.LessThanOrEqual(a, b);
}
}
}

View File

@@ -0,0 +1,37 @@
namespace Unity.VisualScripting
{
/// <summary>
/// Inverts the value of a boolean.
/// </summary>
[UnitCategory("Logic")]
[UnitOrder(3)]
public sealed class Negate : Unit
{
/// <summary>
/// The input boolean.
/// </summary>
[DoNotSerialize]
[PortLabel("X")]
public ValueInput input { get; private set; }
/// <summary>
/// True if the input is false, false if the input is true.
/// </summary>
[DoNotSerialize]
[PortLabel("~X")]
public ValueOutput output { get; private set; }
protected override void Definition()
{
input = ValueInput<bool>(nameof(input));
output = ValueOutput(nameof(output), Operation).Predictable();
Requirement(input, output);
}
public bool Operation(Flow flow)
{
return !flow.GetValue<bool>(input);
}
}
}

View File

@@ -0,0 +1,50 @@
using System;
using UnityEngine;
namespace Unity.VisualScripting
{
/// <summary>
/// Compares two numbers to determine if they are not approximately equal (disregarding floating point precision errors).
/// </summary>
[UnitCategory("Logic")]
[UnitShortTitle("Not Equal")]
[UnitSubtitle("(Approximately)")]
[UnitOrder(8)]
[Obsolete("Use the Not Equal unit with Numeric enabled instead.")]
public sealed class NotApproximatelyEqual : Unit
{
/// <summary>
/// The first number.
/// </summary>
[DoNotSerialize]
public ValueInput a { get; private set; }
/// <summary>
/// The second number.
/// </summary>
[DoNotSerialize]
public ValueInput b { get; private set; }
/// <summary>
/// Whether A is not approximately equal to B.
/// </summary>
[DoNotSerialize]
[PortLabel("A \u2249 B")]
public ValueOutput notEqual { get; private set; }
protected override void Definition()
{
a = ValueInput<float>(nameof(a));
b = ValueInput<float>(nameof(b), 0);
notEqual = ValueOutput(nameof(notEqual), Comparison).Predictable();
Requirement(a, notEqual);
Requirement(b, notEqual);
}
public bool Comparison(Flow flow)
{
return !Mathf.Approximately(flow.GetValue<float>(a), flow.GetValue<float>(b));
}
}
}

View File

@@ -0,0 +1,38 @@
using UnityEngine;
namespace Unity.VisualScripting
{
/// <summary>
/// Compares two inputs to determine whether they are not equal.
/// </summary>
[UnitCategory("Logic")]
[UnitOrder(6)]
public sealed class NotEqual : BinaryComparisonUnit
{
public NotEqual() : base()
{
numeric = false;
}
// Backward compatibility
protected override string outputKey => "notEqual";
/// <summary>
/// Whether A is different than B.
/// </summary>
[DoNotSerialize]
[PortLabel("A \u2260 B")]
[PortKey("notEqual")]
public override ValueOutput comparison => base.comparison;
protected override bool NumericComparison(float a, float b)
{
return !Mathf.Approximately(a, b);
}
protected override bool GenericComparison(object a, object b)
{
return OperatorUtility.NotEqual(a, b);
}
}
}

View File

@@ -0,0 +1,120 @@
using System;
using UnityEngine;
namespace Unity.VisualScripting
{
/// <summary>
/// Compares two numeric inputs.
/// </summary>
[UnitCategory("Logic")]
[UnitTitle("Numeric Comparison")]
[UnitSurtitle("Numeric")]
[UnitShortTitle("Comparison")]
[UnitOrder(99)]
[Obsolete("Use the Comparison unit with Numeric enabled instead.")]
public sealed class NumericComparison : Unit
{
/// <summary>
/// The first input.
/// </summary>
[DoNotSerialize]
public ValueInput a { get; private set; }
/// <summary>
/// The second input.
/// </summary>
[DoNotSerialize]
public ValueInput b { get; private set; }
/// <summary>
/// Whether A is less than B.
/// </summary>
[DoNotSerialize]
[PortLabel("A < B")]
public ValueOutput aLessThanB { get; private set; }
/// <summary>
/// Whether A is less than or equal to B.
/// </summary>
[DoNotSerialize]
[PortLabel("A \u2264 B")]
public ValueOutput aLessThanOrEqualToB { get; private set; }
/// <summary>
/// Whether A is equal to B.
/// </summary>
[DoNotSerialize]
[PortLabel("A = B")]
public ValueOutput aEqualToB { get; private set; }
/// <summary>
/// Whether A is greater than or equal to B.
/// </summary>
[DoNotSerialize]
[PortLabel("A \u2265 B")]
public ValueOutput aGreaterThanOrEqualToB { get; private set; }
/// <summary>
/// Whether A is greater than B.
/// </summary>
[DoNotSerialize]
[PortLabel("A > B")]
public ValueOutput aGreatherThanB { get; private set; }
protected override void Definition()
{
a = ValueInput<float>(nameof(a));
b = ValueInput<float>(nameof(b), 0);
aLessThanB = ValueOutput(nameof(aLessThanB), Less).Predictable();
aLessThanOrEqualToB = ValueOutput(nameof(aLessThanOrEqualToB), LessOrEqual).Predictable();
aEqualToB = ValueOutput(nameof(aEqualToB), Equal).Predictable();
aGreaterThanOrEqualToB = ValueOutput(nameof(aGreaterThanOrEqualToB), GreaterOrEqual).Predictable();
aGreatherThanB = ValueOutput(nameof(aGreatherThanB), Greater).Predictable();
Requirement(a, aLessThanB);
Requirement(b, aLessThanB);
Requirement(a, aLessThanOrEqualToB);
Requirement(b, aLessThanOrEqualToB);
Requirement(a, aEqualToB);
Requirement(b, aEqualToB);
Requirement(a, aGreaterThanOrEqualToB);
Requirement(b, aGreaterThanOrEqualToB);
Requirement(a, aGreatherThanB);
Requirement(b, aGreatherThanB);
}
private bool Less(Flow flow)
{
return flow.GetValue<float>(a) < flow.GetValue<float>(b);
}
private bool LessOrEqual(Flow flow)
{
var a = flow.GetValue<float>(this.a);
var b = flow.GetValue<float>(this.b);
return a < b || Mathf.Approximately(a, b);
}
private bool Equal(Flow flow)
{
return Mathf.Approximately(flow.GetValue<float>(a), flow.GetValue<float>(b));
}
private bool GreaterOrEqual(Flow flow)
{
var a = flow.GetValue<float>(this.a);
var b = flow.GetValue<float>(this.b);
return a > b || Mathf.Approximately(a, b);
}
private bool Greater(Flow flow)
{
return flow.GetValue<float>(a) < flow.GetValue<float>(b);
}
}
}

View File

@@ -0,0 +1,44 @@
namespace Unity.VisualScripting
{
/// <summary>
/// Returns true if either input is true.
/// </summary>
[UnitCategory("Logic")]
[UnitOrder(1)]
public sealed class Or : Unit
{
/// <summary>
/// The first boolean.
/// </summary>
[DoNotSerialize]
public ValueInput a { get; private set; }
/// <summary>
/// The second boolean.
/// </summary>
[DoNotSerialize]
public ValueInput b { get; private set; }
/// <summary>
/// True if either A or B is true; false otherwise.
/// </summary>
[DoNotSerialize]
[PortLabel("A | B")]
public ValueOutput result { get; private set; }
protected override void Definition()
{
a = ValueInput<bool>(nameof(a));
b = ValueInput<bool>(nameof(b));
result = ValueOutput(nameof(result), Operation).Predictable();
Requirement(a, result);
Requirement(b, result);
}
public bool Operation(Flow flow)
{
return flow.GetValue<bool>(a) || flow.GetValue<bool>(b);
}
}
}