-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathNormalizedKeyFrameAnimationBuilder{T}.Composition.cs
More file actions
304 lines (258 loc) · 11.6 KB
/
NormalizedKeyFrameAnimationBuilder{T}.Composition.cs
File metadata and controls
304 lines (258 loc) · 11.6 KB
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// 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.
#if WINUI3
using Microsoft.UI.Composition;
#elif WINUI2
using Windows.UI.Composition;
#endif
using System.Numerics;
using Windows.UI;
namespace CommunityToolkit.WinUI.Animations;
/// <inheritdoc cref="NormalizedKeyFrameAnimationBuilder{T}"/>
public abstract partial class NormalizedKeyFrameAnimationBuilder<T>
{
/// <summary>
/// Gets a <see cref="CompositionAnimation"/> instance representing the animation to start.
/// </summary>
/// <typeparam name="TKeyFrame">The type of keyframes being used to define the animation.</typeparam>
/// <param name="target">The target <see cref="CompositionObject"/> instance to animate.</param>
/// <param name="property">The target property to animate.</param>
/// <param name="delay">The optional initial delay for the animation.</param>
/// <param name="duration">The animation duration.</param>
/// <param name="repeat">The <see cref="RepeatOption"/> value for the animation</param>
/// <param name="delayBehavior">The delay behavior mode to use.</param>
/// <param name="keyFrames">The list of keyframes to use to build the animation.</param>
/// <returns>A <see cref="CompositionAnimation"/> instance with the specified animation.</returns>
public static CompositionAnimation GetAnimation<TKeyFrame>(
CompositionObject target,
string property,
TimeSpan? delay,
TimeSpan duration,
RepeatOption repeat,
AnimationDelayBehavior delayBehavior,
ArraySegment<TKeyFrame> keyFrames)
where TKeyFrame : struct, IKeyFrameInfo
{
KeyFrameAnimation animation;
if (typeof(T) == typeof(bool))
{
BooleanKeyFrameAnimation boolAnimation = target.Compositor.CreateBooleanKeyFrameAnimation();
foreach (var keyFrame in keyFrames)
{
if (keyFrame.TryInsertExpressionKeyFrame(boolAnimation, duration))
{
continue;
}
boolAnimation.InsertKeyFrame(keyFrame.GetNormalizedProgress(duration), keyFrame.GetValueAs<bool>());
}
animation = boolAnimation;
}
else if (typeof(T) == typeof(float))
{
ScalarKeyFrameAnimation scalarAnimation = target.Compositor.CreateScalarKeyFrameAnimation();
foreach (var keyFrame in keyFrames)
{
if (keyFrame.TryInsertExpressionKeyFrame(scalarAnimation, duration))
{
continue;
}
CompositionEasingFunction? easingFunction = target.Compositor.TryCreateEasingFunction(keyFrame.EasingType, keyFrame.EasingMode);
if (easingFunction is null)
{
scalarAnimation.InsertKeyFrame(keyFrame.GetNormalizedProgress(duration), keyFrame.GetValueAs<float>());
}
else
{
scalarAnimation.InsertKeyFrame(keyFrame.GetNormalizedProgress(duration), keyFrame.GetValueAs<float>(), easingFunction);
}
}
animation = scalarAnimation;
}
else if (typeof(T) == typeof(double))
{
ScalarKeyFrameAnimation scalarAnimation = target.Compositor.CreateScalarKeyFrameAnimation();
foreach (var keyFrame in keyFrames)
{
if (keyFrame.TryInsertExpressionKeyFrame(scalarAnimation, duration))
{
continue;
}
CompositionEasingFunction? easingFunction = target.Compositor.TryCreateEasingFunction(keyFrame.EasingType, keyFrame.EasingMode);
if (easingFunction is null)
{
scalarAnimation.InsertKeyFrame(keyFrame.GetNormalizedProgress(duration), (float)keyFrame.GetValueAs<double>());
}
else
{
scalarAnimation.InsertKeyFrame(keyFrame.GetNormalizedProgress(duration), (float)keyFrame.GetValueAs<double>(), easingFunction);
}
}
animation = scalarAnimation;
}
else if (typeof(T) == typeof(Vector2))
{
Vector2KeyFrameAnimation vector2Animation = target.Compositor.CreateVector2KeyFrameAnimation();
foreach (var keyFrame in keyFrames)
{
if (keyFrame.TryInsertExpressionKeyFrame(vector2Animation, duration))
{
continue;
}
CompositionEasingFunction? easingFunction = target.Compositor.TryCreateEasingFunction(keyFrame.EasingType, keyFrame.EasingMode);
if (easingFunction is null)
{
vector2Animation.InsertKeyFrame(keyFrame.GetNormalizedProgress(duration), keyFrame.GetValueAs<Vector2>());
}
else
{
vector2Animation.InsertKeyFrame(keyFrame.GetNormalizedProgress(duration), keyFrame.GetValueAs<Vector2>(), easingFunction);
}
}
animation = vector2Animation;
}
else if (typeof(T) == typeof(Vector3))
{
Vector3KeyFrameAnimation vector3Animation = target.Compositor.CreateVector3KeyFrameAnimation();
foreach (var keyFrame in keyFrames)
{
if (keyFrame.TryInsertExpressionKeyFrame(vector3Animation, duration))
{
continue;
}
CompositionEasingFunction? easingFunction = target.Compositor.TryCreateEasingFunction(keyFrame.EasingType, keyFrame.EasingMode);
if (easingFunction is null)
{
vector3Animation.InsertKeyFrame(keyFrame.GetNormalizedProgress(duration), keyFrame.GetValueAs<Vector3>());
}
else
{
vector3Animation.InsertKeyFrame(keyFrame.GetNormalizedProgress(duration), keyFrame.GetValueAs<Vector3>(), easingFunction);
}
}
animation = vector3Animation;
}
else if (typeof(T) == typeof(Vector4))
{
Vector4KeyFrameAnimation vector4Animation = target.Compositor.CreateVector4KeyFrameAnimation();
foreach (var keyFrame in keyFrames)
{
if (keyFrame.TryInsertExpressionKeyFrame(vector4Animation, duration))
{
continue;
}
CompositionEasingFunction? easingFunction = target.Compositor.TryCreateEasingFunction(keyFrame.EasingType, keyFrame.EasingMode);
if (easingFunction is null)
{
vector4Animation.InsertKeyFrame(keyFrame.GetNormalizedProgress(duration), keyFrame.GetValueAs<Vector4>());
}
else
{
vector4Animation.InsertKeyFrame(keyFrame.GetNormalizedProgress(duration), keyFrame.GetValueAs<Vector4>(), easingFunction);
}
}
animation = vector4Animation;
}
else if (typeof(T) == typeof(Color))
{
ColorKeyFrameAnimation colorAnimation = target.Compositor.CreateColorKeyFrameAnimation();
foreach (var keyFrame in keyFrames)
{
if (keyFrame.TryInsertExpressionKeyFrame(colorAnimation, duration))
{
continue;
}
CompositionEasingFunction? easingFunction = target.Compositor.TryCreateEasingFunction(keyFrame.EasingType, keyFrame.EasingMode);
if (easingFunction is null)
{
colorAnimation.InsertKeyFrame(keyFrame.GetNormalizedProgress(duration), keyFrame.GetValueAs<Color>());
}
else
{
colorAnimation.InsertKeyFrame(keyFrame.GetNormalizedProgress(duration), keyFrame.GetValueAs<Color>(), easingFunction);
}
}
animation = colorAnimation;
}
else if (typeof(T) == typeof(Quaternion))
{
QuaternionKeyFrameAnimation quaternionAnimation = target.Compositor.CreateQuaternionKeyFrameAnimation();
foreach (var keyFrame in keyFrames)
{
if (keyFrame.TryInsertExpressionKeyFrame(quaternionAnimation, duration))
{
continue;
}
CompositionEasingFunction? easingFunction = target.Compositor.TryCreateEasingFunction(keyFrame.EasingType, keyFrame.EasingMode);
if (easingFunction is null)
{
quaternionAnimation.InsertKeyFrame(keyFrame.GetNormalizedProgress(duration), keyFrame.GetValueAs<Quaternion>());
}
else
{
quaternionAnimation.InsertKeyFrame(keyFrame.GetNormalizedProgress(duration), keyFrame.GetValueAs<Quaternion>(), easingFunction);
}
}
animation = quaternionAnimation;
}
else
{
throw new InvalidOperationException("Invalid animation type");
}
animation.Duration = duration;
if (delay.HasValue)
{
animation.DelayBehavior = delayBehavior;
animation.DelayTime = delay!.Value;
}
animation.Target = property;
(animation.IterationBehavior, animation.IterationCount) = repeat.ToBehaviorAndCount();
return animation;
}
/// <summary>
/// A custom <see cref="NormalizedKeyFrameAnimationBuilder{T}"/> class targeting the composition layer.
/// </summary>
public sealed class Composition : NormalizedKeyFrameAnimationBuilder<T>, AnimationBuilder.ICompositionAnimationFactory
{
/// <summary>
/// The target delay behavior to use.
/// </summary>
private readonly AnimationDelayBehavior delayBehavior;
/// <summary>
/// Initializes a new instance of the <see cref="NormalizedKeyFrameAnimationBuilder{T}.Composition"/> class.
/// </summary>
/// <param name="property">The target property to animate.</param>
/// <param name="delay">The target delay for the animation.</param>
/// <param name="duration">The target duration for the animation.</param>
/// <param name="repeat">The repeat options for the animation.</param>
/// <param name="delayBehavior">The delay behavior mode to use.</param>
public Composition(string property, TimeSpan? delay, TimeSpan duration, RepeatOption repeat, AnimationDelayBehavior delayBehavior)
: base(property, delay, duration, repeat)
{
this.delayBehavior = delayBehavior;
}
/// <inheritdoc/>
public override INormalizedKeyFrameAnimationBuilder<T> ExpressionKeyFrame(
double progress,
string expression,
EasingType easingType,
EasingMode easingMode)
{
this.keyFrames.Append(new(progress, expression, easingType, easingMode));
return this;
}
/// <inheritdoc/>
public CompositionAnimation GetAnimation(CompositionObject targetHint, out CompositionObject? target)
{
target = null;
return GetAnimation(
targetHint,
this.property,
this.delay,
this.duration,
this.repeat,
this.delayBehavior,
this.keyFrames.GetArraySegment());
}
}
}