public ImageDrawer()
 {
     BackgroundColor = _defaultBackgroundColor;
 }
Esempio n. 2
0
        /// <summary>
        /// Draws a line with rounded ends.
        /// </summary>
        /// <param name="x1">The X position of the first point.</param>
        /// <param name="y1">The Y position of the first point.</param>
        /// <param name="x2">The X position of the second point.</param>
        /// <param name="y2">The Y position of the second point.</param>
        /// <param name="color">The color of the line.</param>
        /// <param name="thickness">The thickness of the line.</param>
        static public void RoundedLine(float x1, float y1, float x2, float y2, Graphics.Color color, float thickness)
        {
            VertexArray vertices = new VertexArray(PrimitiveType.TriangleFan);

            int rotationSteps = 10;

            var line       = new Vector2(x2 - x1, y2 - y1);
            var normalUp   = new Vector2(y1 - y2, x2 - x1);
            var normalDown = new Vector2(y2 - y1, x1 - x2);

            normalUp.Normalize(thickness * 0.5f);
            normalDown.Normalize(thickness * 0.5f);

            var nextPoint = new Vector2();

            float vx, vy;

            vx = x1;
            vy = y1;

            vertices.Append(new Vertex(new Vector2f(vx, vy), color.SFMLColor));

            vx = (float)(x1 + normalUp.X);
            vy = (float)(y1 + normalUp.Y);

            vertices.Append(new Vertex(new Vector2f(vx, vy), color.SFMLColor));

            nextPoint.X = normalUp.X;
            nextPoint.Y = normalUp.Y;

            for (int i = 0; i < rotationSteps; i++)
            {
                nextPoint = Util.Rotate(nextPoint, -180 / rotationSteps);

                vx = (float)(x1 + nextPoint.X);
                vy = (float)(y1 + nextPoint.Y);

                vertices.Append(new Vertex(new Vector2f(vx, vy), color.SFMLColor));
            }

            vx = (float)(x1 + normalDown.X);
            vy = (float)(y1 + normalDown.Y);

            vertices.Append(new Vertex(new Vector2f(vx, vy), color.SFMLColor));

            vx = (float)(x2 + normalDown.X);
            vy = (float)(y2 + normalDown.Y);

            vertices.Append(new Vertex(new Vector2f(vx, vy), color.SFMLColor));

            for (int i = 0; i < rotationSteps; i++)
            {
                nextPoint = Util.Rotate(nextPoint, -180 / rotationSteps);

                vx = (float)(x2 + nextPoint.X);
                vy = (float)(y2 + nextPoint.Y);

                vertices.Append(new Vertex(new Vector2f(vx, vy), color.SFMLColor));
            }

            vx = (float)(x2 + normalUp.X);
            vy = (float)(y2 + normalUp.Y);

            vertices.Append(new Vertex(new Vector2f(vx, vy), color.SFMLColor));

            vx = (float)(x1 + normalUp.X);
            vy = (float)(y1 + normalUp.Y);

            vertices.Append(new Vertex(new Vector2f(vx, vy), color.SFMLColor));

            Drawable(vertices, RenderStates.Default);
        }
Esempio n. 3
0
        void DrawShadow(Canvas canvas)
        {
            if (_shadowCanvas == null)
            {
                _shadowCanvas = new Canvas();
            }

            if (_shadowPaint == null)
            {
                _shadowPaint = new Android.Graphics.Paint
                {
                    AntiAlias    = true,
                    Dither       = true,
                    FilterBitmap = true
                }
            }
            ;

            Graphics.Color solidColor = null;

            // If need to redraw shadow
            if (_invalidateShadow)
            {
                var viewHeight = _viewBounds.Height();
                var viewWidth  = _viewBounds.Width();

                if (GetChildAt(0) is AView child)
                {
                    if (viewHeight == 0)
                    {
                        viewHeight = child.MeasuredHeight;
                    }

                    if (viewWidth == 0)
                    {
                        viewWidth = child.MeasuredWidth;
                    }
                }

                // If bounds is zero
                if (viewHeight != 0 && viewWidth != 0)
                {
                    var bitmapHeight = viewHeight + MaximumRadius;
                    var bitmapWidth  = viewWidth + MaximumRadius;

                    // Reset bitmap to bounds
                    _shadowBitmap = Bitmap.CreateBitmap(
                        bitmapWidth, bitmapHeight, Bitmap.Config.Argb8888
                        );

                    // Reset Canvas
                    _shadowCanvas.SetBitmap(_shadowBitmap);

                    _invalidateShadow = false;

                    // Create the local copy of all content to draw bitmap as a
                    // bottom layer of natural canvas.
                    base.DispatchDraw(_shadowCanvas);

                    // Get the alpha bounds of bitmap
                    Bitmap extractAlpha = _shadowBitmap.ExtractAlpha();

                    // Clear past content content to draw shadow
                    _shadowCanvas.DrawColor(Android.Graphics.Color.Black, PorterDuff.Mode.Clear);

                    var shadowOpacity = (float)Shadow.Opacity;

                    if (Shadow.Paint is LinearGradientPaint linearGradientPaint)
                    {
                        var linearGradientShaderFactory = PaintExtensions.GetLinearGradientShaderFactory(linearGradientPaint, shadowOpacity);
                        _shadowPaint.SetShader(linearGradientShaderFactory.Resize(bitmapWidth, bitmapHeight));
                    }
                    if (Shadow.Paint is RadialGradientPaint radialGradientPaint)
                    {
                        var radialGradientShaderFactory = PaintExtensions.GetRadialGradientShaderFactory(radialGradientPaint, shadowOpacity);
                        _shadowPaint.SetShader(radialGradientShaderFactory.Resize(bitmapWidth, bitmapHeight));
                    }
                    if (Shadow.Paint is SolidPaint solidPaint)
                    {
                        solidColor = solidPaint.ToColor();
#pragma warning disable CA1416 // https://github.com/xamarin/xamarin-android/issues/6962
                        _shadowPaint.Color = solidColor.WithAlpha(shadowOpacity).ToPlatform();
#pragma warning restore CA1416
                    }

                    // Apply the shadow radius
                    var radius = Shadow.Radius;

                    if (radius <= 0)
                    {
                        radius = 0.01f;
                    }

                    if (radius > 100)
                    {
                        radius = MaximumRadius;
                    }

                    _shadowPaint.SetMaskFilter(new BlurMaskFilter(radius, BlurMaskFilter.Blur.Normal));

                    float shadowOffsetX = (float)Shadow.Offset.X;
                    float shadowOffsetY = (float)Shadow.Offset.Y;

                    if (Clip == null)
                    {
                        _shadowCanvas.DrawBitmap(extractAlpha, shadowOffsetX, shadowOffsetY, _shadowPaint);
                    }
                    else
                    {
                        var bounds = new Graphics.RectF(0, 0, canvas.Width, canvas.Height);
                        var path   = Clip.PathForBounds(bounds)?.AsAndroidPath();

                        path.Offset(shadowOffsetX, shadowOffsetY);

                        _shadowCanvas.DrawPath(path, _shadowPaint);
                    }

                    // Recycle and clear extracted alpha
                    extractAlpha.Recycle();
                }
                else
                {
                    // Create placeholder bitmap when size is zero and wait until new size coming up
                    _shadowBitmap = Bitmap.CreateBitmap(1, 1, Bitmap.Config.Rgb565 !);
                }
            }

            // Reset alpha to draw child with full alpha
            if (solidColor != null)
#pragma warning disable CA1416 // https://github.com/xamarin/xamarin-android/issues/6962
            {
                _shadowPaint.Color = solidColor.ToPlatform();
            }
#pragma warning restore CA1416

            // Draw shadow bitmap
            if (_shadowCanvas != null && _shadowBitmap != null && !_shadowBitmap.IsRecycled)
            {
                canvas.DrawBitmap(_shadowBitmap, 0.0F, 0.0F, _shadowPaint);
            }
        }

        void ClearShadowResources()
        {
            _shadowCanvas?.Dispose();
            _shadowPaint?.Dispose();
            _shadowBitmap?.Dispose();
            _shadowCanvas = null;
            _shadowPaint  = null;
            _shadowBitmap = null;
        }
    }
Esempio n. 4
0
        /// <summary>
        /// Draws a rectangle.  Recommended to use only for debugging purposes.
        /// </summary>
        /// <param name="x">The X position of the top left of the rectangle.</param>
        /// <param name="y">The Y position of the top left of the rectangle.</param>
        /// <param name="width">The width of the rectangle.</param>
        /// <param name="height">The height of the rectangle.</param>
        /// <param name="fill">The fill color of the rectangle.</param>
        /// <param name="outline">The outline color of the rectangle.</param>
        /// <param name="outlineThickness">The outline thickness of the rectangle.</param>
        static public void Rectangle(float x, float y, float width, float height, Graphics.Color fill = null, Graphics.Color outline = null, float outlineThickness = 0)
        {
            tempRect.Size     = new Vector2f(width, height);
            tempRect.Position = new Vector2f(x, y);
            if (outline == null)
            {
                tempRect.OutlineColor = Graphics.Color.None.SFMLColor;
            }
            else
            {
                tempRect.OutlineColor = outline.SFMLColor;
            }
            tempRect.OutlineThickness = outlineThickness;

            if (fill == null)
            {
                tempRect.FillColor = Graphics.Color.White.SFMLColor;
            }
            else
            {
                tempRect.FillColor = fill.SFMLColor;
            }

            Target.Draw(tempRect);
        }
Esempio n. 5
0
        /// <summary>
        /// Draws a circle.  Recommended to use only for debugging purposes.
        /// </summary>
        /// <param name="x">The X position of the top left of the circle.</param>
        /// <param name="y">The Y position of the top left of the circle.</param>
        /// <param name="radius">The radius of the circle.</param>
        /// <param name="fill">The fill color of the circle.</param>
        /// <param name="outline">The outline color of the circle.</param>
        /// <param name="outlineThickness">The outline thickness of the circle.</param>
        static public void Circle(float x, float y, int radius, Graphics.Color fill = null, Graphics.Color outline = null, float outlineThickness = 0)
        {
            tempCircle.Radius   = radius;
            tempCircle.Position = new Vector2f(x, y);
            if (fill == null)
            {
                tempCircle.FillColor = Graphics.Color.White.SFMLColor;
            }
            else
            {
                tempCircle.FillColor = fill.SFMLColor;
            }

            tempCircle.OutlineThickness = outlineThickness;

            if (outline == null)
            {
                tempCircle.OutlineColor = Graphics.Color.None.SFMLColor;
            }
            else
            {
                tempCircle.OutlineColor = outline.SFMLColor;
            }

            Target.Draw(tempCircle);
        }
Esempio n. 6
0
 public static WBrush ToBrush(this Graphics.Color color) => color.ToNative();
Esempio n. 7
0
 public static bool IsDefault(this Graphics.Color color)
 {
     return(color == KnownColor.Default);
 }
Esempio n. 8
0
 public static bool IsNotDefault(this Graphics.Color color)
 {
     return(!IsDefault(color));
 }