|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using Microsoft.Toolkit.Uwp.UI.Media.Pipelines; |
| 8 | +using Windows.UI.Xaml.Markup; |
| 9 | + |
| 10 | +namespace Microsoft.Toolkit.Uwp.UI.Media.Effects |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// A blend effect that merges the current builder with an input one |
| 14 | + /// </summary> |
| 15 | + /// <remarks>This effect maps to the Win2D <see cref="Graphics.Canvas.Effects.CrossFadeEffect"/> effect</remarks> |
| 16 | + [ContentProperty(Name = nameof(Effects))] |
| 17 | + public sealed class CrossFadeEffect : IPipelineEffect |
| 18 | + { |
| 19 | + /// <summary> |
| 20 | + /// Gets or sets the input to merge with the current instance (defaults to a <see cref="BackdropSourceExtension"/> with <see cref="Windows.UI.Xaml.Media.AcrylicBackgroundSource.Backdrop"/> source). |
| 21 | + /// </summary> |
| 22 | + public PipelineBuilder Source { get; set; } |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Gets or sets the effects to apply to the input to merge with the current instance |
| 26 | + /// </summary> |
| 27 | + public List<IPipelineEffect> Effects { get; set; } = new List<IPipelineEffect>(); |
| 28 | + |
| 29 | + private double factor = 0.5; |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Gets or sets the The cross fade factor to blend the input effects (default to 0.5, should be in the [0, 1] range) |
| 33 | + /// </summary> |
| 34 | + public double Factor |
| 35 | + { |
| 36 | + get => this.factor; |
| 37 | + set => this.factor = Math.Clamp(value, 0, 1); |
| 38 | + } |
| 39 | + |
| 40 | + /// <inheritdoc/> |
| 41 | + public PipelineBuilder AppendToPipeline(PipelineBuilder builder) |
| 42 | + { |
| 43 | + PipelineBuilder inputBuilder = Source ?? PipelineBuilder.FromBackdrop(); |
| 44 | + |
| 45 | + foreach (IPipelineEffect effect in this.Effects) |
| 46 | + { |
| 47 | + inputBuilder = effect.AppendToPipeline(inputBuilder); |
| 48 | + } |
| 49 | + |
| 50 | + return builder.CrossFade(inputBuilder, (float)Factor); |
| 51 | + } |
| 52 | + } |
| 53 | +} |
0 commit comments