-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShakeGameControl.xaml.cs
133 lines (113 loc) · 4.04 KB
/
ShakeGameControl.xaml.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System;
using System.Windows;
using System.Windows.Controls;
using ShakeGestures;
using System.Windows.Media;
using ImageTools.IO.Gif;
using System.Windows.Threading;
namespace WakeMeUp.Alarms.AlarmGame.Shake
{
public enum GameLevel
{
Easy, Medium, Hard
}
public partial class ShakeGameControl : UserControl
{
#region Property
public Uri ImageSource { get; set; }
//Min number of shake needed to raise a shake event
private int NumofMinShake;
//Amount of height increase when a shake event is raised
private int NumofFillperShake;
//the higher level, the harder and stronger shake needed
private GameLevel _level;
public GameLevel Level
{
private set
{
_level = value;
NumofMinShake = (int)_level + 1;
NumofFillperShake = 45 - (int)_level * 15;
ShakeGesturesHelper.Instance.MinimumRequiredMovesForShake = NumofMinShake;
}
get
{
return _level;
}
}
#endregion
public ShakeGameControl()
{
InitializeComponent();
ImageSource = new Uri("/Assets/wave.gif", UriKind.Relative);
ImageTools.IO.Decoders.AddDecoder<GifDecoder>();
this.Loaded += ShakeGameControl_Loaded;
this.Unloaded += ShakeGameControl_Unloaded;
LayoutRoot.DataContext = this;
//NotificationTxt.Foreground = new SolidColorBrush(Global.settingsManager.GetColor());
}
public void StartShakeDetection(GameLevel level)
{
Level = level;
ProgressTxt.Text = "0 %";
WaveImg.Height = 20;
// start shake detection
ShakeGesturesHelper.Instance.ShakeGesture += Instance_ShakeGesture;
ShakeGesturesHelper.Instance.Active = true;
}
#region Frame Event
private void ShakeGameControl_Loaded(object sender, RoutedEventArgs e)
{
//ShakeAnimation.RepeatBehavior = new System.Windows.Media.Animation.RepeatBehavior(3);
ShakeStoryboard.Begin();
}
private void ShakeGameControl_Unloaded(object sender, RoutedEventArgs e)
{
ShakeGesturesHelper.Instance.ShakeGesture -= Instance_ShakeGesture;
}
#endregion
#region Shake Event
private void Instance_ShakeGesture(object sender, ShakeGestureEventArgs e)
{
Dispatcher.BeginInvoke(() =>
{
double h = WaveImg.Height;
//Calculate progress in percent
int progress = (int)(((h - 20) / BarRect.Height) * 100);
ProgressTxt.Text = progress.ToString() + " %";
//Stop the animation
FillStoryboard.Stop();
//Full Shake
if (h >= BarRect.Height)
{
WaveImg.Height = BarRect.Height;
ProgressTxt.Text = "100 %";
ShakeGesturesHelper.Instance.Active = false;
//Wait about 400ms more and raise Full Shake event
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(400);
timer.Tick += timer_Tick;
timer.Start();
return;
}
//Setup and start animation
FillAnimation.From = h;
FillAnimation.By = NumofFillperShake;
FillStoryboard.Begin();
});
}
private void timer_Tick(object sender, EventArgs e)
{
(sender as DispatcherTimer).Stop();
if (ShakeFullEvent != null)
{
ShakeFullEvent(this);
}
}
#endregion
#region Event
public event ShakeFullHandler ShakeFullEvent;
public delegate void ShakeFullHandler(object sender);
#endregion
}
}