private void DrawObjectCollider(Graphics g, TmxObject tmxObject, Color color)
        {
            Color brushColor = Color.FromArgb(128, color);

            using (Brush brush = new HatchBrush(HatchStyle.BackwardDiagonal, color, brushColor))
                using (Pen pen = new Pen(color))
                {
                    pen.Alignment = PenAlignment.Inset;

                    GraphicsState state = g.Save();
                    g.TranslateTransform(tmxObject.Position.X, tmxObject.Position.Y);
                    g.RotateTransform(tmxObject.Rotation);

                    if (tmxObject.GetType() == typeof(TmxObjectPolygon))
                    {
                        DrawPolygon(g, pen, brush, tmxObject as TmxObjectPolygon);
                    }
                    else if (tmxObject.GetType() == typeof(TmxObjectRectangle))
                    {
                        // Rectangles are polygons
                        DrawPolygon(g, pen, brush, tmxObject as TmxObjectPolygon);
                    }
                    else if (tmxObject.GetType() == typeof(TmxObjectEllipse))
                    {
                        DrawEllipse(g, pen, brush, tmxObject as TmxObjectEllipse);
                    }
                    else if (tmxObject.GetType() == typeof(TmxObjectPolyline))
                    {
                        DrawPolyline(g, pen, tmxObject as TmxObjectPolyline);
                    }
                    else if (tmxObject.GetType() == typeof(TmxObjectTile))
                    {
                        TmxObjectTile tmxObjectTile = tmxObject as TmxObjectTile;

                        RectangleF rcTile = new RectangleF();
                        rcTile.X    = 0;
                        rcTile.Y    = -tmxObjectTile.Tile.TileSize.Height;
                        rcTile.Size = tmxObjectTile.Tile.TileSize;

                        g.FillRectangle(brush, rcTile);
                        g.DrawRectangle(pen, rcTile.X, rcTile.Y, rcTile.Width - 1, rcTile.Height - 1);
                    }
                    else
                    {
                        g.Restore(state);
                        RectangleF bounds = tmxObject.GetWorldBounds();
                        g.FillRectangle(Brushes.Red, bounds.X, bounds.Y, bounds.Width, bounds.Height);
                        g.DrawRectangle(Pens.White, bounds.X, bounds.Y, bounds.Width, bounds.Height);
                        string message = String.Format("Unhandled object: {0}", tmxObject.GetNonEmptyName());
                        DrawString(g, message, bounds.X, bounds.Y);
                    }

                    // Restore our state
                    g.Restore(state);
                }
        }
        private void DrawObjectCollider(Graphics g, TmxObject tmxObject, Color color)
        {
            Color brushColor = Color.FromArgb(128, color);
            using (Brush brush = new HatchBrush(HatchStyle.BackwardDiagonal, color, brushColor))
            using (Pen pen = new Pen(color))
            {
                pen.Alignment = PenAlignment.Inset;

                GraphicsState state = g.Save();

                PointF xfPosition = TmxMath.ObjectPointFToMapSpace(this.tmxMap, tmxObject.Position);
                g.TranslateTransform(xfPosition.X, xfPosition.Y);
                g.RotateTransform(tmxObject.Rotation);

                if (tmxObject.GetType() == typeof(TmxObjectPolygon))
                {
                    DrawPolygon(g, pen, brush, tmxObject as TmxObjectPolygon);
                }
                else if (tmxObject.GetType() == typeof(TmxObjectRectangle))
                {
                    if (this.tmxMap.Orientation == TmxMap.MapOrientation.Isometric)
                    {
                        TmxObjectPolygon tmxIsometricRectangle = TmxObjectPolygon.FromIsometricRectangle(this.tmxMap, tmxObject as TmxObjectRectangle);
                        DrawPolygon(g, pen, brush, tmxIsometricRectangle);
                    }
                    else
                    {
                        // Rectangles are polygons
                        DrawPolygon(g, pen, brush, tmxObject as TmxObjectPolygon);
                    }
                }
                else if (tmxObject.GetType() == typeof(TmxObjectEllipse))
                {
                    DrawEllipse(g, pen, brush, tmxObject as TmxObjectEllipse);
                }
                else if (tmxObject.GetType() == typeof(TmxObjectPolyline))
                {
                    DrawPolyline(g, pen, tmxObject as TmxObjectPolyline);
                }
                else if (tmxObject.GetType() == typeof(TmxObjectTile))
                {
                    GraphicsState tileState = g.Save();
                    TmxObjectTile tmxObjectTile = tmxObject as TmxObjectTile;

                    // Apply scale
                    SizeF scale = tmxObjectTile.GetTileObjectScale();
                    g.ScaleTransform(scale.Width, scale.Height);

                    // Apply horizontal flip
                    if (tmxObjectTile.FlippedHorizontal)
                    {
                        g.TranslateTransform(tmxObjectTile.Tile.TileSize.Width, 0);
                        g.ScaleTransform(-1, 1);
                    }

                    // Apply vertical flip
                    if (tmxObjectTile.FlippedVertical)
                    {
                        g.TranslateTransform(0, -tmxObjectTile.Tile.TileSize.Height);
                        g.ScaleTransform(1, -1);
                    }

                    // (Note: Now we can draw the tile and collisions as normal as the transforms have been set up.)

                    // Draw the tile
                    Rectangle destination = new Rectangle(0, -tmxObjectTile.Tile.TileSize.Height, tmxObjectTile.Tile.TileSize.Width, tmxObjectTile.Tile.TileSize.Height);
                    Rectangle source = new Rectangle(tmxObjectTile.Tile.LocationOnSource, tmxObjectTile.Tile.TileSize);
                    g.DrawImage(tmxObjectTile.Tile.TmxImage.ImageBitmap, destination, source, GraphicsUnit.Pixel);

                    // Put a black border around the tile so it sticks out a bit as an object
                    g.DrawRectangle(Pens.Black, destination);

                    // Draw the collisions
                    // Make up for the fact that the bottom-left corner is the origin
                    g.TranslateTransform(0, -tmxObjectTile.Tile.TileSize.Height);
                    foreach (var obj in tmxObjectTile.Tile.ObjectGroup.Objects)
                    {
                        DrawObjectCollider(g, obj, Color.Gray);
                    }

                    g.Restore(tileState);
                }
                else
                {
                    g.Restore(state);
                    RectangleF bounds = tmxObject.GetWorldBounds();
                    g.FillRectangle(Brushes.Red, bounds.X, bounds.Y, bounds.Width, bounds.Height);
                    g.DrawRectangle(Pens.White, bounds.X, bounds.Y, bounds.Width, bounds.Height);
                    string message = String.Format("Unhandled object: {0}", tmxObject.GetNonEmptyName());
                    DrawString(g, message, bounds.X, bounds.Y);
                }

                // Restore our state
                g.Restore(state);
            }
        }
        private void DrawObjectCollider(Graphics g, TmxObject tmxObject, Color color)
        {
            Color brushColor = Color.FromArgb(128, color);
            using (Brush brush = new HatchBrush(HatchStyle.BackwardDiagonal, color, brushColor))
            using (Pen pen = new Pen(color))
            {
                pen.Alignment = PenAlignment.Inset;

                GraphicsState state = g.Save();

                PointF xfPosition = TmxMath.ObjectPointFToMapSpace(this.tmxMap, tmxObject.Position);
                g.TranslateTransform(xfPosition.X, xfPosition.Y);
                g.RotateTransform(tmxObject.Rotation);

                if (tmxObject.GetType() == typeof(TmxObjectPolygon))
                {
                    DrawPolygon(g, pen, brush, tmxObject as TmxObjectPolygon);
                }
                else if (tmxObject.GetType() == typeof(TmxObjectRectangle))
                {
                    if (this.tmxMap.Orientation == TmxMap.MapOrientation.Isometric)
                    {
                        TmxObjectPolygon tmxIsometricRectangle = TmxObjectPolygon.FromIsometricRectangle(this.tmxMap, tmxObject as TmxObjectRectangle);
                        DrawPolygon(g, pen, brush, tmxIsometricRectangle);
                    }
                    else
                    {
                        // Rectangles are polygons
                        DrawPolygon(g, pen, brush, tmxObject as TmxObjectPolygon);
                    }
                }
                else if (tmxObject.GetType() == typeof(TmxObjectEllipse))
                {
                    DrawEllipse(g, pen, brush, tmxObject as TmxObjectEllipse);
                }
                else if (tmxObject.GetType() == typeof(TmxObjectPolyline))
                {
                    DrawPolyline(g, pen, tmxObject as TmxObjectPolyline);
                }
                else if (tmxObject.GetType() == typeof(TmxObjectTile))
                {
                    TmxObjectTile tmxObjectTile = tmxObject as TmxObjectTile;

                    RectangleF rcTile = new RectangleF();
                    rcTile.X = 0;
                    rcTile.Y = -tmxObjectTile.Tile.TileSize.Height;
                    rcTile.Size = tmxObjectTile.Tile.TileSize;

                    g.FillRectangle(brush, rcTile);
                    g.DrawRectangle(pen, rcTile.X, rcTile.Y, rcTile.Width - 1, rcTile.Height - 1);
                }
                else
                {
                    g.Restore(state);
                    RectangleF bounds = tmxObject.GetWorldBounds();
                    g.FillRectangle(Brushes.Red, bounds.X, bounds.Y, bounds.Width, bounds.Height);
                    g.DrawRectangle(Pens.White, bounds.X, bounds.Y, bounds.Width, bounds.Height);
                    string message = String.Format("Unhandled object: {0}", tmxObject.GetNonEmptyName());
                    DrawString(g, message, bounds.X, bounds.Y);
                }

                // Restore our state
                g.Restore(state);
            }
        }
Beispiel #4
0
        private void DrawObjectCollider(Graphics g, TmxObject tmxObject, Color color)
        {
            Color brushColor = Color.FromArgb(128, color);

            using (Brush brush = new HatchBrush(HatchStyle.BackwardDiagonal, color, brushColor))
                using (Pen pen = new Pen(color))
                {
                    pen.Alignment = PenAlignment.Inset;

                    GraphicsState state = g.Save();

                    PointF xfPosition = TmxMath.ObjectPointFToMapSpace(this.tmxMap, tmxObject.Position);
                    g.TranslateTransform(xfPosition.X, xfPosition.Y);
                    g.RotateTransform(tmxObject.Rotation);

                    if (tmxObject.GetType() == typeof(TmxObjectPolygon))
                    {
                        DrawPolygon(g, pen, brush, tmxObject as TmxObjectPolygon);
                    }
                    else if (tmxObject.GetType() == typeof(TmxObjectRectangle))
                    {
                        if (this.tmxMap.Orientation == TmxMap.MapOrientation.Isometric)
                        {
                            TmxObjectPolygon tmxIsometricRectangle = TmxObjectPolygon.FromRectangle(this.tmxMap, tmxObject as TmxObjectRectangle);
                            DrawPolygon(g, pen, brush, tmxIsometricRectangle);
                        }
                        else
                        {
                            // Rectangles are polygons
                            DrawPolygon(g, pen, brush, tmxObject as TmxObjectPolygon);
                        }
                    }
                    else if (tmxObject.GetType() == typeof(TmxObjectEllipse))
                    {
                        DrawEllipse(g, pen, brush, tmxObject as TmxObjectEllipse);
                    }
                    else if (tmxObject.GetType() == typeof(TmxObjectPolyline))
                    {
                        DrawPolyline(g, pen, tmxObject as TmxObjectPolyline);
                    }
                    else if (tmxObject.GetType() == typeof(TmxObjectTile))
                    {
                        GraphicsState tileState     = g.Save();
                        TmxObjectTile tmxObjectTile = tmxObject as TmxObjectTile;

                        // Apply scale
                        SizeF scale = tmxObjectTile.GetTileObjectScale();
                        g.ScaleTransform(scale.Width, scale.Height);

                        // Apply horizontal flip
                        if (tmxObjectTile.FlippedHorizontal)
                        {
                            g.TranslateTransform(tmxObjectTile.Tile.TileSize.Width, 0);
                            g.ScaleTransform(-1, 1);
                        }

                        // Apply vertical flip
                        if (tmxObjectTile.FlippedVertical)
                        {
                            g.TranslateTransform(0, -tmxObjectTile.Tile.TileSize.Height);
                            g.ScaleTransform(1, -1);
                        }

                        // (Note: Now we can draw the tile and collisions as normal as the transforms have been set up.)

                        // Draw the tile
                        Rectangle destination = new Rectangle(0, -tmxObjectTile.Tile.TileSize.Height, tmxObjectTile.Tile.TileSize.Width, tmxObjectTile.Tile.TileSize.Height);
                        Rectangle source      = new Rectangle(tmxObjectTile.Tile.LocationOnSource, tmxObjectTile.Tile.TileSize);
                        g.DrawImage(tmxObjectTile.Tile.TmxImage.ImageBitmap, destination, source, GraphicsUnit.Pixel);

                        // Put a black border around the tile so it sticks out a bit as an object
                        g.DrawRectangle(Pens.Black, destination);

                        // Draw the collisions
                        // Make up for the fact that the bottom-left corner is the origin
                        g.TranslateTransform(0, -tmxObjectTile.Tile.TileSize.Height);
                        foreach (var obj in tmxObjectTile.Tile.ObjectGroup.Objects)
                        {
                            DrawObjectCollider(g, obj, Color.Gray);
                        }

                        g.Restore(tileState);
                    }
                    else
                    {
                        g.Restore(state);
                        RectangleF bounds = tmxObject.GetWorldBounds();
                        g.FillRectangle(Brushes.Red, bounds.X, bounds.Y, bounds.Width, bounds.Height);
                        g.DrawRectangle(Pens.White, bounds.X, bounds.Y, bounds.Width, bounds.Height);
                        string message = String.Format("Unhandled object: {0}", tmxObject.GetNonEmptyName());
                        DrawString(g, message, bounds.X, bounds.Y);
                    }

                    // Restore our state
                    g.Restore(state);
                }
        }