Example #1
0
        protected override void OnPaintSurface(SKPaintSurfaceEventArgs e)
        {
            base.OnPaintSurface(e);

            var info   = e.Info;
            var canvas = e.Surface.Canvas;

            canvas.Clear(SKColors.White);

            using (var paint = new SKPaint())
            {
                var topBottom = SKShader.CreateLinearGradient(
                    new SKPoint(0, 0),
                    new SKPoint(0, GridSize),
                    new[] { _color1, _color1, _color2, _color2 },
                    new[] { 0f, 0.5f, 0.5f, 1f },
                    SKShaderTileMode.Repeat);

                var leftRight = SKShader.CreateLinearGradient(
                    new SKPoint(0, 0),
                    new SKPoint(GridSize, 0),
                    new[] { _color1, _color1, _color2, _color2 },
                    new[] { 0f, 0.5f, 0.5f, 1f },
                    SKShaderTileMode.Repeat);

                var shader = SKShader.CreateCompose(topBottom, leftRight, SKBlendMode.Xor);

                paint.Shader = shader;
                canvas.DrawRect(0, 0, info.Width, info.Height, paint);
            }
        }
Example #2
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);
        }
            public void Render(IDrawingContextImpl context)
            {
                var canvas = (context as ISkiaDrawingContextImpl)?.SkCanvas;

                if (canvas == null)
                {
                    using (var c = new DrawingContext(context, false))
                    {
                        c.DrawText(_noSkia, new Point());
                    }
                }
                else
                {
                    canvas.Save();
                    // create the first shader
                    var colors = new SKColor[] {
                        new SKColor(0, 255, 255),
                        new SKColor(255, 0, 255),
                        new SKColor(255, 255, 0),
                        new SKColor(0, 255, 255)
                    };

                    var sx            = Animate(100, 2, 10);
                    var sy            = Animate(1000, 5, 15);
                    var lightPosition = new SKPoint(
                        (float)(Bounds.Width / 2 + Math.Cos(St.Elapsed.TotalSeconds) * Bounds.Width / 4),
                        (float)(Bounds.Height / 2 + Math.Sin(St.Elapsed.TotalSeconds) * Bounds.Height / 4));
                    using (var sweep =
                               SKShader.CreateSweepGradient(new SKPoint((int)Bounds.Width / 2, (int)Bounds.Height / 2), colors,
                                                            null))
                        using (var turbulence = SKShader.CreatePerlinNoiseFractalNoise(0.05f, 0.05f, 4, 0))
                            using (var shader = SKShader.CreateCompose(sweep, turbulence, SKBlendMode.SrcATop))
                                using (var blur = SKImageFilter.CreateBlur(Animate(100, 2, 10), Animate(100, 5, 15)))
                                    using (var paint = new SKPaint
                                    {
                                        Shader = shader,
                                        ImageFilter = blur
                                    })
                                        canvas.DrawPaint(paint);

                    using (var pseudoLight = SKShader.CreateRadialGradient(
                               lightPosition,
                               (float)(Bounds.Width / 3),
                               new [] {
                        new SKColor(255, 200, 200, 100),
                        SKColors.Transparent,
                        new SKColor(40, 40, 40, 220),
                        new SKColor(20, 20, 20, (byte)Animate(100, 200, 220))
                    },
                               new float[] { 0.3f, 0.3f, 0.8f, 1 },
                               SKShaderTileMode.Clamp))
                        using (var paint = new SKPaint
                        {
                            Shader = pseudoLight
                        })
                            canvas.DrawPaint(paint);
                    canvas.Restore();
                }
            }
Example #4
0
        public async ValueTask <IActionResult> GetShaderAsync(int size, string topic = null)
        {
            int center = size / 2;
            var colors = new SKColor[] { new SKColor(0, 255, 90), new SKColor(50, 0, 255), new SKColor(255, 255, 0), new SKColor(0, 255, 255) };

            using (var sweep = SKShader.CreateSweepGradient(new SKPoint(center, center), colors, null))
                using (var turbulence = SKShader.CreatePerlinNoiseTurbulence(0.02f, 0.02f, size / 100, 0))
                    using (var shader = SKShader.CreateCompose(sweep, turbulence))
                    {
                        var response = await GetWithFilterAsync(size, topic, shader : shader);

                        return(response);
                    }
        }
        protected void DrawArea(SKCanvas canvas, SKPoint[] points, SKSize itemSize, float origin)
        {
            if (LineAreaAlpha > 0 && points.Length > 1)
            {
                using (var paint = new SKPaint
                {
                    Style = SKPaintStyle.Fill,
                    Color = SKColors.White,
                    IsAntialias = true,
                })
                {
                    using (var shaderX = CreateXGradient(points, (byte)(LineAreaAlpha * AnimationProgress)))
                        using (var shaderY = CreateYGradient(points, (byte)(LineAreaAlpha * AnimationProgress)))
                        {
                            paint.Shader = EnableYFadeOutGradient ? SKShader.CreateCompose(shaderY, shaderX, SKBlendMode.SrcOut) : shaderX;

                            var path = new SKPath();

                            path.MoveTo(points.First().X, origin);
                            path.LineTo(points.First());

                            var last = (LineMode == LineMode.Spline) ? points.Length - 1 : points.Length;
                            for (int i = 0; i < last; i++)
                            {
                                var entry = Entries.ElementAt(i);
                                if (LineMode == LineMode.Spline)
                                {
                                    var nextEntry = Entries.ElementAt(i + 1);
                                    var cubicInfo = CalculateCubicInfo(points, i, itemSize);
                                    path.CubicTo(cubicInfo.control, cubicInfo.nextControl, cubicInfo.nextPoint);
                                }
                                else if (LineMode == LineMode.Straight)
                                {
                                    if (!entry.Value.HasValue)
                                    {
                                        continue;
                                    }
                                    path.LineTo(points[i]);
                                }
                            }

                            path.LineTo(points.Last().X, origin);

                            path.Close();

                            canvas.DrawPath(path, paint);
                        }
                }
            }
        }
        public override void Paint(SKSurface surface, SKImageInfo info)
        {
            var canvas     = surface.Canvas;
            var colors     = 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((int)(info.Width * Parameter), (int)(info.Height * Parameter)), colors, null);
            var turbulence = SKShader.CreatePerlinNoiseTurbulence(0.05f, 0.05f, 4, 0);
            var shader     = SKShader.CreateCompose(sweep, turbulence);
            var paint      = new SKPaint()
            {
                Shader = shader
            };

            canvas.DrawPaint(paint);
        }
Example #7
0
        protected override void OnDrawSample(SKCanvas canvas, int width, int height)
        {
            var colors = new[] { SKColors.Blue, SKColors.Yellow };
            var center = new SKPoint(width / 2f, height / 2f);

            using (var shader1 = SKShader.CreateRadialGradient(center, 180.0f, colors, null, SKShaderTileMode.Clamp))
                using (var shader2 = SKShader.CreatePerlinNoiseTurbulence(0.025f, 0.025f, 2, 0.0f))
                    using (var shader = SKShader.CreateCompose(shader1, shader2))
                        using (var paint = new SKPaint())
                        {
                            paint.Shader = shader;
                            canvas.DrawPaint(paint);
                        }
        }
Example #8
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);
                        }
        }
Example #9
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);
            }
        }
Example #10
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);
        }
Example #11
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;
            }
            }
        }
Example #12
0
        private void DrawLineArea(SKCanvas canvas, ChartSerie serie, SKPoint[] points, SKSize itemSize, float origin)
        {
            if (LineAreaAlpha > 0 && points.Length > 1)
            {
                using (var paint = new SKPaint
                {
                    Style = SKPaintStyle.Fill,
                    Color = SKColors.White,
                    IsAntialias = true,
                })
                {
                    using (var shaderX = CreateXGradient(points, serie.Entries, serie.Color, (byte)(LineAreaAlpha * AnimationProgress)))
                        using (var shaderY = CreateYGradient(points, (byte)(LineAreaAlpha * AnimationProgress)))
                        {
                            paint.Shader = EnableYFadeOutGradient ? SKShader.CreateCompose(shaderY, shaderX, SKBlendMode.SrcOut) : shaderX;

                            var path = new SKPath();

                            var     isFirst   = true;
                            var     entries   = serie.Entries;
                            var     lineMode  = LineMode;
                            var     last      = (lineMode == LineMode.Spline) ? points.Length - 1 : points.Length;
                            SKPoint lastPoint = points.First();
                            for (int i = 0; i < last; i++)
                            {
                                if (!entries.ElementAt(i).Value.HasValue)
                                {
                                    continue;
                                }

                                if (isFirst)
                                {
                                    path.MoveTo(points[i].X, origin);
                                    path.LineTo(points[i]);
                                    isFirst = false;
                                }

                                if (lineMode == LineMode.Spline)
                                {
                                    int next = i + 1;
                                    while (next < last && !entries.ElementAt(next).Value.HasValue)
                                    {
                                        next++;
                                    }

                                    if (next == last && !entries.ElementAt(next).Value.HasValue)
                                    {
                                        lastPoint = points[i];
                                        break;
                                    }

                                    var cubicInfo = CalculateCubicInfo(points, i, next, itemSize);
                                    path.CubicTo(cubicInfo.control, cubicInfo.nextControl, cubicInfo.nextPoint);
                                    lastPoint = cubicInfo.nextPoint;
                                }
                                else if (lineMode == LineMode.Straight)
                                {
                                    path.LineTo(points[i]);
                                    lastPoint = points[i];
                                }
                            }

                            path.LineTo(lastPoint.X, origin);
                            path.Close();
                            canvas.DrawPath(path, paint);
                        }
                }
            }
        }
Example #13
0
        protected void DrawArea(SKCanvas canvas, SKPoint[] points, SKSize itemSize, float origin, IEnumerable <ChartEntry> entries, SKPoint[] pointsTo = null)
        {
            if (this.LineAreaAlpha > 0 && points.Length > 1)
            {
                using (var paint = new SKPaint
                {
                    Style = SKPaintStyle.Fill,
                    Color = SKColors.White,
                    IsAntialias = true,
                })
                {
                    using (var shaderX = this.CreateXGradient(points, entries, (byte)(this.LineAreaAlpha * this.AnimationProgress)))
                        using (var shaderY = this.CreateYGradient(points, (byte)(this.LineAreaAlpha * this.AnimationProgress)))
                        {
                            paint.Shader = EnableYFadeOutGradient ? SKShader.CreateCompose(shaderY, shaderX, SKBlendMode.SrcOut) : shaderX;

                            var path = new SKPath();

                            var initialY = origin;
                            var finishY  = origin;
                            if (pointsTo != null)
                            {
                                initialY = pointsTo.First().Y;
                                finishY  = pointsTo.Last().Y;
                            }

                            path.MoveTo(points.First().X, initialY);
                            path.LineTo(points.First());

                            var last = (this.LineMode == LineMode.Spline) ? points.Length - 1 : points.Length;
                            for (int i = 0; i < last; i++)
                            {
                                if (this.LineMode == LineMode.Spline)
                                {
                                    var cubicInfo = this.CalculateCubicInfo(points, i, itemSize);
                                    path.CubicTo(cubicInfo.control, cubicInfo.nextControl, cubicInfo.nextPoint);
                                }
                                else if (this.LineMode == LineMode.Straight)
                                {
                                    path.LineTo(points[i]);
                                }
                            }

                            if (pointsTo != null)
                            {
                                var first = 0;
                                if (LineMode == LineMode.Spline)
                                {
                                    path.LineTo(pointsTo.Last());
                                    first = 1;
                                }

                                for (int i = pointsTo.Length - 1; i >= first; i--)
                                {
                                    if (this.LineMode == LineMode.Spline)
                                    {
                                        var cubicInfo = this.CalculateReverseCubicInfo(pointsTo, i, itemSize);
                                        path.CubicTo(cubicInfo.control, cubicInfo.previousControl, cubicInfo.previousPoint);
                                    }
                                    else if (this.LineMode == LineMode.Straight)
                                    {
                                        path.LineTo(pointsTo[i]);
                                    }
                                }
                            }

                            path.LineTo(points.Last().X, finishY);

                            path.Close();

                            canvas.DrawPath(path, paint);
                        }
                }
            }
        }
Example #14
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);
                }
            }
        }
Example #15
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);
        }
Example #16
0
        public void DrawWeather(WeatherRendererInfo info, SKCanvas canvas, int width, int height)
        {
            // background
            using (var paint = new SKPaint())
            {
                var colors = new[] { WeatherColors.DarkBlue, WeatherColors.Orange };

                var headerHeight = 84;

                var shaderGradient = SKShader.CreateLinearGradient(new SKPoint(0, headerHeight), new SKPoint(0, height),
                                                                   colors, null, SKShaderTileMode.Clamp);
                var shaderNoise = SKShader.CreatePerlinNoiseTurbulence(0.99f, 0.99f, 1, 0.0f);
                var shaderComp  = SKShader.CreateCompose(shaderGradient, shaderNoise, SKXferMode.Multiply);

                paint.Color  = WeatherColors.DarkBlue;
                paint.Shader = shaderComp;

                // background
                canvas.DrawRect(new SKRect(0, 0, width, height), paint);

                // header background
                var grad = WeatherBitmapCache.Get("gradient_orange.png");
                canvas.DrawBitmap(grad, new SKRect(0, 0, width, headerHeight));

                // top right cutout
                paint.Shader = null;
                using (var path = new SKPath())
                {
                    var baseX = 180;
                    var slant = 80;
                    path.MoveTo(width - baseX, 0);
                    path.LineTo(width, 0);
                    path.LineTo(width, headerHeight);
                    path.LineTo(width - baseX - slant, headerHeight);
                    canvas.DrawPath(path, paint);
                }
            }

            // ticker
            using (var paint = new SKPaint())
            {
                var alertsActive = info.WeatherResponse.alerts?.Count > 0;
                var rect         = new SKRect(0, height - 100, width, height);

                paint.Color = SKColors.Black;
                canvas.DrawRect(rect, paint);

                rect.Top   += 2;
                paint.Color = SKColors.Gray;
                canvas.DrawRect(rect, paint);

                rect.Top   += 2;
                paint.Color = alertsActive ? WeatherColors.Red : WeatherColors.Teal;
                canvas.DrawRect(rect, paint);

                paint.Color    = SKColors.White;
                paint.TextSize = 36;
                paint.Typeface = WeatherTypeface.Standard;
                string message;
                if (alertsActive)
                {
                    message = string.Join(", ", (from alert in info.WeatherResponse.alerts select alert.title));
                }
                else
                {
                    var unit = info.Unit == Unit.us ? "F" : "C";
                    message =
                        $"Temp: {(int) info.WeatherResponse.currently.temperature}°{unit}   Feels Like: {(int) info.WeatherResponse.currently.apparentTemperature}°{unit}";
                }
                DrawShadowedText(canvas, message, 5, height - 55, paint);
            }

            // logo
            var logo    = WeatherBitmapCache.Get("logo.png");
            var imgRect = new SKRect(20, 5, 20 + logo.Width, 5 + logo.Height);

            ;
            canvas.DrawBitmap(logo, imgRect);

            // city name and extended forecast header text
            using (var paint = new SKPaint())
            {
                paint.TextSize = 36.0f;
                paint.Typeface = WeatherTypeface.Standard;
                var baseX    = 150.0f;
                var baseY    = 32.0f;
                var baseYOff = 30.0f;

                paint.Color = SKColors.White;
                DrawShadowedText(canvas, info.Address, baseX, baseY, paint);

                paint.Color = WeatherColors.Yellow;
                DrawShadowedText(canvas, "Extended Forecast", baseX, baseY + baseYOff, paint);
            }

            // top right date and time
            using (var paint = new SKPaint())
            {
                // TODO: this is probably not right. i have an offset but no clue what the best way is here
                var now = DateTime.UtcNow + TimeSpan.FromHours(info.WeatherResponse.offset);

                var topDateLine    = now.ToString("h:mm:ss tt").ToUpper();
                var bottomDateLine = now.ToString("ddd MMM d").ToUpper();

                var baseX = width - 280.0f;
                var baseY = 45;

                paint.Typeface = WeatherTypeface.Small;
                paint.TextSize = 36;
                paint.Color    = SKColors.LightGray;

                DrawShadowedText(canvas, topDateLine, baseX, baseY, paint);
                DrawShadowedText(canvas, bottomDateLine, baseX, baseY + 20, paint);
            }

            for (var i = 0; i < 4; i++)
            {
                var boxWidth  = 220;
                var boxOffset = width / 4;
                DrawForecast(canvas, new SKRect(i * boxOffset + 10, 90, i * boxOffset + boxWidth, height - 90 - 20),
                             info.WeatherResponse.daily.data[i]);
            }
        }