Skip to content

Commit f7053aa

Browse files
Added Button Animations
1 parent e45d6a6 commit f7053aa

File tree

6 files changed

+106
-14
lines changed

6 files changed

+106
-14
lines changed

AnimationHandler.cs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+

2+
using System;
3+
using System.Diagnostics;
4+
using System.Windows;
5+
using System.Windows.Controls;
6+
using System.Windows.Media.Animation;
7+
8+
namespace On_Screen_Keyboard
9+
{
10+
public class AnimationHandler : Page
11+
{
12+
private const int shrinkModifier = 10;
13+
private const int animationDuration = 50;
14+
private Button button;
15+
private double previousWidth;
16+
private double previousHeight;
17+
private Thickness previousMargin;
18+
public AnimationHandler(Button button)
19+
{
20+
this.button = button;
21+
previousWidth = button.Width;
22+
previousHeight = button.Height;
23+
previousMargin = button.Margin;
24+
25+
NameScope.SetNameScope(this, new NameScope());
26+
RegisterName(button.Name, button);
27+
}
28+
29+
public void ButtonMouseDownAnimation(object sender, RoutedEventArgs e)
30+
{
31+
DoubleAnimation widthShrinkAnimation = new DoubleAnimation(button.Width - shrinkModifier, TimeSpan.FromMilliseconds(animationDuration));
32+
Storyboard.SetTargetName(widthShrinkAnimation, button.Name);
33+
Storyboard.SetTargetProperty(widthShrinkAnimation, new PropertyPath(Button.WidthProperty));
34+
35+
Thickness margin = new Thickness();
36+
margin.Left = shrinkModifier / 2 + button.Margin.Left;
37+
margin.Top = shrinkModifier / 2 + button.Margin.Top;
38+
margin.Right = shrinkModifier / 2 + button.Margin.Right;
39+
margin.Bottom = shrinkModifier / 2 + button.Margin.Bottom;
40+
41+
ThicknessAnimation marginExpandAnimation = new ThicknessAnimation(margin, TimeSpan.FromMilliseconds(animationDuration));
42+
Storyboard.SetTargetName(marginExpandAnimation, button.Name);
43+
Storyboard.SetTargetProperty(marginExpandAnimation, new PropertyPath(Button.MarginProperty));
44+
45+
Storyboard storyboard = new Storyboard();
46+
storyboard.Children.Add(widthShrinkAnimation);
47+
storyboard.Children.Add(marginExpandAnimation);
48+
49+
if (button.Name.Equals("Btn_ENTER")) //the height of Btn_ENTER is not Auto so you have to set it manually
50+
{
51+
DoubleAnimation heightShrinkAnimation = new DoubleAnimation(button.Height - shrinkModifier, TimeSpan.FromMilliseconds(animationDuration));
52+
Storyboard.SetTargetName(heightShrinkAnimation, button.Name);
53+
Storyboard.SetTargetProperty(heightShrinkAnimation, new PropertyPath(Button.HeightProperty));
54+
storyboard.Children.Add(heightShrinkAnimation);
55+
}
56+
57+
storyboard.Begin(this);
58+
}
59+
60+
public void ButtonMouseUpAnimation(object sender, RoutedEventArgs e)
61+
{
62+
DoubleAnimation widthExpandAnimation = new DoubleAnimation(previousWidth, TimeSpan.FromMilliseconds(animationDuration));
63+
Storyboard.SetTargetName(widthExpandAnimation, button.Name);
64+
Storyboard.SetTargetProperty(widthExpandAnimation, new PropertyPath(Button.WidthProperty));
65+
66+
ThicknessAnimation marginShrinkAnimation = new ThicknessAnimation(previousMargin, TimeSpan.FromMilliseconds(animationDuration));
67+
Storyboard.SetTargetName(marginShrinkAnimation, button.Name);
68+
Storyboard.SetTargetProperty(marginShrinkAnimation, new PropertyPath(Button.MarginProperty));
69+
70+
Storyboard storyboard = new Storyboard();
71+
storyboard.Children.Add(widthExpandAnimation);
72+
storyboard.Children.Add(marginShrinkAnimation);
73+
74+
if (button.Name.Equals("Btn_ENTER")) //the height of Btn_ENTER is not Auto so you have to reset it manually
75+
{
76+
DoubleAnimation heightShrinkAnimation = new DoubleAnimation(previousHeight, TimeSpan.FromMilliseconds(animationDuration));
77+
Storyboard.SetTargetName(heightShrinkAnimation, button.Name);
78+
Storyboard.SetTargetProperty(heightShrinkAnimation, new PropertyPath(Button.HeightProperty));
79+
storyboard.Children.Add(heightShrinkAnimation);
80+
}
81+
82+
storyboard.Begin(this);
83+
}
84+
85+
}
86+
}

ButtonClickHandler.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ public void ToggableButton_Click(object sender, RoutedEventArgs e)
7272

7373
public void SimulateToggableButtonPress(string buttonName)
7474
{
75-
Button button = DataFetcher.GetControlByName(buttonName) as Button;
76-
75+
Button button = DataFetcher.GetControlByName(buttonName) as Button;
7776
}
7877

7978
private void ToggleRow1Visibility(bool isFnctToggled)

ButtonGenerator.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ private Button GetSingleButton(KeyboardButtonData buttonData, Visibility visibil
4747
else
4848
keyboardButton.Click += buttonClickHandler.NonToggableButton_Click;
4949

50+
AnimationHandler animationHandler = new AnimationHandler(keyboardButton);
51+
keyboardButton.PreviewMouseDown += animationHandler.ButtonMouseDownAnimation;
52+
keyboardButton.PreviewMouseUp += animationHandler.ButtonMouseUpAnimation;
5053
return keyboardButton;
5154
}
5255
}

OnScreenKeyboard.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
<StackPanel x:Name="row5" Orientation="Horizontal" Height="55"/>
2121
</StackPanel>
2222

23-
<Button x:Name="Btn_Enter" Content="Enter" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="11 82" Width="112" Height="106" Style="{StaticResource KeyboardButtonStyle}"/>
24-
<Button x:Name="Btn_Exit" Content="x" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="5" Padding="2 2" Width="25" Height="25" Style="{StaticResource KeyboardButtonStyle}"/>
23+
<Button x:Name="Btn_ENTER" Content="Enter" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="11 82" Width="112" Height="106" Style="{StaticResource KeyboardButtonStyle}"/>
24+
<Button x:Name="Btn_EXIT" Content="x" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="5" Padding="2 2" Width="25" Height="25" Style="{StaticResource KeyboardButtonStyle}"/>
2525

2626
<Thumb x:Name="thumb" Width="0" Height="0"/>
2727
</Grid>

OnScreenKeyboard.xaml.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public OnScreenKeyboard()
2020
SetPopupSizeAndPosition(0.5, 0.3); //the size of the popup is half of the screen width and a nearly a third od the screen height
2121
ActivateDragBehaviour();
2222

23-
ConfigureBtn_Enter();
24-
ConfigureBtn_Exit();
23+
ConfigureBtn_ENTER();
24+
ConfigureBtn_EXIT();
2525
}
2626

2727
private void SetPopupSizeAndPosition(double screenWidthMultiplier, double screenHeightMuliplier)
@@ -38,7 +38,7 @@ private void SetPopupSizeAndPosition(double screenWidthMultiplier, double screen
3838

3939
private void ActivateDragBehaviour()
4040
{
41-
popup.MouseDown += (s, e) =>
41+
popup.MouseLeftButtonDown += (s, e) =>
4242
{
4343
thumb.RaiseEvent(e);
4444
};
@@ -50,20 +50,24 @@ private void ActivateDragBehaviour()
5050
};
5151
}
5252

53-
private void ConfigureBtn_Enter()
53+
private void ConfigureBtn_ENTER()
5454
{
55-
Btn_Enter.Background = DataFetcher.GetBrushByName("KeyboardButtonColor");
55+
Btn_ENTER.Background = DataFetcher.GetBrushByName("KeyboardButtonColor");
5656

5757
ButtonClickHandler buttonClickHandler = new ButtonClickHandler();
58-
Btn_Enter.Click += buttonClickHandler.Btn_Enter_Click;
58+
Btn_ENTER.Click += buttonClickHandler.Btn_Enter_Click;
59+
60+
AnimationHandler animationHandler = new AnimationHandler(Btn_ENTER);
61+
Btn_ENTER.PreviewMouseDown += animationHandler.ButtonMouseDownAnimation;
62+
Btn_ENTER.PreviewMouseUp += animationHandler.ButtonMouseUpAnimation;
5963
}
6064

61-
private void ConfigureBtn_Exit()
65+
private void ConfigureBtn_EXIT()
6266
{
63-
Btn_Exit.Background = DataFetcher.GetBrushByName("KeyboardButtonColor");
67+
Btn_EXIT.Background = DataFetcher.GetBrushByName("KeyboardButtonColor");
6468

6569
ButtonClickHandler buttonClickHandler = new ButtonClickHandler();
66-
Btn_Exit.Click += buttonClickHandler.Btn_Exit_Click;
70+
Btn_EXIT.Click += buttonClickHandler.Btn_Exit_Click;
6771
}
6872
}
6973
}

Resources/CustomStyles.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
</Style>
1616

1717
<Style TargetType="Button" x:Key="KeyboardButtonStyle">
18-
<Setter Property="Background" Value="#EEE"/>
1918
<Setter Property="Width" Value="55"/>
2019
<Setter Property="Height" Value="Auto"/>
2120
<Setter Property="Margin" Value="2"/>
21+
2222
<Style.Resources>
2323
<Style TargetType="Border">
2424
<Setter Property="CornerRadius" Value="5"/>

0 commit comments

Comments
 (0)