Skip to content

Commit

Permalink
Update orbit view to use expression builder
Browse files Browse the repository at this point in the history
  • Loading branch information
skendrot committed Oct 27, 2017
1 parent afce8cf commit 8fe880f
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions Microsoft.Toolkit.Uwp.UI.Controls/OrbitView/OrbitView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE.
// ******************************************************************

using Microsoft.Toolkit.Uwp.UI.Animations.Expressions;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -630,48 +631,41 @@ private Line CreateAnchor(UIElement element, double x, double y)
var anchorVisual = ElementCompositionPreview.GetElementVisual(anchor);
var elementVisual = ElementCompositionPreview.GetElementVisual(element);
var centerVisual = ElementCompositionPreview.GetElementVisual(_centerContent);
var elementNode = elementVisual.GetReference();
var centerNode = centerVisual.GetReference();

string expression = string.Empty;
var elementY = "(elementVisual.Offset.Y + elementVisual.Size.Y / 2)";
var centerY = "(centerVisual.Offset.Y + centerVisual.Size.Y / 2)";
var elementX = "(elementVisual.Offset.X + elementVisual.Size.X / 2)";
var centerX = "(centerVisual.Offset.X + centerVisual.Size.X / 2)";
ScalarNode expression = null;
var elementY = elementNode.Offset.Y + (elementNode.Size.Y / 2);
var centerY = centerNode.Offset.Y + (centerNode.Size.Y / 2);
var elementX = elementNode.Offset.X + (elementNode.Size.X / 2);
var centerX = centerNode.Offset.X + (centerNode.Size.X / 2);

var startingAngle = Math.Atan2(y, x);

if (startingAngle > Math.PI / 4 && startingAngle < 3 * Math.PI / 4)
{
expression = $"Atan((-1 * ({elementX} - {centerX})) / ( {elementY} - {centerY})) - PI / 2";
expression = ExpressionFunctions.ATan((-1 * (elementX - centerX)) / (elementY - centerY)) - ((float)Math.PI / 2.0f);
}
else if (startingAngle >= 3 * Math.PI / 4 || startingAngle < -3 * Math.PI / 4)
{
expression = $"Atan(({elementY} - {centerY}) / ({elementX} - {centerX})) + PI";
expression = ExpressionFunctions.ATan((elementY - centerY) / (elementX - centerX)) + (float)Math.PI;
}
else if (startingAngle >= -3 * Math.PI / 4 && startingAngle < Math.PI / -4)
{
expression = $"Atan(({elementX} - {centerX}) / (-1 * ({elementY} - {centerY}))) + PI / 2";
expression = ExpressionFunctions.ATan((elementX - centerX) / (-1 * (elementY - centerY))) + ((float)Math.PI / 2.0f);
}
else
{
expression = $"Atan(({elementY} - {centerY}) / ({elementX} - {centerX}))";
expression = ExpressionFunctions.ATan((elementY - centerY) / (elementX - centerX));
}

anchorVisual.CenterPoint = new Vector3(0);
var rotationExpression = _compositor.CreateExpressionAnimation();
rotationExpression.Expression = expression;
rotationExpression.SetReferenceParameter("centerVisual", centerVisual);
rotationExpression.SetReferenceParameter("elementVisual", elementVisual);
anchorVisual.StartAnimation(nameof(anchorVisual.RotationAngle), rotationExpression);

var offsetExpression = _compositor.CreateExpressionAnimation();
offsetExpression.Expression = "Vector3(centerVisual.Offset.X + centerVisual.Size.X / 2, centerVisual.Offset.Y + centerVisual.Size.Y / 2, 0)";
offsetExpression.SetReferenceParameter("centerVisual", centerVisual);
anchorVisual.StartAnimation(nameof(anchorVisual.RotationAngle), expression);

var offsetExpression = ExpressionFunctions.Vector3(centerNode.Offset.X + (centerNode.Size.X / 2), centerNode.Offset.Y + (centerNode.Size.Y / 2), 0);
anchorVisual.StartAnimation(nameof(anchorVisual.Offset), offsetExpression);

var scaleExpression = _compositor.CreateExpressionAnimation();
scaleExpression.Expression = $"Vector3(Pow(Pow({elementX} - {centerX}, 2) + Pow({elementY} - {centerY}, 2), 0.5)/100, 1, 1)";
scaleExpression.SetReferenceParameter("centerVisual", centerVisual);
scaleExpression.SetReferenceParameter("elementVisual", elementVisual);
var scaleExpression = ExpressionFunctions.Vector3(ExpressionFunctions.Pow(ExpressionFunctions.Pow(elementX - centerX, 2) + ExpressionFunctions.Pow(elementY - centerY, 2), 0.5f) / 100, 1, 1);
anchorVisual.StartAnimation(nameof(anchorVisual.Scale), scaleExpression);

return anchor;
Expand Down

0 comments on commit 8fe880f

Please sign in to comment.