private static void DrawGridQuad(Graphics g, TmxMap tmxMap, float scale) { HashSet <Point> points = new HashSet <Point>(); for (int x = 0; x < GetMaxTilesWide(tmxMap); ++x) { for (int y = 0; y < GetMaxTilesHigh(tmxMap); ++y) { // Add the "top-left" corner of a tile points.Add(TmxMath.TileCornerInGridCoordinates(tmxMap, x, y)); // Add all other corners of the tile to our list of grid points // This is complicated by different map types (espcially staggered isometric) if (tmxMap.Orientation == TmxMap.MapOrientation.Orthogonal || tmxMap.Orientation == TmxMap.MapOrientation.Isometric) { points.Add(TmxMath.TileCornerInGridCoordinates(tmxMap, x + 1, y)); points.Add(TmxMath.TileCornerInGridCoordinates(tmxMap, x + 1, y + 1)); points.Add(TmxMath.TileCornerInGridCoordinates(tmxMap, x, y + 1)); } else if (tmxMap.Orientation == TmxMap.MapOrientation.Staggered) { bool sx = TmxMath.DoStaggerX(tmxMap, x); bool sy = TmxMath.DoStaggerY(tmxMap, y); if (sx) { // top-right, bottom-right, and bottom-left points.Add(TmxMath.TileCornerInGridCoordinates(tmxMap, x + 1, y + 1)); points.Add(TmxMath.TileCornerInGridCoordinates(tmxMap, x, y + 1)); points.Add(TmxMath.TileCornerInGridCoordinates(tmxMap, x - 1, y + 1)); } else if (sy) { // top-right, bottom-right, and bottom-left points.Add(TmxMath.TileCornerInGridCoordinates(tmxMap, x + 1, y + 1)); points.Add(TmxMath.TileCornerInGridCoordinates(tmxMap, x, y + 2)); points.Add(TmxMath.TileCornerInGridCoordinates(tmxMap, x, y + 1)); } else if (tmxMap.StaggerAxis == TmxMap.MapStaggerAxis.X) { // top-right, bottom-right, and bottom-left points.Add(TmxMath.TileCornerInGridCoordinates(tmxMap, x + 1, y)); points.Add(TmxMath.TileCornerInGridCoordinates(tmxMap, x, y + 1)); points.Add(TmxMath.TileCornerInGridCoordinates(tmxMap, x - 1, y)); } else if (tmxMap.StaggerAxis == TmxMap.MapStaggerAxis.Y) { // top-right, bottom-right, and bottom-left points.Add(TmxMath.TileCornerInGridCoordinates(tmxMap, x, y + 1)); points.Add(TmxMath.TileCornerInGridCoordinates(tmxMap, x, y + 2)); points.Add(TmxMath.TileCornerInGridCoordinates(tmxMap, x - 1, y + 1)); } } } } // Can take for granted that background is always white float invScale = 1.0f / scale; List <RectangleF> rectangles = new List <RectangleF>(points.Count); foreach (var p in points) { RectangleF rc = new RectangleF(p.X, p.Y, PreviewImage.GridSize * invScale, PreviewImage.GridSize * invScale); rc.Offset(-PreviewImage.GridSize * 0.5f * invScale, -PreviewImage.GridSize * 0.5f * invScale); rectangles.Add(rc); } using (Pen pen = new Pen(Brushes.Black, invScale)) { g.DrawRectangles(pen, rectangles.ToArray()); } }
private static void DrawGridHex(Graphics g, TmxMap tmxMap) { // Our collection of points to render HashSet <Point> points = new HashSet <Point>(); // Note: borrowed heavily from Tiled source (HexagonalRenderer::drawGrid) int tileWidth = tmxMap.TileWidth & ~1; int tileHeight = tmxMap.TileHeight & ~1; int sideLengthX = tmxMap.StaggerAxis == TmxMap.MapStaggerAxis.X ? tmxMap.HexSideLength : 0; int sideLengthY = tmxMap.StaggerAxis == TmxMap.MapStaggerAxis.Y ? tmxMap.HexSideLength : 0; int sideOffsetX = (tmxMap.TileWidth - sideLengthX) / 2; int sideOffsetY = (tmxMap.TileHeight - sideLengthY) / 2; int columnWidth = sideOffsetX + sideLengthX; int rowHeight = sideOffsetY + sideLengthY; bool staggerX = tmxMap.StaggerAxis == TmxMap.MapStaggerAxis.X; // Determine the tile and pixel coordinates to start at Point startTile = new Point(0, 0); Point startPos = TmxMath.TileCornerInScreenCoordinates(tmxMap, startTile.X, startTile.Y); Point[] oct = new Point[8] { new Point(0, tileHeight - sideOffsetY), new Point(0, sideOffsetY), new Point(sideOffsetX, 0), new Point(tileWidth - sideOffsetX, 0), new Point(tileWidth, sideOffsetY), new Point(tileWidth, tileHeight - sideOffsetY), new Point(tileWidth - sideOffsetX, tileHeight), new Point(sideOffsetX, tileHeight) }; if (staggerX) { // Odd row shifting is applied in the rendering loop, so un-apply it here if (TmxMath.DoStaggerX(tmxMap, startTile.X)) { startPos.Y -= rowHeight; } for (; startTile.X < GetMaxTilesWide(tmxMap); startTile.X++) { Point rowTile = startTile; Point rowPos = startPos; if (TmxMath.DoStaggerX(tmxMap, startTile.X)) { rowPos.Y += rowHeight; } for (; rowTile.Y < GetMaxTilesHigh(tmxMap); rowTile.Y++) { points.Add(TmxMath.AddPoints(rowPos, oct[1])); points.Add(TmxMath.AddPoints(rowPos, oct[2])); points.Add(TmxMath.AddPoints(rowPos, oct[3])); points.Add(TmxMath.AddPoints(rowPos, oct[4])); bool isStaggered = TmxMath.DoStaggerX(tmxMap, startTile.X); bool lastRow = rowTile.Y == tmxMap.Height - 1; bool lastColumn = rowTile.X == tmxMap.Width - 1; bool bottomLeft = rowTile.X == 0 || (lastRow && isStaggered); bool bottomRight = lastColumn || (lastRow && isStaggered); if (bottomRight) { points.Add(TmxMath.AddPoints(rowPos, oct[5])); points.Add(TmxMath.AddPoints(rowPos, oct[6])); } if (lastRow) { points.Add(TmxMath.AddPoints(rowPos, oct[6])); points.Add(TmxMath.AddPoints(rowPos, oct[7])); } if (bottomLeft) { points.Add(TmxMath.AddPoints(rowPos, oct[7])); points.Add(TmxMath.AddPoints(rowPos, oct[0])); } rowPos.Y += tileHeight + sideLengthY; } startPos.X += columnWidth; } } else { // Odd row shifting is applied in the rendering loop, so un-apply it here if (TmxMath.DoStaggerY(tmxMap, startTile.Y)) { startPos.X -= columnWidth; } for (; startTile.Y < tmxMap.Height; startTile.Y++) { Point rowTile = startTile; Point rowPos = startPos; if (TmxMath.DoStaggerY(tmxMap, startTile.Y)) { rowPos.X += columnWidth; } for (; rowTile.X < tmxMap.Width; rowTile.X++) { points.Add(TmxMath.AddPoints(rowPos, oct[0])); points.Add(TmxMath.AddPoints(rowPos, oct[1])); points.Add(TmxMath.AddPoints(rowPos, oct[2])); points.Add(TmxMath.AddPoints(rowPos, oct[3])); points.Add(TmxMath.AddPoints(rowPos, oct[4])); bool isStaggered = TmxMath.DoStaggerY(tmxMap, startTile.Y); bool lastRow = rowTile.Y == tmxMap.Height - 1; bool lastColumn = rowTile.Y == tmxMap.Width - 1; bool bottomLeft = lastRow || (rowTile.X == 0 && !isStaggered); bool bottomRight = lastRow || (lastColumn && isStaggered); if (lastColumn) { points.Add(TmxMath.AddPoints(rowPos, oct[4])); points.Add(TmxMath.AddPoints(rowPos, oct[5])); } if (bottomRight) { points.Add(TmxMath.AddPoints(rowPos, oct[5])); points.Add(TmxMath.AddPoints(rowPos, oct[6])); } if (bottomLeft) { points.Add(TmxMath.AddPoints(rowPos, oct[7])); points.Add(TmxMath.AddPoints(rowPos, oct[0])); } rowPos.X += tileWidth + sideLengthX; } startPos.Y += rowHeight; } } foreach (var p in points) { RectangleF rc = new RectangleF(p.X, p.Y, PreviewImage.GridSize, PreviewImage.GridSize); rc.Offset(-PreviewImage.GridSize * 0.5f, -PreviewImage.GridSize * 0.5f); g.DrawRectangle(Pens.Black, rc.X, rc.Y, rc.Width, rc.Height); } }
private static void DrawGridQuad(SKCanvas canvas, TmxMap tmxMap) { HashSet <Point> points = new HashSet <Point>(); for (int x = 0; x < GetMaxTilesWide(tmxMap); ++x) { for (int y = 0; y < GetMaxTilesHigh(tmxMap); ++y) { // Add the "top-left" corner of a tile points.Add(TmxMath.TileCornerFromGridCoordinates(tmxMap, x, y)); // Add all other corners of the tile to our list of grid points // This is complicated by different map types (espcially staggered isometric) if (tmxMap.Orientation == TmxMap.MapOrientation.Orthogonal || tmxMap.Orientation == TmxMap.MapOrientation.Isometric) { points.Add(TmxMath.TileCornerFromGridCoordinates(tmxMap, x + 1, y)); points.Add(TmxMath.TileCornerFromGridCoordinates(tmxMap, x + 1, y + 1)); points.Add(TmxMath.TileCornerFromGridCoordinates(tmxMap, x, y + 1)); } else if (tmxMap.Orientation == TmxMap.MapOrientation.Staggered) { bool sx = TmxMath.DoStaggerX(tmxMap, x); bool sy = TmxMath.DoStaggerY(tmxMap, y); if (sx) { // top-right, bottom-right, and bottom-left points.Add(TmxMath.TileCornerFromGridCoordinates(tmxMap, x + 1, y + 1)); points.Add(TmxMath.TileCornerFromGridCoordinates(tmxMap, x, y + 1)); points.Add(TmxMath.TileCornerFromGridCoordinates(tmxMap, x - 1, y + 1)); } else if (sy) { // top-right, bottom-right, and bottom-left points.Add(TmxMath.TileCornerFromGridCoordinates(tmxMap, x + 1, y + 1)); points.Add(TmxMath.TileCornerFromGridCoordinates(tmxMap, x, y + 2)); points.Add(TmxMath.TileCornerFromGridCoordinates(tmxMap, x, y + 1)); } else if (tmxMap.StaggerAxis == TmxMap.MapStaggerAxis.X) { // top-right, bottom-right, and bottom-left points.Add(TmxMath.TileCornerFromGridCoordinates(tmxMap, x + 1, y)); points.Add(TmxMath.TileCornerFromGridCoordinates(tmxMap, x, y + 1)); points.Add(TmxMath.TileCornerFromGridCoordinates(tmxMap, x - 1, y)); } else if (tmxMap.StaggerAxis == TmxMap.MapStaggerAxis.Y) { // top-right, bottom-right, and bottom-left points.Add(TmxMath.TileCornerFromGridCoordinates(tmxMap, x, y + 1)); points.Add(TmxMath.TileCornerFromGridCoordinates(tmxMap, x, y + 2)); points.Add(TmxMath.TileCornerFromGridCoordinates(tmxMap, x - 1, y + 1)); } } } } // Can take for granted that background is always white in drawing black rectangles using (SKPaint paint = new SKPaint()) { paint.Color = SKColors.Black; paint.Style = SKPaintStyle.Stroke; paint.StrokeWidth = StrokeWidthThin; foreach (var p in points) { SKRect rc = SKRect.Create(p.X, p.Y, PreviewImage.GridSize, PreviewImage.GridSize); rc.Offset(-PreviewImage.GridSize * 0.5f, -PreviewImage.GridSize * 0.5f); canvas.DrawRect(rc, paint); } } }