/// <summary> /// Initializes a new instance of the <see cref="DirectXGraphics" /> class. /// </summary> /// <param name="renderTarget">The render target.</param> /// <param name="resourceManager">The initial resource manager.</param> /// <param name="fillStyle">The initial fill style.</param> /// <param name="lineStyle">The initial line style.</param> /// <param name="lineWidth">Width of the line.</param> /// <exception cref="System.ArgumentNullException"> /// </exception> public DirectXGraphics( [NotNull] RenderTarget renderTarget, [NotNull] IResourceManager resourceManager, [NotNull] IStyle fillStyle, [NotNull] SolidColourStyle lineStyle, float lineWidth) { if (renderTarget == null) { throw new ArgumentNullException(nameof(renderTarget)); } if (resourceManager == null) { throw new ArgumentNullException(nameof(resourceManager)); } if (fillStyle == null) { throw new ArgumentNullException(nameof(fillStyle)); } if (lineStyle == null) { throw new ArgumentNullException(nameof(lineStyle)); } _renderTarget = renderTarget; ResourceManager = resourceManager; FillStyle = fillStyle; LineStyle = lineStyle; LineWidth = lineWidth; }
/// <summary> /// Initializes a new instance of the <see cref="State"/> class. /// </summary> /// <param name="graphics">The graphics.</param> public State([NotNull] DirectXGraphics graphics) { Debug.Assert(graphics != null, "graphics != null"); FillStyle = graphics.FillStyle; LineStyle = graphics.LineStyle; LineWidth = graphics._lineWidth; ResourceManager = graphics._resourceManager; Transform = graphics.Transform; }
/// <summary> /// Handles the DoubleClick event of the _previewPnl control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param> private void _previewPnl_DoubleClick(object sender, EventArgs e) { SolidColourStyle style = Style as SolidColourStyle; if (style != null) { _colourDialog.Color = Color.FromArgb(style.Colour.ToArgb()); } if (_colourDialog.ShowDialog(FindForm()) == DialogResult.OK) { Color col = _colourDialog.Color; Style = new SolidColourStyle(col.R, col.G, col.B, col.A); } }
/// <summary> /// Saves the current state of the graphics, sets the state using the given action, /// and reutrns an <see cref="IDisposable" /> that can can be disposed to restore the saved state. /// </summary> /// <param name="graphics">The graphics.</param> /// <param name="fillStyle">The fill style, or <see langword="null" /> to leave unchanged.</param> /// <param name="lineStyle">The line style, or <see langword="null" /> to leave unchanged.</param> /// <param name="lineWidth">The line width, or <see langword="null" /> to leave unchanged.</param> /// <param name="transform">The transform, or <see langword="null" /> to leave unchanged.</param> /// <param name="resourceManager">The resource manager, or <see langword="null" /> to leave unchanged.</param> /// <param name="setState">An action which can be used to change any values that are needed.</param> /// <returns> /// An <see cref="IDisposable" /> that can can be disposed to restore the saved state. /// </returns> public static IDisposable TempState( [NotNull] this IGraphics graphics, [CanBeNull] IStyle fillStyle = null, [CanBeNull] SolidColourStyle lineStyle = null, [CanBeNull] float?lineWidth = null, [CanBeNull] Matrix3x2?transform = null, [CanBeNull] IResourceManager resourceManager = null, [CanBeNull] Action <IGraphics> setState = null) { if (graphics == null) { throw new ArgumentNullException(nameof(graphics)); } graphics.SaveState(); try { if (fillStyle != null) { graphics.FillStyle = fillStyle; } if (lineStyle != null) { graphics.LineStyle = lineStyle; } if (lineWidth != null) { graphics.LineWidth = lineWidth.Value; } if (transform != null) { graphics.Transform = transform.Value; } if (resourceManager != null) { graphics.ResourceManager = resourceManager; } setState?.Invoke(graphics); return(new TempStateRestorer(graphics)); } catch { graphics.RestoreState(); throw; } }
private Resource <Brush> CreateBrush([NotNull] IStyle style, bool temp) { lock (_lock) { SolidColourStyle solidColour = style as SolidColourStyle; if (solidColour != null) { return(new SolidBrush(solidColour.Colour.ToColor())); } RandomColourStyle randomColour = style as RandomColourStyle; if (randomColour != null) { return(new SolidBrush(randomColour.PositionColour.ToColor())); } LinearGradientStyle linearGradient = style as LinearGradientStyle; if (linearGradient != null) { ColorBlend blend = new ColorBlend(linearGradient.GradientStops.Count); Debug.Assert(blend.Colors != null, "blend.Colors != null"); Debug.Assert(blend.Positions != null, "blend.Positions != null"); for (int i = 0; i < linearGradient.GradientStops.Count; i++) { GradientStop stop = linearGradient.GradientStops[i]; blend.Colors[i] = stop.Colour.ToColor(); blend.Positions[i] = stop.Position; } return(new LinearGradientBrush( linearGradient.Start.ToPointF(), linearGradient.End.ToPointF(), Color.Black, Color.White) { InterpolationColors = blend, WrapMode = WrapMode.Clamp }); } RadialGradientStyle radialGradient = style as RadialGradientStyle; if (radialGradient != null) { ColorBlend blend = new ColorBlend(radialGradient.GradientStops.Count); Debug.Assert(blend.Colors != null, "blend.Colors != null"); Debug.Assert(blend.Positions != null, "blend.Positions != null"); for (int i = 0; i < radialGradient.GradientStops.Count; i++) { GradientStop stop = radialGradient.GradientStops[i]; blend.Colors[i] = stop.Colour.ToColor(); blend.Positions[i] = stop.Position; } GraphicsPath path = new GraphicsPath(); path.AddEllipse(-1, -1, 1, 1); return(new Resource <Brush>( new PathGradientBrush(path) { WrapMode = WrapMode.Clamp, Transform = radialGradient.GradientTransform.ToMatrix(), InterpolationColors = blend, CenterPoint = radialGradient.UnitOriginOffset.ToPointF() }, path)); } ImageStyle image = style as ImageStyle; if (image != null) { Bitmap bitmap = temp ? Get(image.Image) : Add(image.Image); return(new TextureBrush(bitmap, WrapMode.Clamp) { Transform = image.ImageTransform.ToMatrix() }); } throw new NotSupportedException(); } }
private Resource <Brush> CreateBrush([NotNull] IStyle style, bool temp) { lock (_lock) { SolidColourStyle solidColour = style as SolidColourStyle; if (solidColour != null) { return(new SolidColorBrush(_renderTarget, solidColour.Colour.ToRawColor4())); } RandomColourStyle randomColour = style as RandomColourStyle; if (randomColour != null) { return(new SolidColorBrush(_renderTarget, randomColour.PositionColour.ToRawColor4())); } LinearGradientStyle linearGradient = style as LinearGradientStyle; if (linearGradient != null) { GradientStopCollection gradientStops = new GradientStopCollection( _renderTarget, linearGradient.GradientStops.Select(DirectXExtensions.ToGradientStop).ToArray(), Gamma.Linear); return(new Resource <Brush>( new LinearGradientBrush( _renderTarget, new LinearGradientBrushProperties { StartPoint = linearGradient.Start.ToRawVector2(), EndPoint = linearGradient.End.ToRawVector2() }, gradientStops), gradientStops)); } RadialGradientStyle radialGradient = style as RadialGradientStyle; if (radialGradient != null) { GradientStopCollection gradientStops = new GradientStopCollection( _renderTarget, radialGradient.GradientStops.Select(DirectXExtensions.ToGradientStop).ToArray(), Gamma.Linear); return(new Resource <Brush>( new RadialGradientBrush( _renderTarget, new RadialGradientBrushProperties { GradientOriginOffset = radialGradient.UnitOriginOffset.ToRawVector2(), RadiusX = 1, RadiusY = 1 }, new BrushProperties { Opacity = 1, Transform = radialGradient.GradientTransform.ToRawMatrix3x2() }, gradientStops), gradientStops)); } ImageStyle image = style as ImageStyle; if (image != null) { Bitmap bitmap = temp ? Get(image.Image) : Add(image.Image); return(new BitmapBrush( _renderTarget, bitmap, new BitmapBrushProperties { // TODO set from style ExtendModeX = ExtendMode.Clamp, ExtendModeY = ExtendMode.Clamp, InterpolationMode = BitmapInterpolationMode.Linear }, new BrushProperties { Transform = image.ImageTransform.ToRawMatrix3x2(), Opacity = 1 })); } throw new NotSupportedException(); } }