/
CircleAnimation.cs
79 lines (69 loc) · 2.78 KB
/
CircleAnimation.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// // Copyright (c) Microsoft. All rights reserved.
// // Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Windows;
using System.Windows.Media.Animation;
namespace CustomAnimation
{
/// <summary>
/// CircleAnimation: calculates polar coordinates as a function of time.
/// Use two of these (XDirection and YDirection) to move an element in an elliptical manner
/// </summary>
public class CircleAnimation : DoubleAnimationBase
{
public enum DirectionEnum
{
XDirection,
YDirection
}
public static readonly DependencyProperty DirectionProperty =
DependencyProperty.Register("Direction", typeof (DirectionEnum), typeof (CircleAnimation),
new PropertyMetadata(DirectionEnum.XDirection));
public static readonly DependencyProperty RadiusProperty =
DependencyProperty.Register("Radius", typeof (double), typeof (CircleAnimation),
new PropertyMetadata((double) 10));
/// <summary>
/// distance from origin to polar coordinate
/// </summary>
public double Radius
{
get { return (double) GetValue(RadiusProperty); }
set
{
if (value > 0.0)
{
SetValue(RadiusProperty, value);
}
else
{
throw new ArgumentException("a radius of " + value + " is not allowed!");
}
}
}
/// <summary>
/// are we measuring in the X or Y direction?
/// </summary>
public DirectionEnum Direction
{
get { return (DirectionEnum) GetValue(DirectionProperty); }
set { SetValue(DirectionProperty, value); }
}
protected override double GetCurrentValueCore(double defaultOriginValue, double defaultDestinationValue,
AnimationClock clock)
{
var time = clock.CurrentProgress.Value;
// math magic: calculate new coordinates using polar coordinate equations. This requires two
// animations to be wired up in order to move in a circle, since we don't make any assumptions
// about what we're animating (e.g. a TranslateTransform).
var returnValue = Direction == DirectionEnum.XDirection
? Math.Cos(2*Math.PI*time)
: Math.Sin(2*Math.PI*time);
// Need to add the defaultOriginValue so that composition works.
return returnValue*Radius + defaultOriginValue;
}
protected override Freezable CreateInstanceCore()
{
return new CircleAnimation();
}
}
}