Skip to content

Commit 93ce5d0

Browse files
committed
Moved OriginOffset to ShapeLayerContext
1 parent 04cd5fe commit 93ce5d0

File tree

4 files changed

+61
-59
lines changed

4 files changed

+61
-59
lines changed

source/LottieToWinComp/Brushes.cs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,7 @@ static void ApplyCommonStrokeProperties(
257257
public static CompositionBrush? TranslateShapeFill(
258258
LayerContext context,
259259
ShapeFill? shapeFill,
260-
CompositeOpacity opacity,
261-
Rectangles.OriginOffset? originOffset)
260+
CompositeOpacity opacity)
262261
{
263262
if (shapeFill is null)
264263
{
@@ -270,9 +269,9 @@ static void ApplyCommonStrokeProperties(
270269
ShapeFill.ShapeFillKind.SolidColor =>
271270
TranslateSolidColorFill(context, (SolidColorFill)shapeFill, opacity),
272271
ShapeFill.ShapeFillKind.LinearGradient =>
273-
TranslateLinearGradient(context, (LinearGradientFill)shapeFill, opacity, originOffset),
272+
TranslateLinearGradient(context, (LinearGradientFill)shapeFill, opacity),
274273
ShapeFill.ShapeFillKind.RadialGradient =>
275-
TranslateRadialGradient(context, (RadialGradientFill)shapeFill, opacity, originOffset),
274+
TranslateRadialGradient(context, (RadialGradientFill)shapeFill, opacity),
276275
_ => throw new InvalidOperationException(),
277276
};
278277
}
@@ -419,10 +418,9 @@ static CompositionColorBrush TranslateBoundSolidColor(
419418
LayerContext context,
420419
CompositionObject obj,
421420
string propertyName,
422-
TrimmedAnimatable<Vector2> value,
423-
Rectangles.OriginOffset? offset)
421+
TrimmedAnimatable<Vector2> value)
424422
{
425-
if (offset is null)
423+
if (context is not ShapeLayerContext || ((ShapeLayerContext)context).OriginOffset is null)
426424
{
427425
if (value.IsAnimated)
428426
{
@@ -435,6 +433,8 @@ static CompositionColorBrush TranslateBoundSolidColor(
435433
}
436434
}
437435

436+
var offset = ((ShapeLayerContext)context).OriginOffset!;
437+
438438
if (!offset.IsAnimated && !value.IsAnimated)
439439
{
440440
return ConvertTo.Vector2(value.InitialValue) + offset.OffsetValue!;
@@ -469,8 +469,7 @@ static CompositionColorBrush TranslateBoundSolidColor(
469469
static CompositionLinearGradientBrush? TranslateLinearGradient(
470470
LayerContext context,
471471
IGradient linearGradient,
472-
CompositeOpacity opacity,
473-
Rectangles.OriginOffset? originOffset = null)
472+
CompositeOpacity opacity)
474473
{
475474
var result = context.ObjectFactory.CreateLinearGradientBrush();
476475

@@ -480,13 +479,13 @@ static CompositionColorBrush TranslateBoundSolidColor(
480479
var startPoint = Optimizer.TrimAnimatable(context, linearGradient.StartPoint);
481480
var endPoint = Optimizer.TrimAnimatable(context, linearGradient.EndPoint);
482481

483-
var startPointValue = AnimateVector2WithOriginOffsetOrGetValue(context, result, nameof(result.StartPoint), startPoint, originOffset);
482+
var startPointValue = AnimateVector2WithOriginOffsetOrGetValue(context, result, nameof(result.StartPoint), startPoint);
484483
if (startPointValue is not null)
485484
{
486485
result.StartPoint = startPointValue!;
487486
}
488487

489-
var endPointValue = AnimateVector2WithOriginOffsetOrGetValue(context, result, nameof(result.EndPoint), endPoint, originOffset);
488+
var endPointValue = AnimateVector2WithOriginOffsetOrGetValue(context, result, nameof(result.EndPoint), endPoint);
490489
if (endPointValue is not null)
491490
{
492491
result.EndPoint = endPointValue!;
@@ -508,14 +507,13 @@ static CompositionColorBrush TranslateBoundSolidColor(
508507
static CompositionGradientBrush? TranslateRadialGradient(
509508
LayerContext context,
510509
IRadialGradient gradient,
511-
CompositeOpacity opacity,
512-
Rectangles.OriginOffset? originOffset = null)
510+
CompositeOpacity opacity)
513511
{
514512
if (!context.ObjectFactory.IsUapApiAvailable(nameof(CompositionRadialGradientBrush), versionDependentFeatureDescription: "Radial gradient fill"))
515513
{
516514
// CompositionRadialGradientBrush didn't exist until UAP v8. If the target OS doesn't support
517515
// UAP v8 then fall back to linear gradients as a compromise.
518-
return TranslateLinearGradient(context, gradient, opacity, originOffset);
516+
return TranslateLinearGradient(context, gradient, opacity);
519517
}
520518

521519
var result = context.ObjectFactory.CreateRadialGradientBrush();
@@ -526,7 +524,7 @@ static CompositionColorBrush TranslateBoundSolidColor(
526524
var startPoint = Optimizer.TrimAnimatable(context, gradient.StartPoint);
527525
var endPoint = Optimizer.TrimAnimatable(context, gradient.EndPoint);
528526

529-
var startPointValue = AnimateVector2WithOriginOffsetOrGetValue(context, result, nameof(result.EllipseCenter), startPoint, originOffset);
527+
var startPointValue = AnimateVector2WithOriginOffsetOrGetValue(context, result, nameof(result.EllipseCenter), startPoint);
530528
if (startPointValue is not null)
531529
{
532530
result.EllipseCenter = startPointValue!;

source/LottieToWinComp/Rectangles.cs

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.Toolkit.Uwp.UI.Lottie.LottieData;
99
using Microsoft.Toolkit.Uwp.UI.Lottie.WinCompData;
1010
using Microsoft.Toolkit.Uwp.UI.Lottie.WinCompData.Mgcg;
11+
using static Microsoft.Toolkit.Uwp.UI.Lottie.LottieToWinComp.ShapeLayerContext;
1112
using Expressions = Microsoft.Toolkit.Uwp.UI.Lottie.WinCompData.Expressions;
1213
using Sn = System.Numerics;
1314

@@ -18,39 +19,6 @@ namespace Microsoft.Toolkit.Uwp.UI.Lottie.LottieToWinComp
1819
/// </summary>
1920
static class Rectangles
2021
{
21-
// Rectangles are implemented differently in WinComp API
22-
// and Lottie. In WinComp API coordinates inside rectangle start in
23-
// top left corner and in Lottie they start in the middle
24-
// To account for this we need to offset all the points inside
25-
// the rectangle for (Rectangle.Size / 2).
26-
// This class represents this offset (static or animated)
27-
public class OriginOffset
28-
{
29-
public RectangleOrRoundedRectangleGeometry Geometry { get; }
30-
31-
// Use expression if size is animated
32-
public Expressions.Vector2? OffsetExpression { get; }
33-
34-
// Use constant value if size if static
35-
public Sn.Vector2? OffsetValue { get; }
36-
37-
public bool IsAnimated => OffsetValue is null;
38-
39-
public OriginOffset(RectangleOrRoundedRectangleGeometry geometry, Expressions.Vector2 expression)
40-
{
41-
Geometry = geometry;
42-
OffsetExpression = expression;
43-
OffsetValue = null;
44-
}
45-
46-
public OriginOffset(RectangleOrRoundedRectangleGeometry geometry, Sn.Vector2 value)
47-
{
48-
Geometry = geometry;
49-
OffsetExpression = null;
50-
OffsetValue = value;
51-
}
52-
}
53-
5422
// NOTES ABOUT RECTANGLE DRAWING AND CORNERS:
5523
// ==========================================
5624
// A rectangle can be thought of as having 8 components -
@@ -352,17 +320,18 @@ static void ApplyRectangleContentCommon(
352320
var trimOffsetDegrees = (width / (2 * (width + height))) * 360;
353321

354322
// If offset is not animated then other computations for fill brush can be optimized.
355-
OriginOffset originOffset = size.IsAnimated ?
356-
new OriginOffset(geometry, ExpressionFactory.GeometryHalfSize) :
357-
new OriginOffset(geometry, ConvertTo.Vector2(size.InitialValue / 2));
323+
context.LayerContext.OriginOffset = size.IsAnimated ?
324+
new OriginOffsetContainer(geometry, ExpressionFactory.GeometryHalfSize) :
325+
new OriginOffsetContainer(geometry, ConvertTo.Vector2(size.InitialValue / 2));
358326

359327
Shapes.TranslateAndApplyShapeContextWithTrimOffset(
360328
context,
361329
compositionRectangle,
362330
rectangle.DrawingDirection == DrawingDirection.Reverse,
363-
originOffset,
364331
trimOffsetDegrees: trimOffsetDegrees);
365332

333+
context.LayerContext.OriginOffset = null;
334+
366335
compositionRectangle.SetDescription(context, () => rectangle.Name);
367336
compositionRectangle.Geometry.SetDescription(context, () => $"{rectangle.Name}.RectangleGeometry");
368337
}
@@ -448,17 +417,18 @@ static void ApplyRectangleContentCommonXY(
448417
var trimOffsetDegrees = (initialWidth / (2 * (initialWidth + initialHeight))) * 360;
449418

450419
// If offset is not animated then other computations for fill brush can be optimized.
451-
OriginOffset originOffset = width.IsAnimated || height.IsAnimated ?
452-
new OriginOffset(geometry, ExpressionFactory.GeometryHalfSize) :
453-
new OriginOffset(geometry, ConvertTo.Vector2(width.InitialValue / 2, height.InitialValue / 2));
420+
context.LayerContext.OriginOffset = width.IsAnimated || height.IsAnimated ?
421+
new OriginOffsetContainer(geometry, ExpressionFactory.GeometryHalfSize) :
422+
new OriginOffsetContainer(geometry, ConvertTo.Vector2(width.InitialValue / 2, height.InitialValue / 2));
454423

455424
Shapes.TranslateAndApplyShapeContextWithTrimOffset(
456425
context,
457426
compositionRectangle,
458427
rectangle.DrawingDirection == DrawingDirection.Reverse,
459-
originOffset,
460428
trimOffsetDegrees: trimOffsetDegrees);
461429

430+
context.LayerContext.OriginOffset = null;
431+
462432
compositionRectangle.SetDescription(context, () => rectangle.Name);
463433
compositionRectangle.Geometry.SetDescription(context, () => $"{rectangle.Name}.RectangleGeometry");
464434
}

source/LottieToWinComp/ShapeLayerContext.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
using Microsoft.Toolkit.Uwp.UI.Lottie.LottieData;
6+
using Sn = System.Numerics;
67

78
namespace Microsoft.Toolkit.Uwp.UI.Lottie.LottieToWinComp
89
{
@@ -14,6 +15,41 @@ internal ShapeLayerContext(CompositionContext compositionContext, ShapeLayer lay
1415
Layer = layer;
1516
}
1617

18+
// Rectangles are implemented differently in WinComp API
19+
// and Lottie. In WinComp API coordinates inside rectangle start in
20+
// top left corner and in Lottie they start in the middle
21+
// To account for this we need to offset all the points inside
22+
// the rectangle for (Rectangle.Size / 2).
23+
// This class represents this offset (static or animated)
24+
public class OriginOffsetContainer
25+
{
26+
public RectangleOrRoundedRectangleGeometry Geometry { get; }
27+
28+
// Use expression if size is animated
29+
public WinCompData.Expressions.Vector2? OffsetExpression { get; }
30+
31+
// Use constant value if size is static
32+
public Sn.Vector2? OffsetValue { get; }
33+
34+
public bool IsAnimated => OffsetValue is null;
35+
36+
public OriginOffsetContainer(RectangleOrRoundedRectangleGeometry geometry, WinCompData.Expressions.Vector2 expression)
37+
{
38+
Geometry = geometry;
39+
OffsetExpression = expression;
40+
OffsetValue = null;
41+
}
42+
43+
public OriginOffsetContainer(RectangleOrRoundedRectangleGeometry geometry, Sn.Vector2 value)
44+
{
45+
Geometry = geometry;
46+
OffsetExpression = null;
47+
OffsetValue = value;
48+
}
49+
}
50+
51+
internal OriginOffsetContainer? OriginOffset { get; set; }
52+
1753
public new ShapeLayer Layer { get; }
1854
}
1955
}

source/LottieToWinComp/Shapes.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,17 @@ public static void TranslateAndApplyShapeContext(
3838
context,
3939
shape,
4040
reverseDirection,
41-
originOffset: null,
4241
trimOffsetDegrees: 0);
4342

4443
public static void TranslateAndApplyShapeContextWithTrimOffset(
4544
ShapeContext context,
4645
CompositionSpriteShape shape,
4746
bool reverseDirection,
48-
Rectangles.OriginOffset? originOffset,
4947
double trimOffsetDegrees)
5048
{
5149
Debug.Assert(shape.Geometry is not null, "Precondition");
5250

53-
shape.FillBrush = Brushes.TranslateShapeFill(context, context.Fill, context.Opacity, originOffset);
51+
shape.FillBrush = Brushes.TranslateShapeFill(context, context.Fill, context.Opacity);
5452
Brushes.TranslateAndApplyStroke(context, context.Stroke, shape, context.Opacity);
5553

5654
TranslateAndApplyTrimPath(

0 commit comments

Comments
 (0)