Exemple #1
0
        private static RectangleF CalculateBoundary(TmxMap tmxMap)
        {
            RectangleF rcMap = new RectangleF(Point.Empty, tmxMap.MapSizeInPixels());

            // Take boundaries from object groups
            var objBounds = from g in tmxMap.ObjectGroups
                            from o in g.Objects
                            where o.Visible == true
                            select o.GetWorldBounds();

            // Take boundaries from objects embedded in tiles
            var tileBounds = from layer in tmxMap.Layers
                             where layer.Visible == true
                             from y in Enumerable.Range(0, layer.Height)
                             from x in Enumerable.Range(0, layer.Width)
                             let tileId = layer.GetTileIdAt(x, y)
                                          where tileId != 0
                                          let tile = tmxMap.Tiles[tileId]
                                                     from o in tile.ObjectGroup.Objects
                                                     let bound                         = o.GetWorldBounds()
                                                                             let point = TmxMath.TileCornerInScreenCoordinates(tmxMap, x, y)
                                                                                         select new RectangleF(bound.X + point.X, bound.Y + point.Y, bound.Width, bound.Height);

            var allBounds = objBounds.Concat(tileBounds);
            var union     = allBounds.Aggregate(rcMap, RectangleF.Union);

            // Inflate a tile size to make room for the grid
            union.Inflate(tmxMap.TileWidth, tmxMap.TileHeight);
            union.Inflate(PreviewImage.GridSize, PreviewImage.GridSize);

            return(union);
        }
Exemple #2
0
        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);
            }
        }
Exemple #3
0
        private static void DrawTilesInTileLayer(Graphics g, TmxMap tmxMap, TmxLayer layer)
        {
            // Set the opacity for the layer (Not supported on Mac builds)
#if !TILED2UNITY_MAC
            ColorMatrix colorMatrix = new ColorMatrix();
            colorMatrix.Matrix33 = layer.Opacity;

            ImageAttributes imageAttributes = new ImageAttributes();
            imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
#endif
            // The range of x and y depends on the render order of the tiles
            // By default we draw right and down but may reverse the tiles we visit
            var range_x = Enumerable.Range(0, GetMaxTilesWide(layer));
            var range_y = Enumerable.Range(0, GetMaxTilesHigh(layer));

            if (tmxMap.DrawOrderHorizontal == -1)
            {
                range_x = range_x.Reverse();
            }

            if (tmxMap.DrawOrderVertical == -1)
            {
                range_y = range_y.Reverse();
            }

            // Visit the tiles we are going to draw
            var tiles = from y in range_y
                        from x in range_x
                        let rawTileId = layer.GetRawTileIdAt(x, y)
                                        let tileId = layer.GetTileIdAt(x, y)
                                                     where tileId != 0

                                                     let tile = tmxMap.Tiles[tileId]

                                                                // Support for animated tiles. Just show the first frame of the animation.
                                                                let frame = tmxMap.Tiles[tile.Animation.Frames[0].GlobalTileId]

                                                                            select new
            {
                Tile                  = frame,
                Position              = TmxMath.TileCornerInScreenCoordinates(tmxMap, x, y),
                Bitmap                = frame.TmxImage.ImageBitmap,
                IsFlippedDiagnoally   = TmxMath.IsTileFlippedDiagonally(rawTileId),
                IsFlippedHorizontally = TmxMath.IsTileFlippedHorizontally(rawTileId),
                IsFlippedVertically   = TmxMath.IsTileFlippedVertically(rawTileId),
            };

            PointF[] destPoints  = new PointF[4];
            PointF[] destPoints3 = new PointF[3];
            foreach (var t in tiles)
            {
                PointF location = t.Position;

                // Individual tiles may be larger than the given tile size of the overall map
                location.Y = (t.Position.Y - t.Tile.TileSize.Height) + tmxMap.TileHeight;

                // Take tile offset into account
                location.X += t.Tile.Offset.X;
                location.Y += t.Tile.Offset.Y;

                // Make up the 'quad' of texture points and transform them
                PointF center = new PointF(t.Tile.TileSize.Width * 0.5f, t.Tile.TileSize.Height * 0.5f);
                destPoints[0] = new Point(0, 0);
                destPoints[1] = new Point(t.Tile.TileSize.Width, 0);
                destPoints[2] = new Point(t.Tile.TileSize.Width, t.Tile.TileSize.Height);
                destPoints[3] = new Point(0, t.Tile.TileSize.Height);

                // Transform the points based on our flipped flags
                TmxMath.TransformPoints(destPoints, center, t.IsFlippedDiagnoally, t.IsFlippedHorizontally, t.IsFlippedVertically);

                // Put the destination points back into world space
                TmxMath.TranslatePoints(destPoints, location);

                // Stupid DrawImage function only takes 3 destination points otherwise it throws an exception
                destPoints3[0] = destPoints[0];
                destPoints3[1] = destPoints[1];
                destPoints3[2] = destPoints[3];

                // Draw the tile
                Rectangle source = new Rectangle(t.Tile.LocationOnSource, t.Tile.TileSize);
#if !TILED2UNITY_MAC
                g.DrawImage(t.Bitmap, destPoints3, source, GraphicsUnit.Pixel, imageAttributes);
#else
                g.DrawImage(t.Bitmap, destPoints3, source, GraphicsUnit.Pixel);
#endif
            }
        }
Exemple #4
0
        private static SKRect CalculateBoundary(TmxMap tmxMap)
        {
            SKRect rcMap = SKRect.Create(0, 0, tmxMap.MapSizeInPixels.Width, tmxMap.MapSizeInPixels.Height);

            // Any tile layers in the map can be offset
            var tileLayerBounds = from layer in tmxMap.EnumerateTileLayers()
                                  where layer.Visible == true
                                  let offset = layer.GetCombinedOffset()
                                               select SKRect.Create(offset.X, offset.Y, rcMap.Size.Width, rcMap.Size.Height);

            // Take boundaries from object groups
            var objBounds = from g in tmxMap.EnumerateObjectLayers()
                            from o in g.Objects
                            where o.Visible == true
                            let b = o.GetOffsetWorldBounds()
                                    select SKRect.Create(b.X, b.Y, b.Width, b.Height);

            // Take boundaries from objects embedded in tiles
            var tileBounds = from layer in tmxMap.EnumerateTileLayers()
                             where layer.Visible == true
                             from y in Enumerable.Range(0, layer.Height)
                             from x in Enumerable.Range(0, layer.Width)
                             let tileId = layer.GetTileIdAt(x, y)
                                          where tileId != 0
                                          let tile = tmxMap.Tiles[tileId]
                                                     from o in tile.ObjectGroup.Objects
                                                     let bound                         = o.GetOffsetWorldBounds()
                                                                             let point = TmxMath.TileCornerInScreenCoordinates(tmxMap, x, y)
                                                                                         select SKRect.Create(bound.X + point.X, bound.Y + point.Y, bound.Width, bound.Height);

            var allBounds = tileLayerBounds.Concat(objBounds).Concat(tileBounds);
            var union     = allBounds.Aggregate(rcMap, SKRect.Union);

            // Inflate a tile size to make room for the grid
            union.Inflate(tmxMap.TileWidth, tmxMap.TileHeight);
            union.Inflate(PreviewImage.GridSize, PreviewImage.GridSize);

            return(union);
        }