diff --git a/src/Artemis.Core/Utilities/Numeric.cs b/src/Artemis.Core/Utilities/Numeric.cs new file mode 100644 index 000000000..86bec89b8 --- /dev/null +++ b/src/Artemis.Core/Utilities/Numeric.cs @@ -0,0 +1,289 @@ +using System; +using System.Collections.Generic; +using System.Globalization; + +namespace Artemis.Core +{ + /// + /// Represents a number, either decimal or not, with arbitrary precision. + /// + /// Note: This struct is intended to be used by the node system when implementing your own . + /// Usage outside that context is not recommended due to conversion overhead. + /// + /// + public readonly struct Numeric : IComparable + { + private readonly float _value; + + #region Constructors + + /// + /// Creates a new instance of from a + /// + public Numeric(float value) + { + _value = value; + } + + /// + /// Creates a new instance of from an + /// + public Numeric(int value) + { + _value = value; + } + + /// + /// Creates a new instance of from a + /// + public Numeric(double value) + { + _value = (float) value; + } + + /// + /// Creates a new instance of from a + /// + public Numeric(byte value) + { + _value = value; + } + + /// + /// Creates a new instance of from an + /// + public Numeric(object? pathValue) + { + _value = pathValue switch + { + float value => value, + int value => value, + double value => (float) value, + byte value => value, + _ => ParseFloatOrDefault(pathValue?.ToString()) + }; + } + + private static float ParseFloatOrDefault(string? pathValue) + { + float.TryParse(pathValue, NumberStyles.Any, NumberFormatInfo.InvariantInfo, out float parsedFloat); + return parsedFloat; + } + + #endregion + + #region Relational members + + /// + public int CompareTo(Numeric other) + { + return _value.CompareTo(other._value); + } + + #endregion + + #region Equality members + + /// + /// Indicates whether this instance and a specified numeric are equal + /// + /// The numeric to compare with the current instance + /// + /// if this numeric and the provided are equal; otherwise, + /// . + /// + public bool Equals(Numeric other) + { + return _value.Equals(other._value); + } + + /// + public override bool Equals(object? obj) + { + return obj is Numeric other && Equals(other); + } + + /// + public override int GetHashCode() + { + return _value.GetHashCode(); + } + + #endregion + + #region Formatting members + + /// + public override string ToString() + { + return _value.ToString(CultureInfo.InvariantCulture); + } + + #endregion + + #region Operators + +#pragma warning disable 1591 + + public static implicit operator float(Numeric p) + { + return p._value; + } + + public static implicit operator int(Numeric p) + { + return (int) MathF.Round(p._value, MidpointRounding.AwayFromZero); + } + + public static implicit operator double(Numeric p) + { + return p._value; + } + + public static implicit operator byte(Numeric p) + { + return (byte) Math.Clamp(p._value, 0, 255); + } + + public static bool operator >(Numeric a, Numeric b) + { + return a._value > b._value; + } + + public static bool operator <(Numeric a, Numeric b) + { + return a._value < b._value; + } + + public static bool operator ==(Numeric left, Numeric right) + { + return left.Equals(right); + } + + public static bool operator !=(Numeric left, Numeric right) + { + return !(left == right); + } + + public static bool operator <=(Numeric left, Numeric right) + { + return left.CompareTo(right) <= 0; + } + + public static bool operator >=(Numeric left, Numeric right) + { + return left.CompareTo(right) >= 0; + } + + public static Numeric operator +(Numeric a) + { + return new Numeric(+a._value); + } + + public static Numeric operator -(Numeric a) + { + return new Numeric(-a._value); + } + + public static Numeric operator ++(Numeric a) + { + return new Numeric(a._value + 1); + } + + public static Numeric operator --(Numeric a) + { + return new Numeric(a._value - 1); + } + + public static Numeric operator +(Numeric a, Numeric b) + { + return new Numeric(a._value + b._value); + } + + public static Numeric operator -(Numeric a, Numeric b) + { + return new Numeric(a._value - b._value); + } + + public static Numeric operator *(Numeric a, Numeric b) + { + return new Numeric(a._value * b._value); + } + + public static Numeric operator %(Numeric a, Numeric b) + { + return new Numeric(a._value % b._value); + } + + public static Numeric operator /(Numeric a, Numeric b) + { + if (b._value == 0) + throw new DivideByZeroException(); + return new Numeric(a._value / b._value); + } + +#pragma warning restore 1591 + + #endregion + + /// + /// Converts the string representation of a number into a numeric. A return value indicates whether the conversion + /// succeeded or failed. + /// + /// A string representing a number to convert. + /// + /// When this method returns, contains numeric equivalent to the numeric value or symbol contained in + /// , if the conversion succeeded, or zero if the conversion failed. + /// + /// if s was converted successfully; otherwise, . + public static bool TryParse(string? s, out Numeric result) + { + bool parsed = float.TryParse(s, NumberStyles.Any, NumberFormatInfo.InvariantInfo, out float parsedFloat); + if (!parsed) + { + result = new Numeric(0); + return false; + } + + result = new Numeric(parsedFloat); + return true; + } + + /// + /// Returns a boolean indicating whether the provided type can be used as a . + /// + public static bool IsTypeCompatible(Type type) + { + return type == typeof(Numeric) || + type == typeof(float) || + type == typeof(double) || + type == typeof(int) || + type == typeof(byte); + } + } + + /// + /// Provides alternatives for common number-type extensions + /// + public static class NumericExtensions + { + #region Extensions + + /// + /// Sums the numerics in the provided collection + /// + /// The sum of all numerics in the collection + /// + public static Numeric Sum(this IEnumerable source) + { + if (source == null) throw new ArgumentNullException(nameof(source)); + + float sum = 0; + foreach (float v in source) sum += v; + + return new Numeric(sum); + } + + #endregion + } +} \ No newline at end of file diff --git a/src/Artemis.UI.Shared/Converters/StringToNumericConverter.cs b/src/Artemis.UI.Shared/Converters/StringToNumericConverter.cs new file mode 100644 index 000000000..ced2709ae --- /dev/null +++ b/src/Artemis.UI.Shared/Converters/StringToNumericConverter.cs @@ -0,0 +1,27 @@ +using System; +using System.Globalization; +using System.Windows.Data; +using Artemis.Core; + +namespace Artemis.UI.Shared +{ + /// + /// Converts into . + /// + [ValueConversion(typeof(string), typeof(Numeric))] + public class StringToNumericConverter : IValueConverter + { + /// + public object? Convert(object? value, Type targetType, object parameter, CultureInfo culture) + { + return value?.ToString(); + } + + /// + public object ConvertBack(object? value, Type targetType, object parameter, CultureInfo culture) + { + Numeric.TryParse(value as string, out Numeric result); + return result; + } + } +} \ No newline at end of file diff --git a/src/Artemis.UI/Services/RegistrationService.cs b/src/Artemis.UI/Services/RegistrationService.cs index d6a9f6bd3..68d4f2fc5 100644 --- a/src/Artemis.UI/Services/RegistrationService.cs +++ b/src/Artemis.UI/Services/RegistrationService.cs @@ -132,7 +132,7 @@ namespace Artemis.UI.Services _nodeService.RegisterTypeColor(Constants.CorePlugin, typeof(IList), new SKColor(0xFFED3E61)); _nodeService.RegisterTypeColor(Constants.CorePlugin, typeof(Enum), new SKColor(0xFF1E90FF)); - foreach (Type nodeType in typeof(SumIntegersNode).Assembly.GetTypes().Where(t => typeof(INode).IsAssignableFrom(t) && t.IsPublic && !t.IsAbstract && !t.IsInterface)) + foreach (Type nodeType in typeof(SumNumericsNode).Assembly.GetTypes().Where(t => typeof(INode).IsAssignableFrom(t) && t.IsPublic && !t.IsAbstract && !t.IsInterface)) _nodeService.RegisterNodeType(Constants.CorePlugin, nodeType); } diff --git a/src/Artemis.VisualScripting/Artemis.VisualScripting.csproj b/src/Artemis.VisualScripting/Artemis.VisualScripting.csproj index 6fe961c9a..47a179dcc 100644 --- a/src/Artemis.VisualScripting/Artemis.VisualScripting.csproj +++ b/src/Artemis.VisualScripting/Artemis.VisualScripting.csproj @@ -65,7 +65,7 @@ $(DefaultXamlRuntime) - + $(DefaultXamlRuntime) diff --git a/src/Artemis.VisualScripting/Nodes/BoolOperations.cs b/src/Artemis.VisualScripting/Nodes/BoolOperations.cs index 5e44018ea..27b862989 100644 --- a/src/Artemis.VisualScripting/Nodes/BoolOperations.cs +++ b/src/Artemis.VisualScripting/Nodes/BoolOperations.cs @@ -2,6 +2,7 @@ using System.Collections; using System.Linq; using Artemis.Core; +using Artemis.VisualScripting.Nodes.CustomViewModels; namespace Artemis.VisualScripting.Nodes { @@ -33,6 +34,12 @@ namespace Artemis.VisualScripting.Nodes public override void Evaluate() { + if (Input1.Value is Numeric numeric1 && Input2.Value is Numeric numeric2) + { + Result.Value = numeric1 > numeric2; + return; + } + if (Input2.Value != null && Input1.Value != null && Input1.Value.IsNumber() && Input2.Value.IsNumber()) { Result.Value = Convert.ToSingle(Input1.Value) > Convert.ToSingle(Input2.Value); @@ -80,6 +87,12 @@ namespace Artemis.VisualScripting.Nodes public override void Evaluate() { + if (Input1.Value is Numeric numeric1 && Input2.Value is Numeric numeric2) + { + Result.Value = numeric1 < numeric2; + return; + } + if (Input2.Value != null && Input1.Value != null && Input1.Value.IsNumber() && Input2.Value.IsNumber()) { Result.Value = Convert.ToSingle(Input1.Value) < Convert.ToSingle(Input2.Value); @@ -263,4 +276,27 @@ namespace Artemis.VisualScripting.Nodes #endregion } + + [Node("Enum Equals", "Determines the equality between an input and a selected enum value", "Operators", InputType = typeof(Enum), OutputType = typeof(bool))] + public class EnumEqualsNode : Node + { + public EnumEqualsNode() : base("Enum Equals", "Determines the equality between an input and a selected enum value") + { + InputPin = CreateInputPin(); + OutputPin = CreateOutputPin(); + } + + public InputPin InputPin { get; } + public OutputPin OutputPin { get; } + + #region Overrides of Node + + /// + public override void Evaluate() + { + OutputPin.Value = InputPin.Value != null && InputPin.Value.Equals(Storage); + } + + #endregion + } } \ No newline at end of file diff --git a/src/Artemis.VisualScripting/Nodes/Color/HslSKColorNode.cs b/src/Artemis.VisualScripting/Nodes/Color/HslSKColorNode.cs index 724053db7..a4478ad73 100644 --- a/src/Artemis.VisualScripting/Nodes/Color/HslSKColorNode.cs +++ b/src/Artemis.VisualScripting/Nodes/Color/HslSKColorNode.cs @@ -3,20 +3,20 @@ using SkiaSharp; namespace Artemis.VisualScripting.Nodes.Color { - [Node("HSL Color", "Creates a color from hue, saturation and lightness values", "Color", InputType = typeof(float), OutputType = typeof(SKColor))] + [Node("HSL Color", "Creates a color from hue, saturation and lightness values", "Color", InputType = typeof(Numeric), OutputType = typeof(SKColor))] public class HslSKColorNode : Node { public HslSKColorNode() : base("HSL Color", "Creates a color from hue, saturation and lightness values") { - H = CreateInputPin("H"); - S = CreateInputPin("S"); - L = CreateInputPin("L"); + H = CreateInputPin("H"); + S = CreateInputPin("S"); + L = CreateInputPin("L"); Output = CreateOutputPin(); } - public InputPin H { get; set; } - public InputPin S { get; set; } - public InputPin L { get; set; } + public InputPin H { get; set; } + public InputPin S { get; set; } + public InputPin L { get; set; } public OutputPin Output { get; } #region Overrides of Node diff --git a/src/Artemis.VisualScripting/Nodes/ConvertNodes.cs b/src/Artemis.VisualScripting/Nodes/ConvertNodes.cs index bf396f1db..eafe52cb9 100644 --- a/src/Artemis.VisualScripting/Nodes/ConvertNodes.cs +++ b/src/Artemis.VisualScripting/Nodes/ConvertNodes.cs @@ -34,24 +34,24 @@ namespace Artemis.VisualScripting.Nodes #endregion } - [Node("To Integer", "Converts the input to an integer.", "Conversion", InputType = typeof(object), OutputType = typeof(int))] - public class ConvertToIntegerNode : Node + [Node("To Numeric", "Converts the input to a numeric.", "Conversion", InputType = typeof(object), OutputType = typeof(Numeric))] + public class ConvertToNumericNode : Node { #region Properties & Fields public InputPin Input { get; } - public OutputPin Integer { get; } + public OutputPin Output { get; } #endregion #region Constructors - public ConvertToIntegerNode() - : base("To Integer", "Converts the input to an integer.") + public ConvertToNumericNode() + : base("To Numeric", "Converts the input to a numeric.") { Input = CreateInputPin(); - Integer = CreateOutputPin(); + Output = CreateOutputPin(); } #endregion @@ -60,66 +60,19 @@ namespace Artemis.VisualScripting.Nodes public override void Evaluate() { - Integer.Value = Input.Value switch + Output.Value = Input.Value switch { - int input => input, - double input => (int) input, - float input => (int) input, + int input => new Numeric(input), + double input => new Numeric(input), + float input => new Numeric(input), + byte input => new Numeric(input), _ => TryParse(Input.Value) }; } - private int TryParse(object input) + private Numeric TryParse(object input) { - if (!int.TryParse(input?.ToString(), out int value)) - value = 0; - - return value; - } - - #endregion - } - - [Node("To Float", "Converts the input to a float.", "Conversion", InputType = typeof(object), OutputType = typeof(float))] - public class ConvertToFloatNode : Node - { - #region Properties & Fields - - public InputPin Input { get; } - - public OutputPin Float { get; } - - #endregion - - #region Constructors - - public ConvertToFloatNode() - : base("To Float", "Converts the input to a float.") - { - Input = CreateInputPin(); - Float = CreateOutputPin(); - } - - #endregion - - #region Methods - - public override void Evaluate() - { - Float.Value = Input.Value switch - { - int input => input, - double input => (float) input, - float input => input, - _ => TryParse(Input.Value) - }; - } - - private float TryParse(object input) - { - if (!float.TryParse(input?.ToString(), out float value)) - value = 0.0f; - + Numeric.TryParse(input?.ToString(), out Numeric value); return value; } diff --git a/src/Artemis.VisualScripting/Nodes/CustomViewModels/StaticValueNodeViewModels.cs b/src/Artemis.VisualScripting/Nodes/CustomViewModels/StaticValueNodeViewModels.cs index e976d5059..b9df4f59a 100644 --- a/src/Artemis.VisualScripting/Nodes/CustomViewModels/StaticValueNodeViewModels.cs +++ b/src/Artemis.VisualScripting/Nodes/CustomViewModels/StaticValueNodeViewModels.cs @@ -2,23 +2,9 @@ namespace Artemis.VisualScripting.Nodes.CustomViewModels { - public class StaticDoubleValueNodeCustomViewModel : CustomNodeViewModel + public class StaticNumericValueNodeCustomViewModel : CustomNodeViewModel { - public StaticDoubleValueNodeCustomViewModel(INode node) : base(node) - { - } - } - - public class StaticFloatValueNodeCustomViewModel : CustomNodeViewModel - { - public StaticFloatValueNodeCustomViewModel(INode node) : base(node) - { - } - } - - public class StaticIntegerValueNodeCustomViewModel : CustomNodeViewModel - { - public StaticIntegerValueNodeCustomViewModel(INode node) : base(node) + public StaticNumericValueNodeCustomViewModel(INode node) : base(node) { } } diff --git a/src/Artemis.VisualScripting/Nodes/CustomViews/StaticFloatValueNodeCustomView.xaml b/src/Artemis.VisualScripting/Nodes/CustomViews/StaticNumericValueNodeCustomView.xaml similarity index 60% rename from src/Artemis.VisualScripting/Nodes/CustomViews/StaticFloatValueNodeCustomView.xaml rename to src/Artemis.VisualScripting/Nodes/CustomViews/StaticNumericValueNodeCustomView.xaml index 3720a3acd..20bd0db60 100644 --- a/src/Artemis.VisualScripting/Nodes/CustomViews/StaticFloatValueNodeCustomView.xaml +++ b/src/Artemis.VisualScripting/Nodes/CustomViews/StaticNumericValueNodeCustomView.xaml @@ -1,12 +1,15 @@ - + + + + Text="{Binding Node.Storage, Converter={StaticResource StringToNumericConverter}}" /> diff --git a/src/Artemis.VisualScripting/Nodes/DataModel/DataModelNode.cs b/src/Artemis.VisualScripting/Nodes/DataModel/DataModelNode.cs index 8b9b1ebbe..9b0f63666 100644 --- a/src/Artemis.VisualScripting/Nodes/DataModel/DataModelNode.cs +++ b/src/Artemis.VisualScripting/Nodes/DataModel/DataModelNode.cs @@ -53,8 +53,8 @@ namespace Artemis.VisualScripting.Nodes.DataModel } else { - if (pathValue is double doublePathValue) - Output.Value = (float) doublePathValue; + if (Output.Type == typeof(Numeric)) + Output.Value = new Numeric(pathValue); else Output.Value = pathValue; } @@ -64,8 +64,8 @@ namespace Artemis.VisualScripting.Nodes.DataModel public void UpdateOutputPin(bool loadConnections) { Type type = DataModelPath?.GetPropertyType(); - if (type == typeof(double)) - type = typeof(float); + if (Numeric.IsTypeCompatible(type)) + type = typeof(Numeric); if (Output != null && Output.Type == type) return; @@ -76,7 +76,7 @@ namespace Artemis.VisualScripting.Nodes.DataModel Output = null; } - if (type != null) + if (type != null) Output = CreateOutputPin(type); if (loadConnections && Script is NodeScript nodeScript) diff --git a/src/Artemis.VisualScripting/Nodes/Easing/FloatEasingNode.cs b/src/Artemis.VisualScripting/Nodes/Easing/FloatEasingNode.cs index 707767eeb..15758c68d 100644 --- a/src/Artemis.VisualScripting/Nodes/Easing/FloatEasingNode.cs +++ b/src/Artemis.VisualScripting/Nodes/Easing/FloatEasingNode.cs @@ -3,8 +3,8 @@ using Artemis.Core; namespace Artemis.VisualScripting.Nodes.Easing { - [Node("Float Easing", "Outputs an eased float value", "Easing", InputType = typeof(float), OutputType = typeof(float))] - public class FloatEasingNode : Node + [Node("Numeric Easing", "Outputs an eased numeric value", "Easing", InputType = typeof(Numeric), OutputType = typeof(Numeric))] + public class NumericEasingNode : Node { private DateTime _lastEvaluate = DateTime.MinValue; private float _progress; @@ -12,20 +12,20 @@ namespace Artemis.VisualScripting.Nodes.Easing private float _sourceValue; private float _targetValue; - public FloatEasingNode() : base("Float Easing", "Outputs an eased float value") + public NumericEasingNode() : base("Numeric Easing", "Outputs an eased numeric value") { - Input = CreateInputPin(); - EasingTime = CreateInputPin("delay"); + Input = CreateInputPin(); + EasingTime = CreateInputPin("delay"); EasingFunction = CreateInputPin("function"); - Output = CreateOutputPin(); + Output = CreateOutputPin(); } - public InputPin Input { get; set; } - public InputPin EasingTime { get; set; } + public InputPin Input { get; set; } + public InputPin EasingTime { get; set; } public InputPin EasingFunction { get; set; } - public OutputPin Output { get; set; } + public OutputPin Output { get; set; } public override void Evaluate() { @@ -43,12 +43,12 @@ namespace Artemis.VisualScripting.Nodes.Easing if (_progress < 1f) { Update(); - Output.Value = _currentValue; + Output.Value = new Numeric(_currentValue); } // Stop updating past 1 and use the target value else { - Output.Value = _targetValue; + Output.Value = new Numeric(_targetValue); } _lastEvaluate = now; diff --git a/src/Artemis.VisualScripting/Nodes/EnumEqualsNode.cs b/src/Artemis.VisualScripting/Nodes/EnumEqualsNode.cs deleted file mode 100644 index 75eb0dbb0..000000000 --- a/src/Artemis.VisualScripting/Nodes/EnumEqualsNode.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System; -using Artemis.Core; -using Artemis.VisualScripting.Nodes.CustomViewModels; - -namespace Artemis.VisualScripting.Nodes -{ - [Node("Enum Equals", "Determines the equality between an input and a selected enum value", InputType = typeof(Enum), OutputType = typeof(bool))] - public class EnumEqualsNode : Node - { - public EnumEqualsNode() : base("Enum Equals", "Determines the equality between an input and a selected enum value") - { - InputPin = CreateInputPin(); - OutputPin = CreateOutputPin(); - } - - public InputPin InputPin { get; } - public OutputPin OutputPin { get; } - - #region Overrides of Node - - /// - public override void Evaluate() - { - OutputPin.Value = InputPin.Value != null && InputPin.Value.Equals(Storage); - } - - #endregion - } -} \ No newline at end of file diff --git a/src/Artemis.VisualScripting/Nodes/Maths/ExpressionNode.cs b/src/Artemis.VisualScripting/Nodes/Maths/ExpressionNode.cs index 5958a0993..37dbc4434 100644 --- a/src/Artemis.VisualScripting/Nodes/Maths/ExpressionNode.cs +++ b/src/Artemis.VisualScripting/Nodes/Maths/ExpressionNode.cs @@ -8,7 +8,7 @@ using NoStringEvaluating.Models.Values; namespace Artemis.VisualScripting.Nodes.Maths { - [Node("Math Expression", "Outputs the result of a math expression.", "Mathematics", InputType = typeof(float), OutputType = typeof(float))] + [Node("Math Expression", "Outputs the result of a math expression.", "Mathematics", InputType = typeof(Numeric), OutputType = typeof(Numeric))] public class MathExpressionNode : Node { private readonly INoStringEvaluator _evaluator; @@ -20,8 +20,8 @@ namespace Artemis.VisualScripting.Nodes.Maths : base("Math Expression", "Outputs the result of a math expression.") { _evaluator = evaluator; - Output = CreateOutputPin(); - Values = CreateInputPinCollection("Values", 2); + Output = CreateOutputPin(); + Values = CreateInputPinCollection("Values", 2); Values.PinAdded += (_, _) => SetPinNames(); Values.PinRemoved += (_, _) => SetPinNames(); _variables = new PinsVariablesContainer(Values); @@ -33,8 +33,8 @@ namespace Artemis.VisualScripting.Nodes.Maths #region Properties & Fields - public OutputPin Output { get; } - public InputPinCollection Values { get; } + public OutputPin Output { get; } + public InputPinCollection Values { get; } #endregion @@ -43,7 +43,7 @@ namespace Artemis.VisualScripting.Nodes.Maths public override void Evaluate() { if (Storage != null) - Output.Value = (float) _evaluator.CalcNumber(Storage, _variables); + Output.Value = new Numeric(_evaluator.CalcNumber(Storage, _variables)); } private void SetPinNames() @@ -76,9 +76,9 @@ namespace Artemis.VisualScripting.Nodes.Maths public class PinsVariablesContainer : IVariablesContainer { - private readonly InputPinCollection _values; + private readonly InputPinCollection _values; - public PinsVariablesContainer(InputPinCollection values) + public PinsVariablesContainer(InputPinCollection values) { _values = values; } @@ -95,17 +95,23 @@ namespace Artemis.VisualScripting.Nodes.Maths public EvaluatorValue GetValue(string name) { IPin pin = _values.FirstOrDefault(v => v.Name == name); - return pin == null ? new EvaluatorValue(0) : new EvaluatorValue((double) pin.PinValue); + if (pin?.PinValue is Numeric numeric) + return new EvaluatorValue(numeric); + return new EvaluatorValue(0); } /// public bool TryGetValue(string name, out EvaluatorValue value) { IPin pin = _values.FirstOrDefault(v => v.Name == name); - double unboxed = (float) pin.PinValue; - value = pin == null ? new EvaluatorValue(0) : new EvaluatorValue(unboxed); + if (pin?.PinValue is Numeric numeric) + { + value = new EvaluatorValue(numeric); + return true; + } - return pin != null; + value = new EvaluatorValue(0); + return false; } #endregion diff --git a/src/Artemis.VisualScripting/Nodes/Maths/RoundNode.cs b/src/Artemis.VisualScripting/Nodes/Maths/RoundNode.cs new file mode 100644 index 000000000..bfaaed850 --- /dev/null +++ b/src/Artemis.VisualScripting/Nodes/Maths/RoundNode.cs @@ -0,0 +1,28 @@ +using System; +using Artemis.Core; + +namespace Artemis.VisualScripting.Nodes.Maths +{ + [Node("Round", "Outputs a rounded numeric value.", "Mathematics", InputType = typeof(Numeric), OutputType = typeof(Numeric))] + public class RoundNode : Node + { + public RoundNode() : base("Round", "Outputs a rounded numeric value.") + { + Input = CreateInputPin(); + Output = CreateOutputPin(); + } + + public OutputPin Output { get; set; } + public InputPin Input { get; set; } + + #region Overrides of Node + + /// + public override void Evaluate() + { + Output.Value = new Numeric(MathF.Round(Input.Value, MidpointRounding.AwayFromZero)); + } + + #endregion + } +} \ No newline at end of file diff --git a/src/Artemis.VisualScripting/Nodes/StaticValueNodes.cs b/src/Artemis.VisualScripting/Nodes/StaticValueNodes.cs index 682ccacf3..b90c2eba5 100644 --- a/src/Artemis.VisualScripting/Nodes/StaticValueNodes.cs +++ b/src/Artemis.VisualScripting/Nodes/StaticValueNodes.cs @@ -3,50 +3,21 @@ using Artemis.VisualScripting.Nodes.CustomViewModels; namespace Artemis.VisualScripting.Nodes { - [Node("Integer-Value", "Outputs a configurable static integer value.", "Static", OutputType = typeof(int))] - public class StaticIntegerValueNode : Node + [Node("Numeric-Value", "Outputs a configurable static numeric value.", "Static", OutputType = typeof(Numeric))] + public class StaticNumericValueNode : Node { #region Properties & Fields - public OutputPin Output { get; } + public OutputPin Output { get; } #endregion #region Constructors - public StaticIntegerValueNode() - : base("Integer", "Outputs an configurable integer value.") + public StaticNumericValueNode() + : base("Numeric", "Outputs a configurable numeric value.") { - Output = CreateOutputPin(); - } - - #endregion - - #region Methods - - public override void Evaluate() - { - Output.Value = Storage; - } - - #endregion - } - - [Node("Float-Value", "Outputs a configurable static float value.", "Static", OutputType = typeof(float))] - public class StaticFloatValueNode : Node - { - #region Properties & Fields - - public OutputPin Output { get; } - - #endregion - - #region Constructors - - public StaticFloatValueNode() - : base("Float", "Outputs a configurable float value.") - { - Output = CreateOutputPin(); + Output = CreateOutputPin(); } #endregion diff --git a/src/Artemis.VisualScripting/Nodes/SumNode.cs b/src/Artemis.VisualScripting/Nodes/SumNode.cs index 1b3aaa8e3..4efd00df5 100644 --- a/src/Artemis.VisualScripting/Nodes/SumNode.cs +++ b/src/Artemis.VisualScripting/Nodes/SumNode.cs @@ -1,26 +1,17 @@ -using System.Linq; -using Artemis.Core; +using Artemis.Core; namespace Artemis.VisualScripting.Nodes { - [Node("Sum (Integer)", "Sums the connected integer values.", "Mathematics", InputType = typeof(int), OutputType = typeof(int))] - public class SumIntegersNode : Node + [Node("Sum", "Sums the connected numeric values.", "Mathematics", InputType = typeof(Numeric), OutputType = typeof(Numeric))] + public class SumNumericsNode : Node { - #region Properties & Fields - - public InputPinCollection Values { get; } - - public OutputPin Sum { get; } - - #endregion - #region Constructors - public SumIntegersNode() - : base("Sum", "Sums the connected integer values.") + public SumNumericsNode() + : base("Sum", "Sums the connected numeric values.") { - Values = CreateInputPinCollection("Values", 2); - Sum = CreateOutputPin("Sum"); + Values = CreateInputPinCollection("Values", 2); + Sum = CreateOutputPin("Sum"); } #endregion @@ -33,37 +24,13 @@ namespace Artemis.VisualScripting.Nodes } #endregion - } - [Node("Sum (Float)", "Sums the connected float values.", "Mathematics", InputType = typeof(float), OutputType = typeof(float))] - public class SumFloatsNode : Node - { #region Properties & Fields - public InputPinCollection Values { get; } + public InputPinCollection Values { get; } - public OutputPin Sum { get; } - - #endregion - - #region Constructors - - public SumFloatsNode() - : base("Sum", "Sums the connected float values.") - { - Values = CreateInputPinCollection("Values", 2); - Sum = CreateOutputPin("Sum"); - } - - #endregion - - #region Methods - - public override void Evaluate() - { - Sum.Value = Values.Values.Sum(); - } + public OutputPin Sum { get; } #endregion } -} +} \ No newline at end of file