public SelfDestructTimer()
        {
            DefaultStyleKey = typeof(SelfDestructTimer);
            Visibility      = Visibility.Collapsed;

            var ellipse = Window.Current.Compositor.CreateEllipseGeometry();

            ellipse.Radius = new Vector2((float)Radius);
            ellipse.Center = new Vector2((float)Center);

            var shape = Window.Current.Compositor.CreateSpriteShape(ellipse);

            shape.CenterPoint     = new Vector2((float)Center);
            shape.StrokeThickness = 2;
            shape.StrokeStartCap  = CompositionStrokeCap.Round;
            shape.StrokeEndCap    = CompositionStrokeCap.Round;

            if (Foreground is SolidColorBrush brush)
            {
                shape.StrokeBrush = Window.Current.Compositor.CreateColorBrush(brush.Color);
            }

            var visual = Window.Current.Compositor.CreateShapeVisual();

            visual.Shapes.Add(shape);
            visual.Size        = new Vector2((float)Center * 2);
            visual.CenterPoint = new Vector3((float)Center);

            _visual  = visual;
            _shape   = shape;
            _ellipse = ellipse;

            ElementCompositionPreview.SetElementChildVisual(this, visual);
            RegisterPropertyChangedCallback(ForegroundProperty, OnForegroundChanged);
        }
Exemple #2
0
        private void StartClipAnimation(CompositionEllipseGeometry ellipseGeometry, TimeSpan delayTime)
        {
            var compositor = Window.Current.Compositor;
            var animation  = compositor.CreateVector2KeyFrameAnimation();
            var easing     = compositor.CreateCubicBezierEasingFunction(new Vector2(0.1f, 0.9f), new Vector2(0.2f, 1f));

            animation.DelayTime = delayTime;
            animation.Duration  = TimeSpan.FromSeconds(0.7);
            animation.InsertKeyFrame(1, new Vector2(600, 600));
            ellipseGeometry.StartAnimation(nameof(CompositionEllipseGeometry.Radius), animation);
        }
Exemple #3
0
        XElement FromCompositionEllipseGeometry(CompositionEllipseGeometry obj)
        {
            return(new XElement(GetCompositionObjectName(obj), GetContents()));

            IEnumerable <XObject> GetContents()
            {
                foreach (var item in GetCompositionGeometryContents(obj))
                {
                    yield return(item);
                }

                yield return(FromVector2(nameof(obj.Center), obj.Center));

                yield return(FromVector2(nameof(obj.Radius), obj.Radius));
            }
        }
Exemple #4
0
        CompositionEllipseGeometry GetCompositionEllipseGeometry(CompositionEllipseGeometry obj)
        {
            if (GetExisting(obj, out CompositionEllipseGeometry result))
            {
                return(result);
            }

            result = CacheAndInitializeCompositionGeometry(obj, _c.CreateEllipseGeometry());
            if (obj.Center.X != 0 || obj.Center.Y != 0)
            {
                result.Center = obj.Center;
            }

            result.Radius = obj.Radius;
            StartAnimationsAndFreeze(obj, result);
            return(result);
        }
Exemple #5
0
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            // create the compositor
            _compositor = ElementCompositionPreview.GetElementVisual(this).Compositor;

            // create what captures the pointer position
            _hoverPositionPropertySet = ElementCompositionPreview.GetPointerPositionPropertySet(r2);

            // create the two visuals
            _vis      = _compositor.CreateSpriteVisual();
            _pulseVis = _compositor.CreateSpriteVisual();

            // create the main brush with warm colors
            _mainBrush = _compositor.CreateRadialGradientBrush();
            _mainBrush.EllipseCenter = new Vector2(.5f, .5f);
            _mainBrush.EllipseRadius = new Vector2(.5f, .5f);

            _MBGradientStop1        = _compositor.CreateColorGradientStop();
            _MBGradientStop1.Offset = 0;
            _MBGradientStop1.Color  = _warmColor1;

            _MBGradientStop2        = _compositor.CreateColorGradientStop();
            _MBGradientStop2.Offset = .1f;
            _MBGradientStop2.Color  = _warmColor2;

            _MBGradientStop3        = _compositor.CreateColorGradientStop();
            _MBGradientStop3.Offset = 1;
            _MBGradientStop3.Color  = _warmColor3;

            _mainBrush.ColorStops.Add(_MBGradientStop1);
            _mainBrush.ColorStops.Add(_MBGradientStop2);
            _mainBrush.ColorStops.Add(_MBGradientStop3);

            // create the brush for the pulse visual
            _pulseBrush = _compositor.CreateRadialGradientBrush();
            _pulseBrush.EllipseCenter = new Vector2(.5f, .5f);
            _pulseBrush.EllipseRadius = new Vector2(.5f, .5f);

            _PBGradientStop1        = _compositor.CreateColorGradientStop();
            _PBGradientStop1.Offset = 0;
            _PBGradientStop1.Color  = _innerPulseColor;

            _PBGradientStop2        = _compositor.CreateColorGradientStop();
            _PBGradientStop2.Offset = 1;
            _PBGradientStop2.Color  = _innerPulseColor;

            _pulseBrush.ColorStops.Add(_PBGradientStop1);
            _pulseBrush.ColorStops.Add(_PBGradientStop2);

            // finish setting properties of the first visual
            _vis.Size        = new Vector2(300, 300);
            _vis.Offset      = new Vector3(((float)r2.ActualWidth / 2), ((float)r2.ActualHeight / 2), 0);
            _vis.AnchorPoint = new Vector2(.5f, .5f);
            _vis.Brush       = _mainBrush;

            // finish setting properties of the pulsing visual
            _pulseVis.Size        = new Vector2(500, 500);
            _pulseVis.Offset      = new Vector3(((float)r1.ActualWidth / 2), ((float)r1.ActualHeight / 2), 0);
            _pulseVis.AnchorPoint = new Vector2(.5f, .5f);
            _pulseVis.Brush       = _pulseBrush;

            // create the clip that makes the visuals circular
            CompositionGeometricClip   gClip  = _compositor.CreateGeometricClip();
            CompositionEllipseGeometry circle = _compositor.CreateEllipseGeometry();

            circle.Radius  = new Vector2(_vis.Size.X / 2, _vis.Size.Y / 2);
            circle.Center  = new Vector2(_vis.Size.X / 2, _vis.Size.Y / 2);
            gClip.Geometry = circle;

            _vis.Clip = gClip;

            CompositionGeometricClip   gClip2  = _compositor.CreateGeometricClip();
            CompositionEllipseGeometry circle2 = _compositor.CreateEllipseGeometry();

            circle2.Radius  = new Vector2(_pulseVis.Size.X / 2, _pulseVis.Size.Y / 2);
            circle2.Center  = new Vector2(_pulseVis.Size.X / 2, _pulseVis.Size.Y / 2);
            gClip2.Geometry = circle2;

            _pulseVis.Clip = gClip2;

            // set the pointer
            my_pointer = new Vector3(((float)r1.ActualWidth / 2), ((float)r1.ActualHeight / 2), 0);

            // set the visuals in the tree
            ElementCompositionPreview.SetElementChildVisual(r2, _vis);
            ElementCompositionPreview.SetElementChildVisual(r1, _pulseVis);

            // ellipse center follows mouse
            _ellipseCenterAnim = _compositor.CreateExpressionAnimation("Vector2(p.Position.X / 500.0f, p.Position.Y / 500.0f)");
            _ellipseCenterAnim.SetReferenceParameter("p", _hoverPositionPropertySet);
            _mainBrush.StartAnimation("EllipseCenter", _ellipseCenterAnim);

            // second stop is animated for "pulsing" effect within the first visual that runs constantly
            _offsetAnim = _compositor.CreateScalarKeyFrameAnimation();
            _offsetAnim.InsertKeyFrame(0, 0);
            _offsetAnim.InsertKeyFrame(1f, 1f);
            _offsetAnim.Duration       = TimeSpan.FromSeconds(2);
            _offsetAnim.IterationCount = 50;

            _MBGradientStop2.StartAnimation("Offset", _offsetAnim);

            // set up the animation for the backing pulse visual
            // animate the color
            _pulseColorAnim = _compositor.CreateColorKeyFrameAnimation();
            _pulseColorAnim.InsertKeyFrame(0, _innerPulseColor);
            _pulseColorAnim.InsertKeyFrame(.99f, _outerPulseColor);
            _pulseColorAnim.InsertKeyFrame(1, _innerPulseColor);
            _pulseColorAnim.Duration          = TimeSpan.FromSeconds(1);
            _pulseColorAnim.IterationBehavior = AnimationIterationBehavior.Forever;

            _PBGradientStop1.StartAnimation("Color", _pulseColorAnim);

            // animate offset of first stop
            _pulseStop1OffsetAnim = _compositor.CreateScalarKeyFrameAnimation();
            _pulseStop1OffsetAnim.InsertKeyFrame(0, 0);
            _pulseStop1OffsetAnim.InsertKeyFrame(1f, 1f);
            _pulseStop1OffsetAnim.Duration          = TimeSpan.FromSeconds(1);
            _pulseStop1OffsetAnim.IterationBehavior = AnimationIterationBehavior.Forever;

            _PBGradientStop1.StartAnimation("Offset", _pulseStop1OffsetAnim);

            // animate offset of second stop
            _pulseStop2OffsetAnim = _compositor.CreateScalarKeyFrameAnimation();
            _pulseStop2OffsetAnim.InsertKeyFrame(0, 0);
            _pulseStop2OffsetAnim.InsertKeyFrame(1f, 1f);
            _pulseStop2OffsetAnim.Duration          = TimeSpan.FromSeconds(1);
            _pulseStop2OffsetAnim.IterationBehavior = AnimationIterationBehavior.Forever;
            _pulseStop2OffsetAnim.DelayTime         = TimeSpan.FromSeconds(.25f);

            _PBGradientStop2.StartAnimation("Offset", _pulseStop2OffsetAnim);

            _pulseScaleAnim = _compositor.CreateVector3KeyFrameAnimation();
            _pulseScaleAnim.InsertKeyFrame(0, Vector3.Zero);
            _pulseScaleAnim.InsertKeyFrame(1, Vector3.One);
            _pulseScaleAnim.Duration          = TimeSpan.FromSeconds(1);
            _pulseScaleAnim.IterationBehavior = AnimationIterationBehavior.Forever;

            _pulseVis.StartAnimation("Scale", _pulseScaleAnim);
        }
Exemple #6
0
        private void RenderPickerZone(bool force = false, bool hide = false)
        {
            var uc = SelectedColor.GetUniColor();

            if (_container == null)
            {
                return;
            }

            if (selCoord == null || uc == UniColor.Empty || force || hide)
            {
                if (currentShape != null)
                {
                    try
                    {
                        if (_container.Children.Contains(currentShape))
                        {
                            _container.Children.Remove(currentShape);
                        }
                    }
                    catch
                    {
                    }

                    currentShape  = null;
                    currentGeo    = null;
                    currentSprite = null;
                    GC.Collect(0);
                }

                if (selCoord == null || uc == UniColor.Empty || hide)
                {
                    return;
                }
            }

            var pt = (UniPoint)selCoord;

            uc = ((uint)uc) ^ 0x00ffffff;

            var comp = _container.Compositor;

            if (currentShape != null)
            {
                currentSprite.StrokeBrush = comp.CreateColorBrush(uc.GetWinUIColor());
                currentShape.Offset       = new Vector3((float)pt.X - 10, (float)pt.Y - 10, 0);

                return;
            }

            var ellipseGeo = comp.CreateEllipseGeometry();

            ellipseGeo.Radius = new Vector2(8f, 8f);

            var spriteShape = comp.CreateSpriteShape(ellipseGeo);

            ellipseGeo.Center = new Vector2(10f, 10f);

            spriteShape.StrokeThickness = 1.35f;
            spriteShape.StrokeBrush     = comp.CreateColorBrush(uc.GetWinUIColor());

            var shapeVisual = comp.CreateShapeVisual();

            shapeVisual.CompositeMode = CompositionCompositeMode.DestinationInvert;
            shapeVisual.Shapes.Add(spriteShape);

            shapeVisual.Offset = new Vector3((float)pt.X - 10, (float)pt.Y - 10, 0);
            shapeVisual.Size   = new Vector2(20f, 20f);

            _container.Children.InsertAtTop(shapeVisual);

            currentShape  = shapeVisual;
            currentSprite = spriteShape;
            currentGeo    = ellipseGeo;
        }
Exemple #7
0
        private void RenderPicker(int w = 0, int h = 0)
        {
            if (_container == null)
            {
                return;
            }

            double width  = w > 0 ? w : this.Width;
            double height = h > 0 ? h : this.Height;

            if (width == -1 || height == -1)
            {
                return;
            }

            double colorVal = this.ColorValue;
            double offset   = this.HueOffset;
            bool   invert   = this.InvertSaturation;
            float  esize    = this.ElementSize;

            ColorPickerMode mode = this.Mode;

            _ = Task.Run(() =>
            {
                ColorPickerRenderer cw;

                if (w <= 0)
                {
                    if (double.IsNaN(width))
                    {
                        return;
                    }
                    w = (int)width;
                }
                if (h <= 0)
                {
                    if (double.IsNaN(height))
                    {
                        return;
                    }
                    h = (int)height;
                }

                if (w < 32 || h < 32)
                {
                    return;
                }

                if (mode == ColorPickerMode.Wheel || mode == ColorPickerMode.HexagonWheel)
                {
                    int rad;

                    if (h < w)
                    {
                        rad = h / 2;
                        w   = h;
                    }
                    else
                    {
                        rad = w / 2;
                        h   = w;
                    }

                    cw = new ColorPickerRenderer(rad, colorVal, offset, invert, true);
                }
                else
                {
                    cw = new ColorPickerRenderer(w, h, colorVal, offset, invert, mode == ColorPickerMode.LinearVertical, true);
                }

                DispatcherQueue.TryEnqueue(async() =>
                {
                    SKImage img;
                    SKBitmap bmp = new SKBitmap((int)cw.Bounds.Width, (int)cw.Bounds.Height, SKColorType.Bgra8888, SKAlphaType.Premul);

                    var ptr = bmp.GetPixels();

                    unsafe
                    {
                        var gch = GCHandle.Alloc(cw.ImageBytes, GCHandleType.Pinned);

                        Buffer.MemoryCopy((void *)gch.AddrOfPinnedObject(), (void *)ptr, cw.ImageBytes.Length, cw.ImageBytes.Length);
                        gch.Free();
                    }

                    bmp.SetImmutable();
                    img = SKImage.FromBitmap(bmp);

                    SKData encoded = img.Encode();
                    Stream stream  = encoded.AsStream();

                    //var ret = ImageSource.FromStream(() => stream);

                    cpRender = cw;


                    Compositor _compositor = _container.Compositor;
                    SpriteVisual _imageVisual;
                    CompositionSurfaceBrush _imageBrush;

                    _imageBrush = _compositor.CreateSurfaceBrush();

                    // The loadedSurface has a size of 0x0 till the image has been been downloaded, decoded and loaded to the surface. We can assign the surface to the CompositionSurfaceBrush and it will show up once the image is loaded to the surface.
                    LoadedImageSurface _loadedSurface = LoadedImageSurface.StartLoadFromStream(stream.AsRandomAccessStream());

                    _imageBrush.Surface = _loadedSurface;

                    _imageVisual       = _compositor.CreateSpriteVisual();
                    _imageVisual.Brush = _imageBrush;
                    _imageVisual.Size  = new Vector2(cw.Bounds.Width, cw.Bounds.Height);
                    //_imageVisual.Offset = new Vector3((float)Padding.Left, (float)Padding.Top, 0);

                    _container.Children.RemoveAll();
                    _container.Children.InsertAtBottom(_imageVisual);

                    currentShape  = null;
                    currentSprite = null;
                    currentGeo    = null;
                });
            });
        }
        public ProgressBarRing()
        {
            DefaultStyleKey = typeof(ProgressBarRing);

            var ellipse = Window.Current.Compositor.CreateEllipseGeometry();

            ellipse.Radius  = new Vector2((float)Radius);
            ellipse.Center  = new Vector2((float)Center);
            ellipse.TrimEnd = 0f;

            var shape = Window.Current.Compositor.CreateSpriteShape(ellipse);

            shape.CenterPoint     = new Vector2((float)Center);
            shape.StrokeThickness = 2;
            shape.StrokeStartCap  = CompositionStrokeCap.Round;
            shape.StrokeEndCap    = CompositionStrokeCap.Round;

            if (Foreground is SolidColorBrush brush)
            {
                shape.StrokeBrush = Window.Current.Compositor.CreateColorBrush(brush.Color);
            }

            var visual = Window.Current.Compositor.CreateShapeVisual();

            visual.Shapes.Add(shape);
            visual.Size        = new Vector2((float)Center * 2);
            visual.CenterPoint = new Vector3((float)Center);

            var trimEnd = Window.Current.Compositor.CreateScalarKeyFrameAnimation();

            trimEnd.Target = nameof(CompositionGeometry.TrimEnd);
            trimEnd.InsertExpressionKeyFrame(1.0f, "this.FinalValue", Window.Current.Compositor.CreateLinearEasingFunction());

            var visibility = Window.Current.Compositor.CreateExpressionAnimation("target.TrimEnd > 0 && target.TrimEnd < 1");

            visibility.SetReferenceParameter("target", ellipse);

            var animations = Window.Current.Compositor.CreateImplicitAnimationCollection();

            animations[nameof(CompositionGeometry.TrimEnd)] = trimEnd;

            ellipse.ImplicitAnimations = animations;
            visual.StartAnimation("IsVisible", visibility);
            //visual.StartAnimation("RotationAngleInDegrees", forever);

            _visual  = visual;
            _shape   = shape;
            _ellipse = ellipse;

            var easing  = Window.Current.Compositor.CreateLinearEasingFunction();
            var forever = Window.Current.Compositor.CreateScalarKeyFrameAnimation();

            forever.InsertKeyFrame(1, 360, easing);
            forever.IterationBehavior = AnimationIterationBehavior.Forever;
            forever.Duration          = TimeSpan.FromSeconds(3);

            _foreverAnimation = forever;

            ElementCompositionPreview.SetElementChildVisual(this, visual);
            RegisterPropertyChangedCallback(ForegroundProperty, OnForegroundChanged);

            Loaded   += OnLoaded;
            Unloaded += OnUnloaded;
        }