D2D1.Brush GetBrush(Rect frame, Brush brush)
        {
            if (brush == null)
            {
                return(null);
            }
            var sb = brush as SolidBrush;

            if (sb != null)
            {
                return(new D2D1.SolidColorBrush(renderTarget, sb.Color.ToColor4()));
            }

            var lgb = brush as LinearGradientBrush;

            if (lgb != null)
            {
                if (lgb.Stops.Count < 2)
                {
                    return(null);
                }
                var props = new D2D1.LinearGradientBrushProperties {
                    StartPoint = lgb.GetAbsoluteStart(frame).ToVector2(),
                    EndPoint   = lgb.GetAbsoluteEnd(frame).ToVector2(),
                };
                return(new D2D1.LinearGradientBrush(renderTarget, props, GetStops(lgb.Stops)));
            }

            var rgb = brush as RadialGradientBrush;

            if (rgb != null)
            {
                if (rgb.Stops.Count < 2)
                {
                    return(null);
                }
                var rad    = rgb.GetAbsoluteRadius(frame);
                var center = rgb.GetAbsoluteCenter(frame);
                var focus  = rgb.GetAbsoluteFocus(frame);
                var props  = new D2D1.RadialGradientBrushProperties {
                    Center  = center.ToVector2(),
                    RadiusX = (float)rad.Width,
                    RadiusY = (float)rad.Height,
                    GradientOriginOffset = (focus - center).ToVector2(),
                };
                return(new D2D1.RadialGradientBrush(renderTarget, props, GetStops(rgb.Stops)));
            }

            // TODO: Radial gradient brushes
            return(new D2D1.SolidColorBrush(renderTarget, Colors.Black.ToColor4()));
        }
Beispiel #2
0
        D2D1.Brush GetBrush(Rect frame, Brush brush)
        {
            if (brush == null) return null;
            var sb = brush as SolidBrush;
            if (sb != null) {
                return new D2D1.SolidColorBrush (renderTarget, sb.Color.ToColor4 ());
            }

            var lgb = brush as LinearGradientBrush;
            if (lgb != null) {
                if (lgb.Stops.Count < 2) return null;
                var props = new D2D1.LinearGradientBrushProperties {
                    StartPoint = lgb.GetAbsoluteStart (frame).ToVector2 (),
                    EndPoint = lgb.GetAbsoluteEnd (frame).ToVector2 (),
                };
                return new D2D1.LinearGradientBrush (renderTarget, props, GetStops (lgb.Stops));
            }

            var rgb = brush as RadialGradientBrush;
            if (rgb != null) {
                if (rgb.Stops.Count < 2) return null;
                var rad = rgb.GetAbsoluteRadius (frame);
                var center = rgb.GetAbsoluteCenter (frame);
                var focus = rgb.GetAbsoluteFocus (frame);
                var props = new D2D1.RadialGradientBrushProperties {
                    Center = center.ToVector2 (),
                    RadiusX = (float)rad.Width,
                    RadiusY = (float)rad.Height,
                    GradientOriginOffset = (focus - center).ToVector2 (),
                };
                return new D2D1.RadialGradientBrush (renderTarget, props, GetStops (rgb.Stops));
            }

            // TODO: Radial gradient brushes
            return new D2D1.SolidColorBrush (renderTarget, Colors.Black.ToColor4 ());
        }
Beispiel #3
0
        public static async Task <D2D.Brush> ToSharpDX(
            this Jupiter.Media.Brush brush,
            D2D.RenderTarget renderTarget,
            RectangleF rect)
        {
            if (brush == null)
            {
                return(null);
            }

            var solidColorBrush = brush as Jupiter.Media.SolidColorBrush;

            if (solidColorBrush != null)
            {
                var color = solidColorBrush.Color.ToSharpDX();

                return(new D2D.SolidColorBrush(
                           renderTarget,
                           color,
                           new D2D.BrushProperties
                {
                    Opacity = (float)solidColorBrush.Opacity
                }));
            }

            var linearGradientBrush = brush as Jupiter.Media.LinearGradientBrush;

            if (linearGradientBrush != null)
            {
                var properties = new D2D.LinearGradientBrushProperties();
                //properties.StartPoint =
                //    new Vector2(
                //        (float)(linearGradientBrush.StartPoint.X * renderTarget.Size.Width),
                //        (float)(linearGradientBrush.StartPoint.Y * renderTarget.Size.Height));
                //properties.EndPoint =
                //    new Vector2(
                //        (float)(linearGradientBrush.EndPoint.X * renderTarget.Size.Width),
                //        (float)(linearGradientBrush.EndPoint.Y * renderTarget.Size.Height));
                properties.StartPoint =
                    new Vector2(
                        rect.Left + (float)(linearGradientBrush.StartPoint.X * rect.Width),
                        rect.Top + (float)(linearGradientBrush.StartPoint.Y * rect.Height));
                properties.EndPoint =
                    new Vector2(
                        rect.Left + (float)(linearGradientBrush.EndPoint.X * rect.Width),
                        rect.Top + (float)(linearGradientBrush.EndPoint.Y * rect.Height));

                var brushProperties = new D2D.BrushProperties();

                brushProperties.Opacity = (float)linearGradientBrush.Opacity;

                if (linearGradientBrush.Transform != null)
                {
                    brushProperties.Transform = linearGradientBrush.Transform.ToSharpDX();
                }

                var gradientStopCollection = linearGradientBrush.GradientStops.ToSharpDX(renderTarget);

                return(new D2D.LinearGradientBrush(
                           renderTarget,
                           properties,
                           brushProperties,
                           gradientStopCollection));
            }

            var imageBrush = brush as Jupiter.Media.ImageBrush;

            if (imageBrush != null)
            {
                var bitmap = await imageBrush.ImageSource.ToSharpDX(renderTarget);

                var       w         = bitmap.PixelSize.Width;
                var       h         = bitmap.PixelSize.Height;
                Matrix3x2 transform = Matrix3x2.Identity;

                switch (imageBrush.Stretch)
                {
                case Stretch.None:
                    transform.M31 += rect.Left + rect.Width * 0.5f - w / 2;
                    transform.M32 += rect.Top + rect.Height * 0.5f - h / 2;
                    break;

                case Stretch.Fill:
                    transform = Matrix3x2.Scaling(
                        rect.Width / w,
                        rect.Height / h);
                    transform.M31 += rect.Left;
                    transform.M32 += rect.Top;
                    break;

                case Stretch.Uniform:
                    var bitmapAspectRatio  = (float)w / h;
                    var elementAspectRatio = rect.Width / rect.Height;

                    if (bitmapAspectRatio > elementAspectRatio)
                    {
                        var scale = rect.Width / w;
                        transform      = Matrix3x2.Scaling(scale);
                        transform.M31 += rect.Left;
                        transform.M32 += rect.Top + rect.Height * 0.5f - scale * h / 2;
                    }
                    else     // (elementAspectRatio >= bitmapAspectRatio)
                    {
                        var scale = rect.Height / h;
                        transform      = Matrix3x2.Scaling(scale);
                        transform.M31 += rect.Left + rect.Width * 0.5f - scale * w / 2;
                        transform.M32 += rect.Top;
                    }

                    break;

                case Stretch.UniformToFill:
                    var bitmapAspectRatio2  = (float)w / h;
                    var elementAspectRatio2 = rect.Width / rect.Height;

                    if (bitmapAspectRatio2 > elementAspectRatio2)
                    {
                        var scale = rect.Height / h;
                        transform      = Matrix3x2.Scaling(scale);
                        transform.M31 += rect.Left + rect.Width * 0.5f - scale * w / 2;
                        transform.M32 += rect.Top;
                    }
                    else     // (elementAspectRatio >= bitmapAspectRatio)
                    {
                        var scale = rect.Width / w;
                        transform      = Matrix3x2.Scaling(scale);
                        transform.M31 += rect.Left;
                        transform.M32 += rect.Top + rect.Height * 0.5f - scale * h / 2;
                    }

                    break;
                }


                return(new D2D.BitmapBrush1(
                           (D2D.DeviceContext)renderTarget,
                           bitmap,
                           new D2D.BitmapBrushProperties1
                {
                    ExtendModeX = D2D.ExtendMode.Clamp,
                    ExtendModeY = D2D.ExtendMode.Clamp,
                    InterpolationMode = D2D.InterpolationMode.HighQualityCubic
                })
                {
                    Opacity = (float)imageBrush.Opacity,
                    Transform = transform
                });
                //    var writeableBitmap = imageBrush.ImageSource as WriteableBitmap;
                //    var bitmapImage = imageBrush.ImageSource as BitmapImage;

                //    if (bitmapImage != null)
                //    {
                //        writeableBitmap =
                //            await WriteableBitmapFromBitmapImageExtension.FromBitmapImage(bitmapImage);
                //    }
                //    CompositionEngine c;

                //    return new D2D.BitmapBrush(
                //        renderTarget,
                //        writeableBitmap.ToSharpDX(),
                //}
            }

#if DEBUG
            throw new NotSupportedException("Only SolidColorBrush supported for now");
#else
            return(new D2D.SolidColorBrush(renderTarget, Color.Transparent));
#endif
        }
 public LinearBrush(D2D1.RenderTarget render, D2D1.LinearGradientBrushProperties radial,
                    D2D1.GradientStopCollection g) : base(render, radial, g)
 {
     RawStart = radial.StartPoint;
     RawEnd   = radial.EndPoint;
 }
        public static D2D.Brush ToD2DBrush(this Media.Brush brush, global::SharpDX.Vector2 renderSize, D2D.RenderTarget renderTarget)
        {
            if (brush == null)
            {
                return(null);
            }

            if (brush is Media.SolidColorBrush solid)
            {
                return(new D2D.SolidColorBrush(renderTarget, solid.Color.ToColor4()));
            }
            else if (brush is Media.LinearGradientBrush linear)
            {
                var brushProperties = new D2D.LinearGradientBrushProperties()
                {
                    StartPoint = linear.StartPoint.ToVector2(),
                    EndPoint   = linear.EndPoint.ToVector2()
                };

                if (linear.MappingMode == Media.BrushMappingMode.RelativeToBoundingBox)
                {
                    Point strtPoint = new Point(linear.StartPoint.X * renderSize.X, linear.StartPoint.Y * renderSize.Y);
                    Point endPoint  = new Point(linear.EndPoint.X * renderSize.X, linear.EndPoint.Y * renderSize.Y);
                    brushProperties.StartPoint = strtPoint.ToVector2();
                    brushProperties.EndPoint   = endPoint.ToVector2();
                }

                return(new D2D.LinearGradientBrush(renderTarget,
                                                   brushProperties,
                                                   new D2D.GradientStopCollection
                                                   (
                                                       renderTarget,
                                                       linear.GradientStops.Select(x => new D2D.GradientStop()
                {
                    Color = x.Color.ToColor4(), Position = (float)x.Offset
                }).ToArray(),
                                                       linear.ColorInterpolationMode.ToD2DColorInterpolationMode(),
                                                       linear.SpreadMethod.ToD2DExtendMode()
                                                   )
                                                   ));
            }
#if NETFX_CORE
#else
            else if (brush is Media.RadialGradientBrush radial)
            {
                var brushProperties = new D2D.RadialGradientBrushProperties()
                {
                    Center = radial.Center.ToVector2(),
                    GradientOriginOffset = radial.GradientOrigin.ToVector2(),
                    RadiusX = (float)radial.RadiusX,
                    RadiusY = (float)radial.RadiusY
                };

                if (radial.MappingMode == Media.BrushMappingMode.RelativeToBoundingBox)
                {
                    Point center = new Point(radial.Center.X * renderSize.X, radial.Center.Y * renderSize.Y);
                    Point gradientOriginOffset = new Point((radial.GradientOrigin.X - 0.5) * renderSize.X, (radial.GradientOrigin.Y - 0.5) * renderSize.Y);
                    brushProperties.Center = center.ToVector2();
                    brushProperties.GradientOriginOffset = gradientOriginOffset.ToVector2();
                    brushProperties.RadiusX = (float)(renderSize.X * radial.RadiusX);
                    brushProperties.RadiusY = (float)(renderSize.Y * radial.RadiusY);
                }

                return(new D2D.RadialGradientBrush(renderTarget,
                                                   brushProperties,
                                                   new D2D.GradientStopCollection
                                                   (
                                                       renderTarget,
                                                       radial.GradientStops.Select(x => new D2D.GradientStop()
                {
                    Color = x.Color.ToColor4(), Position = (float)x.Offset
                }).ToArray(),
                                                       radial.ColorInterpolationMode.ToD2DColorInterpolationMode(),
                                                       radial.SpreadMethod.ToD2DExtendMode()
                                                   )));
            }
#endif
            else
            {
                throw new NotImplementedException("Brush does not support yet.");
            }
        }
        public static D2D.Brush ToSharpDX(
            this Jupiter.Media.Brush brush,
            D2D.RenderTarget renderTarget,
            RectangleF rect)
        {
            if (brush == null)
            {
                return(null);
            }

            var solidColorBrush = brush as Jupiter.Media.SolidColorBrush;

            if (solidColorBrush != null)
            {
                var color = solidColorBrush.Color.ToSharpDX();

                return(new D2D.SolidColorBrush(
                           renderTarget,
                           color));
            }

            var linearGradientBrush = brush as Jupiter.Media.LinearGradientBrush;

            if (linearGradientBrush != null)
            {
                var properties = new D2D.LinearGradientBrushProperties();
                //properties.StartPoint =
                //    new DrawingPointF(
                //        (float)(linearGradientBrush.StartPoint.X * renderTarget.Size.Width),
                //        (float)(linearGradientBrush.StartPoint.Y * renderTarget.Size.Height));
                //properties.EndPoint =
                //    new DrawingPointF(
                //        (float)(linearGradientBrush.EndPoint.X * renderTarget.Size.Width),
                //        (float)(linearGradientBrush.EndPoint.Y * renderTarget.Size.Height));
                properties.StartPoint =
                    new DrawingPointF(
                        rect.Left + (float)(linearGradientBrush.StartPoint.X * rect.Width),
                        rect.Top + (float)(linearGradientBrush.StartPoint.Y * rect.Height));
                properties.EndPoint =
                    new DrawingPointF(
                        rect.Left + (float)(linearGradientBrush.EndPoint.X * rect.Width),
                        rect.Top + (float)(linearGradientBrush.EndPoint.Y * rect.Height));

                var brushProperties = new D2D.BrushProperties();

                brushProperties.Opacity = (float)linearGradientBrush.Opacity;

                if (linearGradientBrush.Transform != null)
                {
                    brushProperties.Transform = linearGradientBrush.Transform.ToSharpDX();
                }

                var gradientStopCollection = linearGradientBrush.GradientStops.ToSharpDX(renderTarget);

                return(new D2D.LinearGradientBrush(
                           renderTarget,
                           properties,
                           //brushProperties,
                           gradientStopCollection));
            }

            //var imageBrush = brush as Jupiter.Media.ImageBrush;

            //if (imageBrush != null)
            //{
            //    var writeableBitmap = imageBrush.ImageSource as WriteableBitmap;
            //    var bitmapImage = imageBrush.ImageSource as BitmapImage;

            //    if (bitmapImage != null)
            //    {
            //        writeableBitmap =
            //            await WriteableBitmapFromBitmapImageExtension.FromBitmapImage(bitmapImage);
            //    }
            //    CompositionEngine c;

            //    return new D2D.BitmapBrush(
            //        renderTarget,
            //        writeableBitmap.ToSharpDX(),
            //}

#if DEBUG
            throw new NotSupportedException("Only SolidColorBrush supported for now");
#else
            return(new D2D.SolidColorBrush(renderTarget, Color.Transparent));
#endif
        }
        public static async Task<D2D.Brush> ToSharpDX(
            this Jupiter.Media.Brush brush,
            D2D.RenderTarget renderTarget,
            RectangleF rect)
        {
            if (brush == null)
                return null;

            var solidColorBrush = brush as Jupiter.Media.SolidColorBrush;

            if (solidColorBrush != null)
            {
                var color = solidColorBrush.Color.ToSharpDX();
                
                return new D2D.SolidColorBrush(
                    renderTarget,
                    color,
                    new D2D.BrushProperties
                    {
                        Opacity = (float)solidColorBrush.Opacity
                    });
            }

            var linearGradientBrush = brush as Jupiter.Media.LinearGradientBrush;

            if (linearGradientBrush != null)
            {
                var properties = new D2D.LinearGradientBrushProperties();
                //properties.StartPoint =
                //    new Vector2(
                //        (float)(linearGradientBrush.StartPoint.X * renderTarget.Size.Width),
                //        (float)(linearGradientBrush.StartPoint.Y * renderTarget.Size.Height));
                //properties.EndPoint =
                //    new Vector2(
                //        (float)(linearGradientBrush.EndPoint.X * renderTarget.Size.Width),
                //        (float)(linearGradientBrush.EndPoint.Y * renderTarget.Size.Height));
                properties.StartPoint =
                    new Vector2(
                        rect.Left + (float)(linearGradientBrush.StartPoint.X * rect.Width),
                        rect.Top + (float)(linearGradientBrush.StartPoint.Y * rect.Height));
                properties.EndPoint =
                    new Vector2(
                        rect.Left + (float)(linearGradientBrush.EndPoint.X * rect.Width),
                        rect.Top + (float)(linearGradientBrush.EndPoint.Y * rect.Height));

                var brushProperties = new D2D.BrushProperties();

                brushProperties.Opacity = (float)linearGradientBrush.Opacity;

                if (linearGradientBrush.Transform != null)
                {
                    brushProperties.Transform = linearGradientBrush.Transform.ToSharpDX();
                }

                var gradientStopCollection = linearGradientBrush.GradientStops.ToSharpDX(renderTarget);

                return new D2D.LinearGradientBrush(
                    renderTarget,
                    properties,
                    brushProperties,
                    gradientStopCollection);
            }

            var imageBrush = brush as Jupiter.Media.ImageBrush;

            if (imageBrush != null)
            {
                var bitmap = await imageBrush.ImageSource.ToSharpDX(renderTarget);

                var w = bitmap.PixelSize.Width;
                var h = bitmap.PixelSize.Height;
                Matrix3x2 transform = Matrix3x2.Identity;

                switch (imageBrush.Stretch)
                {
                    case Stretch.None:
                        transform.M31 += rect.Left + rect.Width * 0.5f - w / 2;
                        transform.M32 += rect.Top + rect.Height * 0.5f - h / 2;
                        break;
                    case Stretch.Fill:
                        transform = Matrix3x2.Scaling(
                            rect.Width / w,
                            rect.Height / h);
                        transform.M31 += rect.Left;
                        transform.M32 += rect.Top;
                        break;
                    case Stretch.Uniform:
                        var bitmapAspectRatio = (float)w / h;
                        var elementAspectRatio = rect.Width / rect.Height;

                        if (bitmapAspectRatio > elementAspectRatio)
                        {
                            var scale = rect.Width / w;
                            transform = Matrix3x2.Scaling(scale);
                            transform.M31 += rect.Left;
                            transform.M32 += rect.Top + rect.Height * 0.5f - scale * h / 2;
                        }
                        else // (elementAspectRatio >= bitmapAspectRatio)
                        {
                            var scale = rect.Height / h;
                            transform = Matrix3x2.Scaling(scale);
                            transform.M31 += rect.Left + rect.Width * 0.5f - scale * w / 2;
                            transform.M32 += rect.Top;
                        }

                        break;
                    case Stretch.UniformToFill:
                        var bitmapAspectRatio2 = (float)w / h;
                        var elementAspectRatio2 = rect.Width / rect.Height;

                        if (bitmapAspectRatio2 > elementAspectRatio2)
                        {
                            var scale = rect.Height / h;
                            transform = Matrix3x2.Scaling(scale);
                            transform.M31 += rect.Left + rect.Width * 0.5f - scale * w / 2;
                            transform.M32 += rect.Top;
                        }
                        else // (elementAspectRatio >= bitmapAspectRatio)
                        {
                            var scale = rect.Width / w;
                            transform = Matrix3x2.Scaling(scale);
                            transform.M31 += rect.Left;
                            transform.M32 += rect.Top + rect.Height * 0.5f - scale * h / 2;
                        }

                        break;
                }
                

                return new D2D.BitmapBrush1(
                    (D2D.DeviceContext)renderTarget,
                    bitmap,
                    new D2D.BitmapBrushProperties1
                    {
                        ExtendModeX = D2D.ExtendMode.Clamp,
                        ExtendModeY = D2D.ExtendMode.Clamp,
                        InterpolationMode = D2D.InterpolationMode.HighQualityCubic
                    })
                    {
                        Opacity = (float)imageBrush.Opacity,
                        Transform = transform
                    };
                //    var writeableBitmap = imageBrush.ImageSource as WriteableBitmap;
                //    var bitmapImage = imageBrush.ImageSource as BitmapImage;

                //    if (bitmapImage != null)
                //    {
                //        writeableBitmap =
                //            await WriteableBitmapFromBitmapImageExtension.FromBitmapImage(bitmapImage);
                //    }
                //    CompositionEngine c;

                //    return new D2D.BitmapBrush(
                //        renderTarget,
                //        writeableBitmap.ToSharpDX(),
                //}
            }

#if DEBUG
            throw new NotSupportedException("Only SolidColorBrush supported for now");
#else
            return new D2D.SolidColorBrush(renderTarget, Color.Transparent);
#endif
        }
        public static D2D.Brush ToSharpDX(
            this Jupiter.Media.Brush brush,
            D2D.RenderTarget renderTarget,
            RectangleF rect)
        {
            if (brush == null)
                return null;

            var solidColorBrush = brush as Jupiter.Media.SolidColorBrush;

            if (solidColorBrush != null)
            {
                var color = solidColorBrush.Color.ToSharpDX();

                return new D2D.SolidColorBrush(
                    renderTarget,
                    color);
            }

            var linearGradientBrush = brush as Jupiter.Media.LinearGradientBrush;

            if (linearGradientBrush != null)
            {
                var properties = new D2D.LinearGradientBrushProperties();
                //properties.StartPoint =
                //    new DrawingPointF(
                //        (float)(linearGradientBrush.StartPoint.X * renderTarget.Size.Width),
                //        (float)(linearGradientBrush.StartPoint.Y * renderTarget.Size.Height));
                //properties.EndPoint =
                //    new DrawingPointF(
                //        (float)(linearGradientBrush.EndPoint.X * renderTarget.Size.Width),
                //        (float)(linearGradientBrush.EndPoint.Y * renderTarget.Size.Height));
                properties.StartPoint =
                    new DrawingPointF(
                        rect.Left + (float)(linearGradientBrush.StartPoint.X * rect.Width),
                        rect.Top + (float)(linearGradientBrush.StartPoint.Y * rect.Height));
                properties.EndPoint =
                    new DrawingPointF(
                        rect.Left + (float)(linearGradientBrush.EndPoint.X * rect.Width),
                        rect.Top + (float)(linearGradientBrush.EndPoint.Y * rect.Height));

                var brushProperties = new D2D.BrushProperties();

                brushProperties.Opacity = (float)linearGradientBrush.Opacity;

                if (linearGradientBrush.Transform != null)
                {
                    brushProperties.Transform = linearGradientBrush.Transform.ToSharpDX();
                }

                var gradientStopCollection = linearGradientBrush.GradientStops.ToSharpDX(renderTarget);

                return new D2D.LinearGradientBrush(
                    renderTarget,
                    properties,
                    //brushProperties,
                    gradientStopCollection);
            }

            //var imageBrush = brush as Jupiter.Media.ImageBrush;

            //if (imageBrush != null)
            //{
            //    var writeableBitmap = imageBrush.ImageSource as WriteableBitmap;
            //    var bitmapImage = imageBrush.ImageSource as BitmapImage;

            //    if (bitmapImage != null)
            //    {
            //        writeableBitmap =
            //            await WriteableBitmapFromBitmapImageExtension.FromBitmapImage(bitmapImage);
            //    }
            //    CompositionEngine c;

            //    return new D2D.BitmapBrush(
            //        renderTarget,
            //        writeableBitmap.ToSharpDX(),
            //}

#if DEBUG
            throw new NotSupportedException("Only SolidColorBrush supported for now");
#else
            return new D2D.SolidColorBrush(renderTarget, Color.Transparent);
#endif
        }