-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
267 lines (217 loc) · 8.88 KB
/
MainWindow.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace ScheduleRevealTool
{
public partial class MainWindow : Window
{
private DispatcherTimer countdownTimer;
private DateTime countdownEnd = DateTime.Now;
public MainWindow()
{
InitializeComponent();
NextRunControl.Opacity = 0.0;
CountdownViewBox.Opacity = 0.0;
countdownTimer = new DispatcherTimer(
TimeSpan.FromMilliseconds(100),
DispatcherPriority.Render,
CountdownTimer_Tick,
Application.Current.Dispatcher);
}
protected override void OnClosing(CancelEventArgs e)
{
e.Cancel = true;
}
private void Delay(TimeSpan delay, Action action)
{
Task.Delay(delay).ContinueWith(t => Dispatcher.Invoke(action));
}
private void Delay(double delayMs, Action action)
{
Delay(TimeSpan.FromMilliseconds(delayMs), action);
}
private static readonly DependencyProperty ScrollOffsetProperty
= DependencyProperty.Register(
"ScrollOffset",
typeof(double),
typeof(MainWindow),
new FrameworkPropertyMetadata(
0.0,
new PropertyChangedCallback(OnScrollOffsetChanged)));
public double ScrollOffset
{
get { return (double)GetValue(ScrollOffsetProperty); }
set { SetValue(ScrollOffsetProperty, value); }
}
private static void OnScrollOffsetChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
MainWindow win = obj as MainWindow;
if (win == null)
return;
win.MainCardsScroll.ScrollToVerticalOffset((double)args.NewValue);
}
public void ScrollOverList(double durationSec = 30.0, double pauseSec = 5.0)
{
DoubleAnimation scrollUp = new DoubleAnimation(MainCardsScroll.VerticalOffset, 0.0, TimeSpan.FromSeconds(durationSec / 2));
scrollUp.Completed += (s, e) =>
{
Delay(pauseSec * 1000, () =>
{
DoubleAnimation scrollDown = new DoubleAnimation(0.0, MainCardsScroll.ScrollableHeight, TimeSpan.FromSeconds(durationSec / 2));
BeginAnimation(ScrollOffsetProperty, scrollDown);
});
};
BeginAnimation(ScrollOffsetProperty, scrollUp);
}
public void AddRunToList(Run run, double speedMul = 1.0)
{
if (speedMul < 0.01)
speedMul = 0.01;
if (run == null || run.Game == "")
return;
ScheduleItemControl ctrl = new ScheduleItemControl();
ctrl.FromRun(NextRunControl.ToRun());
var margin = ctrl.Margin;
margin.Bottom = 5;
ctrl.Margin = margin;
ctrl.Opacity = 0.0;
MainCardsStack.Children.Add(ctrl);
UpdateLayout();
DoubleAnimation scrollDown = new DoubleAnimation(MainCardsScroll.VerticalOffset, MainCardsScroll.ScrollableHeight, TimeSpan.FromMilliseconds(2000 * speedMul));
scrollDown.EasingFunction = new BounceEase { EasingMode = EasingMode.EaseOut };
scrollDown.Completed += (s, e) =>
{
DoubleAnimation fadeIn = new DoubleAnimation(1.0, TimeSpan.FromMilliseconds(2000 * speedMul));
fadeIn.BeginTime = TimeSpan.FromMilliseconds(500 * speedMul);
ctrl.BeginAnimation(OpacityProperty, fadeIn);
};
BeginAnimation(ScrollOffsetProperty, scrollDown);
}
bool inProgress = false;
public bool RevealNextRun(Run run, double speedMul = 1.0)
{
if (speedMul < 0.01)
speedMul = 0.01;
if (inProgress)
return false;
inProgress = true;
Run prevRun = NextRunControl.ToRun();
if (prevRun != null && prevRun.Game != "")
{
prevRun.PropertyChanged -= CurRunPropChanged;
AddRunToList(prevRun, speedMul);
}
if (run != null && run.Game != "")
run.PropertyChanged += CurRunPropChanged;
DoubleAnimation fadeOut = new DoubleAnimation(0.0, TimeSpan.FromMilliseconds(2000 * speedMul));
DoubleAnimation fadeIn = new DoubleAnimation(1.0, TimeSpan.FromMilliseconds(2000 * speedMul));
fadeIn.BeginTime = TimeSpan.FromMilliseconds(2500 * speedMul);
fadeOut.BeginTime = TimeSpan.FromMilliseconds(2000 * speedMul);
fadeOut.Completed += (s, e) =>
{
if (run == null || run.Game == "")
{
inProgress = false;
NextRunControl.Clear();
}
else
{
NextRunControl.FromRun(run);
NextRunControl.BeginAnimation(OpacityProperty, fadeIn);
inProgress = false;
}
};
NextRunControl.BeginAnimation(OpacityProperty, fadeOut);
return true;
}
private void CurRunPropChanged(object sender, PropertyChangedEventArgs _)
{
Run run = (Run)sender;
if (!run.Presented)
{
run.PropertyChanged -= CurRunPropChanged;
inProgress = true;
DoubleAnimation fadeOut = new DoubleAnimation(0.0, TimeSpan.FromMilliseconds(250));
fadeOut.Completed += (s, e) =>
{
inProgress = false;
NextRunControl.Clear();
};
NextRunControl.BeginAnimation(OpacityProperty, fadeOut);
}
}
public void Clear()
{
inProgress = true;
DoubleAnimation fadeOutRun = new DoubleAnimation(0.0, TimeSpan.FromMilliseconds(1000));
DoubleAnimation fadeOutList = new DoubleAnimation(0.0, TimeSpan.FromMilliseconds(1000));
fadeOutRun.Completed += (s, e) =>
{
NextRunControl.Clear();
inProgress = false;
};
fadeOutList.Completed += (s, e) =>
{
MainCardsStack.Children.Clear();
UpdateLayout();
MainCardsScroll.ScrollToTop();
MainCardsScroll.BeginAnimation(OpacityProperty, null);
MainCardsScroll.Opacity = 1.0;
};
NextRunControl.BeginAnimation(OpacityProperty, fadeOutRun);
MainCardsScroll.BeginAnimation(OpacityProperty, fadeOutList);
}
public void UpdateOmniBar(string text, double speedMul = 1.0)
{
DoubleAnimation fadeOut = new DoubleAnimation(0.0, TimeSpan.FromMilliseconds(2500 * speedMul));
DoubleAnimation fadeIn = new DoubleAnimation(1.0, TimeSpan.FromMilliseconds(2500 * speedMul));
fadeOut.Completed += (s, e) =>
{
OmnibarTextBox.Text = text;
Delay(2500 * speedMul, () => {
OmnibarTextBox.BeginAnimation(OpacityProperty, fadeIn);
});
};
OmnibarTextBox.BeginAnimation(OpacityProperty, fadeOut);
}
public void SetCountdown(double seconds)
{
countdownTimer.Stop();
countdownEnd = DateTime.Now.AddSeconds(seconds);
CountdownTimer_Tick(null, null);
DoubleAnimation fadeIn = new DoubleAnimation(1.0, TimeSpan.FromMilliseconds(2500));
fadeIn.Completed += (s, e) =>
{
countdownEnd = DateTime.Now.AddSeconds(seconds);
countdownTimer.Start();
};
CountdownViewBox.BeginAnimation(OpacityProperty, fadeIn);
}
private void CountdownTimer_Tick(object sender, EventArgs e)
{
TimeSpan diff = countdownEnd - DateTime.Now;
if (diff.TotalMilliseconds <= 0)
{
countdownTimer.Stop();
DoubleAnimation fadeOut = new DoubleAnimation(0.0, TimeSpan.FromMilliseconds(2500));
CountdownViewBox.BeginAnimation(OpacityProperty, fadeOut);
return;
}
CountdownTextBox.Text = Math.Floor(diff.TotalMinutes).ToString("00") + ":" + diff.Seconds.ToString("00");
}
}
}