using System; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using MonoGame.Extended.Shapes; namespace MonoGame.Extended { /// /// Sprite batch extensions for drawing primitive shapes /// public static class ShapeExtensions { private static Texture2D _whitePixelTexture; private static Texture2D GetTexture(SpriteBatch spriteBatch) { if (_whitePixelTexture == null || _whitePixelTexture.IsDisposed) { _whitePixelTexture = new Texture2D(spriteBatch.GraphicsDevice, 1, 1, false, SurfaceFormat.Color); _whitePixelTexture.SetData(new[] { Color.White }); spriteBatch.Disposing += (sender, args) => { _whitePixelTexture?.Dispose(); _whitePixelTexture = null; }; } return _whitePixelTexture; } /// /// Draws a closed polygon from a shape /// /// The destination drawing surface /// /// /// Where to position the polygon /// The polygon to draw /// The color to use /// The thickness of the lines /// /// The depth of the layer of this shape public static void DrawPolygon(this SpriteBatch spriteBatch, Vector2 position, Polygon polygon, Color color, float thickness = 1f, float layerDepth = 0) { DrawPolygon(spriteBatch, position, polygon.Vertices, color, thickness, layerDepth); } /// /// Draws a closed polygon from an array of points /// /// The destination drawing surface /// /// /// Where to offset the points /// The points to connect with lines /// The color to use /// The thickness of the lines /// The depth of the layer of this shape public static void DrawPolygon(this SpriteBatch spriteBatch, Vector2 offset, IReadOnlyList points, Color color, float thickness = 1f, float layerDepth = 0) { if (points.Count == 0) return; if (points.Count == 1) { DrawPoint(spriteBatch, points[0], color, (int)thickness); return; } var texture = GetTexture(spriteBatch); for (var i = 0; i < points.Count - 1; i++) DrawPolygonEdge(spriteBatch, texture, points[i] + offset, points[i + 1] + offset, color, thickness, layerDepth); DrawPolygonEdge(spriteBatch, texture, points[points.Count - 1] + offset, points[0] + offset, color, thickness, layerDepth); } private static void DrawPolygonEdge(SpriteBatch spriteBatch, Texture2D texture, Vector2 point1, Vector2 point2, Color color, float thickness, float layerDepth) { var length = Vector2.Distance(point1, point2); var angle = (float)Math.Atan2(point2.Y - point1.Y, point2.X - point1.X); var scale = new Vector2(length, thickness); spriteBatch.Draw(texture, point1, null, color, angle, Vector2.Zero, scale, SpriteEffects.None, layerDepth); } /// /// Draws a filled rectangle /// /// The destination drawing surface /// The rectangle to draw /// The color to draw the rectangle in /// The depth of the layer of this shape public static void FillRectangle(this SpriteBatch spriteBatch, RectangleF rectangle, Color color, float layerDepth = 0) { FillRectangle(spriteBatch, rectangle.Position, rectangle.Size, color, layerDepth); } /// /// Draws a filled rectangle /// /// The destination drawing surface /// Where to draw /// The size of the rectangle /// The color to draw the rectangle in /// The depth of the layer of this shape public static void FillRectangle(this SpriteBatch spriteBatch, Vector2 location, SizeF size, Color color, float layerDepth = 0) { spriteBatch.Draw(GetTexture(spriteBatch), location, null, color, 0, Vector2.Zero, size, SpriteEffects.None, layerDepth); } /// /// Draws a filled rectangle /// /// The destination drawing surface /// The X coord of the left side /// The Y coord of the upper side /// Width /// Height /// The color to draw the rectangle in /// The depth of the layer of this shape public static void FillRectangle(this SpriteBatch spriteBatch, float x, float y, float width, float height, Color color, float layerDepth = 0) { FillRectangle(spriteBatch, new Vector2(x, y), new SizeF(width, height), color, layerDepth); } /// /// Draws a rectangle with the thickness provided /// /// The destination drawing surface /// The rectangle to draw /// The color to draw the rectangle in /// The thickness of the lines /// The depth of the layer of this shape public static void DrawRectangle(this SpriteBatch spriteBatch, RectangleF rectangle, Color color, float thickness = 1f, float layerDepth = 0) { var texture = GetTexture(spriteBatch); var topLeft = new Vector2(rectangle.X, rectangle.Y); var topRight = new Vector2(rectangle.Right - thickness, rectangle.Y); var bottomLeft = new Vector2(rectangle.X, rectangle.Bottom - thickness); var horizontalScale = new Vector2(rectangle.Width, thickness); var verticalScale = new Vector2(thickness, rectangle.Height); spriteBatch.Draw(texture, topLeft, null, color, 0f, Vector2.Zero, horizontalScale, SpriteEffects.None, layerDepth); spriteBatch.Draw(texture, topLeft, null, color, 0f, Vector2.Zero, verticalScale, SpriteEffects.None, layerDepth); spriteBatch.Draw(texture, topRight, null, color, 0f, Vector2.Zero, verticalScale, SpriteEffects.None, layerDepth); spriteBatch.Draw(texture, bottomLeft, null, color, 0f, Vector2.Zero, horizontalScale, SpriteEffects.None, layerDepth); } /// /// Draws a rectangle with the thickness provided /// /// The destination drawing surface /// Where to draw /// The size of the rectangle /// The color to draw the rectangle in /// The thickness of the line /// The depth of the layer of this shape public static void DrawRectangle(this SpriteBatch spriteBatch, Vector2 location, SizeF size, Color color, float thickness = 1f, float layerDepth = 0) { DrawRectangle(spriteBatch, new RectangleF(location.X, location.Y, size.Width, size.Height), color, thickness, layerDepth); } /// /// Draws a rectangle outline. /// public static void DrawRectangle(this SpriteBatch spriteBatch, float x, float y, float width, float height, Color color, float thickness = 1f, float layerDepth = 0) { DrawRectangle(spriteBatch, new RectangleF(x, y, width, height), color, thickness, layerDepth); } /// /// Draws a line from point1 to point2 with an offset /// /// The destination drawing surface /// The X coord of the first point /// The Y coord of the first point /// The X coord of the second point /// The Y coord of the second point /// The color to use /// The thickness of the line /// The depth of the layer of this shape public static void DrawLine(this SpriteBatch spriteBatch, float x1, float y1, float x2, float y2, Color color, float thickness = 1f, float layerDepth = 0) { DrawLine(spriteBatch, new Vector2(x1, y1), new Vector2(x2, y2), color, thickness, layerDepth); } /// /// Draws a line from point1 to point2 with an offset /// /// The destination drawing surface /// The first point /// The second point /// The color to use /// The thickness of the line /// The depth of the layer of this shape public static void DrawLine(this SpriteBatch spriteBatch, Vector2 point1, Vector2 point2, Color color, float thickness = 1f, float layerDepth = 0) { // calculate the distance between the two vectors var distance = Vector2.Distance(point1, point2); // calculate the angle between the two vectors var angle = (float)Math.Atan2(point2.Y - point1.Y, point2.X - point1.X); DrawLine(spriteBatch, point1, distance, angle, color, thickness, layerDepth); } /// /// Draws a line from point1 to point2 with an offset /// /// The destination drawing surface /// The starting point /// The length of the line /// The angle of this line from the starting point /// The color to use /// The thickness of the line /// The depth of the layer of this shape public static void DrawLine(this SpriteBatch spriteBatch, Vector2 point, float length, float angle, Color color, float thickness = 1f, float layerDepth = 0) { var origin = new Vector2(0f, 0.5f); var scale = new Vector2(length, thickness); spriteBatch.Draw(GetTexture(spriteBatch), point, null, color, angle, origin, scale, SpriteEffects.None, layerDepth); } /// /// Draws a point at the specified x, y position. The center of the point will be at the position. /// public static void DrawPoint(this SpriteBatch spriteBatch, float x, float y, Color color, float size = 1f, float layerDepth = 0) { DrawPoint(spriteBatch, new Vector2(x, y), color, size, layerDepth); } /// /// Draws a point at the specified position. The center of the point will be at the position. /// public static void DrawPoint(this SpriteBatch spriteBatch, Vector2 position, Color color, float size = 1f, float layerDepth = 0) { var scale = Vector2.One * size; var offset = new Vector2(0.5f) - new Vector2(size * 0.5f); spriteBatch.Draw(GetTexture(spriteBatch), position + offset, null, color, 0f, Vector2.Zero, scale, SpriteEffects.None, layerDepth); } /// /// Draw a circle from a shape /// /// The destination drawing surface /// The circle shape to draw /// The number of sides to generate /// The color of the circle /// The thickness of the lines used /// The depth of the layer of this shape public static void DrawCircle(this SpriteBatch spriteBatch, CircleF circle, int sides, Color color, float thickness = 1f, float layerDepth = 0) { DrawCircle(spriteBatch, circle.Center, circle.Radius, sides, color, thickness, layerDepth); } /// /// Draw a circle /// /// The destination drawing surface /// The center of the circle /// The radius of the circle /// The number of sides to generate /// The color of the circle /// The thickness of the lines used /// The depth of the layer of this shape public static void DrawCircle(this SpriteBatch spriteBatch, Vector2 center, float radius, int sides, Color color, float thickness = 1f, float layerDepth = 0) { DrawPolygon(spriteBatch, center, CreateCircle(radius, sides), color, thickness, layerDepth); } /// /// Draw a circle /// /// The destination drawing surface /// The center X of the circle /// The center Y of the circle /// The radius of the circle /// The number of sides to generate /// The color of the circle /// The thickness of the line /// The depth of the layer of this shape public static void DrawCircle(this SpriteBatch spriteBatch, float x, float y, float radius, int sides, Color color, float thickness = 1f, float layerDepth = 0) { DrawPolygon(spriteBatch, new Vector2(x, y), CreateCircle(radius, sides), color, thickness, layerDepth); } /// /// Draw an ellipse. /// /// The destination drawing surface /// Center of the ellipse /// Radius of the ellipse /// The number of sides to generate. /// The color of the ellipse. /// The thickness of the line around the ellipse. /// The depth of the layer of this shape public static void DrawEllipse(this SpriteBatch spriteBatch, Vector2 center, Vector2 radius, int sides, Color color, float thickness = 1f, float layerDepth = 0) { DrawPolygon(spriteBatch, center, CreateEllipse(radius.X, radius.Y, sides), color, thickness, layerDepth); } private static Vector2[] CreateCircle(double radius, int sides) { const double max = 2.0 * Math.PI; var points = new Vector2[sides]; var step = max / sides; var theta = 0.0; for (var i = 0; i < sides; i++) { points[i] = new Vector2((float)(radius * Math.Cos(theta)), (float)(radius * Math.Sin(theta))); theta += step; } return points; } private static Vector2[] CreateEllipse(float rx, float ry, int sides) { var vertices = new Vector2[sides]; var t = 0.0; var dt = 2.0 * Math.PI / sides; for (var i = 0; i < sides; i++, t += dt) { var x = (float)(rx * Math.Cos(t)); var y = (float)(ry * Math.Sin(t)); vertices[i] = new Vector2(x, y); } return vertices; } } }