diff --git a/src/Artemis.Core/Artemis.Core.csproj.DotSettings b/src/Artemis.Core/Artemis.Core.csproj.DotSettings new file mode 100644 index 000000000..790b5129b --- /dev/null +++ b/src/Artemis.Core/Artemis.Core.csproj.DotSettings @@ -0,0 +1,60 @@ + + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True + True \ No newline at end of file diff --git a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Colors/SKColorBrightenModifierType.cs b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Colors/SKColorBrightenModifierType.cs index 4c79778e7..c99e407f3 100644 --- a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Colors/SKColorBrightenModifierType.cs +++ b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Colors/SKColorBrightenModifierType.cs @@ -1,23 +1,17 @@ -using System; -using System.Collections.Generic; -using SkiaSharp; +using SkiaSharp; namespace Artemis.Core.DefaultTypes { - internal class SKColorBrightenModifierType : DataBindingModifierType + internal class SKColorBrightenModifierType : DataBindingModifierType { - public override IReadOnlyCollection CompatibleTypes => new List {typeof(SKColor)}; - public override Type ParameterType => typeof(float); - public override string Name => "Brighten by"; public override string Icon => "CarLightHigh"; public override string Description => "Brightens the color by the amount in percent"; - public override object Apply(object currentValue, object parameterValue) + public override SKColor Apply(SKColor currentValue, float parameterValue) { - ((SKColor) currentValue).ToHsl(out float h, out float s, out float l); - l *= (Convert.ToSingle(parameterValue) + 100f) / 100f; - + currentValue.ToHsl(out float h, out float s, out float l); + l *= (parameterValue + 100f) / 100f; return SKColor.FromHsl(h, s, l); } } diff --git a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Colors/SKColorDarkenModifierType.cs b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Colors/SKColorDarkenModifierType.cs index 39f427dd6..a6ae0a1df 100644 --- a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Colors/SKColorDarkenModifierType.cs +++ b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Colors/SKColorDarkenModifierType.cs @@ -1,22 +1,17 @@ -using System; -using System.Collections.Generic; -using SkiaSharp; +using SkiaSharp; namespace Artemis.Core.DefaultTypes { - internal class SKColorDarkenModifierType : DataBindingModifierType + internal class SKColorDarkenModifierType : DataBindingModifierType { - public override IReadOnlyCollection CompatibleTypes => new List {typeof(SKColor)}; - public override Type ParameterType => typeof(float); - public override string Name => "Darken by"; public override string Icon => "CarLightDimmed"; public override string Description => "Darkens the color by the amount in percent"; - public override object Apply(object currentValue, object parameterValue) + public override SKColor Apply(SKColor currentValue, float parameterValue) { - ((SKColor) currentValue).ToHsl(out float h, out float s, out float l); - l *= (Convert.ToSingle(parameterValue) * -1 + 100f) / 100f; + currentValue.ToHsl(out float h, out float s, out float l); + l *= (parameterValue * -1 + 100f) / 100f; return SKColor.FromHsl(h, s, l); } } diff --git a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Colors/SKColorRotateHueModifierType.cs b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Colors/SKColorRotateHueModifierType.cs index 3591efacc..24b787f16 100644 --- a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Colors/SKColorRotateHueModifierType.cs +++ b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Colors/SKColorRotateHueModifierType.cs @@ -1,24 +1,17 @@ -using System; -using System.Collections.Generic; -using SkiaSharp; +using SkiaSharp; namespace Artemis.Core.DefaultTypes { - internal class SKColorRotateHueModifierType : DataBindingModifierType + internal class SKColorRotateHueModifierType : DataBindingModifierType { - public override IReadOnlyCollection CompatibleTypes => new List {typeof(SKColor)}; - public override Type ParameterType => typeof(float); - public override string Name => "Rotate Hue by"; public override string Icon => "RotateRight"; public override string Description => "Rotates the hue of the color by the amount in degrees"; - public override object Apply(object currentValue, object parameterValue) + public override SKColor Apply(SKColor currentValue, float parameterValue) { - ((SKColor) currentValue).ToHsl(out float h, out float s, out float l); - - h += (float)parameterValue; - + currentValue.ToHsl(out float h, out float s, out float l); + h += parameterValue; return SKColor.FromHsl(h % 360, s, l); } } diff --git a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Colors/SKColorSumModifierType.cs b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Colors/SKColorSumModifierType.cs index f75b2e19e..294c368d6 100644 --- a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Colors/SKColorSumModifierType.cs +++ b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Colors/SKColorSumModifierType.cs @@ -1,20 +1,16 @@ -using System; -using System.Collections.Generic; -using SkiaSharp; +using SkiaSharp; namespace Artemis.Core.DefaultTypes { - internal class SKColorSumModifierType : DataBindingModifierType + internal class SKColorSumModifierType : DataBindingModifierType { - public override IReadOnlyCollection CompatibleTypes => new List {typeof(SKColor)}; - public override string Name => "Combine with"; public override string Icon => "FormatColorFill"; public override string Description => "Adds the two colors together"; - public override object Apply(object currentValue, object parameterValue) + public override SKColor Apply(SKColor currentValue, SKColor parameterValue) { - return ((SKColor) currentValue).Sum((SKColor) parameterValue); + return currentValue.Sum(parameterValue); } } } \ No newline at end of file diff --git a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/AbsoluteModifierType.cs b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/AbsoluteModifierType.cs index 948dfe3ac..cf4aba7bc 100644 --- a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/AbsoluteModifierType.cs +++ b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/AbsoluteModifierType.cs @@ -1,21 +1,17 @@ using System; -using System.Collections.Generic; namespace Artemis.Core.DefaultTypes { - internal class AbsoluteModifierType : DataBindingModifierType + internal class AbsoluteModifierType : DataBindingModifierType { - public override IReadOnlyCollection CompatibleTypes => Constants.NumberTypes; - public override bool SupportsParameter => false; - public override string Name => "Absolute"; public override string Icon => "NumericPositive1"; public override string Category => "Advanced"; public override string Description => "Converts the input value to an absolute value"; - public override object Apply(object currentValue, object parameterValue) + public override double Apply(double currentValue) { - return Math.Abs(Convert.ToSingle(currentValue)); + return Math.Abs(currentValue); } } } \ No newline at end of file diff --git a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/DivideModifierType.cs b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/DivideModifierType.cs index 2bd4d304c..ce87e4158 100644 --- a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/DivideModifierType.cs +++ b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/DivideModifierType.cs @@ -1,24 +1,17 @@ -using System; -using System.Collections.Generic; - -namespace Artemis.Core.DefaultTypes +namespace Artemis.Core.DefaultTypes { - internal class DivideModifierType : DataBindingModifierType + internal class DivideModifierType : DataBindingModifierType { - public override IReadOnlyCollection CompatibleTypes => Constants.NumberTypes; - public override Type ParameterType => typeof(float); - public override string Name => "Divide by"; public override string Icon => "Divide"; - public override object Apply(object currentValue, object parameterValue) + public override double Apply(double currentValue, double parameterValue) { - float parameter = Convert.ToSingle(parameterValue); // Ye ye none of that - if (parameter == 0) + if (parameterValue == 0) return 0; - return Convert.ToSingle(currentValue) / parameter; + return currentValue / parameterValue; } } } \ No newline at end of file diff --git a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/MaxModifierType.cs b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/MaxModifierType.cs index 81c7d35a3..edd978108 100644 --- a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/MaxModifierType.cs +++ b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/MaxModifierType.cs @@ -1,20 +1,17 @@ using System; -using System.Collections.Generic; namespace Artemis.Core.DefaultTypes { - internal class MaxModifierType : DataBindingModifierType + internal class MaxModifierType : DataBindingModifierType { - public override IReadOnlyCollection CompatibleTypes => Constants.NumberTypes; - public override string Name => "Max"; public override string Icon => "ChevronUpBoxOutline"; public override string Category => "Advanced"; public override string Description => "Keeps only the largest of input value and parameter"; - public override object Apply(object currentValue, object parameterValue) + public override double Apply(double currentValue, double parameterValue) { - return Math.Max(Convert.ToSingle(currentValue), Convert.ToSingle(parameterValue)); + return Math.Max(currentValue, parameterValue); } } } \ No newline at end of file diff --git a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/MinModifierType.cs b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/MinModifierType.cs index 9c8b8b1dc..2b2913d9f 100644 --- a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/MinModifierType.cs +++ b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/MinModifierType.cs @@ -1,20 +1,17 @@ using System; -using System.Collections.Generic; namespace Artemis.Core.DefaultTypes { - internal class MinModifierType : DataBindingModifierType + internal class MinModifierType : DataBindingModifierType { - public override IReadOnlyCollection CompatibleTypes => Constants.NumberTypes; - public override string Name => "Min"; public override string Icon => "ChevronDownBoxOutline"; public override string Category => "Advanced"; public override string Description => "Keeps only the smallest of input value and parameter"; - public override object Apply(object currentValue, object parameterValue) + public override double Apply(double currentValue, double parameterValue) { - return Math.Min(Convert.ToSingle(currentValue), Convert.ToSingle(parameterValue)); + return Math.Min(currentValue, parameterValue); } } } \ No newline at end of file diff --git a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/ModuloModifierType.cs b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/ModuloModifierType.cs index 1d8d88af6..b378f3174 100644 --- a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/ModuloModifierType.cs +++ b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/ModuloModifierType.cs @@ -1,20 +1,15 @@ -using System; -using System.Collections.Generic; - -namespace Artemis.Core.DefaultTypes +namespace Artemis.Core.DefaultTypes { - internal class ModuloModifierType : DataBindingModifierType + internal class ModuloModifierType : DataBindingModifierType { - public override IReadOnlyCollection CompatibleTypes => Constants.NumberTypes; - public override string Name => "Modulo"; public override string Icon => "Stairs"; public override string Category => "Advanced"; public override string Description => "Calculates the remained of the division between the input value and the parameter"; - public override object Apply(object currentValue, object parameterValue) + public override double Apply(double currentValue, double parameterValue) { - return Convert.ToSingle(currentValue) % Convert.ToSingle(parameterValue); + return currentValue % parameterValue; } } } \ No newline at end of file diff --git a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/MultiplicationModifier.cs b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/MultiplicationModifier.cs index bf9a804b8..9992868e6 100644 --- a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/MultiplicationModifier.cs +++ b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/MultiplicationModifier.cs @@ -1,19 +1,13 @@ -using System; -using System.Collections.Generic; - -namespace Artemis.Core.DefaultTypes +namespace Artemis.Core.DefaultTypes { - internal class MultiplicationModifierType : DataBindingModifierType + internal class MultiplicationModifierType : DataBindingModifierType { - public override IReadOnlyCollection CompatibleTypes => Constants.NumberTypes; - public override Type ParameterType => typeof(float); - public override string Name => "Multiply by"; public override string Icon => "Close"; - public override object Apply(object currentValue, object parameterValue) + public override double Apply(double currentValue, double parameterValue) { - return Convert.ToSingle(currentValue) * Convert.ToSingle(parameterValue); + return currentValue * parameterValue; } } } \ No newline at end of file diff --git a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/PercentageOfModifierType.cs b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/PercentageOfModifierType.cs index 09ba43359..bf435ca11 100644 --- a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/PercentageOfModifierType.cs +++ b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/PercentageOfModifierType.cs @@ -1,25 +1,18 @@ -using System; -using System.Collections.Generic; - -namespace Artemis.Core.DefaultTypes +namespace Artemis.Core.DefaultTypes { - internal class PercentageOfModifierType : DataBindingModifierType + internal class PercentageOfModifierType : DataBindingModifierType { - public override IReadOnlyCollection CompatibleTypes => Constants.NumberTypes; - public override Type ParameterType => typeof(float); - public override string Name => "Percentage of"; public override string Icon => "Percent"; public override string Description => "Calculates how much percent the parameter value is of the current value"; - public override object Apply(object currentValue, object parameterValue) + public override double Apply(double currentValue, double parameterValue) { - float parameter = Convert.ToSingle(parameterValue); // Ye ye none of that - if (parameter == 0f) - return 100f; + if (parameterValue == 0d) + return 100d; - return 100f / Convert.ToSingle(parameterValue) * Convert.ToSingle(currentValue); + return 100d / parameterValue * currentValue; } } } \ No newline at end of file diff --git a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/PowerModifierType.cs b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/PowerModifierType.cs index d9499f9e2..c5338510d 100644 --- a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/PowerModifierType.cs +++ b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/PowerModifierType.cs @@ -1,20 +1,17 @@ using System; -using System.Collections.Generic; namespace Artemis.Core.DefaultTypes { - internal class PowerModifierType : DataBindingModifierType + internal class PowerModifierType : DataBindingModifierType { - public override IReadOnlyCollection CompatibleTypes => Constants.NumberTypes; - public override string Name => "Power"; public override string Icon => "Exponent"; public override string Category => "Advanced"; public override string Description => "Raises the input value to the power of the parameter value"; - public override object Apply(object currentValue, object parameterValue) + public override double Apply(double currentValue, double parameterValue) { - return Math.Pow(Convert.ToSingle(currentValue), Convert.ToSingle(parameterValue)); + return Math.Pow(currentValue, parameterValue); } } } \ No newline at end of file diff --git a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/Rounding/CeilingModifierType.cs b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/Rounding/CeilingModifierType.cs index a0ec3bd8e..a3f71d8e3 100644 --- a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/Rounding/CeilingModifierType.cs +++ b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/Rounding/CeilingModifierType.cs @@ -1,22 +1,17 @@ using System; -using System.Collections.Generic; namespace Artemis.Core.DefaultTypes { - internal class CeilingModifierType : DataBindingModifierType + internal class CeilingModifierType : DataBindingModifierType { - public override IReadOnlyCollection CompatibleTypes => Constants.NumberTypes; - public override bool SupportsParameter => false; - public override string Name => "Round up"; public override string Icon => "ArrowUp"; public override string Category => "Rounding"; public override string Description => "Ceils the input, rounding it up to the nearest whole number"; - public override object Apply(object currentValue, object parameterValue) + public override double Apply(double currentValue) { - float floatValue = Convert.ToSingle(currentValue); - return Math.Ceiling(floatValue); + return Math.Ceiling(currentValue); } } } \ No newline at end of file diff --git a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/Rounding/FloorModifierType.cs b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/Rounding/FloorModifierType.cs index 1c61ca9a4..77882edfe 100644 --- a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/Rounding/FloorModifierType.cs +++ b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/Rounding/FloorModifierType.cs @@ -1,22 +1,17 @@ using System; -using System.Collections.Generic; namespace Artemis.Core.DefaultTypes { - internal class FloorModifierType : DataBindingModifierType + internal class FloorModifierType : DataBindingModifierType { - public override IReadOnlyCollection CompatibleTypes => Constants.NumberTypes; - public override bool SupportsParameter => false; - public override string Name => "Round down"; public override string Icon => "ArrowDown"; public override string Category => "Rounding"; public override string Description => "Floors the input, rounding it down to the nearest whole number"; - public override object Apply(object currentValue, object parameterValue) + public override double Apply(double currentValue) { - float floatValue = Convert.ToSingle(currentValue); - return Math.Floor(floatValue); + return Math.Floor(currentValue); } } } \ No newline at end of file diff --git a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/Rounding/RoundModifierType.cs b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/Rounding/RoundModifierType.cs index 7952718ee..b45f8fbd5 100644 --- a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/Rounding/RoundModifierType.cs +++ b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/Rounding/RoundModifierType.cs @@ -1,22 +1,17 @@ using System; -using System.Collections.Generic; namespace Artemis.Core.DefaultTypes { - internal class RoundModifierType : DataBindingModifierType + internal class RoundModifierType : DataBindingModifierType { - public override IReadOnlyCollection CompatibleTypes => Constants.NumberTypes; - public override bool SupportsParameter => false; - public override string Name => "Round"; public override string Icon => "ArrowCollapse"; public override string Category => "Rounding"; public override string Description => "Rounds the input to the nearest whole number"; - public override object Apply(object currentValue, object parameterValue) + public override double Apply(double currentValue) { - float floatValue = Convert.ToSingle(currentValue); - return Math.Round(floatValue, MidpointRounding.AwayFromZero); + return Math.Round(currentValue, MidpointRounding.AwayFromZero); } } } \ No newline at end of file diff --git a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/SquareRootModifierType.cs b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/SquareRootModifierType.cs index eb4b1384c..fa8daed13 100644 --- a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/SquareRootModifierType.cs +++ b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/SquareRootModifierType.cs @@ -1,21 +1,17 @@ using System; -using System.Collections.Generic; namespace Artemis.Core.DefaultTypes { - internal class SquareRootModifierType : DataBindingModifierType + internal class SquareRootModifierType : DataBindingModifierType { - public override IReadOnlyCollection CompatibleTypes => Constants.NumberTypes; - public override bool SupportsParameter => false; - public override string Name => "Square root"; public override string Icon => "SquareRoot"; public override string Category => "Advanced"; public override string Description => "Calculates square root of the input value"; - public override object Apply(object currentValue, object parameterValue) + public override double Apply(double currentValue) { - return Math.Sqrt(Convert.ToSingle(currentValue)); + return Math.Sqrt(currentValue); } } } \ No newline at end of file diff --git a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/SubtractModifierType.cs b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/SubtractModifierType.cs index 4bc68a3a4..79cee880b 100644 --- a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/SubtractModifierType.cs +++ b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/SubtractModifierType.cs @@ -1,18 +1,13 @@ -using System; -using System.Collections.Generic; - -namespace Artemis.Core.DefaultTypes +namespace Artemis.Core.DefaultTypes { - internal class SubtractModifierType : DataBindingModifierType + internal class SubtractModifierType : DataBindingModifierType { - public override IReadOnlyCollection CompatibleTypes => Constants.NumberTypes; - public override string Name => "Subtract"; public override string Icon => "Minus"; - public override object Apply(object currentValue, object parameterValue) + public override double Apply(double currentValue, double parameterValue) { - return Convert.ToSingle(currentValue) - Convert.ToSingle(parameterValue); + return currentValue - parameterValue; } } } \ No newline at end of file diff --git a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/SumModifierType.cs b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/SumModifierType.cs index 2b933780f..eac8f9a15 100644 --- a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/SumModifierType.cs +++ b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/SumModifierType.cs @@ -1,18 +1,13 @@ -using System; -using System.Collections.Generic; - -namespace Artemis.Core.DefaultTypes +namespace Artemis.Core.DefaultTypes { - internal class SumModifierType : DataBindingModifierType + internal class SumModifierType : DataBindingModifierType { - public override IReadOnlyCollection CompatibleTypes => Constants.NumberTypes; - public override string Name => "Sum"; public override string Icon => "Plus"; - public override object Apply(object currentValue, object parameterValue) + public override double Apply(double currentValue, double parameterValue) { - return Convert.ToSingle(currentValue) + Convert.ToSingle(parameterValue); + return currentValue + parameterValue; } } } \ No newline at end of file diff --git a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/Trigonometry/CosecantModifierType.cs b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/Trigonometry/CosecantModifierType.cs index df76c1efc..af974d203 100644 --- a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/Trigonometry/CosecantModifierType.cs +++ b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/Trigonometry/CosecantModifierType.cs @@ -1,21 +1,17 @@ using System; -using System.Collections.Generic; namespace Artemis.Core.DefaultTypes { - internal class CosecantModifierType : DataBindingModifierType + internal class CosecantModifierType : DataBindingModifierType { - public override IReadOnlyCollection CompatibleTypes => Constants.NumberTypes; - public override bool SupportsParameter => false; - public override string Name => "Cosecant"; public override string Icon => null; public override string Category => "Trigonometry"; public override string Description => "Treats the input as an angle and calculates the cosecant"; - public override object Apply(object currentValue, object parameterValue) + public override double Apply(double currentValue) { - return 1f / Math.Sin(Convert.ToSingle(currentValue)); + return 1d / Math.Sin(currentValue); } } } \ No newline at end of file diff --git a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/Trigonometry/CosineModifierType.cs b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/Trigonometry/CosineModifierType.cs index 4ceb0988c..b0fbf2932 100644 --- a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/Trigonometry/CosineModifierType.cs +++ b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/Trigonometry/CosineModifierType.cs @@ -1,21 +1,17 @@ using System; -using System.Collections.Generic; namespace Artemis.Core.DefaultTypes { - internal class CosineModifierType : DataBindingModifierType + internal class CosineModifierType : DataBindingModifierType { - public override IReadOnlyCollection CompatibleTypes => Constants.NumberTypes; - public override bool SupportsParameter => false; - public override string Name => "Cosine"; public override string Icon => "MathCos"; public override string Category => "Trigonometry"; public override string Description => "Treats the input as an angle and calculates the cosine"; - public override object Apply(object currentValue, object parameterValue) + public override double Apply(double currentValue) { - return Math.Cos(Convert.ToSingle(currentValue)); + return Math.Cos(currentValue); } } } \ No newline at end of file diff --git a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/Trigonometry/CotangentModifierType.cs b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/Trigonometry/CotangentModifierType.cs index 1648aa16a..76bd0adc6 100644 --- a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/Trigonometry/CotangentModifierType.cs +++ b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/Trigonometry/CotangentModifierType.cs @@ -1,21 +1,17 @@ using System; -using System.Collections.Generic; namespace Artemis.Core.DefaultTypes { - internal class CotangentModifierType : DataBindingModifierType + internal class CotangentModifierType : DataBindingModifierType { - public override IReadOnlyCollection CompatibleTypes => Constants.NumberTypes; - public override bool SupportsParameter => false; - public override string Name => "Cotangent"; public override string Icon => null; public override string Category => "Trigonometry"; public override string Description => "Treats the input as an angle and calculates the cotangent"; - public override object Apply(object currentValue, object parameterValue) + public override double Apply(double currentValue) { - return 1f / Math.Tan(Convert.ToSingle(currentValue)); + return 1d / Math.Tan(currentValue); } } } \ No newline at end of file diff --git a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/Trigonometry/SecantModifierType.cs b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/Trigonometry/SecantModifierType.cs index d44a107b0..f55f2982b 100644 --- a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/Trigonometry/SecantModifierType.cs +++ b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/Trigonometry/SecantModifierType.cs @@ -1,21 +1,17 @@ using System; -using System.Collections.Generic; namespace Artemis.Core.DefaultTypes { - internal class SecantModifierType : DataBindingModifierType + internal class SecantModifierType : DataBindingModifierType { - public override IReadOnlyCollection CompatibleTypes => Constants.NumberTypes; - public override bool SupportsParameter => false; - public override string Name => "Secant"; public override string Icon => null; public override string Category => "Trigonometry"; public override string Description => "Treats the input as an angle and calculates the secant"; - public override object Apply(object currentValue, object parameterValue) + public override double Apply(double currentValue) { - return 1f / Math.Cos(Convert.ToSingle(currentValue)); + return 1d / Math.Cos(currentValue); } } } \ No newline at end of file diff --git a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/Trigonometry/SineModifierType.cs b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/Trigonometry/SineModifierType.cs index 9915cdf87..ba502c6d0 100644 --- a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/Trigonometry/SineModifierType.cs +++ b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/Trigonometry/SineModifierType.cs @@ -1,21 +1,17 @@ using System; -using System.Collections.Generic; namespace Artemis.Core.DefaultTypes { - internal class SineModifierType : DataBindingModifierType + internal class SineModifierType : DataBindingModifierType { - public override IReadOnlyCollection CompatibleTypes => Constants.NumberTypes; - public override bool SupportsParameter => false; - public override string Name => "Sine"; public override string Icon => "MathSin"; public override string Category => "Trigonometry"; public override string Description => "Treats the input as an angle and calculates the sine"; - public override object Apply(object currentValue, object parameterValue) + public override double Apply(double currentValue) { - return Math.Sin(Convert.ToSingle(currentValue)); + return Math.Sin(currentValue); } } } \ No newline at end of file diff --git a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/Trigonometry/TangentModifierType.cs b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/Trigonometry/TangentModifierType.cs index f366f3096..14c23d7ce 100644 --- a/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/Trigonometry/TangentModifierType.cs +++ b/src/Artemis.Core/DefaultTypes/DataBindings/Modifiers/Numbers/Trigonometry/TangentModifierType.cs @@ -1,21 +1,17 @@ using System; -using System.Collections.Generic; namespace Artemis.Core.DefaultTypes { - internal class TangentModifierType : DataBindingModifierType + internal class TangentModifierType : DataBindingModifierType { - public override IReadOnlyCollection CompatibleTypes => Constants.NumberTypes; - public override bool SupportsParameter => false; - public override string Name => "Tangent"; public override string Icon => "MathTan"; public override string Category => "Trigonometry"; public override string Description => "Treats the input as an angle and calculates the tangent"; - public override object Apply(object currentValue, object parameterValue) + public override double Apply(double currentValue) { - return Math.Tan(Convert.ToSingle(currentValue)); + return Math.Tan(currentValue); } } } \ No newline at end of file diff --git a/src/Artemis.Core/Models/Profile/Conditions/Abstract/BaseConditionOperator.cs b/src/Artemis.Core/Models/Profile/Conditions/Abstract/BaseConditionOperator.cs index f95557020..e67e73d78 100644 --- a/src/Artemis.Core/Models/Profile/Conditions/Abstract/BaseConditionOperator.cs +++ b/src/Artemis.Core/Models/Profile/Conditions/Abstract/BaseConditionOperator.cs @@ -33,7 +33,7 @@ namespace Artemis.Core public abstract Type LeftSideType { get; } /// - /// Gets the right side type of this condition operator. May be null if the operator does not support a left side type + /// Gets the right side type of this condition operator. May be null if the operator does not support a right side /// public abstract Type? RightSideType { get; } @@ -42,7 +42,14 @@ namespace Artemis.Core /// /// The type to check for, must be either the same or be castable to the target type /// Which side of the operator to check, left or right - public abstract bool SupportsType(Type type, ConditionParameterSide side); + public bool SupportsType(Type type, ConditionParameterSide side) + { + if (type == null) + return true; + if (side == ConditionParameterSide.Left) + return LeftSideType.IsCastableFrom(type); + return RightSideType != null && RightSideType.IsCastableFrom(type); + } /// /// Evaluates the condition with the input types being provided as objects diff --git a/src/Artemis.Core/Models/Profile/Conditions/Abstract/ConditionOperator.cs b/src/Artemis.Core/Models/Profile/Conditions/Abstract/ConditionOperator.cs index 16d42e147..416f41912 100644 --- a/src/Artemis.Core/Models/Profile/Conditions/Abstract/ConditionOperator.cs +++ b/src/Artemis.Core/Models/Profile/Conditions/Abstract/ConditionOperator.cs @@ -13,17 +13,7 @@ namespace Artemis.Core /// The parameter on the left side of the expression /// The parameter on the right side of the expression public abstract bool Evaluate(TLeftSide a, TRightSide b); - - /// - public override bool SupportsType(Type type, ConditionParameterSide side) - { - if (type == null) - return true; - if (side == ConditionParameterSide.Left) - return LeftSideType.IsCastableFrom(type); - return RightSideType.IsCastableFrom(type); - } - + /// internal override bool InternalEvaluate(object? leftSideValue, object? rightSideValue) { @@ -40,7 +30,7 @@ namespace Artemis.Core else rightSide = default; - return Evaluate(leftSide, rightSide); + return Evaluate(leftSide!, rightSide!); } /// @@ -61,16 +51,6 @@ namespace Artemis.Core /// The parameter on the left side of the expression public abstract bool Evaluate(TLeftSide a); - /// - public override bool SupportsType(Type type, ConditionParameterSide side) - { - if (type == null) - return true; - if (side == ConditionParameterSide.Left) - return LeftSideType.IsCastableFrom(type); - return false; - } - /// internal override bool InternalEvaluate(object? leftSideValue, object? rightSideValue) { @@ -81,7 +61,7 @@ namespace Artemis.Core else leftSide = default; - return Evaluate(leftSide); + return Evaluate(leftSide!); } /// diff --git a/src/Artemis.Core/Models/Profile/Conditions/DataModelConditionListPredicate.cs b/src/Artemis.Core/Models/Profile/Conditions/DataModelConditionListPredicate.cs index dcacd42b8..601483aef 100644 --- a/src/Artemis.Core/Models/Profile/Conditions/DataModelConditionListPredicate.cs +++ b/src/Artemis.Core/Models/Profile/Conditions/DataModelConditionListPredicate.cs @@ -388,7 +388,7 @@ namespace Artemis.Core return; if (!Operator.SupportsType(RightStaticValue.GetType(), ConditionParameterSide.Right)) - UpdateRightSideDynamic(null); + UpdateRightSideStatic(null); } } diff --git a/src/Artemis.Core/Models/Profile/Conditions/DataModelConditionPredicate.cs b/src/Artemis.Core/Models/Profile/Conditions/DataModelConditionPredicate.cs index 27cdcc4b2..db96b8c22 100644 --- a/src/Artemis.Core/Models/Profile/Conditions/DataModelConditionPredicate.cs +++ b/src/Artemis.Core/Models/Profile/Conditions/DataModelConditionPredicate.cs @@ -144,7 +144,7 @@ namespace Artemis.Core return; } - // No need to clear compiled expressions, without a left data model they are already null + // No need to check for compatibility without a left side, when left site does get set it will be checked again if (LeftPath == null || !LeftPath.IsValid) { Operator = conditionOperator; @@ -323,7 +323,7 @@ namespace Artemis.Core return; if (!Operator.SupportsType(RightStaticValue.GetType(), ConditionParameterSide.Right)) - UpdateRightSideDynamic(null); + UpdateRightSideStatic(null); } } diff --git a/src/Artemis.Core/Models/Profile/DataBindings/Modes/ConditionalDataBinding.cs b/src/Artemis.Core/Models/Profile/DataBindings/Modes/Conditional/ConditionalDataBinding.cs similarity index 100% rename from src/Artemis.Core/Models/Profile/DataBindings/Modes/ConditionalDataBinding.cs rename to src/Artemis.Core/Models/Profile/DataBindings/Modes/Conditional/ConditionalDataBinding.cs diff --git a/src/Artemis.Core/Models/Profile/DataBindings/Modes/DataBindingCondition.cs b/src/Artemis.Core/Models/Profile/DataBindings/Modes/Conditional/DataBindingCondition.cs similarity index 100% rename from src/Artemis.Core/Models/Profile/DataBindings/Modes/DataBindingCondition.cs rename to src/Artemis.Core/Models/Profile/DataBindings/Modes/Conditional/DataBindingCondition.cs diff --git a/src/Artemis.Core/Models/Profile/DataBindings/Modes/IDataBindingCondition.cs b/src/Artemis.Core/Models/Profile/DataBindings/Modes/Conditional/IDataBindingCondition.cs similarity index 100% rename from src/Artemis.Core/Models/Profile/DataBindings/Modes/IDataBindingCondition.cs rename to src/Artemis.Core/Models/Profile/DataBindings/Modes/Conditional/IDataBindingCondition.cs diff --git a/src/Artemis.Core/Models/Profile/DataBindings/Modes/DataBindingModifierType.cs b/src/Artemis.Core/Models/Profile/DataBindings/Modes/DataBindingModifierType.cs deleted file mode 100644 index 7a5a60ef2..000000000 --- a/src/Artemis.Core/Models/Profile/DataBindings/Modes/DataBindingModifierType.cs +++ /dev/null @@ -1,79 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; - -namespace Artemis.Core -{ - /// - /// A modifier that changes the source value of a data binding in some way - /// - public abstract class DataBindingModifierType - { - /// - /// Gets the plugin info this data binding modifier belongs to - /// Note: Not set until after registering - /// - public PluginInfo PluginInfo { get; internal set; } - - /// - /// Gets the types this modifier supports - /// - public abstract IReadOnlyCollection CompatibleTypes { get; } - - /// - /// Gets the supported parameter type - /// If null, the parameter type will match the source property - /// - public virtual Type ParameterType => null; - - /// - /// Gets or sets whether this modifier supports a parameter, defaults to true - /// - public virtual bool SupportsParameter => true; - - /// - /// Gets the name of this modifier - /// - public abstract string Name { get; } - - /// - /// Gets or sets the icon of this modifier - /// - public abstract string Icon { get; } - - /// - /// Gets the description of this modifier - /// - public virtual string Description => null; - - /// - /// Gets the category of this modifier - /// - public virtual string Category => null; - - /// - /// Returns whether the given type is supported by the modifier - /// - public bool SupportsType(Type type) - { - if (type == null) - return true; - return CompatibleTypes.Any(t => t.IsCastableFrom(type)); - } - - /// - /// Called whenever the modifier must apply to a specific value, must be a value of a type contained in - /// - /// - /// - /// The current value before modification, is always of a type contained in - /// - /// - /// - /// The parameter to use for the modification, is always of a type contained in - /// - /// - /// The modified value, must be a value of a type contained in - public abstract object Apply(object currentValue, object parameterValue); - } -} \ No newline at end of file diff --git a/src/Artemis.Core/Models/Profile/DataBindings/Modes/Direct/BaseDataBindingModifierType.cs b/src/Artemis.Core/Models/Profile/DataBindings/Modes/Direct/BaseDataBindingModifierType.cs new file mode 100644 index 000000000..d4658d212 --- /dev/null +++ b/src/Artemis.Core/Models/Profile/DataBindings/Modes/Direct/BaseDataBindingModifierType.cs @@ -0,0 +1,82 @@ +using System; + +namespace Artemis.Core +{ + /// + /// A modifier that changes the source value of a data binding in some way + /// + /// To implement your own condition operator, inherit or + /// + /// + /// + public abstract class BaseDataBindingModifierType + { + /// + /// Gets the plugin info this data binding modifier belongs to + /// Note: Not set until after registering + /// + public PluginInfo PluginInfo { get; internal set; } + + /// + /// Gets the value type of this modifier type + /// + public abstract Type ValueType { get; } + + /// + /// Gets the parameter type of this modifier type. May be null if the modifier type does not support a parameter + /// + public abstract Type? ParameterType { get; } + + /// + /// Gets the name of this modifier + /// + public abstract string Name { get; } + + /// + /// Gets or sets the icon of this modifier + /// + public abstract string Icon { get; } + + /// + /// Gets the description of this modifier + /// + public virtual string Description => null; + + /// + /// Gets the category of this modifier + /// + public virtual string Category => null; + + /// + /// Returns whether the given type is supported by the modifier + /// + /// The type to check for, must be either the same or be castable to the target type + /// Which part of the modifier to check, the value or the parameter + public bool SupportsType(Type type, ModifierTypePart part) + { + if (type == null) + return true; + if (part == ModifierTypePart.Value) + return ValueType.IsCastableFrom(type); + return ParameterType != null && ParameterType.IsCastableFrom(type); + } + + /// + /// Applies the modifier to the provided current value + /// + /// This leaves the caller responsible for the types matching and + /// + /// + /// + /// The current value before modification, type should match + /// The parameter to use for the modification, type should match + /// The modified value, with a type of + internal abstract object? InternalApply(object? currentValue, object? parameterValue); + } + + public enum ModifierTypePart + { + Value, + Parameter + } +} \ No newline at end of file diff --git a/src/Artemis.Core/Models/Profile/DataBindings/Modes/DataBindingModifier.cs b/src/Artemis.Core/Models/Profile/DataBindings/Modes/Direct/DataBindingModifier.cs similarity index 84% rename from src/Artemis.Core/Models/Profile/DataBindings/Modes/DataBindingModifier.cs rename to src/Artemis.Core/Models/Profile/DataBindings/Modes/Direct/DataBindingModifier.cs index d570463de..05d4c0aba 100644 --- a/src/Artemis.Core/Models/Profile/DataBindings/Modes/DataBindingModifier.cs +++ b/src/Artemis.Core/Models/Profile/DataBindings/Modes/Direct/DataBindingModifier.cs @@ -30,7 +30,7 @@ namespace Artemis.Core /// /// Gets the type of modifier that is being applied /// - public DataBindingModifierType ModifierType { get; private set; } + public BaseDataBindingModifierType? ModifierType { get; private set; } /// /// Gets the direct data binding this modifier is applied to @@ -40,7 +40,7 @@ namespace Artemis.Core /// /// Gets the type of the parameter, can either be dynamic (based on a data model value) or static /// - public ProfileRightSideType ParameterType { get; private set; } + public ProfileRightSideType ParameterType { get; set; } /// /// Gets or sets the position at which the modifier appears on the data binding @@ -56,7 +56,7 @@ namespace Artemis.Core /// Gets the parameter static value, only used it is /// /// - public object ParameterStaticValue { get; private set; } + public object? ParameterStaticValue { get; private set; } internal DataBindingModifierEntity Entity { get; set; } @@ -65,7 +65,7 @@ namespace Artemis.Core /// /// The value to apply the modifier to, should be of the same type as the data binding target /// The modified value - public object Apply(object? currentValue) + public object? Apply(object? currentValue) { if (_disposed) throw new ObjectDisposedException("DataBindingModifier"); @@ -73,17 +73,17 @@ namespace Artemis.Core if (ModifierType == null) return currentValue; - if (!ModifierType.SupportsParameter) - return ModifierType.Apply(currentValue, null); + if (ModifierType.ParameterType == null) + return ModifierType.InternalApply(currentValue, null); if (ParameterType == ProfileRightSideType.Dynamic && ParameterPath != null && ParameterPath.IsValid) { object? value = ParameterPath.GetValue(); - return ModifierType.Apply(currentValue, value); + return ModifierType.InternalApply(currentValue, value); } if (ParameterType == ProfileRightSideType.Static) - return ModifierType.Apply(currentValue, ParameterStaticValue); + return ModifierType.InternalApply(currentValue, ParameterStaticValue); return currentValue; } @@ -92,12 +92,11 @@ namespace Artemis.Core /// Updates the modifier type of the modifier and re-compiles the expression /// /// - public void UpdateModifierType(DataBindingModifierType modifierType) + public void UpdateModifierType(BaseDataBindingModifierType? modifierType) { if (_disposed) throw new ObjectDisposedException("DataBindingModifier"); - // Calling CreateExpression will clear compiled expressions if (modifierType == null) { ModifierType = null; @@ -105,11 +104,36 @@ namespace Artemis.Core } Type targetType = DirectDataBinding.DataBinding.GetTargetType(); - if (!modifierType.SupportsType(targetType)) + if (!modifierType.SupportsType(targetType, ModifierTypePart.Value)) throw new ArtemisCoreException($"Cannot apply modifier type {modifierType.GetType().Name} to this modifier because " + $"it does not support this data binding's type {targetType.Name}"); ModifierType = modifierType; + ValidateParameter(); + } + + private void ValidateParameter() + { + if (ModifierType == null) + return; + + if (ParameterType == ProfileRightSideType.Dynamic) + { + if (ParameterPath == null || !ParameterPath.IsValid) + return; + + Type parameterType = ParameterPath.GetPropertyType()!; + if (!ModifierType.SupportsType(parameterType, ModifierTypePart.Parameter)) + UpdateParameterDynamic(null); + } + else + { + if (ParameterStaticValue == null) + return; + + if (!ModifierType.SupportsType(ParameterStaticValue.GetType(), ModifierTypePart.Parameter)) + UpdateParameterStatic(null); + } } /// @@ -165,7 +189,7 @@ namespace Artemis.Core // Modifier type if (Entity.ModifierTypePluginGuid != null && ModifierType == null) { - DataBindingModifierType modifierType = DataBindingModifierTypeStore.Get(Entity.ModifierTypePluginGuid.Value, Entity.ModifierType)?.DataBindingModifierType; + BaseDataBindingModifierType modifierType = DataBindingModifierTypeStore.Get(Entity.ModifierTypePluginGuid.Value, Entity.ModifierType)?.DataBindingModifierType; if (modifierType != null) UpdateModifierType(modifierType); } @@ -261,7 +285,7 @@ namespace Artemis.Core if (ModifierType != null) return; - DataBindingModifierType modifierType = e.TypeRegistration.DataBindingModifierType; + BaseDataBindingModifierType modifierType = e.TypeRegistration.DataBindingModifierType; if (modifierType.PluginInfo.Guid == Entity.ModifierTypePluginGuid && modifierType.GetType().Name == Entity.ModifierType) UpdateModifierType(modifierType); } diff --git a/src/Artemis.Core/Models/Profile/DataBindings/Modes/Direct/DataBindingModifierType.cs b/src/Artemis.Core/Models/Profile/DataBindings/Modes/Direct/DataBindingModifierType.cs new file mode 100644 index 000000000..c09cd8fcc --- /dev/null +++ b/src/Artemis.Core/Models/Profile/DataBindings/Modes/Direct/DataBindingModifierType.cs @@ -0,0 +1,81 @@ +using System; + +namespace Artemis.Core +{ + /// + /// A modifier that changes the source value of a data binding in some way using a parameter + /// + public abstract class DataBindingModifierType : BaseDataBindingModifierType + { + /// + public override Type ValueType => typeof(TValue); + + /// + public override Type ParameterType => typeof(TParameter); + + /// + /// Called whenever the modifier must apply to a specific value + /// + /// + /// The current value before modification + /// + /// + /// The parameter to use for the modification + /// + /// The modified value> + public abstract TValue Apply(TValue currentValue, TParameter parameterValue); + + /// + internal override object? InternalApply(object? currentValue, object? parameterValue) + { + // TODO: Can we avoid boxing/unboxing? + TValue current; + if (currentValue != null) + current = (TValue) Convert.ChangeType(currentValue, typeof(TValue)); + else + current = default; + + TParameter parameter; + if (parameterValue != null) + parameter = (TParameter) Convert.ChangeType(parameterValue, typeof(TParameter)); + else + parameter = default; + + return Apply(current!, parameter!); + } + } + + /// + /// A modifier that changes the source value of a data binding in some way + /// + public abstract class DataBindingModifierType : BaseDataBindingModifierType + { + /// + public override Type ValueType => typeof(TValue); + + /// + public override Type? ParameterType => null; + + /// + /// Called whenever the modifier must apply to a specific value + /// + /// + /// The current value before modification + /// + /// The modified value + public abstract TValue Apply(TValue currentValue); + + /// + internal override object? InternalApply(object? currentValue, object? parameterValue) + { + // TODO: Can we avoid boxing/unboxing? + TValue current; + if (currentValue != null) + current = (TValue) Convert.ChangeType(currentValue, typeof(TValue)); + else + current = default; + + return Apply(current!); + } + } +} \ No newline at end of file diff --git a/src/Artemis.Core/Models/Profile/DataBindings/Modes/IDataBindingModifier.cs b/src/Artemis.Core/Models/Profile/DataBindings/Modes/Direct/IDataBindingModifier.cs similarity index 100% rename from src/Artemis.Core/Models/Profile/DataBindings/Modes/IDataBindingModifier.cs rename to src/Artemis.Core/Models/Profile/DataBindings/Modes/Direct/IDataBindingModifier.cs diff --git a/src/Artemis.Core/Services/Registration/DataBindingService.cs b/src/Artemis.Core/Services/Registration/DataBindingService.cs index 9b39cd61b..28218cf65 100644 --- a/src/Artemis.Core/Services/Registration/DataBindingService.cs +++ b/src/Artemis.Core/Services/Registration/DataBindingService.cs @@ -12,7 +12,7 @@ namespace Artemis.Core.Services RegisterBuiltInModifiers(); } - public DataBindingModifierTypeRegistration RegisterModifierType(PluginInfo pluginInfo, DataBindingModifierType dataBindingModifierType) + public DataBindingModifierTypeRegistration RegisterModifierType(PluginInfo pluginInfo, BaseDataBindingModifierType dataBindingModifierType) { if (pluginInfo == null) throw new ArgumentNullException(nameof(pluginInfo)); @@ -30,12 +30,12 @@ namespace Artemis.Core.Services DataBindingModifierTypeStore.Remove(registration); } - public List GetCompatibleModifierTypes(Type type) + public List GetCompatibleModifierTypes(Type type, ModifierTypePart part) { - return DataBindingModifierTypeStore.GetForType(type).Select(r => r.DataBindingModifierType).ToList(); + return DataBindingModifierTypeStore.GetForType(type, part).Select(r => r.DataBindingModifierType).ToList(); } - public DataBindingModifierType GetModifierType(Guid modifierTypePluginGuid, string modifierType) + public BaseDataBindingModifierType GetModifierType(Guid modifierTypePluginGuid, string modifierType) { return DataBindingModifierTypeStore.Get(modifierTypePluginGuid, modifierType)?.DataBindingModifierType; } diff --git a/src/Artemis.Core/Services/Registration/Interfaces/IDataBindingService.cs b/src/Artemis.Core/Services/Registration/Interfaces/IDataBindingService.cs index b40248b37..bf33b1867 100644 --- a/src/Artemis.Core/Services/Registration/Interfaces/IDataBindingService.cs +++ b/src/Artemis.Core/Services/Registration/Interfaces/IDataBindingService.cs @@ -14,7 +14,7 @@ namespace Artemis.Core.Services /// /// The PluginInfo of the plugin this modifier type belongs to /// The modifier type to register - DataBindingModifierTypeRegistration RegisterModifierType([NotNull] PluginInfo pluginInfo, [NotNull] DataBindingModifierType dataBindingModifierType); + DataBindingModifierTypeRegistration RegisterModifierType([NotNull] PluginInfo pluginInfo, [NotNull] BaseDataBindingModifierType dataBindingModifierType); /// /// Removes a modifier type so it is no longer available for use in data bindings @@ -25,7 +25,7 @@ namespace Artemis.Core.Services /// /// Returns all the data binding modifier types compatible with the provided type /// - List GetCompatibleModifierTypes(Type type); + List GetCompatibleModifierTypes(Type type, ModifierTypePart part); /// /// Gets a modifier type by its plugin GUID and type name @@ -33,6 +33,6 @@ namespace Artemis.Core.Services /// The modifier type's plugin GUID /// The type name of the modifier type /// - DataBindingModifierType GetModifierType(Guid modifierTypePluginGuid, string modifierType); + BaseDataBindingModifierType GetModifierType(Guid modifierTypePluginGuid, string modifierType); } } \ No newline at end of file diff --git a/src/Artemis.Core/Stores/DataBindingModifierTypeStore.cs b/src/Artemis.Core/Stores/DataBindingModifierTypeStore.cs index 3536bdb8d..bd2402f3c 100644 --- a/src/Artemis.Core/Stores/DataBindingModifierTypeStore.cs +++ b/src/Artemis.Core/Stores/DataBindingModifierTypeStore.cs @@ -8,7 +8,7 @@ namespace Artemis.Core { private static readonly List Registrations = new List(); - public static DataBindingModifierTypeRegistration Add(DataBindingModifierType modifierType) + public static DataBindingModifierTypeRegistration Add(BaseDataBindingModifierType modifierType) { DataBindingModifierTypeRegistration typeRegistration; lock (Registrations) @@ -46,19 +46,21 @@ namespace Artemis.Core } } - public static List GetForType(Type type) + public static List GetForType(Type type, ModifierTypePart part) { lock (Registrations) { if (type == null) return new List(Registrations); - List candidates = Registrations.Where(r => r.DataBindingModifierType.CompatibleTypes.Any(t => t == type)).ToList(); + List candidates = Registrations.Where(r => r.DataBindingModifierType.SupportsType(type, part)).ToList(); - // If there are multiple operators with the same description, use the closest match + // If there are multiple modifiers with the same description, use the closest match foreach (IGrouping displayDataBindingModifiers in candidates.GroupBy(r => r.DataBindingModifierType.Name).Where(g => g.Count() > 1).ToList()) { - DataBindingModifierTypeRegistration closest = displayDataBindingModifiers.OrderByDescending(r => r.DataBindingModifierType.CompatibleTypes.Contains(type)).FirstOrDefault(); + DataBindingModifierTypeRegistration closest = part == ModifierTypePart.Value + ? displayDataBindingModifiers.OrderByDescending(r => r.DataBindingModifierType.ValueType.ScoreCastability(type)).First() + : displayDataBindingModifiers.OrderByDescending(r => r.DataBindingModifierType.ParameterType!.ScoreCastability(type)).First(); foreach (DataBindingModifierTypeRegistration displayDataBindingModifier in displayDataBindingModifiers) { if (displayDataBindingModifier != closest) diff --git a/src/Artemis.Core/Stores/Registrations/DataBindingModifierTypeRegistration.cs b/src/Artemis.Core/Stores/Registrations/DataBindingModifierTypeRegistration.cs index d00e19444..a49f32fa9 100644 --- a/src/Artemis.Core/Stores/Registrations/DataBindingModifierTypeRegistration.cs +++ b/src/Artemis.Core/Stores/Registrations/DataBindingModifierTypeRegistration.cs @@ -7,7 +7,7 @@ namespace Artemis.Core /// public class DataBindingModifierTypeRegistration { - internal DataBindingModifierTypeRegistration(DataBindingModifierType dataBindingModifierType, Plugin plugin) + internal DataBindingModifierTypeRegistration(BaseDataBindingModifierType dataBindingModifierType, Plugin plugin) { DataBindingModifierType = dataBindingModifierType; Plugin = plugin; @@ -18,7 +18,7 @@ namespace Artemis.Core /// /// Gets the data binding modifier that has been registered /// - public DataBindingModifierType DataBindingModifierType { get; } + public BaseDataBindingModifierType DataBindingModifierType { get; } /// /// Gets the plugin the data binding modifier is associated with diff --git a/src/Artemis.UI.Shared/DataModelVisualization/Input/DataModelDynamicView.xaml b/src/Artemis.UI.Shared/DataModelVisualization/Input/DataModelDynamicView.xaml index ed33a05da..1f516180f 100644 --- a/src/Artemis.UI.Shared/DataModelVisualization/Input/DataModelDynamicView.xaml +++ b/src/Artemis.UI.Shared/DataModelVisualization/Input/DataModelDynamicView.xaml @@ -41,6 +41,8 @@ + - -