-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimationHandler.cs
86 lines (72 loc) · 4.12 KB
/
AnimationHandler.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
80
81
82
83
84
85
86
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
namespace On_Screen_Keyboard
{
public class AnimationHandler : Page
{
private const int shrinkModifier = 10;
private const int animationDuration = 50;
private Button button;
private double previousWidth;
private double previousHeight;
private Thickness previousMargin;
public AnimationHandler(Button button)
{
this.button = button;
previousWidth = button.Width;
previousHeight = button.Height;
previousMargin = button.Margin;
NameScope.SetNameScope(this, new NameScope());
RegisterName(button.Name, button);
}
public void ButtonMouseDownAnimation(object sender, RoutedEventArgs e)
{
DoubleAnimation widthShrinkAnimation = new DoubleAnimation(button.Width - shrinkModifier, TimeSpan.FromMilliseconds(animationDuration));
Storyboard.SetTargetName(widthShrinkAnimation, button.Name);
Storyboard.SetTargetProperty(widthShrinkAnimation, new PropertyPath(Button.WidthProperty));
Thickness margin = new Thickness();
margin.Left = shrinkModifier / 2 + button.Margin.Left;
margin.Top = shrinkModifier / 2 + button.Margin.Top;
margin.Right = shrinkModifier / 2 + button.Margin.Right;
margin.Bottom = shrinkModifier / 2 + button.Margin.Bottom;
ThicknessAnimation marginExpandAnimation = new ThicknessAnimation(margin, TimeSpan.FromMilliseconds(animationDuration));
Storyboard.SetTargetName(marginExpandAnimation, button.Name);
Storyboard.SetTargetProperty(marginExpandAnimation, new PropertyPath(Button.MarginProperty));
Storyboard storyboard = new Storyboard();
storyboard.Children.Add(widthShrinkAnimation);
storyboard.Children.Add(marginExpandAnimation);
if (button.Name.Equals("Btn_ENTER")) //the height of Btn_ENTER is not Auto so you have to set it manually
{
DoubleAnimation heightShrinkAnimation = new DoubleAnimation(button.Height - shrinkModifier, TimeSpan.FromMilliseconds(animationDuration));
Storyboard.SetTargetName(heightShrinkAnimation, button.Name);
Storyboard.SetTargetProperty(heightShrinkAnimation, new PropertyPath(Button.HeightProperty));
storyboard.Children.Add(heightShrinkAnimation);
}
storyboard.Begin(this);
}
public void ButtonMouseUpAnimation(object sender, RoutedEventArgs e)
{
DoubleAnimation widthExpandAnimation = new DoubleAnimation(previousWidth, TimeSpan.FromMilliseconds(animationDuration));
Storyboard.SetTargetName(widthExpandAnimation, button.Name);
Storyboard.SetTargetProperty(widthExpandAnimation, new PropertyPath(Button.WidthProperty));
ThicknessAnimation marginShrinkAnimation = new ThicknessAnimation(previousMargin, TimeSpan.FromMilliseconds(animationDuration));
Storyboard.SetTargetName(marginShrinkAnimation, button.Name);
Storyboard.SetTargetProperty(marginShrinkAnimation, new PropertyPath(Button.MarginProperty));
Storyboard storyboard = new Storyboard();
storyboard.Children.Add(widthExpandAnimation);
storyboard.Children.Add(marginShrinkAnimation);
if (button.Name.Equals("Btn_ENTER")) //the height of Btn_ENTER is not Auto so you have to reset it manually
{
DoubleAnimation heightShrinkAnimation = new DoubleAnimation(previousHeight, TimeSpan.FromMilliseconds(animationDuration));
Storyboard.SetTargetName(heightShrinkAnimation, button.Name);
Storyboard.SetTargetProperty(heightShrinkAnimation, new PropertyPath(Button.HeightProperty));
storyboard.Children.Add(heightShrinkAnimation);
}
storyboard.Begin(this);
}
}
}