Skip to content

Commit 4a59111

Browse files
author
msftbot[bot]
authored
TileControl: Remove Animations.Expressions usage (#3633)
<!-- 🚨 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! 🎉 --> <!-- Add a brief overview here of the feature/bug & fix. --> Sept towards #3578. Remove the `TileControl`'s usage of the `Animations.Expressions` ns and thus the animations package. ## 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? <!-- Please describe the current behavior that you are modifying, or link to a relevant issue. --> `TileControl`'s uses `Animations.Expressions` to build expression animation. ## What is the new behavior? <!-- Describe how was this issue resolved or changed? --> `TileControl`'s uses strings to build expression animation. ## PR Checklist Please check if your PR fulfills the following requirements: - [ ] 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*) - [ ] 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
2 parents 97937ae + 57c5486 commit 4a59111

File tree

1 file changed

+47
-56
lines changed

1 file changed

+47
-56
lines changed

Microsoft.Toolkit.Uwp.UI.Controls/TileControl/TileControl.cs

Lines changed: 47 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using System.Numerics;
99
using System.Threading;
1010
using System.Threading.Tasks;
11-
using Microsoft.Toolkit.Uwp.UI.Animations.Expressions;
1211
using Microsoft.Toolkit.Uwp.UI.Extensions;
1312
using Windows.Foundation;
1413
using Windows.UI.Composition;
@@ -439,11 +438,16 @@ private async Task CreateModuloExpression(ScrollViewer scrollViewer = null)
439438
/// <param name="scrollOrientation">The ScrollOrientation</param>
440439
private void CreateModuloExpression(ScrollViewer scrollViewer, double imageWidth, double imageHeight, ScrollOrientation scrollOrientation)
441440
{
442-
const string offsetXParam = "offsetX";
443-
const string offsetYParam = "offsetY";
444-
const string imageWidthParam = "imageWidth";
445-
const string imageHeightParam = "imageHeight";
446-
const string speedParam = "speed";
441+
const string propSetParam = "p";
442+
const string offsetXParam = nameof(OffsetX);
443+
const string qualifiedOffsetXParam = propSetParam + "." + offsetXParam;
444+
const string offsetYParam = nameof(OffsetY);
445+
const string qualifiedOffsetYParam = propSetParam + "." + offsetYParam;
446+
const string imageWidthParam = nameof(imageWidth);
447+
const string qualifiedImageWidthParam = propSetParam + "." + imageWidthParam;
448+
const string imageHeightParam = nameof(imageHeight);
449+
const string qualifiedImageHeightParam = propSetParam + "." + imageHeightParam;
450+
const string speedParam = nameof(ParallaxSpeedRatio);
447451

448452
if (_containerVisual == null)
449453
{
@@ -453,10 +457,8 @@ private void CreateModuloExpression(ScrollViewer scrollViewer, double imageWidth
453457
var compositor = _containerVisual.Compositor;
454458

455459
// Setup the expression
456-
ExpressionNode expressionX = null;
457-
ExpressionNode expressionY = null;
458-
ExpressionNode expressionXVal;
459-
ExpressionNode expressionYVal;
460+
var expressionX = compositor.CreateExpressionAnimation();
461+
var expressionY = compositor.CreateExpressionAnimation();
460462

461463
var propertySetModulo = compositor.CreatePropertySet();
462464
propertySetModulo.InsertScalar(imageWidthParam, (float)imageWidth);
@@ -465,78 +467,67 @@ private void CreateModuloExpression(ScrollViewer scrollViewer, double imageWidth
465467
propertySetModulo.InsertScalar(offsetYParam, (float)OffsetY);
466468
propertySetModulo.InsertScalar(speedParam, (float)ParallaxSpeedRatio);
467469

468-
var propertySetNodeModulo = propertySetModulo.GetReference();
469-
470-
var imageHeightNode = propertySetNodeModulo.GetScalarProperty(imageHeightParam);
471-
var imageWidthNode = propertySetNodeModulo.GetScalarProperty(imageWidthParam);
470+
expressionX.SetReferenceParameter(propSetParam, propertySetModulo);
471+
expressionY.SetReferenceParameter(propSetParam, propertySetModulo);
472+
473+
string GenerateFormula(string common, string dimension)
474+
=> string.Format(
475+
"{0} == 0 " +
476+
"? 0 " +
477+
": {0} < 0 " +
478+
"? -(Abs({0} - (Ceil({0} / {1}) * {1})) % {1}) " +
479+
": -({1} - ({0} % {1}))",
480+
common,
481+
dimension);
482+
483+
string expressionXVal;
484+
string expressionYVal;
472485
if (scrollViewer == null)
473486
{
474-
var offsetXNode = ExpressionFunctions.Ceil(propertySetNodeModulo.GetScalarProperty(offsetXParam));
475-
var offsetYNode = ExpressionFunctions.Ceil(propertySetNodeModulo.GetScalarProperty(offsetYParam));
476-
477487
// expressions are created to simulate a positive and negative modulo with the size of the image and the offset
478-
expressionXVal = ExpressionFunctions.Conditional(
479-
offsetXNode == 0,
480-
0,
481-
ExpressionFunctions.Conditional(
482-
offsetXNode < 0,
483-
-(ExpressionFunctions.Abs(offsetXNode - (ExpressionFunctions.Ceil(offsetXNode / imageWidthNode) * imageWidthNode)) % imageWidthNode),
484-
-(imageWidthNode - (offsetXNode % imageWidthNode))));
485-
486-
expressionYVal = ExpressionFunctions.Conditional(
487-
offsetYNode == 0,
488-
0,
489-
ExpressionFunctions.Conditional(
490-
offsetYNode < 0,
491-
-(ExpressionFunctions.Abs(offsetYNode - (ExpressionFunctions.Ceil(offsetYNode / imageHeightNode) * imageHeightNode)) % imageHeightNode),
492-
-(imageHeightNode - (offsetYNode % imageHeightNode))));
488+
expressionXVal = GenerateFormula("Ceil(" + qualifiedOffsetXParam + ")", qualifiedImageHeightParam);
489+
490+
expressionYVal = GenerateFormula("Ceil(" + qualifiedOffsetYParam + ")", qualifiedImageWidthParam);
493491
}
494492
else
495493
{
496494
// expressions are created to simulate a positive and negative modulo with the size of the image and the offset and the ScrollViewer offset (Translation)
497495
var scrollProperties = ElementCompositionPreview.GetScrollViewerManipulationPropertySet(scrollViewer);
498-
var scrollPropSet = scrollProperties.GetSpecializedReference<ManipulationPropertySetReferenceNode>();
499-
500-
var speed = propertySetNodeModulo.GetScalarProperty(speedParam);
501-
var xCommon = ExpressionFunctions.Ceil((scrollPropSet.Translation.X * speed) + propertySetNodeModulo.GetScalarProperty(offsetXParam));
502-
expressionXVal = ExpressionFunctions.Conditional(
503-
xCommon == 0,
504-
0,
505-
ExpressionFunctions.Conditional(
506-
xCommon < 0,
507-
-(ExpressionFunctions.Abs(xCommon - (ExpressionFunctions.Ceil(xCommon / imageWidthNode) * imageWidthNode)) % imageWidthNode),
508-
-(imageWidthNode - (xCommon % imageWidthNode))));
509-
510-
var yCommon = ExpressionFunctions.Ceil((scrollPropSet.Translation.Y * speed) + propertySetNodeModulo.GetScalarProperty(offsetYParam));
511-
expressionYVal = ExpressionFunctions.Conditional(
512-
yCommon == 0,
513-
0,
514-
ExpressionFunctions.Conditional(
515-
yCommon < 0,
516-
-(ExpressionFunctions.Abs(yCommon - (ExpressionFunctions.Ceil(yCommon / imageHeightNode) * imageHeightNode)) % imageHeightNode),
517-
-(imageHeightNode - (yCommon % imageHeightNode))));
496+
const string scrollParam = "s";
497+
const string translationParam = scrollParam + "." + nameof(scrollViewer.Translation);
498+
const string qualifiedSpeedParam = propSetParam + "." + speedParam;
499+
500+
expressionX.SetReferenceParameter(scrollParam, scrollProperties);
501+
expressionY.SetReferenceParameter(scrollParam, scrollProperties);
502+
503+
string GenerateParallaxFormula(string scrollTranslation, string speed, string offset, string dimension)
504+
=> GenerateFormula(string.Format("Ceil(({0} * {1}) + {2})", scrollTranslation, speed, offset), dimension);
505+
506+
expressionXVal = GenerateParallaxFormula(translationParam + "." + nameof(scrollViewer.Translation.X), qualifiedSpeedParam, qualifiedOffsetXParam, qualifiedImageWidthParam);
507+
508+
expressionYVal = GenerateParallaxFormula(translationParam + "." + nameof(scrollViewer.Translation.Y), qualifiedSpeedParam, qualifiedOffsetYParam, qualifiedImageHeightParam);
518509
}
519510

520511
if (scrollOrientation == ScrollOrientation.Horizontal || scrollOrientation == ScrollOrientation.Both)
521512
{
522-
expressionX = expressionXVal;
513+
expressionX.Expression = expressionXVal;
523514

524515
if (scrollOrientation == ScrollOrientation.Horizontal)
525516
{
526517
// In horizontal mode we never move the offset y
527-
expressionY = (ScalarNode)0.0f;
518+
expressionY.Expression = "0";
528519
_containerVisual.Offset = new Vector3((float)OffsetY, 0, 0);
529520
}
530521
}
531522

532523
if (scrollOrientation == ScrollOrientation.Vertical || scrollOrientation == ScrollOrientation.Both)
533524
{
534-
expressionY = expressionYVal;
525+
expressionY.Expression = expressionYVal;
535526

536527
if (scrollOrientation == ScrollOrientation.Vertical)
537528
{
538529
// In vertical mode we never move the offset x
539-
expressionX = (ScalarNode)0.0f;
530+
expressionX.Expression = "0";
540531
_containerVisual.Offset = new Vector3(0, (float)OffsetX, 0);
541532
}
542533
}

0 commit comments

Comments
 (0)