Skip to content

Commit

Permalink
Add unary operator (#3604)
Browse files Browse the repository at this point in the history
<!-- 🚨 Please Do Not skip any instructions and information mentioned below as they are all required and essential to evaluate and test the PR. By fulfilling all the required information you will be able to reduce the volume of questions and most likely help merge the PR faster 🚨 -->

<!-- 📝 It is preferred if you keep the "☑️ Allow edits by maintainers" checked in the Pull Request Template as it increases collaboration with the Toolkit maintainers by permitting commits to your PR branch (only) created from your fork.  This can let us quickly make fixes for minor typos or forgotten StyleCop issues during review without needing to wait on you doing extra work. Let us help you help us! 🎉 -->


## Fixes #
<!-- Add the relevant issue number after the "#" mentioned above (for ex: Fixes #1234) which will automatically close the issue once the PR is merged. -->

Fixes #3523 

<!-- Add a brief overview here of the feature/bug & fix. -->

Introduce UnaryOperator to animation expressions, a new type of operation that only expects one operator, to fix the issue where the ! operator failed because it expected 2 operators like the other (non-unary) operators.

## PR Type
What kind of change does this PR introduce?
<!-- Please uncomment one or more that apply to this PR. -->

Bugfix 
<!-- - Feature -->
<!-- - Code style update (formatting) -->
<!-- - Refactoring (no functional changes, no api changes) -->
<!-- - Build or CI related changes -->
<!-- - Documentation content changes -->
<!-- - Sample app changes -->
<!-- - Other... Please describe: -->


## What is the current behavior?
With the current version of the toolkit, code like
```
    var visual = Windows.UI.Xaml.Hosting.ElementCompositionPreview.GetElementVisual(this);
    var a = !(visual.GetReference().Offset.X > 10);
    visual.StartAnimation("Offset.Y", ExpressionFunctions.Conditional(a, 1,2));
```
will throw this exception
`Can't have an operator that doesn't have 2 exactly params`

## What is the new behavior?
After these changes, no exception is thrown and the correct animation expression is generated

## PR Checklist

Please check if your PR fulfills the following requirements:

- [X] Tested code with current [supported SDKs](../readme.md#supported)
- [ ] Pull Request has been submitted to the documentation repository [instructions](..\contributing.md#docs). Link: <!-- docs PR link -->
- [ ] Sample in sample app has been added / updated (for bug fixes / features)
    - [ ] Icon has been created (if new sample) following the [Thumbnail Style Guide and templates](https://github.com/windows-toolkit/WindowsCommunityToolkit-design-assets)
- [ ] Tests for the changes have been added (for bug fixes / features) (if applicable)
- [ ] Header has been added to all new source files (run *build/UpdateHeaders.bat*)
- [X] Contains **NO** breaking changes

<!-- If this PR contains a breaking change, please describe the impact and migration path for existing applications below. 
     Please note that breaking changes are likely to be rejected within minor release cycles or held until major versions. -->


## Other information
  • Loading branch information
msftbot[bot] authored Dec 8, 2020
2 parents 9b75c9f + fe1182b commit e0a5d28
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 75 deletions.
148 changes: 74 additions & 74 deletions Microsoft.Toolkit.Uwp.UI.Animations/Expressions/ExpressionFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1253,80 +1253,80 @@ public ExpressionNodeInfo(OperationType nodeOperationKind, string operationStrin
/// </summary>
private static readonly Dictionary<ExpressionNodeType, ExpressionNodeInfo> _expressionNodeInfo = new Dictionary<ExpressionNodeType, ExpressionNodeInfo>
{
{ ExpressionNodeType.ConstantValue, new ExpressionNodeInfo(OperationType.Constant, null) },
{ ExpressionNodeType.ConstantParameter, new ExpressionNodeInfo(OperationType.Constant, null) },
{ ExpressionNodeType.CurrentValueProperty, new ExpressionNodeInfo(OperationType.Reference, null) },
{ ExpressionNodeType.Reference, new ExpressionNodeInfo(OperationType.Reference, null) },
{ ExpressionNodeType.ReferenceProperty, new ExpressionNodeInfo(OperationType.Reference, null) },
{ ExpressionNodeType.StartingValueProperty, new ExpressionNodeInfo(OperationType.Reference, null) },
{ ExpressionNodeType.TargetReference, new ExpressionNodeInfo(OperationType.Reference, null) },
{ ExpressionNodeType.Conditional, new ExpressionNodeInfo(OperationType.Conditional, null) },
{ ExpressionNodeType.Swizzle, new ExpressionNodeInfo(OperationType.Swizzle, null) },
{ ExpressionNodeType.Add, new ExpressionNodeInfo(OperationType.Operator, "+") },
{ ExpressionNodeType.And, new ExpressionNodeInfo(OperationType.Operator, "&&") },
{ ExpressionNodeType.Divide, new ExpressionNodeInfo(OperationType.Operator, "/") },
{ ExpressionNodeType.Equals, new ExpressionNodeInfo(OperationType.Operator, "==") },
{ ExpressionNodeType.GreaterThan, new ExpressionNodeInfo(OperationType.Operator, ">") },
{ ExpressionNodeType.GreaterThanEquals, new ExpressionNodeInfo(OperationType.Operator, ">=") },
{ ExpressionNodeType.LessThan, new ExpressionNodeInfo(OperationType.Operator, "<") },
{ ExpressionNodeType.LessThanEquals, new ExpressionNodeInfo(OperationType.Operator, "<=") },
{ ExpressionNodeType.Multiply, new ExpressionNodeInfo(OperationType.Operator, "*") },
{ ExpressionNodeType.Not, new ExpressionNodeInfo(OperationType.Operator, "!") },
{ ExpressionNodeType.NotEquals, new ExpressionNodeInfo(OperationType.Operator, "!=") },
{ ExpressionNodeType.Or, new ExpressionNodeInfo(OperationType.Operator, "||") },
{ ExpressionNodeType.Subtract, new ExpressionNodeInfo(OperationType.Operator, "-") },
{ ExpressionNodeType.Absolute, new ExpressionNodeInfo(OperationType.Function, "abs") },
{ ExpressionNodeType.Acos, new ExpressionNodeInfo(OperationType.Function, "acos") },
{ ExpressionNodeType.Asin, new ExpressionNodeInfo(OperationType.Function, "asin") },
{ ExpressionNodeType.Atan, new ExpressionNodeInfo(OperationType.Function, "atan") },
{ ExpressionNodeType.Cos, new ExpressionNodeInfo(OperationType.Function, "cos") },
{ ExpressionNodeType.Ceil, new ExpressionNodeInfo(OperationType.Function, "ceil") },
{ ExpressionNodeType.Clamp, new ExpressionNodeInfo(OperationType.Function, "clamp") },
{ ExpressionNodeType.ColorHsl, new ExpressionNodeInfo(OperationType.Function, "colorhsl") },
{ ExpressionNodeType.ColorRgb, new ExpressionNodeInfo(OperationType.Function, "colorrgb") },
{ ExpressionNodeType.ColorLerp, new ExpressionNodeInfo(OperationType.Function, "colorlerp") },
{ ExpressionNodeType.ColorLerpHsl, new ExpressionNodeInfo(OperationType.Function, "colorhsllerp") },
{ ExpressionNodeType.ColorLerpRgb, new ExpressionNodeInfo(OperationType.Function, "colorrgblerp") },
{ ExpressionNodeType.Concatenate, new ExpressionNodeInfo(OperationType.Function, "concatenate") },
{ ExpressionNodeType.Distance, new ExpressionNodeInfo(OperationType.Function, "distance") },
{ ExpressionNodeType.DistanceSquared, new ExpressionNodeInfo(OperationType.Function, "distancesquared") },
{ ExpressionNodeType.Floor, new ExpressionNodeInfo(OperationType.Function, "floor") },
{ ExpressionNodeType.Inverse, new ExpressionNodeInfo(OperationType.Function, "inverse") },
{ ExpressionNodeType.Length, new ExpressionNodeInfo(OperationType.Function, "length") },
{ ExpressionNodeType.LengthSquared, new ExpressionNodeInfo(OperationType.Function, "lengthsquared") },
{ ExpressionNodeType.Lerp, new ExpressionNodeInfo(OperationType.Function, "lerp") },
{ ExpressionNodeType.Ln, new ExpressionNodeInfo(OperationType.Function, "ln") },
{ ExpressionNodeType.Log10, new ExpressionNodeInfo(OperationType.Function, "log10") },
{ ExpressionNodeType.Max, new ExpressionNodeInfo(OperationType.Function, "max") },
{ ExpressionNodeType.Matrix3x2FromRotation, new ExpressionNodeInfo(OperationType.Function, "matrix3x2.createrotation") },
{ ExpressionNodeType.Matrix3x2FromScale, new ExpressionNodeInfo(OperationType.Function, "matrix3x2.createscale") },
{ ExpressionNodeType.Matrix3x2FromSkew, new ExpressionNodeInfo(OperationType.Function, "matrix3x2.createskew") },
{ ExpressionNodeType.Matrix3x2FromTranslation, new ExpressionNodeInfo(OperationType.Function, "matrix3x2.createtranslation") },
{ ExpressionNodeType.Matrix3x2, new ExpressionNodeInfo(OperationType.Function, "matrix3x2") },
{ ExpressionNodeType.Matrix4x4FromAxisAngle, new ExpressionNodeInfo(OperationType.Function, "matrix4x4.createfromaxisangle") },
{ ExpressionNodeType.Matrix4x4FromScale, new ExpressionNodeInfo(OperationType.Function, "matrix4x4.createscale") },
{ ExpressionNodeType.Matrix4x4FromTranslation, new ExpressionNodeInfo(OperationType.Function, "matrix4x4.createtranslation") },
{ ExpressionNodeType.Matrix4x4, new ExpressionNodeInfo(OperationType.Function, "matrix4x4") },
{ ExpressionNodeType.Min, new ExpressionNodeInfo(OperationType.Function, "min") },
{ ExpressionNodeType.Modulus, new ExpressionNodeInfo(OperationType.Function, "mod") },
{ ExpressionNodeType.Negate, new ExpressionNodeInfo(OperationType.Function, "-") },
{ ExpressionNodeType.Normalize, new ExpressionNodeInfo(OperationType.Function, "normalize") },
{ ExpressionNodeType.Pow, new ExpressionNodeInfo(OperationType.Function, "pow") },
{ ExpressionNodeType.QuaternionFromAxisAngle, new ExpressionNodeInfo(OperationType.Function, "quaternion.createfromaxisangle") },
{ ExpressionNodeType.Quaternion, new ExpressionNodeInfo(OperationType.Function, "quaternion") },
{ ExpressionNodeType.Round, new ExpressionNodeInfo(OperationType.Function, "round") },
{ ExpressionNodeType.Scale, new ExpressionNodeInfo(OperationType.Function, "scale") },
{ ExpressionNodeType.Sin, new ExpressionNodeInfo(OperationType.Function, "sin") },
{ ExpressionNodeType.Slerp, new ExpressionNodeInfo(OperationType.Function, "slerp") },
{ ExpressionNodeType.Sqrt, new ExpressionNodeInfo(OperationType.Function, "sqrt") },
{ ExpressionNodeType.Square, new ExpressionNodeInfo(OperationType.Function, "square") },
{ ExpressionNodeType.Tan, new ExpressionNodeInfo(OperationType.Function, "tan") },
{ ExpressionNodeType.ToDegrees, new ExpressionNodeInfo(OperationType.Function, "todegrees") },
{ ExpressionNodeType.ToRadians, new ExpressionNodeInfo(OperationType.Function, "toradians") },
{ ExpressionNodeType.Transform, new ExpressionNodeInfo(OperationType.Function, "transform") },
{ ExpressionNodeType.Vector2, new ExpressionNodeInfo(OperationType.Function, "vector2") },
{ ExpressionNodeType.Vector3, new ExpressionNodeInfo(OperationType.Function, "vector3") },
{ ExpressionNodeType.Vector4, new ExpressionNodeInfo(OperationType.Function, "vector4") },
{ ExpressionNodeType.ConstantValue, new ExpressionNodeInfo(OperationType.Constant, null) },
{ ExpressionNodeType.ConstantParameter, new ExpressionNodeInfo(OperationType.Constant, null) },
{ ExpressionNodeType.CurrentValueProperty, new ExpressionNodeInfo(OperationType.Reference, null) },
{ ExpressionNodeType.Reference, new ExpressionNodeInfo(OperationType.Reference, null) },
{ ExpressionNodeType.ReferenceProperty, new ExpressionNodeInfo(OperationType.Reference, null) },
{ ExpressionNodeType.StartingValueProperty, new ExpressionNodeInfo(OperationType.Reference, null) },
{ ExpressionNodeType.TargetReference, new ExpressionNodeInfo(OperationType.Reference, null) },
{ ExpressionNodeType.Conditional, new ExpressionNodeInfo(OperationType.Conditional, null) },
{ ExpressionNodeType.Swizzle, new ExpressionNodeInfo(OperationType.Swizzle, null) },
{ ExpressionNodeType.Add, new ExpressionNodeInfo(OperationType.Operator, "+") },
{ ExpressionNodeType.And, new ExpressionNodeInfo(OperationType.Operator, "&&") },
{ ExpressionNodeType.Divide, new ExpressionNodeInfo(OperationType.Operator, "/") },
{ ExpressionNodeType.Equals, new ExpressionNodeInfo(OperationType.Operator, "==") },
{ ExpressionNodeType.GreaterThan, new ExpressionNodeInfo(OperationType.Operator, ">") },
{ ExpressionNodeType.GreaterThanEquals, new ExpressionNodeInfo(OperationType.Operator, ">=") },
{ ExpressionNodeType.LessThan, new ExpressionNodeInfo(OperationType.Operator, "<") },
{ ExpressionNodeType.LessThanEquals, new ExpressionNodeInfo(OperationType.Operator, "<=") },
{ ExpressionNodeType.Multiply, new ExpressionNodeInfo(OperationType.Operator, "*") },
{ ExpressionNodeType.Not, new ExpressionNodeInfo(OperationType.UnaryOperator, "!") },
{ ExpressionNodeType.NotEquals, new ExpressionNodeInfo(OperationType.Operator, "!=") },
{ ExpressionNodeType.Or, new ExpressionNodeInfo(OperationType.Operator, "||") },
{ ExpressionNodeType.Subtract, new ExpressionNodeInfo(OperationType.Operator, "-") },
{ ExpressionNodeType.Absolute, new ExpressionNodeInfo(OperationType.Function, "abs") },
{ ExpressionNodeType.Acos, new ExpressionNodeInfo(OperationType.Function, "acos") },
{ ExpressionNodeType.Asin, new ExpressionNodeInfo(OperationType.Function, "asin") },
{ ExpressionNodeType.Atan, new ExpressionNodeInfo(OperationType.Function, "atan") },
{ ExpressionNodeType.Cos, new ExpressionNodeInfo(OperationType.Function, "cos") },
{ ExpressionNodeType.Ceil, new ExpressionNodeInfo(OperationType.Function, "ceil") },
{ ExpressionNodeType.Clamp, new ExpressionNodeInfo(OperationType.Function, "clamp") },
{ ExpressionNodeType.ColorHsl, new ExpressionNodeInfo(OperationType.Function, "colorhsl") },
{ ExpressionNodeType.ColorRgb, new ExpressionNodeInfo(OperationType.Function, "colorrgb") },
{ ExpressionNodeType.ColorLerp, new ExpressionNodeInfo(OperationType.Function, "colorlerp") },
{ ExpressionNodeType.ColorLerpHsl, new ExpressionNodeInfo(OperationType.Function, "colorhsllerp") },
{ ExpressionNodeType.ColorLerpRgb, new ExpressionNodeInfo(OperationType.Function, "colorrgblerp") },
{ ExpressionNodeType.Concatenate, new ExpressionNodeInfo(OperationType.Function, "concatenate") },
{ ExpressionNodeType.Distance, new ExpressionNodeInfo(OperationType.Function, "distance") },
{ ExpressionNodeType.DistanceSquared, new ExpressionNodeInfo(OperationType.Function, "distancesquared") },
{ ExpressionNodeType.Floor, new ExpressionNodeInfo(OperationType.Function, "floor") },
{ ExpressionNodeType.Inverse, new ExpressionNodeInfo(OperationType.Function, "inverse") },
{ ExpressionNodeType.Length, new ExpressionNodeInfo(OperationType.Function, "length") },
{ ExpressionNodeType.LengthSquared, new ExpressionNodeInfo(OperationType.Function, "lengthsquared") },
{ ExpressionNodeType.Lerp, new ExpressionNodeInfo(OperationType.Function, "lerp") },
{ ExpressionNodeType.Ln, new ExpressionNodeInfo(OperationType.Function, "ln") },
{ ExpressionNodeType.Log10, new ExpressionNodeInfo(OperationType.Function, "log10") },
{ ExpressionNodeType.Max, new ExpressionNodeInfo(OperationType.Function, "max") },
{ ExpressionNodeType.Matrix3x2FromRotation, new ExpressionNodeInfo(OperationType.Function, "matrix3x2.createrotation") },
{ ExpressionNodeType.Matrix3x2FromScale, new ExpressionNodeInfo(OperationType.Function, "matrix3x2.createscale") },
{ ExpressionNodeType.Matrix3x2FromSkew, new ExpressionNodeInfo(OperationType.Function, "matrix3x2.createskew") },
{ ExpressionNodeType.Matrix3x2FromTranslation, new ExpressionNodeInfo(OperationType.Function, "matrix3x2.createtranslation") },
{ ExpressionNodeType.Matrix3x2, new ExpressionNodeInfo(OperationType.Function, "matrix3x2") },
{ ExpressionNodeType.Matrix4x4FromAxisAngle, new ExpressionNodeInfo(OperationType.Function, "matrix4x4.createfromaxisangle") },
{ ExpressionNodeType.Matrix4x4FromScale, new ExpressionNodeInfo(OperationType.Function, "matrix4x4.createscale") },
{ ExpressionNodeType.Matrix4x4FromTranslation, new ExpressionNodeInfo(OperationType.Function, "matrix4x4.createtranslation") },
{ ExpressionNodeType.Matrix4x4, new ExpressionNodeInfo(OperationType.Function, "matrix4x4") },
{ ExpressionNodeType.Min, new ExpressionNodeInfo(OperationType.Function, "min") },
{ ExpressionNodeType.Modulus, new ExpressionNodeInfo(OperationType.Function, "mod") },
{ ExpressionNodeType.Negate, new ExpressionNodeInfo(OperationType.Function, "-") },
{ ExpressionNodeType.Normalize, new ExpressionNodeInfo(OperationType.Function, "normalize") },
{ ExpressionNodeType.Pow, new ExpressionNodeInfo(OperationType.Function, "pow") },
{ ExpressionNodeType.QuaternionFromAxisAngle, new ExpressionNodeInfo(OperationType.Function, "quaternion.createfromaxisangle") },
{ ExpressionNodeType.Quaternion, new ExpressionNodeInfo(OperationType.Function, "quaternion") },
{ ExpressionNodeType.Round, new ExpressionNodeInfo(OperationType.Function, "round") },
{ ExpressionNodeType.Scale, new ExpressionNodeInfo(OperationType.Function, "scale") },
{ ExpressionNodeType.Sin, new ExpressionNodeInfo(OperationType.Function, "sin") },
{ ExpressionNodeType.Slerp, new ExpressionNodeInfo(OperationType.Function, "slerp") },
{ ExpressionNodeType.Sqrt, new ExpressionNodeInfo(OperationType.Function, "sqrt") },
{ ExpressionNodeType.Square, new ExpressionNodeInfo(OperationType.Function, "square") },
{ ExpressionNodeType.Tan, new ExpressionNodeInfo(OperationType.Function, "tan") },
{ ExpressionNodeType.ToDegrees, new ExpressionNodeInfo(OperationType.Function, "todegrees") },
{ ExpressionNodeType.ToRadians, new ExpressionNodeInfo(OperationType.Function, "toradians") },
{ ExpressionNodeType.Transform, new ExpressionNodeInfo(OperationType.Function, "transform") },
{ ExpressionNodeType.Vector2, new ExpressionNodeInfo(OperationType.Function, "vector2") },
{ ExpressionNodeType.Vector3, new ExpressionNodeInfo(OperationType.Function, "vector3") },
{ ExpressionNodeType.Vector4, new ExpressionNodeInfo(OperationType.Function, "vector4") },
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,15 @@ private string ToExpressionStringInternal()
ret = $"({Children[0].ToExpressionStringInternal()} {GetOperationString()} {Children[1].ToExpressionStringInternal()})";
break;

case OperationType.UnaryOperator:
if (Children.Count != 1)
{
throw new Exception("Can't have an unary operator that doesn't have exactly one params");
}

ret = $"( {GetOperationString()} {Children[0].ToExpressionStringInternal()} )";
break;

case OperationType.Constant:
if (Children.Count == 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ internal enum OperationType
Function,

/// <summary>
/// The operator
/// The operator (takes two operands)
/// </summary>
Operator,

/// <summary>
/// The operator that only takes one operand
/// </summary>
UnaryOperator,

/// <summary>
/// The constant
/// </summary>
Expand Down

0 comments on commit e0a5d28

Please sign in to comment.