From b2cafaacd3621afbb78b1b44c5b9a59e26a0664c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 21 Jun 2017 00:42:51 +0200 Subject: [PATCH] Added a drop shadow extension method --- .../CompositionExtensions.cs | 51 +++++++++++++++++++ UICompositionAnimations/project.json | 1 + 2 files changed, 52 insertions(+) diff --git a/UICompositionAnimations/CompositionExtensions.cs b/UICompositionAnimations/CompositionExtensions.cs index 0397b29..525d038 100644 --- a/UICompositionAnimations/CompositionExtensions.cs +++ b/UICompositionAnimations/CompositionExtensions.cs @@ -4,7 +4,10 @@ using Windows.UI.Xaml.Hosting; using System.Numerics; using Windows.Foundation; +using Windows.UI; using Windows.UI.Composition; +using Windows.UI.Xaml.Controls; +using JetBrains.Annotations; using UICompositionAnimations.Composition; using UICompositionAnimations.Enums; using UICompositionAnimations.Helpers; @@ -833,6 +836,54 @@ public static async Task StartCompositionRollAnimationAsync(this FrameworkElemen #endregion + #region Shadows + + /// + /// Creates a object from the given + /// + /// The source element for the shadow + /// The optional target element to apply the shadow to (it can be the same as the source element) + /// Indicates whether or not to immediately add the shadow to the visual tree + /// The optional width of the shadow (if null, the element width will be used) + /// The optional height of the shadow (if null, the element height will be used) + /// The shadow color (the default is + /// The opacity of the shadow + /// The optional horizontal offset of the shadow + /// The optional vertical offset of the shadow + /// The optional margin of the clip area of the shadow + /// The optional horizontal offset of the clip area of the shadow + /// The optional vertical offset of the clip area of the shadow + /// The object that hosts the shadow, and the itself + public static (SpriteVisual, DropShadow) GetVisualShadow( + [NotNull] FrameworkElement element, [NotNull] UIElement target, bool apply, + float? width, float? height, + Color color, float opacity, + float offsetX = 0, float offsetY = 0, + Thickness? clipMargin = null, float clipOffsetX = 0, float clipOffsetY = 0) + { + // Setup the shadow + Visual elementVisual = ElementCompositionPreview.GetElementVisual(element); + Compositor compositor = elementVisual.Compositor; + SpriteVisual visual = compositor.CreateSpriteVisual(); + DropShadow shadow = compositor.CreateDropShadow(); + shadow.Color = color; + shadow.Opacity = opacity; + visual.Shadow = shadow; + visual.Size = new Vector2(width ?? (float)element.Width, height ?? (float)element.Height); + visual.Offset = new Vector3(-0.5f, -0.5f, 0); + + // Clip it and add it to the visual tree + InsetClip clip = compositor.CreateInsetClip( + (float)(clipMargin?.Left ?? 0), (float)(clipMargin?.Top ?? 0), + (float)(clipMargin?.Right ?? 0), (float)(clipMargin?.Bottom ?? 0)); + clip.Offset = new Vector2(clipOffsetX, clipOffsetY); + visual.Clip = clip; + if (apply) ElementCompositionPreview.SetElementChildVisual(target, visual); + return (visual, shadow); + } + + #endregion + #region Utility extensions /// diff --git a/UICompositionAnimations/project.json b/UICompositionAnimations/project.json index bd58e1f..b8f8a57 100644 --- a/UICompositionAnimations/project.json +++ b/UICompositionAnimations/project.json @@ -3,6 +3,7 @@ "JetBrains.Annotations": "10.4.0", "Microsoft.NETCore.UniversalWindowsPlatform": "5.3.3", "System.Threading.Tasks.Extensions": "4.3.0", + "System.ValueTuple": "4.3.1", "Win2D.uwp": "1.20.0" }, "frameworks": {