private void DrawRegion(Graphics graphics, Color color, bool drawPoints, bool fill, BTBLib.Region region) { Pen pen = new Pen(color, _regionPenWidth / _zoom); Brush brush = new SolidBrush(color); Point[] points = null; bool shouldFill = fill && region.IsClosed; if (shouldFill) { points = new Point[region.Lines.Count]; } for (int i = 0; i < region.Lines.Count; ++i) { var segment = region.Lines[i]; int startX = segment.StartX / 8; int startY = (_battle.Height - segment.StartY) / 8; int endX = segment.EndX / 8; int endY = (_battle.Height - segment.EndY) / 8; graphics.DrawLine(pen, startX, startY, endX, endY); if (drawPoints) { graphics.FillRectangle(brush, startX - _regionPointSize / 2, startY - _regionPointSize / 2, _regionPointSize, _regionPointSize); } if (shouldFill) { points[i] = new Point(startX, startY); } } if (drawPoints) { if (!region.IsClosed && region.Lines.Count > 0) { BTBLib.Region.LineSegment segment = region.Lines[region.Lines.Count - 1]; int endX = segment.EndX / 8; int endY = (_battle.Height - segment.EndY) / 8; graphics.FillRectangle(brush, endX - _regionPointSize / 2, endY - _regionPointSize / 2, _regionPointSize, _regionPointSize); } } if (shouldFill) { Brush regionBrush = new SolidBrush(Color.FromArgb(128, color.R, color.G, color.B)); if (region.IsBoundaryInversed) { System.Drawing.Region graphicsRegion = new System.Drawing.Region(); graphicsRegion.MakeInfinite(); GraphicsPath path = new GraphicsPath(); path.AddPolygon(points); graphicsRegion.Exclude(path); graphics.FillRegion(regionBrush, graphicsRegion); } else { graphics.FillPolygon(regionBrush, points); } } }
private void MarkForFullRedraw() { foreach (Image i in this.myCachedLayerImages) { if (i != null) { i.Dispose(); } } this.myCachedLayerImages.Clear(); // Mark the whole 1x1 canvas dirty. for (int i = 0; i < this.myLayerDirtyRegions.Count; i++) { System.Drawing.Region r = new System.Drawing.Region(); r.MakeInfinite(); this.myLayerDirtyRegions[i] = r; } }
public void EmptyRegionWithInfiniteRegion () { Region empty = new Region (); empty.MakeEmpty (); Assert.IsTrue (empty.IsEmpty (graphic), "IsEmpty"); Region region = new Region (); Assert.IsTrue (region.IsInfinite (graphic), "IsInfinite"); region.Union (empty); Assert.IsTrue (region.IsInfinite (graphic), "Union-IsInfinite"); region.Xor (empty); Assert.IsTrue (region.IsInfinite (graphic), "Xor-IsInfinite"); region.Exclude (empty); Assert.IsTrue (region.IsInfinite (graphic), "Exclude-IsInfinite"); region.Intersect (empty); Assert.IsTrue (region.IsEmpty (graphic), "Intersect-IsEmpty"); region.MakeInfinite (); region.Complement (empty); Assert.IsTrue (region.IsEmpty (graphic), "Complement-IsEmpty"); }
public void EmptyPathWithInfiniteRegion () { GraphicsPath gp = new GraphicsPath (); Region region = new Region (); Assert.IsTrue (region.IsInfinite (graphic), "IsInfinite"); region.Union (gp); Assert.IsTrue (region.IsInfinite (graphic), "Union-IsInfinite"); region.Xor (gp); Assert.IsTrue (region.IsInfinite (graphic), "Xor-IsInfinite"); region.Exclude (gp); Assert.IsTrue (region.IsInfinite (graphic), "Exclude-IsInfinite"); region.Intersect (gp); Assert.IsTrue (region.IsEmpty (graphic), "Intersect-IsEmpty"); region.MakeInfinite (); region.Complement (gp); Assert.IsTrue (region.IsEmpty (graphic), "Complement-IsEmpty"); }
private void cropHandWindow() { Graphics g = Graphics.FromImage(this.bitmap); Point point2D = convertPoint3D(lastPoint); int length = this.bitmap.Height / 2; int x = point2D.X - length / 2; int y = point2D.Y - length / 2; Rectangle rect = new Rectangle(x, y, length, length); Region region = new Region(); region.MakeInfinite(); region.Exclude(rect); g.FillRegion(Brushes.Black, region); g.Dispose(); }
private void EnsureRegion() { if (invalidRegion == null) { invalidRegion = new Region(); invalidRegion.MakeInfinite(); } }
public override void VisitRadialGradientBrush( Types.Rectangle bounds, Paint.Brushes.RadialGradientBrush radialGradientBrush ) { double dx = radialGradientBrush.InnerPoint.X - radialGradientBrush.OuterPoint.X; double dy = radialGradientBrush.InnerPoint.Y - radialGradientBrush.OuterPoint.Y; float radius = (float) Math.Sqrt( dx * dx + dy * dy ); if( radius == 0 ) { return; } using( GraphicsPath gp = new GraphicsPath() ) { gp.StartFigure(); gp.AddEllipse( (float) radialGradientBrush.InnerPoint.X - radius, (float) radialGradientBrush.InnerPoint.Y - radius, radius * 2, radius * 2 ); Region around = new Region(); around.MakeInfinite(); around.Xor( gp ); SolidBrush b = new SolidBrush( GdiPlusUtility.Convert.Color( radialGradientBrush.OuterColor ) ); _brushes.Add( new ClippedStandardBrushStage( _graphics, b, around ) ); PathGradientBrush radialBrush = new PathGradientBrush( gp ); radialBrush.CenterColor = GdiPlusUtility.Convert.Color( radialGradientBrush.InnerColor ); radialBrush.CenterPoint = GdiPlusUtility.Convert.Point( radialGradientBrush.InnerPoint ); radialBrush.SurroundColors = new Color[] { GdiPlusUtility.Convert.Color( radialGradientBrush.OuterColor ) }; radialBrush.WrapMode = WrapMode.Clamp; _brushes.Add( new StandardBrushStage( radialBrush ) ); } }
protected void SetWindowRegion(Size winSize) { // Style specific handling if (_style == VisualStyle.IDE) { int shadowHeight = _position[(int)_style, (int)PI.ShadowHeight]; int shadowWidth = _position[(int)_style, (int)PI.ShadowWidth]; // Create a new region object using (Region drawRegion = new Region()) { // Can draw anywhere drawRegion.MakeInfinite(); // Remove the area above the right hand shadow drawRegion.Xor(new Rectangle(winSize.Width - shadowWidth, 0, shadowWidth, shadowHeight)); // When drawing upwards from a vertical menu we need to allow a connection between the // MenuControl selection box and the PopupMenu shadow if (!((_direction == Direction.Vertical) && !_excludeTop)) { // Remove the area left of the bottom shadow drawRegion.Xor(new Rectangle(0, winSize.Height - shadowHeight, shadowWidth, shadowHeight)); } // Define a region to prevent drawing over exposed corners of shadows using(Graphics g = Graphics.FromHwnd(this.Handle)) WindowsAPI.SetWindowRgn(this.Handle, drawRegion.GetHrgn(g), false); } } }
private void DrawGradient(Graphics g) { g.PixelOffsetMode = PixelOffsetMode.Half; float gradientAngle; switch (this._Orientation) { case Orientation.Horizontal: { gradientAngle = 180.0f; break; } case Orientation.Vertical: { gradientAngle = 90.0f; break; } default: { throw new InvalidEnumArgumentException(); } } // draw gradient Rectangle gradientRect = this.ClientRectangle; switch (this._Orientation) { case Orientation.Horizontal: { gradientRect.Inflate(-TriangleHalfLength, -TriangleSize + 3); break; } case Orientation.Vertical: { gradientRect.Inflate(-TriangleSize + 3, -TriangleHalfLength); break; } default: { throw new InvalidEnumArgumentException(); } } g.FillRectangle(new HatchBrush( HatchStyle.LargeCheckerBoard, Color.White, Color.Gray), gradientRect); if (this._CustomGradient != null && this._CustomGradient.Length > 2 && gradientRect.Width > 1 && gradientRect.Height > 1) { var gradientSurface = new Bitmap( gradientRect.Size.Width, gradientRect.Size.Height, PixelFormat.Format32bppArgb); if (Orientation == Orientation.Horizontal) { for (int x = 0; x < gradientSurface.Width; ++x) { // TODO: refactor, double buffer, save this computation in a bitmap somewhere double index = x * (this._CustomGradient.Length - 1) / (double)(gradientSurface.Width - 1); var indexL = (int)Math.Floor(index); double t = 1.0 - (index - indexL); var indexR = (int)Math.Min( this._CustomGradient.Length - 1, Math.Ceiling(index)); Color colorL = this._CustomGradient[indexL]; Color colorR = this._CustomGradient[indexR]; double a1 = colorL.A / 255.0; double r1 = colorL.R / 255.0; double g1 = colorL.G / 255.0; double b1 = colorL.B / 255.0; double a2 = colorR.A / 255.0; double r2 = colorR.R / 255.0; double g2 = colorR.G / 255.0; double b2 = colorR.B / 255.0; double at = (t * a1) + ((1.0 - t) * a2); double rt; double gt; double bt; if (Equals(at, 0.0) == true) { rt = 0; gt = 0; bt = 0; } else { rt = ((t * a1 * r1) + ((1.0 - t) * a2 * r2)) / at; gt = ((t * a1 * g1) + ((1.0 - t) * a2 * g2)) / at; bt = ((t * a1 * b1) + ((1.0 - t) * a2 * b2)) / at; } var ap = (int)(Math.Round(at * 255.0)).Clamp(0, 255); var rp = (int)(Math.Round(rt * 255.0)).Clamp(0, 255); var gp = (int)(Math.Round(gt * 255.0)).Clamp(0, 255); var bp = (int)(Math.Round(bt * 255.0)).Clamp(0, 255); for (int y = 0; y < gradientSurface.Height; ++y) { const int srcA = 0; const int srcR = 0; const int srcG = 0; const int srcB = 0; // we are assuming that src.A = 255 int ad = ((ap * ap) + (srcA * (255 - ap))) / 255; int rd = ((rp * ap) + (srcR * (255 - ap))) / 255; int gd = ((gp * ap) + (srcG * (255 - ap))) / 255; int bd = ((bp * ap) + (srcB * (255 - ap))) / 255; // TODO: proper alpha blending! gradientSurface.SetPixel(x, y, Color.FromArgb(ad, rd, gd, bd)); } } g.DrawImage( gradientSurface, gradientRect, new Rectangle(new Point(0, 0), gradientSurface.Size), GraphicsUnit.Pixel); } else if (Orientation == Orientation.Vertical) { g.FillRectangle(Brushes.Red, gradientRect); /* g.DrawImage( gradientSurface, gradientRect, new Rectangle(new Point(0, 0), gradientSurface.Size), GraphicsUnit.Pixel); */ } else { throw new InvalidEnumArgumentException(); } } else if (this._CustomGradient != null && this._CustomGradient.Length == 2) { using (var lgb = new LinearGradientBrush( this.ClientRectangle, this._CustomGradient[1], this._CustomGradient[0], gradientAngle, false)) { g.FillRectangle(lgb, gradientRect); } } else { using (var lgb = new LinearGradientBrush( this.ClientRectangle, this._MaxColor, this._MinColor, gradientAngle, false)) { g.FillRectangle(lgb, gradientRect); } } // fill background using (var nonGradientRegion = new Region()) { nonGradientRegion.MakeInfinite(); nonGradientRegion.Exclude(gradientRect); using (var sb = new SolidBrush(this.BackColor)) { //g.FillRegion(sb, nonGradientRegion.GetRegionReadOnly()); g.FillRegion(sb, nonGradientRegion); } } // draw value triangles for (int i = 0; i < this._Values.Length; i++) { int pos = ValueToPosition(this._Values[i]); Brush brush; Pen pen; if (i == this._Highlight) { brush = Brushes.Blue; pen = (Pen)Pens.White.Clone(); } else { brush = Brushes.Black; pen = (Pen)Pens.Gray.Clone(); } g.SmoothingMode = SmoothingMode.AntiAlias; Point a1; Point b1; Point c1; Point a2; Point b2; Point c2; switch (this._Orientation) { case Orientation.Horizontal: { a1 = new Point(pos - TriangleHalfLength, 0); b1 = new Point(pos, TriangleSize - 1); c1 = new Point(pos + TriangleHalfLength, 0); a2 = new Point(a1.X, Height - 1 - a1.Y); b2 = new Point(b1.X, Height - 1 - b1.Y); c2 = new Point(c1.X, Height - 1 - c1.Y); break; } case Orientation.Vertical: { a1 = new Point(0, pos - TriangleHalfLength); b1 = new Point(TriangleSize - 1, pos); c1 = new Point(0, pos + TriangleHalfLength); a2 = new Point(Width - 1 - a1.X, a1.Y); b2 = new Point(Width - 1 - b1.X, b1.Y); c2 = new Point(Width - 1 - c1.X, c1.Y); break; } default: { throw new InvalidEnumArgumentException(); } } if (this._DrawNearNub) { g.FillPolygon(brush, new[] {a1, b1, c1, a1}); } if (this._DrawFarNub) { g.FillPolygon(brush, new[] {a2, b2, c2, a2}); } // ReSharper disable ConditionIsAlwaysTrueOrFalse if (pen != null) // ReSharper restore ConditionIsAlwaysTrueOrFalse { if (this._DrawNearNub) { g.DrawPolygon(pen, new[] {a1, b1, c1, a1}); } if (this._DrawFarNub) { g.DrawPolygon(pen, new[] {a2, b2, c2, a2}); } pen.Dispose(); } } }
public void ExcludeBug402613 () { Region r = new Region(); r.MakeInfinite (); r.Exclude (new Rectangle (387,292,189,133)); r.Exclude (new Rectangle (387,66,189,133)); Assert.IsTrue (r.IsVisible (new Rectangle (66,292,189,133))); }
public void GetHrgn_Empty_MakeInfinite () { Region r = new Region (new GraphicsPath ()); Assert.IsTrue (r.IsEmpty (graphic), "Empty"); Assert.IsFalse (r.IsInfinite (graphic), "!Infinite"); IntPtr h = r.GetHrgn (graphic); Assert.IsFalse (h == IntPtr.Zero, "Handle!=0"); r.MakeInfinite (); Assert.IsFalse (r.IsEmpty (graphic), "!Empty"); Assert.IsTrue (r.IsInfinite (graphic), "Infinite"); Assert.AreEqual (IntPtr.Zero, r.GetHrgn (graphic), "Handle==0"); r.ReleaseHrgn (h); }
public void Complement_383878 () { using (Region clipRegion = new Region ()) { clipRegion.MakeInfinite (); Rectangle smaller = new Rectangle (5, 5, -10, -10); Rectangle bigger = new Rectangle (-5, -5, 12, 12); clipRegion.Intersect (smaller); clipRegion.Complement (bigger); Assert.IsFalse (clipRegion.IsEmpty (graphic), "IsEmpty"); Assert.IsFalse (clipRegion.IsInfinite (graphic), "IsInfinite"); RectangleF [] rects = clipRegion.GetRegionScans (new Matrix ()); Assert.AreEqual (2, rects.Length, "Length"); Assert.AreEqual (new RectangleF (5, -5, 2, 10), rects [0], "0"); Assert.AreEqual (new RectangleF (-5, 5, 12, 2), rects [1], "1"); } }
public void TestInfiniteAndEmpty() { Bitmap bmp = new Bitmap (600, 800); Graphics dc = Graphics.FromImage (bmp); Rectangle rect1, rect2; Region rgn1; RectangleF [] rects; Matrix matrix = new Matrix (); rect1 = new Rectangle (500, 30, 60, 80); rect2 = new Rectangle (520, 40, 60, 80); rgn1 = new Region (rect1); rgn1.Union (rect2); Assert.AreEqual (false, rgn1.IsEmpty (dc)); Assert.AreEqual (false, rgn1.IsInfinite (dc)); rgn1.MakeEmpty(); Assert.AreEqual (true, rgn1.IsEmpty (dc)); rgn1 = new Region (rect1); rgn1.Union (rect2); rgn1.MakeInfinite (); rects = rgn1.GetRegionScans (matrix); Assert.AreEqual (1, rects.Length); Assert.AreEqual (-4194304, rects[0].X); Assert.AreEqual (-4194304, rects[0].Y); Assert.AreEqual (8388608, rects[0].Width); Assert.AreEqual (8388608, rects[0].Height); Assert.AreEqual (true, rgn1.IsInfinite (dc)); }
private void TilesetView_PaintTiles(object sender, TilesetViewPaintTilesEventArgs e) { Color highlightColor = Color.FromArgb(255, 255, 255); Color baseTileDrawColor = Color.FromArgb(255, 192, 128); Color externalDrawColor = Color.FromArgb(128, 192, 255); Color nonConnectedColor = Color.FromArgb(128, 0, 0, 0); Brush brushNonConnected = new SolidBrush(nonConnectedColor); TilesetAutoTileInput autoTile = this.SelectedAutoTile; // Early-out if there is nothing we can edit right now if (autoTile == null) return; // If we're in a special draw mode, switch highlight colors to indicate this. if (this.isExternalDraw) highlightColor = externalDrawColor; else if (this.isBaseTileDraw) highlightColor = baseTileDrawColor; // Set up shared working data TilesetAutoTileItem[] tileInput = autoTile.TileInput.Data; TilesetViewPaintTileData hoveredData = default(TilesetViewPaintTileData); TilesetAutoTileItem hoveredItem = default(TilesetAutoTileItem); GraphicsPath connectedOutlines = new GraphicsPath(); GraphicsPath connectedRegion = new GraphicsPath(); // Draw all the tiles that we're supposed to paint for (int i = 0; i < e.PaintedTiles.Count; i++) { TilesetViewPaintTileData paintData = e.PaintedTiles[i]; // Prepare some data we'll need for drawing the per-tile info overlay bool tileHovered = this.TilesetView.HoveredTileIndex == paintData.TileIndex; bool isBaseTile = autoTile.BaseTileIndex == paintData.TileIndex; TilesetAutoTileItem item = (autoTile.TileInput.Count > paintData.TileIndex) ? tileInput[paintData.TileIndex] : default(TilesetAutoTileItem); // Remember hovered item data for later (post-overlay) if (tileHovered) { hoveredData = paintData; hoveredItem = item; } // Accumulate a shared region for displaying connectivity, as well as a path of all their outlines if (item.IsAutoTile) { Rectangle centerRect = GetConnectivityRegionRect(TileConnection.None, paintData.ViewRect); connectedRegion.AddRectangle(centerRect); DrawConnectivityRegion(connectedRegion, item.Neighbours, paintData.ViewRect); DrawConnectivityOutlines(connectedOutlines, item.Neighbours, paintData.ViewRect); } else if (item.ConnectsToAutoTile) { connectedRegion.AddRectangle(paintData.ViewRect); } // Highlight base tile and external connecting tiles if (isBaseTile) DrawTileHighlight(e.Graphics, paintData.ViewRect, baseTileDrawColor); else if (!item.IsAutoTile && item.ConnectsToAutoTile) DrawTileHighlight(e.Graphics, paintData.ViewRect, externalDrawColor); } // Fill all non-connected regions with the overlay brush Region surroundingRegion = new Region(); surroundingRegion.MakeInfinite(); surroundingRegion.Exclude(connectedRegion); e.Graphics.IntersectClip(surroundingRegion); e.Graphics.FillRectangle(brushNonConnected, this.TilesetView.ClientRectangle); e.Graphics.ResetClip(); // Draw connected region outlines e.Graphics.DrawPath(Pens.White, connectedOutlines); // Draw a tile-based hover indicator if (!hoveredData.ViewRect.IsEmpty && !this.isBaseTileDraw && !this.isExternalDraw) DrawHoverIndicator(e.Graphics, hoveredData.ViewRect, 64, highlightColor); // Draw a hover indicator for a specific hovered region if (!hoveredData.ViewRect.IsEmpty) { if (!this.isBaseTileDraw && !this.isExternalDraw) DrawHoverIndicator(e.Graphics, GetConnectivityRegionRect(this.hoveredArea, hoveredData.ViewRect), 255, highlightColor); else DrawHoverIndicator(e.Graphics, hoveredData.ViewRect, 255, highlightColor); } }