forked from BAndysc/WoWDatabaseEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimationHelper.cs
75 lines (64 loc) · 2.7 KB
/
AnimationHelper.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
#region License
// From the CodeProject article by Ashley Davis:
// http://www.codeproject.com/Articles/85603/A-WPF-custom-control-for-zooming-and-panning
// Licensed under the Code Project Open License (CPOL):
// http://www.codeproject.com/info/cpol10.aspx
#endregion
using System;
using System.Windows;
using System.Windows.Media.Animation;
namespace GeminiGraphEditor
{
/// <summary>
/// A helper class to simplify animation.
/// </summary>
internal static class AnimationHelper
{
/// <summary>
/// Starts an animation to a particular value on the specified dependency property.
/// </summary>
public static void StartAnimation(UIElement animatableElement,
DependencyProperty dependencyProperty,
double toValue,
double animationDurationSeconds)
{
StartAnimation(animatableElement, dependencyProperty, toValue, animationDurationSeconds, null);
}
/// <summary>
/// Starts an animation to a particular value on the specified dependency property.
/// You can pass in an event handler to call when the animation has completed.
/// </summary>
public static void StartAnimation(UIElement animatableElement,
DependencyProperty dependencyProperty,
double toValue,
double animationDurationSeconds,
EventHandler completedEvent)
{
var fromValue = (double) animatableElement.GetValue(dependencyProperty);
DoubleAnimation animation = new();
animation.From = fromValue;
animation.To = toValue;
animation.Duration = TimeSpan.FromSeconds(animationDurationSeconds);
animation.Completed += delegate(object sender, EventArgs e)
{
//
// When the animation has completed bake final value of the animation
// into the property.
//
animatableElement.SetValue(dependencyProperty, animatableElement.GetValue(dependencyProperty));
CancelAnimation(animatableElement, dependencyProperty);
if (completedEvent != null)
completedEvent(sender, e);
};
animation.Freeze();
animatableElement.BeginAnimation(dependencyProperty, animation);
}
/// <summary>
/// Cancel any animations that are running on the specified dependency property.
/// </summary>
public static void CancelAnimation(UIElement animatableElement, DependencyProperty dependencyProperty)
{
animatableElement.BeginAnimation(dependencyProperty, null);
}
}
}