Skip to content

Commit 2fbe421

Browse files
author
Simeon
authored
Show frame numbers in generated code. (#425)
This makes it much easier to figure out what is causing issues in animations. Also remove the "readonly" on properties in the generated C# because they aren't supported by the language versions used in UWP.
1 parent 12769c4 commit 2fbe421

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

source/UIDataCodeGen/CodeGen/CSharp/CSharpInstantiatorGenerator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ void WriteIDynamicAnimatedVisualSource(CodeBuilder builder)
599599
void WriteFrameCountPropertyImpl(CodeBuilder builder)
600600
{
601601
builder.WriteSummaryComment("Gets the number of frames in the animation.");
602-
builder.WriteLine($"public readonly double FrameCount => {_s.Double(SourceInfo.SourceMetadata.LottieMetadata.Duration.Frames)};");
602+
builder.WriteLine($"public double FrameCount => {_s.Double(SourceInfo.SourceMetadata.LottieMetadata.Duration.Frames)};");
603603
}
604604

605605
/// <summary>
@@ -608,7 +608,7 @@ void WriteFrameCountPropertyImpl(CodeBuilder builder)
608608
void WriteFrameratePropertyImpl(CodeBuilder builder)
609609
{
610610
builder.WriteSummaryComment("Gets the frame rate of the animation.");
611-
builder.WriteLine($"public readonly double Framerate => {_s.Double(SourceInfo.SourceMetadata.LottieMetadata.Duration.FPS)};");
611+
builder.WriteLine($"public double Framerate => {_s.Double(SourceInfo.SourceMetadata.LottieMetadata.Duration.FPS)};");
612612
}
613613

614614
/// <summary>
@@ -617,7 +617,7 @@ void WriteFrameratePropertyImpl(CodeBuilder builder)
617617
void WriteDurationPropertyImpl(CodeBuilder builder)
618618
{
619619
builder.WriteSummaryComment("Gets the duration of the animation.");
620-
builder.WriteLine($"public readonly TimeSpan Duration => {_s.TimeSpan(SourceInfo.SourceMetadata.LottieMetadata.Duration.Time)};");
620+
builder.WriteLine($"public TimeSpan Duration => {_s.TimeSpan(SourceInfo.SourceMetadata.LottieMetadata.Duration.Time)};");
621621
}
622622

623623
/// <summary>

source/UIDataCodeGen/CodeGen/InstantiatorGeneratorBase.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,6 +1511,11 @@ void WritePopulateShapesCollection(CodeBuilder builder, IList<CompositionShape>
15111511
}
15121512
}
15131513

1514+
void WriteFrameNumberComment(CodeBuilder builder, double progress)
1515+
{
1516+
builder.WriteComment($"Frame {_owner._sourceMetadata.ProgressToFrameNumber(progress):0.##}.");
1517+
}
1518+
15141519
internal void WriteAnimatedVisualCode(CodeBuilder builder)
15151520
{
15161521
_owner._currentAnimatedVisualGenerator = this;
@@ -2419,6 +2424,7 @@ bool GenerateBooleanKeyFrameAnimationFactory(CodeBuilder builder, BooleanKeyFram
24192424
// Call the helper to create the animation. This will also set the duration and
24202425
// take the first key frame.
24212426
var kf = (KeyFrameAnimation<bool, Expr.Boolean>.ValueKeyFrame)firstKeyFrame;
2427+
WriteFrameNumberComment(builder, kf.Progress);
24222428
WriteCreateAssignment(builder, node, $"Create{obj.Type}({Float(kf.Progress)}, {Bool(kf.Value)})");
24232429
InitializeCompositionAnimation(builder, obj, node);
24242430
keyFrames = keyFrames.Skip(1);
@@ -2430,6 +2436,8 @@ bool GenerateBooleanKeyFrameAnimationFactory(CodeBuilder builder, BooleanKeyFram
24302436

24312437
foreach (var kf in keyFrames)
24322438
{
2439+
WriteFrameNumberComment(builder, kf.Progress);
2440+
24332441
switch (kf.Type)
24342442
{
24352443
case KeyFrameType.Expression:
@@ -2466,6 +2474,7 @@ bool GenerateColorKeyFrameAnimationFactory(CodeBuilder builder, ColorKeyFrameAni
24662474
// Call the helper to create the animation. This will also set the duration and
24672475
// take the first key frame.
24682476
var kf = (KeyFrameAnimation<Wui.Color, Expr.Color>.ValueKeyFrame)firstKeyFrame;
2477+
WriteFrameNumberComment(builder, kf.Progress);
24692478
WriteCreateAssignment(builder, node, $"Create{obj.Type}({Float(kf.Progress)}, {Color(kf.Value)}, {CallFactoryFromFor(node, kf.Easing)})");
24702479
InitializeCompositionAnimation(builder, obj, node);
24712480
keyFrames = keyFrames.Skip(1);
@@ -2482,6 +2491,8 @@ bool GenerateColorKeyFrameAnimationFactory(CodeBuilder builder, ColorKeyFrameAni
24822491

24832492
foreach (var kf in keyFrames)
24842493
{
2494+
WriteFrameNumberComment(builder, kf.Progress);
2495+
24852496
switch (kf.Type)
24862497
{
24872498
case KeyFrameType.Expression:
@@ -2518,6 +2529,7 @@ bool GenerateVector2KeyFrameAnimationFactory(CodeBuilder builder, Vector2KeyFram
25182529
// Call the helper to create the animation. This will also set the duration and
25192530
// take the first key frame.
25202531
var kf = (KeyFrameAnimation<Vector2, Expr.Vector2>.ValueKeyFrame)firstKeyFrame;
2532+
WriteFrameNumberComment(builder, kf.Progress);
25212533
WriteCreateAssignment(builder, node, $"Create{obj.Type}({Float(kf.Progress)}, {Vector2(kf.Value)}, {CallFactoryFromFor(node, kf.Easing)})");
25222534
InitializeCompositionAnimation(builder, obj, node);
25232535
keyFrames = keyFrames.Skip(1);
@@ -2529,6 +2541,8 @@ bool GenerateVector2KeyFrameAnimationFactory(CodeBuilder builder, Vector2KeyFram
25292541

25302542
foreach (var kf in keyFrames)
25312543
{
2544+
WriteFrameNumberComment(builder, kf.Progress);
2545+
25322546
switch (kf.Type)
25332547
{
25342548
case KeyFrameType.Expression:
@@ -2563,6 +2577,7 @@ bool GenerateVector3KeyFrameAnimationFactory(CodeBuilder builder, Vector3KeyFram
25632577
// Call the helper to create the animation. This will also set the duration and
25642578
// take the first key frame.
25652579
var kf = (KeyFrameAnimation<Vector3, Expr.Vector3>.ValueKeyFrame)firstKeyFrame;
2580+
WriteFrameNumberComment(builder, kf.Progress);
25662581
WriteCreateAssignment(builder, node, $"Create{obj.Type}({Float(kf.Progress)}, {Vector3(kf.Value)}, {CallFactoryFromFor(node, kf.Easing)})");
25672582
InitializeCompositionAnimation(builder, obj, node);
25682583
keyFrames = keyFrames.Skip(1);
@@ -2574,6 +2589,8 @@ bool GenerateVector3KeyFrameAnimationFactory(CodeBuilder builder, Vector3KeyFram
25742589

25752590
foreach (var kf in keyFrames)
25762591
{
2592+
WriteFrameNumberComment(builder, kf.Progress);
2593+
25772594
switch (kf.Type)
25782595
{
25792596
case KeyFrameType.Expression:
@@ -2608,6 +2625,7 @@ bool GenerateVector4KeyFrameAnimationFactory(CodeBuilder builder, Vector4KeyFram
26082625
// Call the helper to create the animation. This will also set the duration and
26092626
// take the first key frame.
26102627
var kf = (KeyFrameAnimation<Vector4, Expr.Vector4>.ValueKeyFrame)firstKeyFrame;
2628+
WriteFrameNumberComment(builder, kf.Progress);
26112629
WriteCreateAssignment(builder, node, $"Create{obj.Type}({Float(kf.Progress)}, {Vector4(kf.Value)}, {CallFactoryFromFor(node, kf.Easing)})");
26122630
InitializeCompositionAnimation(builder, obj, node);
26132631
keyFrames = keyFrames.Skip(1);
@@ -2619,6 +2637,8 @@ bool GenerateVector4KeyFrameAnimationFactory(CodeBuilder builder, Vector4KeyFram
26192637

26202638
foreach (var kf in keyFrames)
26212639
{
2640+
WriteFrameNumberComment(builder, kf.Progress);
2641+
26222642
switch (kf.Type)
26232643
{
26242644
case KeyFrameType.Expression:
@@ -2652,6 +2672,7 @@ bool GeneratePathKeyFrameAnimationFactory(CodeBuilder builder, PathKeyFrameAnima
26522672
// Call the helper to create the animation. This will also set the duration and
26532673
// take the first key frame.
26542674
var kf = (PathKeyFrameAnimation.ValueKeyFrame)firstKeyFrame;
2675+
WriteFrameNumberComment(builder, kf.Progress);
26552676
WriteCreateAssignment(builder, node, $"Create{obj.Type}({Float(kf.Progress)}, {CallFactoryFromFor(node, kf.Value)}, {CallFactoryFromFor(node, kf.Easing)})");
26562677
InitializeCompositionAnimation(builder, obj, node);
26572678
keyFrames = keyFrames.Skip(1);
@@ -2663,6 +2684,7 @@ bool GeneratePathKeyFrameAnimationFactory(CodeBuilder builder, PathKeyFrameAnima
26632684

26642685
foreach (var kf in keyFrames)
26652686
{
2687+
WriteFrameNumberComment(builder, kf.Progress);
26662688
var valueKeyFrame = (PathKeyFrameAnimation.ValueKeyFrame)kf;
26672689
builder.WriteLine($"result{Deref}InsertKeyFrame({Float(kf.Progress)}, {CallFactoryFromFor(node, valueKeyFrame.Value)}, {CallFactoryFromFor(node, kf.Easing)});");
26682690
}
@@ -2686,6 +2708,7 @@ bool GenerateScalarKeyFrameAnimationFactory(CodeBuilder builder, ScalarKeyFrameA
26862708
// Call the helper to create the animation. This will also set the duration and
26872709
// take the first key frame.
26882710
var kf = (KeyFrameAnimation<float, Expr.Scalar>.ValueKeyFrame)firstKeyFrame;
2711+
WriteFrameNumberComment(builder, kf.Progress);
26892712
WriteCreateAssignment(builder, node, $"Create{obj.Type}({Float(kf.Progress)}, {Float(kf.Value)}, {CallFactoryFromFor(node, kf.Easing)})");
26902713
InitializeCompositionAnimation(builder, obj, node);
26912714
keyFrames = keyFrames.Skip(1);
@@ -2697,6 +2720,8 @@ bool GenerateScalarKeyFrameAnimationFactory(CodeBuilder builder, ScalarKeyFrameA
26972720

26982721
foreach (var kf in keyFrames)
26992722
{
2723+
WriteFrameNumberComment(builder, kf.Progress);
2724+
27002725
switch (kf.Type)
27012726
{
27022727
case KeyFrameType.Expression:

source/UIDataCodeGen/CodeGen/SourceMetadata.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,8 @@ internal IReadOnlyList<PropertyBinding> PropertyBindings
5454
return _propertyBindings;
5555
}
5656
}
57+
58+
internal double ProgressToFrameNumber(double progress)
59+
=> LottieMetadata.Duration.Frames * progress;
5760
}
5861
}

0 commit comments

Comments
 (0)