Пример #1
0
        public override void Render(IDrawingContextImpl drw_context)
        {
            if (drw_context is ISkiaDrawingContextImpl context)
            {
                var canvas = context.SkCanvas;

                canvas.Save();

                var    info = new SKImageInfo((int)Bounds.Width, (int)Bounds.Height);
                var    s_r  = stroke / 2;
                SKRect rect = new SKRect(s_r, s_r, info.Height - s_r, info.Width - s_r);

                using (var paint = new SKPaint())
                {
                    paint.Shader      = SKShader.CreateColor(StrokeColor.ToSKColor());
                    paint.Style       = SKPaintStyle.Stroke;
                    paint.StrokeWidth = stroke;
                    paint.IsAntialias = true;
                    paint.Color       = StrokeColor.ToSKColor();
                    canvas.DrawArc(rect, _angle1, _angle2, false, paint);
                }

                canvas.Restore();

                Debug.WriteLine(StrokeColor.ToString());
                Debug.WriteLine("Arc rendered");
            }
        }
 public override void Render(SKCanvas canvas, SKRect bounds, SKPaint paint)
 {
     paint.Shader = SKShader.CreateColor(_currentColor);
     canvas.DrawRect(bounds, paint);
     paint.Shader?.Dispose();
     paint.Shader = null;
 }
Пример #3
0
        public void Draw(SKCanvas canvas, float scale)
        {
            var screenDimensions = _drawingConfig.ScreenDimensions(scale);

            var sliderCenter = new SKPoint(_slider.Position.X * scale, _slider.Position.Y * scale);

            var radiusScale = !_slider.IsSelected ? 1f : 1.1f;
            var radius      = _slider.Radius * radiusScale * scale;

            canvas.DrawCircle(sliderCenter, radius + screenDimensions.BetweenCirclesMargin, new SKPaint()
            {
                Shader = SKShader.CreateColor(_drawingConfig.BackgroundColor), IsStroke = false, IsAntialias = true
            });
            canvas.DrawCircle(sliderCenter, radius, new SKPaint()
            {
                Shader = SKShader.CreateColor(_slider.Color), IsStroke = false, IsAntialias = true
            });
            if (_slider.IsSelected)
            {
                canvas.DrawCircle(sliderCenter, radius / 2f, new SKPaint()
                {
                    Shader = SKShader.CreateColor(_drawingConfig.BackgroundColor), IsStroke = false, IsAntialias = true
                });
            }
        }
Пример #4
0
        /// <summary>
        /// Draw a textured background
        /// </summary>
        /// <param name="backgroundColor">the backgroud color</param>
        /// <param name="canvas">the canvas</param>
        /// <param name="bounds">the bounds</param>
        public static void DrawBackground(Color backgroundColor, SKCanvas canvas, SKRect bounds)
        {
            if (canvas == null)
            {
                throw new ArgumentNullException(nameof(canvas));
            }

            using var bitmap = new SKBitmap(128, 128);
            using (var bitmapCanvas = new SKCanvas(bitmap))
            {
                //bitmapCanvas.Clear(backgroundColor.ToSKColor());
                using var colorShader  = SKShader.CreateColor(backgroundColor.ToSKColor());
                using var perlinShader = SKShader.CreatePerlinNoiseFractalNoise(0.8f, 0.8f, 1, 1, new SKPointI(64, 64));
                using var paint        = new SKPaint
                      {
                          Color       = backgroundColor.ToSKColor(),
                          Shader      = SKShader.CreateCompose(colorShader, perlinShader),
                          IsAntialias = true,
                          IsStroke    = false
                      };
                bitmapCanvas.DrawRect(new SKRect(0, 0, 128, 128), paint);
            }

            using var paint2 = new SKPaint
                  {
                      Shader      = SKShader.CreateBitmap(bitmap, SKShaderTileMode.Repeat, SKShaderTileMode.Repeat),
                      IsStroke    = false,
                      IsAntialias = true
                  };
            canvas.Clear(Color.Transparent.ToSKColor());

            canvas.DrawRect(bounds, paint2);
        }
            private Texture2D getOrCreateTexture(GraphicsDevice device)
            {
                if (this.bitmap is null || this.texture is null)
                {
                    this.bitmap?.Dispose();
                    this.texture?.Dispose();

                    this.bitmap = new SKBitmap(this.mBounds.Width, this.mBounds.Height);

                    using var ms            = new MemoryStream();
                    using var canvas        = new SKCanvas(this.bitmap);
                    using var white_skpaint = new SKPaint()
                          {
                              Shader = SKShader.CreateLinearGradient(new SKPoint(0.0f, 0.0f), new SKPoint(this.mBounds.Width, 0.0f), new SKColor[] { SKColors.Transparent, SKColors.White }, SKShaderTileMode.Decal)
                          };
                    using var black_skpaint = new SKPaint()
                          {
                              Shader = SKShader.CreateLinearGradient(new SKPoint(0.0f, 0.0f), new SKPoint(this.mBounds.Height, 0.0f), new SKColor[] { SKColors.Transparent, new SKColor(1, 1, 1, 255) }, SKShaderTileMode.Decal)
                          };
                    using var colour_skpaint = new SKPaint()
                          {
                              Shader = SKShader.CreateColor(this.hueColour.AsSKColor())
                          };

                    canvas.DrawPaint(colour_skpaint);
                    canvas.DrawPaint(white_skpaint);
                    canvas.RotateDegrees(90.0f);
                    canvas.DrawPaint(black_skpaint);

                    this.bitmap.Encode(ms, SKEncodedImageFormat.Png, 95);
                    this.texture = Texture2D.FromStream(device, ms);
                }

                return(this.texture);
            }
Пример #6
0
        private void CreateSolid()
        {
            _color = Properties.Color.CurrentValue;

            _shader?.Dispose();
            _shader = SKShader.CreateColor(_color);
            UpdatePaint();
        }
Пример #7
0
 private SKShader Shader(float scale)
 {
     if (_color != null)
     {
         return(SKShader.CreateColor(_color.Value));
     }
     if (_colorPositions != null)
     {
         return(SKShader.CreateLinearGradient(_p0.Multiply(scale), _p1.Multiply(scale), _colorPositions.Select(cp => cp.Color).ToArray(), _colorPositions.Select(cp => cp.Position).ToArray(), SKShaderTileMode.Clamp));
     }
     return(SKShader.CreateColor(SKColors.Red));
 }
Пример #8
0
        public override void Render(IDrawingContextImpl context)
        {
            var canvas  = (context as ISkiaDrawingContextImpl)?.SkCanvas;
            var surface = (context as ISkiaDrawingContextImpl)?.SkSurface;

            if (canvas == null | surface == null)
            {
                context.Clear(Colors.White);
                context.DrawText(new SolidColorBrush(Colors.Black), new Point(), NoSkia);
            }
            else
            {
                Surface = surface;
                var width  = (int)Bounds.Width;
                var height = (int)Bounds.Height;

                var info = new SKImageInfo(width, height);

                using (SKPaint paint = new SKPaint())
                {
                    SKColor[] colors = { SKColors.White, HueColor.ToSKColor() };

                    SKPoint center = new SKPoint(info.Rect.MidX, info.Rect.MidY);

                    paint.Shader = SKShader.CreateLinearGradient(new SKPoint(0, 0), new SKPoint(width, 0), colors, SKShaderTileMode.Repeat);

                    var rect = SKRect.Create(width, height);
                    canvas.DrawRect(rect, paint);
                }

                // Creates the black gradient effect (transparent to black)
                using (SKPaint paint = new SKPaint())
                {
                    SKColor[] colors = { SKColors.Transparent, SKColors.Black };

                    paint.Shader = SKShader.CreateLinearGradient(new SKPoint(0, 0), new SKPoint(0, height), colors, SKShaderTileMode.Repeat);

                    var rect = SKRect.Create(width, height);

                    canvas.DrawRect(rect, paint);
                }

                using (SKPaint paint = new SKPaint())
                {
                    paint.Shader = SKShader.CreateColor(StrokeColor.ToSKColor());
                    var rect = SKRect.Create(width, height);
                    paint.Style       = SKPaintStyle.Stroke;
                    paint.StrokeWidth = StrokeWidth;
                    canvas.DrawRect(rect, paint);
                }
            }
        }
Пример #9
0
        /// <inheritdoc />
        public override void Render(SKCanvas canvas, SKRect bounds, SKPaint paint)
        {
            paint.Shader = Properties.ColorMode.CurrentValue switch
            {
                SolidBrushColorMode.Static => SKShader.CreateColor(Properties.Color),
                SolidBrushColorMode.GradientPosition => SKShader.CreateColor(Properties.Colors.CurrentValue.GetColor(Properties.GradientPosition / 100f)),
                SolidBrushColorMode.GradientAnimation => SKShader.CreateColor(Properties.Colors.CurrentValue.GetColor(_animationPosition)),
                _ => SKShader.CreateColor(Properties.Color)
            };

            canvas.DrawRect(bounds, paint);
            paint.Shader?.Dispose();
            paint.Shader = null;
        }
Пример #10
0
        /// <inheritdoc />
        public override void Render(SKCanvas canvas, SKRect bounds, SKPaint paint)
        {
            if (Properties.EnableColorAnimation)
            {
                paint.Shader = SKShader.CreateColor(Properties.Colors.CurrentValue.GetColor(_position));
            }
            else
            {
                paint.Shader = SKShader.CreateColor(Properties.Color);
            }

            canvas.DrawRect(bounds, paint);
            paint.Shader?.Dispose();
            paint.Shader = null;
        }
        public override void Render(SKCanvas canvas, SKRect bounds, SKPaint paint)
        {
            if (rect != bounds)
            {
                rect = bounds;
            }

            foreach (Particle particle in _particles)
            {
                //handle color modes here.
                switch (Properties.Color.ColorMode.CurrentValue)
                {
                case ParticleColorMode.Lifetime:
                    paint.Shader = SKShader.CreateColor(Properties.Color.Gradient.CurrentValue.GetColor((float)particle.LifetimePercent));
                    break;

                case ParticleColorMode.Static:
                    paint.Shader = SKShader.CreateColor(Properties.Color.Color);
                    break;

                case ParticleColorMode.Sweep:
                    paint.Shader = SKShader.CreateSweepGradient(particle.Position, Properties.Color.Gradient.CurrentValue.GetColorsArray());
                    break;

                case ParticleColorMode.Radial:
                    paint.Shader = SKShader.CreateRadialGradient(particle.Position, particle.Radius, Properties.Color.Gradient.CurrentValue.GetColorsArray(), SKShaderTileMode.Clamp);
                    break;

                default:
                    break;
                }
                canvas.DrawCircle(particle.Position, particle.Radius, paint);

                if (Properties.Trail.DrawTrail.CurrentValue)
                {
                    SKPoint normalized       = SKPoint.Normalize(particle.Velocity);
                    float   mult             = Properties.Trail.TrailLength.CurrentValue.GetValueAtPercent((float)particle.LifetimePercent);
                    SKPoint trailEndPosition = new SKPoint(
                        particle.Position.X - (normalized.X * mult),
                        particle.Position.Y - (normalized.Y * mult)
                        );
                    paint.StrokeWidth = Properties.Trail.TrailWidth.CurrentValue.GetValueAtPercent((float)particle.LifetimePercent);
                    canvas.DrawLine(particle.Position, trailEndPosition, paint);
                }
            }
        }
Пример #12
0
        internal PaintWrapper CreateAcrylicPaint(SKPaint paint, IExperimentalAcrylicMaterial material, bool disposePaint = false)
        {
            var paintWrapper = new PaintWrapper(paint, disposePaint);

            paint.IsAntialias = true;

            double opacity = _currentOpacity;

            var tintOpacity =
                material.BackgroundSource == AcrylicBackgroundSource.Digger ?
                material.TintOpacity : 1;

            const double noiseOpcity = 0.0225;

            var tintColor = material.TintColor;
            var tint      = new SKColor(tintColor.R, tintColor.G, tintColor.B, tintColor.A);

            if (s_acrylicNoiseShader == null)
            {
                using (var stream = typeof(DrawingContextImpl).Assembly.GetManifestResourceStream("Avalonia.Skia.Assets.NoiseAsset_256X256_PNG.png"))
                    using (var bitmap = SKBitmap.Decode(stream))
                    {
                        s_acrylicNoiseShader = SKShader.CreateBitmap(bitmap, SKShaderTileMode.Repeat, SKShaderTileMode.Repeat)
                                               .WithColorFilter(CreateAlphaColorFilter(noiseOpcity));
                    }
            }

            using (var backdrop = SKShader.CreateColor(new SKColor(material.MaterialColor.R, material.MaterialColor.G, material.MaterialColor.B, material.MaterialColor.A)))
                using (var tintShader = SKShader.CreateColor(tint))
                    using (var effectiveTint = SKShader.CreateCompose(backdrop, tintShader))
                        using (var compose = SKShader.CreateCompose(effectiveTint, s_acrylicNoiseShader))
                        {
                            paint.Shader = compose;

                            if (material.BackgroundSource == AcrylicBackgroundSource.Digger)
                            {
                                paint.BlendMode = SKBlendMode.Src;
                            }

                            return(paintWrapper);
                        }
        }
Пример #13
0
        void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
        {
            SKImageInfo info    = args.Info;
            SKSurface   surface = args.Surface;
            SKCanvas    canvas  = surface.Canvas;

            canvas.Clear();

            // Get values from sliders and stepper
            float baseFreqX = (float)Math.Pow(10, baseFrequencyXSlider.Value - 4);

            baseFrequencyXText.Text = String.Format("Base Frequency X = {0:F4}", baseFreqX);

            float baseFreqY = (float)Math.Pow(10, baseFrequencyYSlider.Value - 4);

            baseFrequencyYText.Text = String.Format("Base Frequency Y = {0:F4}", baseFreqY);

            int numOctaves = (int)octavesStepper.Value;

            using (SKPaint paint = new SKPaint())
            {
                paint.Shader = SKShader.CreateCompose(
                    SKShader.CreateColor(SKColors.Blue),
                    SKShader.CreatePerlinNoiseFractalNoise(baseFreqX,
                                                           baseFreqY,
                                                           numOctaves,
                                                           0));

                SKRect rect = new SKRect(0, 0, info.Width, info.Height / 2);
                canvas.DrawRect(rect, paint);

                paint.Shader = SKShader.CreateCompose(
                    SKShader.CreateColor(SKColors.Blue),
                    SKShader.CreatePerlinNoiseTurbulence(baseFreqX,
                                                         baseFreqY,
                                                         numOctaves,
                                                         0));

                rect = new SKRect(0, info.Height / 2, info.Width, info.Height);
                canvas.DrawRect(rect, paint);
            }
        }
Пример #14
0
        private void CreateShader()
        {
            var      center = new SKPoint(Layer.Rectangle.MidX, Layer.Rectangle.MidY);
            SKShader shader;

            switch (Settings.GradientType)
            {
            case GradientType.Solid:
                shader = SKShader.CreateColor(_testColors.First());
                break;

            case GradientType.LinearGradient:
                shader = SKShader.CreateLinearGradient(new SKPoint(0, 0), new SKPoint(Layer.Rectangle.Width, 0), _testColors.ToArray(), SKShaderTileMode.Repeat);
                break;

            case GradientType.RadialGradient:
                shader = SKShader.CreateRadialGradient(center, Math.Min(Layer.Rectangle.Width, Layer.Rectangle.Height), _testColors.ToArray(), SKShaderTileMode.Repeat);
                break;

            case GradientType.SweepGradient:
                shader = SKShader.CreateSweepGradient(center, _testColors.ToArray(), null, SKShaderTileMode.Clamp, 0, 360);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            var oldShader = _shader;
            var oldPaint  = _paint;

            _shader = shader;
            _paint  = new SKPaint {
                Shader = _shader, FilterQuality = SKFilterQuality.Low
            };
            oldShader?.Dispose();
            oldPaint?.Dispose();
        }
Пример #15
0
        public static SKShader ToSkiaShader(this Brush brush, double width, double height)
        {
            if (brush == null)
            {
                return(null);
            }
            else if (brush is SolidColorBrush)
            {
                return(SKShader.CreateColor((brush as SolidColorBrush).Color.ToSKColor()));
            }
            else if (brush is LinearGradientBrush)
            {
                var b = brush as LinearGradientBrush;

                return(SKShader.CreateLinearGradient(
                           new SKPoint((float)(b.StartPoint.X * width), (float)(b.StartPoint.Y * height)),
                           new SKPoint((float)(b.EndPoint.X * width), (float)(b.EndPoint.Y * height)),
                           b.GradientStops.Select(x => x.Color.ToSKColor()).ToArray(),
                           b.GradientStops.Select(x => (float)x.Offset).ToArray(),
                           SKShaderTileMode.Clamp));
            }
            else if (brush is RadialGradientBrush)
            {
                var b = brush as RadialGradientBrush;

                return(SKShader.CreateRadialGradient(
                           new SKPoint((float)(b.Center.X * width), (float)(b.Center.Y * height)),
                           (float)b.RadiusX,
                           b.GradientStops.Select(x => x.Color.ToSKColor()).ToArray(),
                           b.GradientStops.Select(x => (float)x.Offset).ToArray(),
                           SKShaderTileMode.Clamp));
            }
            else
            {
                throw new NotSupportedException($"Unsupported brush type {brush.GetType().Name}.");
            }
        }
Пример #16
0
        public static Tuple <SKPaint, IEnumerable <IDisposable> > CreatePaint(this XInkStroke stroke, SKPaintStyle paintStyle = SKPaintStyle.Stroke, SKBlendMode blendMode = SKBlendMode.SrcATop)
        {
            if (stroke == null)
            {
                throw new ArgumentNullException(nameof(stroke));
            }

            var disposables = new List <IDisposable>();

            SKShader shader = null;

            if (stroke.DrawingAttributes.Kind == XInkDrawingAttributesKind.Pencil)
            {
                var perlin = SKShader.CreatePerlinNoiseFractalNoise(0.01f, 0.01f, 1, 1.0f);
                var color  = SKShader.CreateColor(stroke.DrawingAttributes.Color.ToSKColor().WithAlpha(0x7F));

                disposables.Add(perlin);
                disposables.Add(color);

                shader = SKShader.CreateCompose(
                    perlin,
                    color,
                    blendMode);
            }

            Tuple <SKPaint, IEnumerable <IDisposable> > tuple = null;
            SKPaint paint = null;

            if (!stroke.DrawingAttributes.IgnorePressure)
            {
                paintStyle = SKPaintStyle.Fill;
            }

            try
            {
                paint = new SKPaint
                {
                    Color       = stroke.DrawingAttributes.Kind == XInkDrawingAttributesKind.Default ? stroke.DrawingAttributes.Color.ToSKColor() : new SKColor(),
                    StrokeWidth = stroke.DrawingAttributes.IgnorePressure? stroke.DrawingAttributes.Size : 0.0f,
                    Style       = paintStyle,
                    IsAntialias = true,
                    StrokeCap   = stroke.DrawingAttributes.PenTip == Inking.XPenTipShape.Circle ? SKStrokeCap.Round : SKStrokeCap.Butt,
                    PathEffect  = SKPathEffect.CreateCorner(100)
                };

                if (shader != null)
                {
                    paint.Shader = shader;
                }

                tuple = Tuple.Create(paint, disposables as IEnumerable <IDisposable>);

                paint = null;
            }
            finally
            {
                paint?.Dispose();
            }

            return(tuple);
        }
Пример #17
0
        /// <summary>
        /// Configure paint wrapper for using gradient brush.
        /// </summary>
        /// <param name="paintWrapper">Paint wrapper.</param>
        /// <param name="targetSize">Target size.</param>
        /// <param name="gradientBrush">Gradient brush.</param>
        private void ConfigureGradientBrush(ref PaintWrapper paintWrapper, Size targetSize, IGradientBrush gradientBrush)
        {
            var tileMode    = gradientBrush.SpreadMethod.ToSKShaderTileMode();
            var stopColors  = gradientBrush.GradientStops.Select(s => s.Color.ToSKColor()).ToArray();
            var stopOffsets = gradientBrush.GradientStops.Select(s => (float)s.Offset).ToArray();

            switch (gradientBrush)
            {
            case ILinearGradientBrush linearGradient:
            {
                var start = linearGradient.StartPoint.ToPixels(targetSize).ToSKPoint();
                var end   = linearGradient.EndPoint.ToPixels(targetSize).ToSKPoint();

                // would be nice to cache these shaders possibly?
                using (var shader =
                           SKShader.CreateLinearGradient(start, end, stopColors, stopOffsets, tileMode))
                {
                    paintWrapper.Paint.Shader = shader;
                }

                break;
            }

            case IRadialGradientBrush radialGradient:
            {
                var center = radialGradient.Center.ToPixels(targetSize).ToSKPoint();
                var radius = (float)(radialGradient.Radius * targetSize.Width);

                var origin = radialGradient.GradientOrigin.ToPixels(targetSize).ToSKPoint();

                if (origin.Equals(center))
                {
                    // when the origin is the same as the center the Skia RadialGradient acts the same as D2D
                    using (var shader =
                               SKShader.CreateRadialGradient(center, radius, stopColors, stopOffsets, tileMode))
                    {
                        paintWrapper.Paint.Shader = shader;
                    }
                }
                else
                {
                    // when the origin is different to the center use a two point ConicalGradient to match the behaviour of D2D

                    // reverse the order of the stops to match D2D
                    var reversedColors = new SKColor[stopColors.Length];
                    Array.Copy(stopColors, reversedColors, stopColors.Length);
                    Array.Reverse(reversedColors);

                    // and then reverse the reference point of the stops
                    var reversedStops = new float[stopOffsets.Length];
                    for (var i = 0; i < stopOffsets.Length; i++)
                    {
                        reversedStops[i] = stopOffsets[i];
                        if (reversedStops[i] > 0 && reversedStops[i] < 1)
                        {
                            reversedStops[i] = Math.Abs(1 - stopOffsets[i]);
                        }
                    }

                    // compose with a background colour of the final stop to match D2D's behaviour of filling with the final color
                    using (var shader = SKShader.CreateCompose(
                               SKShader.CreateColor(reversedColors[0]),
                               SKShader.CreateTwoPointConicalGradient(center, radius, origin, 0, reversedColors, reversedStops, tileMode)
                               ))
                    {
                        paintWrapper.Paint.Shader = shader;
                    }
                }

                break;
            }

            case IConicGradientBrush conicGradient:
            {
                var center = conicGradient.Center.ToPixels(targetSize).ToSKPoint();

                // Skia's default is that angle 0 is from the right hand side of the center point
                // but we are matching CSS where the vertical point above the center is 0.
                var angle    = (float)(conicGradient.Angle - 90);
                var rotation = SKMatrix.CreateRotationDegrees(angle, center.X, center.Y);

                using (var shader =
                           SKShader.CreateSweepGradient(center, stopColors, stopOffsets, rotation))
                {
                    paintWrapper.Paint.Shader = shader;
                }

                break;
            }
            }
        }
Пример #18
0
        void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
        {
            SKImageInfo info = args.Info;

            //determine find smallet axis length
            MinorAxisLength = info.Width > info.Height ? info.Height : info.Width;
            arcWidth        = MinorAxisLength / 30;
            arcPadding      = MinorAxisLength / 15;
            KnobRadius      = MinorAxisLength / 30;
            float radius = (MinorAxisLength / 2);

            //update center
            Center = new SKPoint(info.Width / 2, info.Height / 2);
            SKSurface surface = args.Surface;
            SKCanvas  canvas  = surface.Canvas;

            canvas.Clear();
            // canvas.Scale((float)(canvasView.CanvasSize.Width / canvasView.Width));


            //  SKPaint BackgroundPaint = new SKPaint
            //  {
            //      Style = SKPaintStyle.Fill,
            //      Shader = SKShader.CreateColor(BackgroundColor.ToSKColor()),
            //  };
            //
            //  canvas.DrawRect(new SKRect(Center.X-(MinorAxisLength/2), Center.Y - (MinorAxisLength / 2), Center.X + (MinorAxisLength / 2), Center.Y + (MinorAxisLength / 2)), BackgroundPaint);

            SKPaint arcPaint = new SKPaint
            {
                Style       = SKPaintStyle.Stroke,
                Shader      = SKShader.CreateColor(ArcColor.ToSKColor()),
                StrokeCap   = SKStrokeCap.Round,
                StrokeWidth = arcWidth + 1,
                IsAntialias = true,
            };
            SKPaint arcBackgroundPaint = new SKPaint
            {
                Style       = SKPaintStyle.Stroke,
                Shader      = SKShader.CreateColor(ArcBackgroundColor.ToSKColor()),
                StrokeCap   = SKStrokeCap.Round,
                StrokeWidth = arcWidth,
                IsAntialias = true,
                // ImageFilter = SKImageFilter.CreateDropShadow(0, 0, 2, 2, Color.Black.ToSKColor(), SKDropShadowImageFilterShadowMode.DrawShadowAndForeground)
            };
            SKPaint KnobPaint = new SKPaint
            {
                Style       = SKPaintStyle.Fill,
                Shader      = SKShader.CreateColor(KnobColor.ToSKColor()),
                IsAntialias = true,
                ImageFilter = SKImageFilter.CreateDropShadow(0, 0, 3, 3, ArcColor.MultiplyAlpha(.5).ToSKColor())
            };
            SKPaint TextPaint = new SKPaint
            {
                Style       = SKPaintStyle.Fill,
                Shader      = SKShader.CreateColor(TextColor.ToSKColor()),
                IsAntialias = true,
                TextSize    = 40f,
                IsStroke    = false,
                TextAlign   = SKTextAlign.Center,
            };

            //create the rectangle that contains the arc
            //SKRect rect = new SKRect(arcPadding, arcPadding, MinorAxisLength - arcPadding, MinorAxisLength - arcPadding);
            SKRect rect = new SKRect(arcPadding + Center.X - (MinorAxisLength / 2), arcPadding + Center.Y - (MinorAxisLength / 2), Center.X + (MinorAxisLength / 2) - arcPadding, Center.Y + (MinorAxisLength / 2) - arcPadding);


            //var point = LastTouchedPoint;

            //calcualte the angle of the last touched point realtive to the center of the arc



            var   sweepAngle = getsweepangle();
            float realAngle  = startAngle + sweepAngle;

            //Debug.WriteLine($"realAngle:{realAngle % 360}");



            float arcRadius = (MinorAxisLength / 2) - arcPadding;;

            //get x and y compoents o the point on ther arc
            float x = arcRadius * (float)Math.Cos(DegreeToRadian(realAngle));
            float y = arcRadius * (float)Math.Sin(DegreeToRadian(realAngle));

            //move the points realitve to the center of the chart
            // SKRect rect = new SKRect(arcPadding + Center.X - (MinorAxisLength / 2), arcPadding + Center.Y - (MinorAxisLength / 2), Center.X + (MinorAxisLength / 2) - arcPadding, Center.Y + (MinorAxisLength / 2) - arcPadding);

            float realtiveX = x + Center.X;
            float realtiveY = y + Center.Y;


            using (SKPath path = new SKPath())
            {
                //draw background arc
                path.AddArc(rect, startAngle, 300);
                canvas.DrawPath(path, arcBackgroundPaint);
            }
            using (SKPath path = new SKPath())
            {
                //draw arc showing value
                path.AddArc(rect, startAngle, sweepAngle);
                canvas.DrawPath(path, arcPaint);
            }
            //draw current postion
            var TextBounds  = new SKRect();
            var ValueString = String.Format("{0:F" + Precision + "}", Value);

            TextPaint.MeasureText(ValueString, ref TextBounds);

            canvas.DrawOval(new SKRect(realtiveX - KnobRadius, realtiveY - KnobRadius, realtiveX + KnobRadius, realtiveY + KnobRadius), KnobPaint);
            if (ShowValue)
            {
                canvas.DrawText(ValueString, Center.X, Center.Y + (TextBounds.Height / 2), TextPaint);
            }
        }
Пример #19
0
        public void Render(IStateOwner pOwner, SKCanvas pRenderTarget, PauseGameState Source, GameStateSkiaDrawParameters Element)
        {
            if (!Source.DrawDataInitialized)
            {
                InitDrawData(pOwner, Source, Element);
                Source.DrawDataInitialized = true;
            }

            if (GameOverTextPaint == null)
            {
                GameOverTextPaint             = new SKPaint();
                GameOverTextPaint.Color       = SKColors.Black;
                GameOverTextPaint.TextSize    = 24;
                GameOverTextPaint.IsAntialias = true;
                GameOverTextPaint.Typeface    = TetrisGame.RetroFontSK;
            }
            String   sPauseText    = "Pause";
            SKCanvas g             = pRenderTarget;
            var      Bounds        = Element.Bounds;
            var      FallImages    = Source.FallImages;
            SKRect   MeasureBounds = new SKRect();
            var      measureresult = GameOverTextPaint.MeasureText(sPauseText, ref MeasureBounds);

            //render the paused state.
            //TetrisGame.RetroFontSK

            if (Source.PauseGamePlayerState != null)
            {
                RenderingProvider.Static.DrawElement(Source, pRenderTarget, Source.PauseGamePlayerState, Element);
            }
            var ColorShader = SKShader.CreateColor(new SKColor(128, 128, 0, 128));

            var PerlinShader     = SKShader.CreatePerlinNoiseFractalNoise(0.5f, 0.5f, 4, 0, new SKPointI((int)Element.Bounds.Width, (int)Element.Bounds.Height)); //SKShader.CreatePerlinNoiseFractalNoise(0.5f, 0.5f, 5, (float)TetrisGame.rgen.NextDouble() * 3000000);
            var DualPerlinShader = SKShader.CreateCompose(SKShader.CreatePerlinNoiseTurbulence(0.5f, 0.5f, 4, (float)TetrisGame.rgen.NextDouble() * 3000000), PerlinShader);


            var Gradcolors = new SKColor[] {
                new SKColor(0, 255, 255),
                new SKColor(255, 0, 255),
                new SKColor(255, 255, 0),
                new SKColor(0, 255, 255)
            };
            //var sweep = SKShader.CreateSweepGradient(new SKPoint(128, 128), Gradcolors, null);
            //var sweep = SKShader.CreateTwoPointConicalGradient(new SKPoint(Element.Bounds.Width / 2, Element.Bounds.Height - 64), Element.Bounds.Width,
            //    new SKPoint(Element.Bounds.Width / 2, 64), Element.Bounds.Width / 4, Gradcolors, null, SKShaderTileMode.Clamp);
            var sweep = SKShader.CreateLinearGradient(new SKPoint(0, 0), new SKPoint(Element.Bounds.Width, Element.Bounds.Height), Gradcolors, null, SKShaderTileMode.Repeat);
            // create the second shader
            var turbulence = SKShader.CreatePerlinNoiseTurbulence(0.05f, 0.05f, 4, 0);

            // create the compose shader
            var shader = SKShader.CreateCompose(sweep, turbulence, SKBlendMode.SrcOver);



            GrayBG.BlendMode = SKBlendMode.Luminosity;
            //GrayBG.BlendMode = SKBlendMode.Nor;
            //GrayBG.Color = new SKColor(0,0,0,128);
            //GrayBG.ColorFilter = SKColorFilter.CreateHighContrast(SKHighContrastConfig.Default);
            //GrayBG.Shader = SKShader.CreateLinearGradient(new SKPoint(Bounds.Left, Bounds.Top), new SKPoint(Bounds.Right, Bounds.Bottom), new SKColor[] { SKColors.Red, SKColors.Yellow, SKColors.Blue, SKColors.Green }, null, SKShaderTileMode.Repeat);

            //ColorShader; // SKShader.CreateCompose(ColorShader,DualPerlinShader);
            GrayBG.Shader = shader;
            g.DrawRect(Bounds, GrayBG);
            foreach (var iterate in FallImages)
            {
                iterate.Draw(g);
            }
            g.ResetMatrix();



            SKPoint DrawPos = new SKPoint(Bounds.Width / 2 - MeasureBounds.Width / 2, Bounds.Height / 2 - MeasureBounds.Height / 2);

            GameOverTextPaint.Color = SKColors.White;
            g.DrawText(sPauseText, new SKPoint(DrawPos.X + 2, DrawPos.Y + 2), GameOverTextPaint);
            GameOverTextPaint.Color = SKColors.Navy;
            g.DrawText(sPauseText, DrawPos, GameOverTextPaint);

            //retrieve the renderer for the MenuState object.

            //var basecall = RenderingProvider.Static.GetHandler(typeof(SKCanvas), typeof(MenuState), typeof(GameStateSkiaDrawParameters));
            base.Render(pOwner, pRenderTarget, Source, Element);
            //basecall?.Render(pOwner, pRenderTarget, Source, Element);
        }
Пример #20
0
        public override void Render(IDrawingContextImpl context)
        {
            var canvas = ((ISkiaDrawingContextImpl)context)?.SkCanvas;

            if (canvas == null)
            {
                context.Clear(Colors.White);
                context.DrawText(new SolidColorBrush(Colors.Black), new Point(), NoSkia);
            }
            else
            {
                int width  = (int)Bounds.Width;
                int height = (int)Bounds.Height;

                var info = new SKImageInfo(width, height);

                //canvas.Scale((float)Bounds.Width, (float)Bounds.Height, -(float)Bounds.Width, -(float)Bounds.Height);
                canvas.RotateDegrees(-90f, (float)Bounds.Center.X, (float)Bounds.Center.Y);

                using (SKPaint paint = new SKPaint())
                {
                    SKColor[] colors = new SKColor[8];

                    for (int i = 0; i < colors.Length; i++)
                    {
                        colors[i] = SKColor.FromHsl(i * 360f / 7, 100, 50); //sets the colors
                    }

                    SKPoint center = new SKPoint(info.Rect.MidX, info.Rect.MidY); // creates the center

                    // Create sweep gradient based on center of canvas
                    paint.Shader = SKShader.CreateSweepGradient(center, colors, null);


                    float radius = (Math.Min(info.Width, info.Height) - StrokeWidth) / 2; //computes the radius
                    canvas.DrawCircle(center, radius, paint);                             // draw a circle with its respects parameters
                }

                // Creates the black gradient effect (transparent to black)
                using (SKPaint paint = new SKPaint())
                {
                    SKColor[] colors = { SKColors.White, SKColors.Transparent };

                    SKPoint center = new SKPoint(info.Rect.MidX, info.Rect.MidY); // creates the center

                    int   strokeWidth = 20;
                    float radius      = (Math.Min(info.Width, info.Height) - strokeWidth) / 2; //computes the radius

                    paint.Shader = SKShader.CreateRadialGradient(center, radius, colors, null, SKShaderTileMode.Repeat);

                    canvas.DrawCircle(center, radius, paint);
                }

                using (SKPaint paint = new SKPaint())
                {
                    var colorStroke = Stroke.ToSKColor();

                    SKPoint center = new SKPoint(info.Rect.MidX, info.Rect.MidY);           // creates the center
                    float   radius = (Math.Min(info.Width, info.Height) - StrokeWidth) / 2; //computes the radius

                    paint.Shader = SKShader.CreateColor(colorStroke);

                    paint.Style       = SKPaintStyle.Stroke;
                    paint.StrokeWidth = StrokeWidth;

                    canvas.DrawCircle(center, radius, paint);
                }
            }
        }
Пример #21
0
        void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
        {
            SKImageInfo info    = args.Info;
            SKSurface   surface = args.Surface;
            SKCanvas    canvas  = surface.Canvas;

            canvas.Clear();

            float x = (info.Width - monkeyBitmap.Width) / 2;
            float y = info.Height - monkeyBitmap.Height;

            // Draw monkey bitmap
            if (step >= 1)
            {
                canvas.DrawBitmap(monkeyBitmap, x, y);
            }

            // Draw matte to exclude monkey's surroundings
            if (step >= 2)
            {
                using (SKPaint paint = new SKPaint())
                {
                    paint.BlendMode = SKBlendMode.DstIn;
                    canvas.DrawBitmap(matteBitmap, x, y, paint);
                }
            }

            const float sidewalkHeight = 80;
            SKRect      rect           = new SKRect(info.Rect.Left, info.Rect.Bottom - sidewalkHeight,
                                                    info.Rect.Right, info.Rect.Bottom);

            // Draw gravel sidewalk for monkey to sit on
            if (step >= 3)
            {
                using (SKPaint paint = new SKPaint())
                {
                    paint.Shader = SKShader.CreateCompose(
                        SKShader.CreateColor(SKColors.SandyBrown),
                        SKShader.CreatePerlinNoiseTurbulence(0.1f, 0.3f, 1, 9));

                    paint.BlendMode = SKBlendMode.DstOver;
                    canvas.DrawRect(rect, paint);
                }
            }

            // Draw bitmap tiled brick wall behind monkey
            if (step >= 4)
            {
                using (SKPaint paint = new SKPaint())
                {
                    SKBitmap bitmap  = BrickWallTile;
                    float    yAdjust = (info.Height - sidewalkHeight) % bitmap.Height;

                    paint.Shader = SKShader.CreateBitmap(bitmap,
                                                         SKShaderTileMode.Repeat,
                                                         SKShaderTileMode.Repeat,
                                                         SKMatrix.MakeTranslation(0, yAdjust));
                    paint.BlendMode = SKBlendMode.DstOver;
                    canvas.DrawRect(info.Rect, paint);
                }
            }
        }