Пример #1
1
        public Button(string pText, float pX, float pY, float pWidth, float pHeight, bool pSmall)
        {
            width = pWidth;
            height = pHeight;
            x = pX;
            y = pY;

            // Text properties
            var brushProp = new LinearGradientBrushProperties();
            brushProp.StartPoint = new Vector2(x, y);
            brushProp.EndPoint = new Vector2(x, y + height);

            var stops = new GradientStop[2];
            stops[0] = new GradientStop() { Color = new Color4(1, 1, 1, 0.5f), Position = 0 };
            stops[1] = new GradientStop() { Color = new Color4(1, 1, 1, 1.0f), Position = 1 };

            textBrush = new LinearGradientBrush(GraphicsWindow.Instance.RenderTarget2D, brushProp, new GradientStopCollection(GraphicsWindow.Instance.RenderTarget2D, stops));
            textLayout = new TextLayout(Factories.FactoryWrite, pText, pSmall? Constants.SmallFont : Constants.RegularFont, width, height);

            // Frame properties
            borderBrush = new SolidColorBrush(GraphicsWindow.Instance.RenderTarget2D, new Color4(0, 0, 0, 0.3f));
            backBrush = new SolidColorBrush(GraphicsWindow.Instance.RenderTarget2D, new Color4(0.1f, 0.3f, 0.4f, 0.5f));
            highlightBrush = new SolidColorBrush(GraphicsWindow.Instance.RenderTarget2D, new Color4(0.2f, 0.6f, 0.8f, 0.5f));
            highlight = false;
        }
        public D2DLinearGradientBrush(RenderContext context, PointF startPoint, PointF endPoint, params Color[] gradientColors)
        {
            if (gradientColors.Length < 2)
            {
                throw new ArgumentException("Linear gradient brush requires at least 2 colors.", nameof(gradientColors));
            }
            var properties = new LinearGradientBrushProperties {
                StartPoint = new RawVector2(startPoint.X, startPoint.Y),
                EndPoint   = new RawVector2(endPoint.X, endPoint.Y)
            };
            var colorCount    = gradientColors.Length;
            var gradientStops = new GradientStop[colorCount];

            for (var i = 0; i < colorCount; ++i)
            {
                gradientStops[i] = new GradientStop {
                    Color    = gradientColors[i].ToRC4(),
                    Position = (float)i / (colorCount - 1)
                };
            }
            var collection = new GradientStopCollection(context.RenderTarget.DeviceContext2D, gradientStops);

            NativeBrush = new LinearGradientBrush(context.RenderTarget.DeviceContext2D, properties, collection);
            _collection = collection;
        }
Пример #3
0
        public override void OnGotContext(RenderContext context)
        {
            base.OnGotContext(context);

            NotesImage = D2DHelper.LoadBitmap(Definitions.NotesBitmapFilePath, context.RenderTarget);

            var size  = context.ClientSize;
            var props = new LinearGradientBrushProperties {
                StartPoint = new RawVector2(size.Height, 0),
                EndPoint   = new RawVector2(0, size.Height)
            };
            var gradientStops = new List <GradientStop>();
            var colorCount    = RibbonColors.Length;

            for (var i = 0; i < colorCount; ++i)
            {
                gradientStops.Add(new GradientStop {
                    Color    = RibbonColors[i].ColorToRC4(),
                    Position = (float)i / (colorCount - 1)
                });
            }
            var collection = new GradientStopCollection(context.RenderTarget, gradientStops.ToArray(), ExtendMode.Wrap);

            RibbonBrush = new LinearGradientBrushEx(context.RenderTarget, props, collection);

            SyncLinePen = new Pen(Color.White, 3, context);
        }
Пример #4
0
        internal LinearGradientBrush(Direct2DRenderTarget renderTargetOwner, 
                                     GradientStop[] gradientStops, 
                                     ExtendMode extendMode, 
                                     PointF startPoint, 
                                     PointF endPoint)
        {
            m_renderTargetOwner = renderTargetOwner;

            var gradientStopList = new List<SlimDX.Direct2D.GradientStop>(gradientStops.Length);

            for (int i = 0; i < gradientStops.Length; i++)
            {
                gradientStopList.Add(gradientStops[i].InternalGradientStop);
            }

            var props = new LinearGradientBrushProperties();
            props.StartPoint = startPoint.InternalPointF;
            props.EndPoint = endPoint.InternalPointF;

            m_startPoint = startPoint;
            m_endPoint = endPoint;

            var internalRt = m_renderTargetOwner.InternalRenderTarget;

            m_internalGradientStopCollection = new GradientStopCollection(internalRt, 
                                                                          gradientStopList.ToArray(), 
                                                                          Gamma.Linear, 
                                                                          (SlimDX.Direct2D.ExtendMode)extendMode);

            m_internalLinearGradientBrush = new SlimDX.Direct2D.LinearGradientBrush(internalRt,
                                                                                    m_internalGradientStopCollection, 
                                                                                    props);
        }
Пример #5
0
 public D2DSpriteLinearGradientBrush(D2DSpriteBatch batch, D2DSpriteGradientStopCollection gradientStops,
                                     LinearGradientBrushProperties lgbp)
 {
     _linearGradientBrushProperties = lgbp;
     _gradientStops        = gradientStops;
     _batch                = batch;
     batch.BatchDisposing += batch_BatchDisposing;
     Brush = new LinearGradientBrush(_batch.DWRenderTarget, _gradientStops.GradientStopCollection, _linearGradientBrushProperties);
 }
Пример #6
0
        private void Selector_D2DPaint(D2DGraphics g)
        {
            if (!IsSelected)
            {
                return;
            }

            g.RenderTarget.AntialiasMode = AntialiasMode.Aliased;
            RawRectangleF   rect     = new RawRectangleF(1, 1, selector.Width - 2, selector.Height - 9);
            RawColor4       rawColor = GDIDataD2DUtils.TransToRawColor4(SelectorColor);
            SolidColorBrush brush    = new SolidColorBrush(g.RenderTarget, rawColor);

            RoundedRectangle rounderRect = new RoundedRectangle
            {
                RadiusX = 1,
                RadiusY = 1,
                Rect    = rect
            };

            g.RenderTarget.DrawRoundedRectangle(rounderRect, brush, 2f);
            brush.Dispose();


            //
            GradientStop[] gradientStops = new GradientStop[2];
            gradientStops[0].Color    = new RawColor4(0, 0, 0, 0.1f);
            gradientStops[0].Position = 0f;
            gradientStops[1].Color    = new RawColor4(0, 0, 0, 0);
            gradientStops[1].Position = 1f;

            //
            GradientStopCollection gradientStopCollection = new GradientStopCollection(g.RenderTarget, gradientStops, Gamma.StandardRgb, ExtendMode.Clamp);

            //
            rect = new RawRectangleF(0, selector.Height - 9, selector.Width - 1, selector.Height - 1);
            LinearGradientBrushProperties props = new LinearGradientBrushProperties()
            {
                StartPoint = new RawVector2(rect.Left, rect.Top),
                EndPoint   = new RawVector2(rect.Left, rect.Bottom)
            };

            SharpDX.Direct2D1.LinearGradientBrush linearGradientBrush = new SharpDX.Direct2D1.LinearGradientBrush(g.RenderTarget, props, gradientStopCollection);
            g.RenderTarget.FillRectangle(rect, linearGradientBrush);
            gradientStopCollection.Dispose();
            linearGradientBrush.Dispose();

            //Graphics gdiGraphics = g.CreateGdiGraphics();
            //gdiGraphics.SmoothingMode = SmoothingMode.HighQuality;
            //RectangleF rect1 = new RectangleF(0, 0, (float)Math.Ceiling(Width - 1), (float)Math.Ceiling(Height - 1));
            //Pen pen = new Pen(Color.FromArgb(255, 191, 152, 90), 1f);
            //DrawUtils.DrawRoundRectangle(gdiGraphics, Pens.Gold, rect1, 4);
            //g.RelaseGdiGraphics(gdiGraphics);
            //pen.Dispose();
        }
Пример #7
0
        /// <summary>
        /// Create a <see cref="LinearGradientBrush"/> from parameters.
        /// </summary>
        /// <param name="renderTarget">The render target to use.</param>
        /// <param name="gradientStops">Collection of <see cref="GradientStop"/>s.</param>
        /// <param name="startPosition">The <see cref="LinearGradientBrush"/> start vector.</param>
        /// <param name="endPosition">The <see cref="LinearGradientBrush"/> end vector.</param>
        /// <returns><see cref="LinearGradientBrush"/></returns>
        public static LinearGradientBrush GetLinearGradientBrush(RenderTarget renderTarget, IList <GradientStop> gradientStops, RawVector2 startPosition, RawVector2 endPosition)
        {
            var lprops = new LinearGradientBrushProperties
            {
                StartPoint = startPosition,
                EndPoint   = endPosition
            };

            var brush = new LinearGradientBrush(renderTarget, lprops, null, new GradientStopCollection(renderTarget, gradientStops.ToArray()));

            return(brush);
        }
Пример #8
0
        /// <summary>
        /// Creates new brushes in order to perform animations on the individual gradient based squares.
        /// </summary>
        /// <param name="direct2DWindowTarget">The object we use to draw! Our device! Our soul!</param>
        private static void AnimateBrushGradient(RenderTarget direct2DWindowTarget)
        {
            // Animate the gradientstops.
            for (int x = 0; x < _gradientStops.Length; x++)
            {
                float currentPosition = _gradientStops[x].Position;

                if (currentPosition >= 1F)
                {
                    currentPosition = 0F;
                }
                else
                {
                    currentPosition += 0.01F;
                }

                _gradientStops[x].Position = currentPosition;
            }

            // Conditionally dispose.
            _squareGradientBrush?.Factory?.Dispose();
            _squareGradientBrush?.GradientStopCollection?.Dispose();
            _squareGradientBrush?.Dispose();
            _squareRadialGradientBrush?.Factory?.Dispose();
            _squareRadialGradientBrush?.GradientStopCollection?.Dispose();
            _squareRadialGradientBrush?.Dispose();

            // Fix memory leak with GradientStopCollection
            // Thanks, anonymous StackOverflow user! https://stackoverflow.com/questions/47876001/mysterious-direct2d-memory-leak-with-gradients
            using (var gradientStopCollection = new GradientStopCollection(direct2DWindowTarget, _gradientStops))
            {
                /* How about a brush with a gradient? */
                LinearGradientBrushProperties linearGradientBrushProperties = new LinearGradientBrushProperties()
                {
                    StartPoint = new RawVector2(_gradientRectangle.Top - 50, _gradientRectangle.Left - 50),      // Note: Physical location of the start point, in this case top left corner.
                    EndPoint   = new RawVector2(_gradientRectangle.Bottom + 50, _gradientRectangle.Right + 50)   // Note: Physical location of the start point, in this case bottom right corner.
                };
                _squareGradientBrush = new LinearGradientBrush(direct2DWindowTarget, linearGradientBrushProperties, gradientStopCollection);

                /* A radial brush? */
                RadialGradientBrushProperties radialGradientBrushProperties = new RadialGradientBrushProperties()
                {
                    Center = new RawVector2(_radialRectangle.Left + 50, _radialRectangle.Top + 50),    // Note: Physical location of the start point, in this case the actual center.
                    GradientOriginOffset = new RawVector2(0, 0),
                    RadiusX = 100,
                    RadiusY = 100
                };
                _squareRadialGradientBrush = new RadialGradientBrush(direct2DWindowTarget, radialGradientBrushProperties, gradientStopCollection);
            }
        }
Пример #9
0
        private void LinearGradientBrushAddClicked(object sender, EventArgs e)
        {
            ExtendMode ex = gradBrushExtendModeCombo.SelectedIndex > 0 ? (ExtendMode)gradBrushExtendModeCombo.SelectedIndex : ExtendMode.Clamp;
            Gamma      gamma;

            switch (gammaComboBox.SelectedIndex)
            {
            case 0:
                gamma = Gamma.Linear;
                break;

            case 1:
                gamma = Gamma.StandardRgb;
                break;

            default:
                throw new InvalidOperationException("Unknown gamma selected");
            }

            GradientStop[] stops =
            {
                new GradientStop(0.0f, color1),
                new GradientStop(1.0f, color2),
            };

            GradientStopCollection stopCollection = renderTarget.CreateGradientStopCollection(
                stops, gamma, ex);

            LinearGradientBrushProperties properties;

            if (ex == ExtendMode.Clamp)
            {
                properties = new LinearGradientBrushProperties(
                    new Point2F(50, 50), new Point2F(600, 400));
            }
            else
            {
                properties = new LinearGradientBrushProperties(
                    new Point2F(50, 50), new Point2F(0, 0));
            }


            LinearGradientBrush brush = renderTarget.CreateLinearGradientBrush(
                properties, stopCollection);

            parent.brushes.Add(brush);
            parent.currentBrushIndex = parent.brushes.Count - 1;
            FillBrushesListBox();
        }
Пример #10
0
        /// <summary>
        /// This method creates the render target and all associated D2D and DWrite resources
        /// </summary>
        void CreateDeviceResources()
        {
            // Only calls if resources have not been initialize before
            if (renderTarget == null)
            {
                // The text format
                textFormat = dwriteFactory.CreateTextFormat("Bodoni MT", 24, DWrite.FontWeight.Normal, DWrite.FontStyle.Italic, DWrite.FontStretch.Normal);

                // Create the render target
                SizeU size = new SizeU((uint)host.ActualWidth, (uint)host.ActualHeight);
                RenderTargetProperties     props     = new RenderTargetProperties();
                HwndRenderTargetProperties hwndProps = new HwndRenderTargetProperties(host.Handle, size, PresentOptions.None);
                renderTarget = d2dFactory.CreateHwndRenderTarget(props, hwndProps);

                // A black brush to be used for drawing text
                ColorF cf = new ColorF(0, 0, 0, 1);
                blackBrush = renderTarget.CreateSolidColorBrush(cf);

                // Create a linear gradient.
                GradientStop[] stops =
                {
                    new GradientStop(1, new ColorF(1f, 0f, 0f, 0.25f)),
                    new GradientStop(0, new ColorF(0f, 0f, 1f, 1f))
                };

                GradientStopCollection        pGradientStops = renderTarget.CreateGradientStopCollection(stops, Gamma.Linear, ExtendMode.Wrap);
                LinearGradientBrushProperties gradBrushProps = new LinearGradientBrushProperties(new Point2F(50, 25), new Point2F(25, 50));

                linearGradientBrush = renderTarget.CreateLinearGradientBrush(gradBrushProps, pGradientStops);

                gridPatternBitmapBrush = CreateGridPatternBrush(renderTarget);

                solidBrush1 = renderTarget.CreateSolidColorBrush(new ColorF(0.3F, 0.5F, 0.65F, 0.25F));
                solidBrush2 = renderTarget.CreateSolidColorBrush(new ColorF(0.0F, 0.0F, 0.65F, 0.5F));
                solidBrush3 = renderTarget.CreateSolidColorBrush(new ColorF(0.9F, 0.5F, 0.3F, 0.75F));

                // Create a linear gradient.
                stops[0] = new GradientStop(1, new ColorF(0f, 0f, 0f, 0.25f));
                stops[1] = new GradientStop(0, new ColorF(1f, 1f, 0.2f, 1f));
                GradientStopCollection radiantGradientStops = renderTarget.CreateGradientStopCollection(stops, Gamma.Linear, ExtendMode.Wrap);

                RadialGradientBrushProperties radialBrushProps = new RadialGradientBrushProperties(new Point2F(25, 25), new Point2F(0, 0), 10, 10);
                radialGradientBrush = renderTarget.CreateRadialGradientBrush(radialBrushProps, radiantGradientStops);
            }
        }
Пример #11
0
 public static LinearGradientBrush New(string name, Direct2DDevice device, LinearGradient linearGradient)
 {
     using (var d2dGradientStopCollection = new SharpDX.Direct2D1.GradientStopCollection(device,
                                                                                         linearGradient.GradientStops.Select(gs => (SharpDX.Direct2D1.GradientStop)gs).ToArray(),
                                                                                         (SharpDX.Direct2D1.ExtendMode)linearGradient.GradientStops.ExtendMode))
     {
         var lgbProperties = new LinearGradientBrushProperties()
         {
             StartPoint = linearGradient.StartPoint, EndPoint = linearGradient.EndPoint
         };
         var brushProperties = new BrushProperties()
         {
             Opacity = linearGradient.Opacity
         };
         var brush = new SharpDX.Direct2D1.LinearGradientBrush(device, lgbProperties, brushProperties, d2dGradientStopCollection);
         return(new LinearGradientBrush(name, device, linearGradient, brush));
     }
 }
Пример #12
0
        public Title(string pText, float pX, float pY)
        {
            width = Constants.Width - 2 * pX;
            height = 70;
            x = pX;
            y = pY;

            var brushProp = new LinearGradientBrushProperties();
            brushProp.StartPoint = new Vector2(x, y);
            brushProp.EndPoint = new Vector2(x, y + height);

            var stops = new GradientStop[2];
            stops[0] = new GradientStop() { Color = new Color4(0.1f, 0.3f, 0.4f, 0.5f), Position = 0 };
            stops[1] = new GradientStop() { Color = new Color4(0.1f, 0.3f, 0.4f, 0.5f), Position = 1 };

            brush = new LinearGradientBrush(GraphicsWindow.Instance.RenderTarget2D, brushProp, new GradientStopCollection(GraphicsWindow.Instance.RenderTarget2D, stops));
            textLayout = new TextLayout(Factories.FactoryWrite, pText, Constants.BoldFont, width, height);
        }
Пример #13
0
        internal override void Create()
        {
            if (NativeBrush != null)
                NativeBrush.Dispose();
            var stops = new GradientStop[m_gradientStops.Length];
            for (int s = 0; s < m_gradientStops.Length; s++)
            {
                stops[s].Color = m_gradientStops[s].Color.ToColor4();
                stops[s].Position = m_gradientStops[s].Position;
            }

            using (var stopcol = new GradientStopCollection(Owner.D2dRenderTarget, stops, (Gamma)m_gamma, (ExtendMode)m_extendMode))
            {
                var props = new LinearGradientBrushProperties();
                props.StartPoint = new DrawingPointF(m_start.X, m_start.Y);
                props.EndPoint = new DrawingPointF(m_end.X, m_end.Y);
                NativeBrush = new LinearGradientBrush(Owner.D2dRenderTarget, props, stopcol);
            }
        }
Пример #14
0
        void FillIn(D2DGraphics g, Color boxColor)
        {
            RawRectangleF   rect     = new RawRectangleF(FillMargin.left + 1, FillMargin.top + 1, Width - FillMargin.right, Height - FillMargin.bottom);
            RawColor4       rawColor = GDIDataD2DUtils.TransToRawColor4(boxColor);
            SolidColorBrush brush    = new SolidColorBrush(g.RenderTarget, rawColor);

            g.RenderTarget.DrawRectangle(rect, brush, 1);
            brush.Dispose();

            //
            RawRectangleF fillRect = new RawRectangleF(FillMargin.left, FillMargin.top, Width - FillMargin.right, Height - FillMargin.bottom);

            g.RenderTarget.AntialiasMode = AntialiasMode.PerPrimitive;
            GradientStop[] gradientStops = new GradientStop[3];


            gradientStops[0].Color    = GDIDataD2DUtils.TransToRawColor4(Color.FromArgb(255, boxColor.R, boxColor.G, boxColor.B));
            gradientStops[0].Position = 0f;


            gradientStops[1].Color    = GDIDataD2DUtils.TransToRawColor4(Color.FromArgb(20, boxColor.R, boxColor.G, boxColor.B));
            gradientStops[1].Position = 0.5f;

            gradientStops[2].Color    = GDIDataD2DUtils.TransToRawColor4(Color.FromArgb(0, boxColor.R, boxColor.G, boxColor.B));
            gradientStops[2].Position = 1f;

            //
            GradientStopCollection gradientStopCollection = new GradientStopCollection(g.RenderTarget, gradientStops, Gamma.StandardRgb, ExtendMode.Clamp);

            LinearGradientBrushProperties props = new LinearGradientBrushProperties()
            {
                StartPoint = new RawVector2(fillRect.Left, fillRect.Top),
                EndPoint   = new RawVector2(fillRect.Right, fillRect.Bottom)
            };

            SharpDX.Direct2D1.LinearGradientBrush linearGradientBrush = new SharpDX.Direct2D1.LinearGradientBrush(g.RenderTarget, props, gradientStopCollection);
            g.RenderTarget.FillRectangle(fillRect, linearGradientBrush);
            gradientStopCollection.Dispose();
            linearGradientBrush.Dispose();
        }
Пример #15
0
        internal override void Create()
        {
            if (NativeBrush != null)
            {
                NativeBrush.Dispose();
            }
            var stops = new GradientStop[m_gradientStops.Length];

            for (int s = 0; s < m_gradientStops.Length; s++)
            {
                stops[s].Color    = m_gradientStops[s].Color.ToColor4();
                stops[s].Position = m_gradientStops[s].Position;
            }

            using (var stopcol = new GradientStopCollection(Owner.D2dRenderTarget, stops, (Gamma)m_gamma, (ExtendMode)m_extendMode))
            {
                var props = new LinearGradientBrushProperties
                {
                    StartPoint = m_start.ToSharpDX(),
                    EndPoint   = m_end.ToSharpDX()
                };
                NativeBrush = new LinearGradientBrush(Owner.D2dRenderTarget, props, stopcol);
            }
        }
Пример #16
0
        private void Form1_Load(object sender, EventArgs e)
        {
            //Init Direct Draw
            //Set Rendering properties
            RenderTargetProperties renderProp = new RenderTargetProperties()
            {
                DpiX        = 0,
                DpiY        = 0,
                MinLevel    = FeatureLevel.Level_10,
                PixelFormat = new PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied),
                Type        = RenderTargetType.Hardware,
                Usage       = RenderTargetUsage.None
            };

            //set hwnd target properties (permit to attach Direct2D to window)
            HwndRenderTargetProperties winProp = new HwndRenderTargetProperties()
            {
                Hwnd           = this.Handle,
                PixelSize      = new Size2(this.ClientSize.Width, this.ClientSize.Height),
                PresentOptions = PresentOptions.Immediately
            };

            //target creation
            target = new WindowRenderTarget(factory, renderProp, winProp);

            //create red and white brushes
            redBrush   = new SharpDX.Direct2D1.SolidColorBrush(target, SharpDX.Color.Red);
            whiteBrush = new SharpDX.Direct2D1.SolidColorBrush(target, SharpDX.Color.White);

            //create a linear gradient brush
            var grad = new LinearGradientBrushProperties()
            {
                StartPoint = new Vector2(ClientSize.Width / 2, ClientSize.Height / 2),
                EndPoint   = new Vector2(ClientSize.Width, ClientSize.Height)
            };

            var stopCollection = new GradientStopCollection(target, new GradientStop[]
            {
                new GradientStop()
                {
                    Color = SharpDX.Color.Azure, Position = 0F
                },
                new GradientStop()
                {
                    Color = SharpDX.Color.Yellow, Position = 0.2F
                },
                new GradientStop()
                {
                    Color = SharpDX.Color.Green, Position = 0.4F
                },
                new GradientStop()
                {
                    Color = SharpDX.Color.Red, Position = 1F
                },
            }, ExtendMode.Mirror);

            gradient = new SharpDX.Direct2D1.LinearGradientBrush(target, grad, stopCollection);



            //create textformat
            textFormat = new SharpDX.DirectWrite.TextFormat(factoryWrite, "Arial", 36);

            //avoid artifacts
            this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
        }
Пример #17
0
 public ILinearGradientBrush CreateLinearGradientBrush(LinearGradientBrushProperties linearGradientBrushProperties, BrushProperties?brushProperties, IGradientStopCollection gradientStopCollection) =>
 base.innerRefT.CreateLinearGradientBrush(linearGradientBrushProperties, brushProperties, gradientStopCollection);
Пример #18
0
        private LinearGradientBrush GetCachedLinearGradientBrush(Color color1, Color color2)
        {
            long kh = color1.ToArgb(); //  high bits
            long kl = color2.ToArgb(); //  low bits
            var colorKey = (kh << 32) | kl;

            LinearGradientBrush brush;
            if (!m_linearGradients.TryGetValue(colorKey, out brush))
            {
                var stops = new GradientStop[2];
                stops[0].Color = color1.ToColor4();
                stops[0].Position = 0;
                stops[1].Color = color2.ToColor4();
                stops[1].Position = 1;
                using (var stopCollection = new GradientStopCollection(m_renderTarget, stops, Gamma.StandardRgb, ExtendMode.Clamp))
                {
                    var props = new LinearGradientBrushProperties();
                    brush = new LinearGradientBrush(m_renderTarget, props, stopCollection);
                }
                m_linearGradients.Add(colorKey, brush);
            }

            return brush;
        }
Пример #19
0
        /// <summary>
        /// This method creates the render target and all associated D2D and DWrite resources
        /// </summary>
        void CreateDeviceResources()
        {
            // Only calls if resources have not been initialize before
            if (renderTarget == null)
            {
                // The text format
                textFormat = dwriteFactory.CreateTextFormat("Bodoni MT", 24, DWrite.FontWeight.Normal, DWrite.FontStyle.Italic, DWrite.FontStretch.Normal);

                // Create the render target
                SizeU size = new SizeU((uint)host.ActualWidth, (uint)host.ActualHeight);
                RenderTargetProperties props = new RenderTargetProperties();
                HwndRenderTargetProperties hwndProps = new HwndRenderTargetProperties(host.Handle, size, PresentOptions.None);
                renderTarget = d2dFactory.CreateHwndRenderTarget(props, hwndProps);

                // A black brush to be used for drawing text
                ColorF cf = new ColorF(0, 0, 0, 1);
                blackBrush = renderTarget.CreateSolidColorBrush(cf);

                // Create a linear gradient.
                GradientStop[] stops = 
                { 
                    new GradientStop(1, new ColorF(1f, 0f, 0f, 0.25f)),
                    new GradientStop(0, new ColorF(0f, 0f, 1f, 1f))
                };

                GradientStopCollection pGradientStops = renderTarget.CreateGradientStopCollection(stops, Gamma.Linear, ExtendMode.Wrap);
                LinearGradientBrushProperties gradBrushProps = new LinearGradientBrushProperties(new Point2F(50, 25), new Point2F(25, 50));

                linearGradientBrush = renderTarget.CreateLinearGradientBrush(gradBrushProps, pGradientStops);

                gridPatternBitmapBrush = CreateGridPatternBrush(renderTarget);

                solidBrush1 = renderTarget.CreateSolidColorBrush(new ColorF(0.3F, 0.5F, 0.65F, 0.25F));
                solidBrush2 = renderTarget.CreateSolidColorBrush(new ColorF(0.0F, 0.0F, 0.65F, 0.5F));
                solidBrush3 = renderTarget.CreateSolidColorBrush(new ColorF(0.9F, 0.5F, 0.3F, 0.75F));

                // Create a linear gradient.
                stops[0] = new GradientStop(1, new ColorF(0f, 0f, 0f, 0.25f));
                stops[1] = new GradientStop(0, new ColorF(1f, 1f, 0.2f, 1f));
                GradientStopCollection radiantGradientStops = renderTarget.CreateGradientStopCollection(stops, Gamma.Linear, ExtendMode.Wrap);

                RadialGradientBrushProperties radialBrushProps = new RadialGradientBrushProperties(new Point2F(25, 25), new Point2F(0, 0), 10, 10);
                radialGradientBrush = renderTarget.CreateRadialGradientBrush(radialBrushProps, radiantGradientStops);
            }
        }
Пример #20
0
        void FillItemGeometry(D2DGraphics g)
        {
            g.RenderTarget.AntialiasMode = AntialiasMode.PerPrimitive;

            RawRectangleF    rect        = new RawRectangleF(1, 1, Width - 1, Height - 1);
            RoundedRectangle roundedRect = new RoundedRectangle()
            {
                RadiusX = this.RadiusX,
                RadiusY = this.RadiusY,
                Rect    = rect
            };

            GradientStop[] gradientStops = new GradientStop[5];

            gradientStops[0].Color    = GDIDataD2DUtils.TransToRawColor4(colors[0]);
            gradientStops[0].Position = 0f;

            gradientStops[1].Color    = GDIDataD2DUtils.TransToRawColor4(colors[1]);
            gradientStops[1].Position = 0.4f;

            gradientStops[2].Color    = GDIDataD2DUtils.TransToRawColor4(colors[2]);
            gradientStops[2].Position = 0.5f;

            gradientStops[3].Color    = GDIDataD2DUtils.TransToRawColor4(colors[1]);
            gradientStops[3].Position = 0.6f;

            gradientStops[4].Color    = GDIDataD2DUtils.TransToRawColor4(colors[0]);
            gradientStops[4].Position = 1f;

            //
            GradientStopCollection gradientStopCollection = new GradientStopCollection(g.RenderTarget, gradientStops, Gamma.StandardRgb, ExtendMode.Clamp);

            //
            LinearGradientBrushProperties props = new LinearGradientBrushProperties()
            {
                StartPoint = new RawVector2(rect.Left, rect.Top),
                EndPoint   = new RawVector2(rect.Left, rect.Bottom)
            };

            SharpDX.Direct2D1.LinearGradientBrush linearGradientBrush = new SharpDX.Direct2D1.LinearGradientBrush(g.RenderTarget, props, gradientStopCollection);
            g.RenderTarget.FillRoundedRectangle(roundedRect, linearGradientBrush);

            //
            gradientStops = new GradientStop[5];

            gradientStops[0].Color    = GDIDataD2DUtils.TransToRawColor4(colors[2]);
            gradientStops[0].Position = 0f;

            gradientStops[1].Color    = GDIDataD2DUtils.TransToRawColor4(colors[1]);
            gradientStops[1].Position = 0.1f;

            gradientStops[2].Color    = GDIDataD2DUtils.TransToRawColor4(colors[0]);
            gradientStops[2].Position = 0.5f;

            gradientStops[3].Color    = GDIDataD2DUtils.TransToRawColor4(colors[1]);
            gradientStops[3].Position = 0.9f;


            gradientStops[4].Color    = GDIDataD2DUtils.TransToRawColor4(colors[2]);
            gradientStops[4].Position = 1f;

            //
            gradientStopCollection = new GradientStopCollection(g.RenderTarget, gradientStops, Gamma.StandardRgb, ExtendMode.Clamp);

            //
            props = new LinearGradientBrushProperties()
            {
                StartPoint = new RawVector2(rect.Left, rect.Top),
                EndPoint   = new RawVector2(rect.Right, rect.Top)
            };

            linearGradientBrush = new SharpDX.Direct2D1.LinearGradientBrush(g.RenderTarget, props, gradientStopCollection);
            g.RenderTarget.FillRoundedRectangle(roundedRect, linearGradientBrush);



            RawColor4       rawColor = GDIDataD2DUtils.TransToRawColor4(Color.FromArgb(SideShadowAlpha, 0, 0, 0));
            SolidColorBrush brush    = new SolidColorBrush(g.RenderTarget, rawColor);

            g.RenderTarget.DrawRoundedRectangle(roundedRect, brush);
        }
Пример #21
0
 public LinearGradientBrushEx(RenderTarget renderTarget, LinearGradientBrushProperties properties, GradientStopCollection collection)
 {
     _brush      = new LinearGradientBrush(renderTarget, properties, collection);
     _collection = collection;
 }
Пример #22
0
 public D2DSpriteLinearGradientBrush CreateLinearGradientBrush(D2DSpriteGradientStopCollection collection,
                                                               LinearGradientBrushProperties gradient)
 {
     return(new D2DSpriteLinearGradientBrush(this, collection, gradient));
 }
Пример #23
0
 private static void ConfigureLinearGradient(out LinearGradientBrushProperties description)
 {
     description = new LinearGradientBrushProperties();
 }
Пример #24
0
        private void LinearGradientBrushAddClicked(object sender, EventArgs e)
        {
            ExtendMode ex = gradBrushExtendModeCombo.SelectedIndex > 0 ? (ExtendMode)gradBrushExtendModeCombo.SelectedIndex : ExtendMode.Clamp;
            Gamma gamma;

            switch (gammaComboBox.SelectedIndex)
            {
                case 0:
                    gamma = Gamma.Linear;
                    break;
                case 1:
                    gamma = Gamma.StandardRgb;
                    break;
                default:
                    throw new InvalidOperationException("Unknown gamma selected");
            }

            GradientStop[] stops = 
            {
                new GradientStop(0.0f, color1),
                new GradientStop(1.0f, color2),
            };

            GradientStopCollection stopCollection = renderTarget.CreateGradientStopCollection(
                stops, gamma, ex);

            LinearGradientBrushProperties properties;
            if (ex == ExtendMode.Clamp)
            {
                properties = new LinearGradientBrushProperties(
                    new Point2F(50, 50), new Point2F(600, 400));
            }
            else
            {
                properties = new LinearGradientBrushProperties(
                    new Point2F(50, 50), new Point2F(0, 0));
            }
            

            LinearGradientBrush brush = renderTarget.CreateLinearGradientBrush(
                properties, stopCollection);

            parent.brushes.Add(brush);
            parent.currentBrushIndex = parent.brushes.Count - 1;
            FillBrushesListBox();

        }
Пример #25
0
        private void LinearGradientBrushAddClicked(object sender, EventArgs e)
        {
            ExtendMode ex = gradBrushExtendModeCombo.SelectedIndex > 0 ? (ExtendMode)gradBrushExtendModeCombo.SelectedIndex : ExtendMode.Clamp;
            Gamma gamma = gammaComboBox.SelectedIndex > 0 ? (Gamma)gammaComboBox.SelectedIndex : Gamma.Gamma_10;

            GradientStop[] stops = 
            {
                new GradientStop(0.0f, color1),
                new GradientStop(1.0f, color2),
            };

            GradientStopCollection stopCollection = renderTarget.CreateGradientStopCollection(
                stops, gamma, ex);

            LinearGradientBrushProperties properties;
            if (ex == ExtendMode.Clamp)
            {
                properties = new LinearGradientBrushProperties(
                    new Point2F(50, 50), new Point2F(600, 400));
            }
            else
            {
                properties = new LinearGradientBrushProperties(
                    new Point2F(50, 50), new Point2F(0, 0));
            }
            

            LinearGradientBrush brush = renderTarget.CreateLinearGradientBrush(
                properties, stopCollection);

            parent.brushes.Add(brush);
            parent.currentBrushIndex = parent.brushes.Count - 1;
            FillBrushesListBox();

        }
Пример #26
0
 public ID2D1LinearGradientBrush CreateLinearGradientBrush(LinearGradientBrushProperties linearGradientBrushProperties, BrushProperties brushProperties, ID2D1GradientStopCollection gradientStopCollection)
 {
     return(CreateLinearGradientBrush(linearGradientBrushProperties, brushProperties, gradientStopCollection));
 }
Пример #27
0
        private void Form1_Load(object sender, EventArgs e)
        {
            //Init Direct Draw
            //Set Rendering properties
            RenderTargetProperties renderProp = new RenderTargetProperties()
            {
                DpiX = 0,
                DpiY = 0,
                MinLevel = FeatureLevel.Level_10,
                PixelFormat = new PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied),
                Type = RenderTargetType.Hardware,
                Usage = RenderTargetUsage.None
            };

            //set hwnd target properties (permit to attach Direct2D to window)
            HwndRenderTargetProperties winProp = new HwndRenderTargetProperties()
            {
                Hwnd = this.Handle,
                PixelSize = new DrawingSize(this.ClientSize.Width, this.ClientSize.Height),
                PresentOptions = PresentOptions.Immediately
            };

            //target creation
            target = new WindowRenderTarget(factory, renderProp, winProp);

            //create red and white brushes
            redBrush = new SharpDX.Direct2D1.SolidColorBrush(target, SharpDX.Color.Red);
            whiteBrush = new SharpDX.Direct2D1.SolidColorBrush(target, SharpDX.Color.White);

            //create a linear gradient brush
            var grad = new LinearGradientBrushProperties()
            {
                StartPoint = new DrawingPointF(ClientSize.Width / 2, ClientSize.Height / 2),
                EndPoint = new DrawingPointF(ClientSize.Width, ClientSize.Height)
            };

            var stopCollection = new GradientStopCollection(target, new GradientStop[]
            {
                new GradientStop(){Color=SharpDX.Color.Azure ,Position=0F},
                new GradientStop(){Color=SharpDX.Color.Yellow,Position=0.2F},
                new GradientStop(){Color=SharpDX.Color.Green,Position=0.4F},
                new GradientStop(){Color=SharpDX.Color.Red,Position=1F},
            }, ExtendMode.Mirror);

            gradient = new SharpDX.Direct2D1.LinearGradientBrush(target, grad, stopCollection);

            //create textformat
            textFormat = new SharpDX.DirectWrite.TextFormat(factoryWrite, "Arial", 36);

            //avoid artifacts
            this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
        }