-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathFeedbackLottie.xaml.cs
194 lines (164 loc) · 6.93 KB
/
FeedbackLottie.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Windows.UI.Xaml.Controls;
namespace LottieViewer
{
/// <summary>
/// FeedbackLottie.
/// </summary>
public sealed partial class FeedbackLottie : UserControl
{
// Shrinks from the expanded JSON file back to the initial size.
static readonly CompositionSegment ShrinkToInitial =
new CompositionSegment("ShrinkToInitial", 0.007, 0.1188811, playbackRate: -1, isLoopingEnabled: false);
// Expands from the initial state to a large JSON file.
static readonly CompositionSegment ExpandFromInitial = new CompositionSegment("ExpandFromInitial", 0.007, 0.1188811);
// A loop where the JSON file looks excited about being dropped.
static readonly CompositionSegment ExcitedDropLoop =
new CompositionSegment("ExcitedDropLoop", 0.1188811, 0.3426574, playbackRate: 1, isLoopingEnabled: true);
// Follows on from ExcitedDropLoop.
static readonly CompositionSegment ExcitedResolution = new CompositionSegment("ExcitedResolution", 0.3426574, 0.489);
// The explosion at the end of loading.
static readonly CompositionSegment FinishLoading = new CompositionSegment("FinishLoading", 0.6923078, 1);
static readonly CompositionSegment FailLoading = new CompositionSegment("FailLoading", 0.4895105, 0.69 /* 0.6923077 */);
Task _currentPlay = Task.CompletedTask;
DragNDropHintState _dragNDropHintState = DragNDropHintState.Initial;
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
public FeedbackLottie()
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
{
this.InitializeComponent();
}
internal void PlayInitialStateAnimation()
{
switch (_dragNDropHintState)
{
case DragNDropHintState.Failed:
case DragNDropHintState.Finished:
case DragNDropHintState.Initial:
break;
case DragNDropHintState.Disabled:
case DragNDropHintState.Encouraging:
case DragNDropHintState.Shrinking:
default:
return;
}
EnsureVisible();
_dragNDropHintState = DragNDropHintState.Initial;
_dragNDropHint.SetProgress(0.007);
}
// Avoid "async void" method. Not valid here because we handle all async exceptions.
#pragma warning disable VSTHRD100
internal async void PlayDragEnterAnimation()
{
#pragma warning restore VSTHRD100
EnsureVisible();
if (_dragNDropHintState == DragNDropHintState.Initial ||
_dragNDropHintState == DragNDropHintState.Shrinking)
{
_dragNDropHintState = DragNDropHintState.Encouraging;
try
{
await PlaySegmentAsync(ExpandFromInitial);
await PlaySegmentAsync(ExcitedDropLoop);
}
catch
{
// Ignore async exceptions so they won't crash the process.
}
}
}
internal async Task PlayDroppedAnimationAsync()
{
EnsureVisible();
if (_dragNDropHintState == DragNDropHintState.Encouraging)
{
await PlaySegmentAsync(ExcitedResolution);
if (_dragNDropHintState != DragNDropHintState.Encouraging)
{
return;
}
}
_dragNDropHintState = DragNDropHintState.Finished;
// Fade out. This is only necessary for RS4 builds that
// do not handle 0-size strokes correctly, leaving crud on
// the screen.
_fadeOutStoryboard.Begin();
await PlaySegmentAsync(FinishLoading);
}
internal void PlayDragLeaveAnimation()
{
EnsureVisible();
if (_dragNDropHintState == DragNDropHintState.Encouraging)
{
_dragNDropHintState = DragNDropHintState.Shrinking;
#pragma warning disable VSTHRD110 // Observe result of async calls
PlaySegmentAsync(ShrinkToInitial);
#pragma warning restore VSTHRD110 // Observe result of async calls
}
}
internal async Task PlayLoadFailedAnimationAsync()
{
EnsureVisible();
_dragNDropHintState = DragNDropHintState.Failed;
await PlaySegmentAsync(FailLoading);
_dragNDropHintState = DragNDropHintState.Initial;
_dragNDropHint.SetProgress(FailLoading.ToProgress);
}
Task PlaySegmentAsync(CompositionSegment segment)
{
_dragNDropHint.PlaybackRate = segment.PlaybackRate;
return _currentPlay = _dragNDropHint.PlayAsync(
fromProgress: segment.FromProgress,
toProgress: segment.ToProgress,
looped: segment.IsLoopingEnabled).AsTask();
}
void EnsureVisible()
{
Debug.WriteLine("Stopping opacity animation");
_fadeOutStoryboard.Stop();
_dragNDropHint.Opacity = 1;
}
enum DragNDropHintState
{
Disabled,
Initial,
Encouraging,
Finished,
Failed,
Shrinking,
}
/// <summary>
/// Defines a segment of a composition that can be played by the AnimatedVisualPlayer.
/// </summary>
sealed class CompositionSegment
{
public double FromProgress { get; }
public double ToProgress { get; }
public double PlaybackRate { get; }
public bool IsLoopingEnabled { get; }
public string Name { get; }
public CompositionSegment(string name, double fromProgress, double toProgress, double playbackRate, bool isLoopingEnabled)
{
Name = name;
FromProgress = fromProgress;
ToProgress = toProgress;
PlaybackRate = playbackRate;
IsLoopingEnabled = isLoopingEnabled;
}
/// <summary>
/// Initializes a new instance of the <see cref="CompositionSegment"/> class.
/// Defines a segment that plays from <paramref name="fromProgress"/> to <paramref name="toProgress"/>
/// without looping or repeating.
/// </summary>
public CompositionSegment(string name, double fromProgress, double toProgress)
: this(name, fromProgress, toProgress, playbackRate: 1, isLoopingEnabled: false)
{
}
}
}
}