Esempio n. 1
0
        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            var scrollViewer = ScrollingHost as ScrollViewer;

            if (scrollViewer == null && ScrollingHost != null)
            {
                scrollViewer = ScrollingHost.Descendants <ScrollViewer>().FirstOrDefault() as ScrollViewer;
            }

            if (scrollViewer == null)
            {
                return;
            }

            var props = ElementCompositionPreview.GetScrollViewerManipulationPropertySet(scrollViewer);

            if (props == null)
            {
                return;
            }

            if (VerticalAlignment == VerticalAlignment.Top)
            {
                _animation = _compositor.CreateExpressionAnimation("Max(Scroll.Translation.Y, 0)");
                _animation.SetReferenceParameter("Scroll", props);

                _visual.StopAnimation("Size.Y");
                _visual.StartAnimation("Size.Y", _animation);
            }
        }
Esempio n. 2
0
        private void GridView_Loaded(object sender, RoutedEventArgs e)
        {
            var scroll = List.Descendants <ScrollViewer>().FirstOrDefault() as ScrollViewer;

            if (scroll != null)
            {
                _scrollingHost = scroll;
                _scrollingHost.ChangeView(null, 0, null, true);
                scroll.ViewChanged += Scroll_ViewChanged;
                Scroll_ViewChanged(scroll, null);

                _scrollingHost.Padding = new Thickness(0, Math.Max(0, BackgroundPanel.Margin.Top - _scrollingHost.VerticalOffset), 0, 0);

                var brush = App.Current.Resources["SystemControlBackgroundChromeMediumLowBrush"] as SolidColorBrush;
                var props = ElementCompositionPreview.GetScrollViewerManipulationPropertySet(scroll);

                if (_background == null)
                {
                    _background = ElementCompositionPreview.GetElementVisual(BackgroundPanel).Compositor.CreateSpriteVisual();
                    ElementCompositionPreview.SetElementChildVisual(BackgroundPanel, _background);
                }

                _background.Brush = _background.Compositor.CreateColorBrush(brush.Color);
                _background.Size  = new System.Numerics.Vector2((float)BackgroundPanel.ActualWidth, (float)BackgroundPanel.ActualHeight);
                _background.Clip  = _background.Compositor.CreateInsetClip();

                _groupHeader = ElementCompositionPreview.GetElementVisual(GroupHeader);

                _expression = _expression ?? _background.Compositor.CreateExpressionAnimation("Max(Maximum, Scrolling.Translation.Y)");
                _expression.SetReferenceParameter("Scrolling", props);
                _expression.SetScalarParameter("Maximum", -(float)BackgroundPanel.Margin.Top + 1);
                _background.StopAnimation("Offset.Y");
                _background.StartAnimation("Offset.Y", _expression);

                _expressionHeader = _expressionHeader ?? _background.Compositor.CreateExpressionAnimation("Max(0, Maximum - Scrolling.Translation.Y)");
                _expressionHeader.SetReferenceParameter("Scrolling", props);
                _expressionHeader.SetScalarParameter("Maximum", -(float)BackgroundPanel.Margin.Top);
                _groupHeader.StopAnimation("Offset.Y");
                _groupHeader.StartAnimation("Offset.Y", _expressionHeader);

                _expressionClip = _expressionClip ?? _background.Compositor.CreateExpressionAnimation("Min(0, Maximum - Scrolling.Translation.Y)");
                _expressionClip.SetReferenceParameter("Scrolling", props);
                _expressionClip.SetScalarParameter("Maximum", -(float)BackgroundPanel.Margin.Top + 1);
                _background.Clip.StopAnimation("Offset.Y");
                _background.Clip.StartAnimation("Offset.Y", _expressionClip);
            }

            var panel = List.ItemsPanelRoot as ItemsWrapGrid;

            if (panel != null)
            {
                panel.SizeChanged += (s, args) =>
                {
                    Scroll_ViewChanged(scroll, null);
                };
            }
        }
Esempio n. 3
0
 /// <summary>
 /// Starts the animation to follow the pointer position
 /// </summary>
 /// <param name="sender">Pointer</param>
 /// <param name="e">PointerRoutedEventArgs</param>
 private void OnPointerReleased(object sender, PointerRoutedEventArgs e)
 {
     _dragVisual.StopAnimation(() => _dragVisual.Offset);
 }
Esempio n. 4
0
        public static async Task <AttachedStaticCompositionEffect <T> > AttachCompositionInAppCustomAcrylicEffectAsync <TSource, T>(
            [NotNull] this TSource element, [NotNull] T target, float blur, int ms, Color color, float colorMix, float?saturation,
            [CanBeNull] CanvasControl canvas, [NotNull] Uri uri, int timeThreshold = 1000, bool reload = false, bool fadeIn = false, bool disposeOnUnload = false)
            where TSource : FrameworkElement
            where T : FrameworkElement
        {
            // Percentage check
            if (saturation < 0 || saturation > 1)
            {
                throw new ArgumentOutOfRangeException("The input saturation value must be in the [0,1] range");
            }
            if (colorMix <= 0 || colorMix >= 1)
            {
                throw new ArgumentOutOfRangeException("The mix factors must be in the [0,1] range");
            }
            if (timeThreshold <= 0)
            {
                throw new ArgumentOutOfRangeException("The time threshold must be a positive number");
            }

            // Setup the compositor
            Visual     visual     = ElementCompositionPreview.GetElementVisual(element);
            Compositor compositor = visual.Compositor;

            // Prepare a luminosity to alpha effect to adjust the background contrast
            CompositionBackdropBrush backdropBrush = compositor.CreateBackdropBrush();
            const String
                blurName                  = "Blur",
                blurParameterName         = "Blur.BlurAmount";
            GaussianBlurEffect blurEffect = new GaussianBlurEffect
            {
                Name         = blurName,
                BlurAmount   = 0f,
                BorderMode   = EffectBorderMode.Hard,
                Optimization = EffectOptimization.Balanced,
                Source       = new CompositionEffectSourceParameter(nameof(backdropBrush))
            };

            // Background with blur and tint overlay
            IDictionary <String, CompositionBrush> sourceParameters = new Dictionary <String, CompositionBrush>
            {
                { nameof(backdropBrush), backdropBrush }
            };

            // Get the noise brush using Win2D
            IGraphicsEffect source = await AcrylicEffectHelper.ConcatenateEffectWithTintAndBorderAsync(compositor,
                                                                                                       blurEffect, sourceParameters, color, colorMix, canvas, uri, timeThreshold, reload);

            // Add the final saturation effect if needed
            if (saturation != null)
            {
                SaturationEffect saturationEffect = new SaturationEffect
                {
                    Saturation = saturation.Value,
                    Source     = source
                };
                source = saturationEffect;
            }

            // Make sure the Win2D brush was loaded correctly
            CompositionEffectFactory factory = compositor.CreateEffectFactory(source, new[] { blurParameterName });

            // Create the effect factory and apply the final effect
            CompositionEffectBrush effectBrush = factory.CreateBrush();

            foreach (KeyValuePair <String, CompositionBrush> pair in sourceParameters)
            {
                effectBrush.SetSourceParameter(pair.Key, pair.Value);
            }

            // Create the sprite to display and add it to the visual tree
            SpriteVisual sprite = compositor.CreateSpriteVisual();

            sprite.Brush = effectBrush;

            // Assign the visual
            if (fadeIn)
            {
                sprite.StopAnimation("Opacity");
                sprite.Opacity = 0;
            }
            await AddToTreeAndBindSizeAsync(target.GetVisual(), target, sprite);

            if (fadeIn)
            {
                // Fade the effect in
                ScalarKeyFrameAnimation opacityAnimation = sprite.Compositor.CreateScalarKeyFrameAnimation(1, 0,
                                                                                                           TimeSpan.FromMilliseconds(ms), null, sprite.GetEasingFunction(EasingFunctionNames.Linear));
                sprite.StartAnimation("Opacity", opacityAnimation);
            }

            // Animate the blur and return the result
            effectBrush.StartAnimationAsync(blurParameterName, blur, TimeSpan.FromMilliseconds(ms)).Forget();
            return(new AttachedStaticCompositionEffect <T>(target, sprite, disposeOnUnload));
        }
Esempio n. 5
0
        private void AnimateCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (_compositor == null)
            {
                return;
            }

            string selected = (string)AnimateCombo.SelectedValue;

            if (selected == "None")
            {
                _sprite.StopAnimation("RotationAngleInDegrees");
                _sprite.RotationAngleInDegrees = 0;
            }
            else if (selected == "X Rotation")
            {
                // Rotate the preview surface around the X axis
                var animation = _compositor.CreateScalarKeyFrameAnimation();
                animation.IterationBehavior = AnimationIterationBehavior.Forever;
                animation.InsertKeyFrame(0, 0);
                animation.InsertKeyFrame(.25f, 90, _compositor.CreateLinearEasingFunction());
                animation.InsertKeyFrame(.75f, -90, _compositor.CreateLinearEasingFunction());
                animation.InsertKeyFrame(1f, 0, _compositor.CreateLinearEasingFunction());
                animation.Duration = TimeSpan.FromSeconds(20);

                _sprite.RotationAxis = new Vector3(1, 0, 0);
                _sprite.StartAnimation("RotationAngleInDegrees", animation);
            }
            else if (selected == "Y Rotation")
            {
                // Rotate the preview surface around the Y axis
                var animation = _compositor.CreateScalarKeyFrameAnimation();
                animation.IterationBehavior = AnimationIterationBehavior.Forever;
                animation.InsertKeyFrame(0, 0);
                animation.InsertKeyFrame(.25f, 90, _compositor.CreateLinearEasingFunction());
                animation.InsertKeyFrame(.75f, -90, _compositor.CreateLinearEasingFunction());
                animation.InsertKeyFrame(1f, 0, _compositor.CreateLinearEasingFunction());
                animation.Duration = TimeSpan.FromSeconds(20);

                _sprite.RotationAxis = new Vector3(0, 1, 0);
                _sprite.StartAnimation("RotationAngleInDegrees", animation);
            }
            else if (selected == "Z Rotation")
            {
                // Rotate the preview surface around the Z axis
                var animation = _compositor.CreateScalarKeyFrameAnimation();
                animation.IterationBehavior = AnimationIterationBehavior.Forever;
                animation.InsertKeyFrame(0, 0);
                animation.InsertKeyFrame(.25f, 90, _compositor.CreateLinearEasingFunction());
                animation.InsertKeyFrame(.75f, -90, _compositor.CreateLinearEasingFunction());
                animation.InsertKeyFrame(1f, 0, _compositor.CreateLinearEasingFunction());
                animation.Duration = TimeSpan.FromSeconds(20);

                _sprite.RotationAxis = new Vector3(0, 0, 1);
                _sprite.StartAnimation("RotationAngleInDegrees", animation);
            }
            else
            {
                Debug.Assert(false);
            }
        }