exposed layer depth on the shape extensions to implement #623

This commit is contained in:
Dylan Wilson
2019-08-09 21:15:26 +10:00
parent 3ed010ec0c
commit b2b90832a0
3 changed files with 135 additions and 75 deletions
+53
View File
@@ -0,0 +1,53 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Extended;
using MonoGame.Extended.Shapes;
namespace Features.Demos
{
public class ShapesDemo : DemoBase
{
private SpriteBatch _spriteBatch;
public ShapesDemo(GameMain game)
: base(game)
{
}
public override string Name => "Shapes";
private readonly Polygon _polygon = new Polygon(new[]
{
new Vector2(0, 0),
new Vector2(40, 0),
new Vector2(40, 40),
new Vector2(60, 40),
new Vector2(60, 60),
new Vector2(0, 60),
});
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
}
protected override void Draw(GameTime gameTime)
{
_spriteBatch.Begin(samplerState: SamplerState.PointClamp, blendState: BlendState.AlphaBlend);
_spriteBatch.FillRectangle(10, 10, 50, 50, Color.DarkRed);
_spriteBatch.DrawRectangle(10, 10, 50, 50, Color.Red, 2);
_spriteBatch.DrawCircle(100, 100, 32, 32, Color.Green, 4);
_spriteBatch.DrawEllipse(new Vector2(200, 200), new Vector2(50, 25), 32, Color.Orange, 8);
_spriteBatch.DrawLine(300, 300, 400, 100, Color.White, 6);
_spriteBatch.DrawPoint(500, 300, Color.Brown, 64);
_spriteBatch.DrawPolygon(new Vector2(600, 200), _polygon, Color.Aqua);
_spriteBatch.End();
}
}
}
+1
View File
@@ -45,6 +45,7 @@ namespace Features
//new GuiLayoutDemo(this),
//new GuiDemo(this),
//new ScreensDemo(this),
new ShapesDemo(this),
new ViewportAdaptersDemo(this),
new CollisionDemo(this),
new TiledMapsDemo(this),
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Net.NetworkInformation;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Extended.Shapes;
@@ -19,7 +18,12 @@ namespace MonoGame.Extended
if (_whitePixelTexture == null)
{
_whitePixelTexture = new Texture2D(spriteBatch.GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
_whitePixelTexture.SetData(new[] {Color.White});
_whitePixelTexture.SetData(new[] { Color.White });
spriteBatch.Disposing += (sender, args) =>
{
_whitePixelTexture?.Dispose();
_whitePixelTexture = null;
};
}
return _whitePixelTexture;
@@ -34,10 +38,10 @@ namespace MonoGame.Extended
/// <param name="polygon">The polygon to draw</param>
/// <param name="color">The color to use</param>
/// <param name="thickness">The thickness of the lines</param>
public static void DrawPolygon(this SpriteBatch spriteBatch, Vector2 position, Polygon polygon, Color color,
float thickness = 1f)
/// /// <param name="layerDepth">The depth of the layer of this shape</param>
public static void DrawPolygon(this SpriteBatch spriteBatch, Vector2 position, Polygon polygon, Color color, float thickness = 1f, int layerDepth = 0)
{
DrawPolygon(spriteBatch, position, polygon.Vertices, color, thickness);
DrawPolygon(spriteBatch, position, polygon.Vertices, color, thickness, layerDepth);
}
/// <summary>
@@ -49,61 +53,61 @@ namespace MonoGame.Extended
/// <param name="points">The points to connect with lines</param>
/// <param name="color">The color to use</param>
/// <param name="thickness">The thickness of the lines</param>
public static void DrawPolygon(this SpriteBatch spriteBatch, Vector2 offset, IReadOnlyList<Vector2> points, Color color,
float thickness = 1f)
/// <param name="layerDepth">The depth of the layer of this shape</param>
public static void DrawPolygon(this SpriteBatch spriteBatch, Vector2 offset, IReadOnlyList<Vector2> points, Color color, float thickness = 1f, int layerDepth = 0)
{
if (points.Count == 0)
return;
if (points.Count == 1)
{
DrawPoint(spriteBatch, points[0], color, (int) thickness);
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);
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);
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)
private static void DrawPolygonEdge(SpriteBatch spriteBatch, Texture2D texture, Vector2 point1, Vector2 point2, Color color, float thickness, int layerDepth)
{
var length = Vector2.Distance(point1, point2);
var angle = (float) Math.Atan2(point2.Y - point1.Y, point2.X - point1.X);
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, 0);
spriteBatch.Draw(texture, point1, null, color, angle, Vector2.Zero, scale, SpriteEffects.None, layerDepth);
}
/// <summary>
/// Draws a filled rectangle
/// Draws a filled rectangle
/// </summary>
/// <param name="spriteBatch">The destination drawing surface</param>
/// <param name="rectangle">The rectangle to draw</param>
/// <param name="color">The color to draw the rectangle in</param>
public static void FillRectangle(this SpriteBatch spriteBatch, RectangleF rectangle, Color color)
/// <param name="layerDepth">The depth of the layer of this shape</param>
public static void FillRectangle(this SpriteBatch spriteBatch, RectangleF rectangle, Color color, int layerDepth = 0)
{
FillRectangle(spriteBatch, rectangle.Position, rectangle.Size, color);
FillRectangle(spriteBatch, rectangle.Position, rectangle.Size, color, layerDepth);
}
/// <summary>
/// Draws a filled rectangle
/// Draws a filled rectangle
/// </summary>
/// <param name="spriteBatch">The destination drawing surface</param>
/// <param name="location">Where to draw</param>
/// <param name="size">The size of the rectangle</param>
/// <param name="color">The color to draw the rectangle in</param>
public static void FillRectangle(this SpriteBatch spriteBatch, Vector2 location, Size2 size, Color color)
/// <param name="layerDepth">The depth of the layer of this shape</param>
public static void FillRectangle(this SpriteBatch spriteBatch, Vector2 location, Size2 size, Color color, int layerDepth = 0)
{
spriteBatch.Draw(GetTexture(spriteBatch), location, null, color, 0, Vector2.Zero, size, SpriteEffects.None, 0);
spriteBatch.Draw(GetTexture(spriteBatch), location, null, color, 0, Vector2.Zero, size, SpriteEffects.None, layerDepth);
}
/// <summary>
/// Draws a filled rectangle
/// Draws a filled rectangle
/// </summary>
/// <param name="spriteBatch">The destination drawing surface</param>
/// <param name="x">The X coord of the left side</param>
@@ -111,19 +115,21 @@ namespace MonoGame.Extended
/// <param name="width">Width</param>
/// <param name="height">Height</param>
/// <param name="color">The color to draw the rectangle in</param>
public static void FillRectangle(this SpriteBatch spriteBatch, float x, float y, float width, float height, Color color)
/// <param name="layerDepth">The depth of the layer of this shape</param>
public static void FillRectangle(this SpriteBatch spriteBatch, float x, float y, float width, float height, Color color, int layerDepth = 0)
{
FillRectangle(spriteBatch, new Vector2(x, y), new Size2(width, height), color);
FillRectangle(spriteBatch, new Vector2(x, y), new Size2(width, height), color, layerDepth);
}
/// <summary>
/// Draws a rectangle with the thickness provided
/// Draws a rectangle with the thickness provided
/// </summary>
/// <param name="spriteBatch">The destination drawing surface</param>
/// <param name="rectangle">The rectangle to draw</param>
/// <param name="color">The color to draw the rectangle in</param>
/// <param name="thickness">The thickness of the lines</param>
public static void DrawRectangle(this SpriteBatch spriteBatch, RectangleF rectangle, Color color, float thickness = 1f)
/// <param name="layerDepth">The depth of the layer of this shape</param>
public static void DrawRectangle(this SpriteBatch spriteBatch, RectangleF rectangle, Color color, float thickness = 1f, int layerDepth = 0)
{
var texture = GetTexture(spriteBatch);
var topLeft = new Vector2(rectangle.X, rectangle.Y);
@@ -132,10 +138,10 @@ namespace MonoGame.Extended
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, 0);
spriteBatch.Draw(texture, topLeft, null, color, 0f, Vector2.Zero, verticalScale, SpriteEffects.None, 0);
spriteBatch.Draw(texture, topRight, null, color, 0f, Vector2.Zero, verticalScale, SpriteEffects.None, 0);
spriteBatch.Draw(texture, bottomLeft, null, color, 0f, Vector2.Zero, horizontalScale, SpriteEffects.None, 0);
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);
}
/// <summary>
@@ -146,23 +152,23 @@ namespace MonoGame.Extended
/// <param name="size">The size of the rectangle</param>
/// <param name="color">The color to draw the rectangle in</param>
/// <param name="thickness">The thickness of the line</param>
public static void DrawRectangle(this SpriteBatch spriteBatch, Vector2 location, Size2 size, Color color,
float thickness = 1f)
/// <param name="layerDepth">The depth of the layer of this shape</param>
public static void DrawRectangle(this SpriteBatch spriteBatch, Vector2 location, Size2 size, Color color, float thickness = 1f, int layerDepth = 0)
{
DrawRectangle(spriteBatch, new RectangleF(location.X, location.Y, size.Width, size.Height), color, thickness);
DrawRectangle(spriteBatch, new RectangleF(location.X, location.Y, size.Width, size.Height), color, thickness, layerDepth);
}
/// <summary>
/// Draws a rectangle outline.
/// </summary>
public static void DrawRectangle(this SpriteBatch spriteBatch, float x, float y, float width, float height, Color color, float thickness = 1f)
public static void DrawRectangle(this SpriteBatch spriteBatch, float x, float y, float width, float height, Color color, float thickness = 1f, int layerDepth = 0)
{
DrawRectangle(spriteBatch, new RectangleF(x, y, width, height), color, thickness);
DrawRectangle(spriteBatch, new RectangleF(x, y, width, height), color, thickness, layerDepth);
}
/// <summary>
/// Draws a line from point1 to point2 with an offset
/// Draws a line from point1 to point2 with an offset
/// </summary>
/// <param name="spriteBatch">The destination drawing surface</param>
/// <param name="x1">The X coord of the first point</param>
@@ -171,34 +177,34 @@ namespace MonoGame.Extended
/// <param name="y2">The Y coord of the second point</param>
/// <param name="color">The color to use</param>
/// <param name="thickness">The thickness of the line</param>
public static void DrawLine(this SpriteBatch spriteBatch, float x1, float y1, float x2, float y2, Color color,
float thickness = 1f)
/// <param name="layerDepth">The depth of the layer of this shape</param>
public static void DrawLine(this SpriteBatch spriteBatch, float x1, float y1, float x2, float y2, Color color, float thickness = 1f, int layerDepth = 0)
{
DrawLine(spriteBatch, new Vector2(x1, y1), new Vector2(x2, y2), color, thickness);
DrawLine(spriteBatch, new Vector2(x1, y1), new Vector2(x2, y2), color, thickness, layerDepth);
}
/// <summary>
/// Draws a line from point1 to point2 with an offset
/// Draws a line from point1 to point2 with an offset
/// </summary>
/// <param name="spriteBatch">The destination drawing surface</param>
/// <param name="point1">The first point</param>
/// <param name="point2">The second point</param>
/// <param name="color">The color to use</param>
/// <param name="thickness">The thickness of the line</param>
public static void DrawLine(this SpriteBatch spriteBatch, Vector2 point1, Vector2 point2, Color color,
float thickness = 1f)
/// <param name="layerDepth">The depth of the layer of this shape</param>
public static void DrawLine(this SpriteBatch spriteBatch, Vector2 point1, Vector2 point2, Color color, float thickness = 1f, int 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);
var angle = (float)Math.Atan2(point2.Y - point1.Y, point2.X - point1.X);
DrawLine(spriteBatch, point1, distance, angle, color, thickness);
DrawLine(spriteBatch, point1, distance, angle, color, thickness, layerDepth);
}
/// <summary>
/// Draws a line from point1 to point2 with an offset
/// Draws a line from point1 to point2 with an offset
/// </summary>
/// <param name="spriteBatch">The destination drawing surface</param>
/// <param name="point">The starting point</param>
@@ -206,48 +212,48 @@ namespace MonoGame.Extended
/// <param name="angle">The angle of this line from the starting point</param>
/// <param name="color">The color to use</param>
/// <param name="thickness">The thickness of the line</param>
public static void DrawLine(this SpriteBatch spriteBatch, Vector2 point, float length, float angle, Color color,
float thickness = 1f)
/// <param name="layerDepth">The depth of the layer of this shape</param>
public static void DrawLine(this SpriteBatch spriteBatch, Vector2 point, float length, float angle, Color color, float thickness = 1f, int 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, 0);
spriteBatch.Draw(GetTexture(spriteBatch), point, null, color, angle, origin, scale, SpriteEffects.None, layerDepth);
}
/// <summary>
/// Draws a point at the specified x, y position. The center of the point will be at the position.
/// Draws a point at the specified x, y position. The center of the point will be at the position.
/// </summary>
public static void DrawPoint(this SpriteBatch spriteBatch, float x, float y, Color color, float size = 1f)
public static void DrawPoint(this SpriteBatch spriteBatch, float x, float y, Color color, float size = 1f, int layerDepth = 0)
{
DrawPoint(spriteBatch, new Vector2(x, y), color, size);
DrawPoint(spriteBatch, new Vector2(x, y), color, size, layerDepth);
}
/// <summary>
/// Draws a point at the specified position. The center of the point will be at the position.
/// Draws a point at the specified position. The center of the point will be at the position.
/// </summary>
public static void DrawPoint(this SpriteBatch spriteBatch, Vector2 position, Color color, float size = 1f)
public static void DrawPoint(this SpriteBatch spriteBatch, Vector2 position, Color color, float size = 1f, int 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, 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);
}
/// <summary>
/// Draw a circle from a <see cref="CircleF" /> shape
/// Draw a circle from a <see cref="CircleF" /> shape
/// </summary>
/// <param name="spriteBatch">The destination drawing surface</param>
/// <param name="circle">The circle shape to draw</param>
/// <param name="sides">The number of sides to generate</param>
/// <param name="color">The color of the circle</param>
/// <param name="thickness">The thickness of the lines used</param>
public static void DrawCircle(this SpriteBatch spriteBatch, CircleF circle, int sides, Color color,
float thickness = 1f)
/// <param name="layerDepth">The depth of the layer of this shape</param>
public static void DrawCircle(this SpriteBatch spriteBatch, CircleF circle, int sides, Color color, float thickness = 1f, int layerDepth = 0)
{
DrawCircle(spriteBatch, circle.Center, circle.Radius, sides, color, thickness);
DrawCircle(spriteBatch, circle.Center, circle.Radius, sides, color, thickness, layerDepth);
}
/// <summary>
/// Draw a circle
/// Draw a circle
/// </summary>
/// <param name="spriteBatch">The destination drawing surface</param>
/// <param name="center">The center of the circle</param>
@@ -255,14 +261,14 @@ namespace MonoGame.Extended
/// <param name="sides">The number of sides to generate</param>
/// <param name="color">The color of the circle</param>
/// <param name="thickness">The thickness of the lines used</param>
public static void DrawCircle(this SpriteBatch spriteBatch, Vector2 center, float radius, int sides, Color color,
float thickness = 1f)
/// <param name="layerDepth">The depth of the layer of this shape</param>
public static void DrawCircle(this SpriteBatch spriteBatch, Vector2 center, float radius, int sides, Color color, float thickness = 1f, int layerDepth = 0)
{
DrawPolygon(spriteBatch, center, CreateCircle(radius, sides), color, thickness);
DrawPolygon(spriteBatch, center, CreateCircle(radius, sides), color, thickness, layerDepth);
}
/// <summary>
/// Draw a circle
/// Draw a circle
/// </summary>
/// <param name="spriteBatch">The destination drawing surface</param>
/// <param name="x">The center X of the circle</param>
@@ -271,14 +277,14 @@ namespace MonoGame.Extended
/// <param name="sides">The number of sides to generate</param>
/// <param name="color">The color of the circle</param>
/// <param name="thickness">The thickness of the line</param>
public static void DrawCircle(this SpriteBatch spriteBatch, float x, float y, float radius, int sides,
Color color, float thickness = 1f)
/// <param name="layerDepth">The depth of the layer of this shape</param>
public static void DrawCircle(this SpriteBatch spriteBatch, float x, float y, float radius, int sides, Color color, float thickness = 1f, int layerDepth = 0)
{
DrawPolygon(spriteBatch, new Vector2(x, y), CreateCircle(radius, sides), color, thickness);
DrawPolygon(spriteBatch, new Vector2(x, y), CreateCircle(radius, sides), color, thickness, layerDepth);
}
/// <summary>
/// Draw an ellipse.
/// Draw an ellipse.
/// </summary>
/// <param name="spriteBatch">The destination drawing surface</param>
/// <param name="center">Center of the ellipse</param>
@@ -286,22 +292,22 @@ namespace MonoGame.Extended
/// <param name="sides">The number of sides to generate.</param>
/// <param name="color">The color of the ellipse.</param>
/// <param name="thickness">The thickness of the line around the ellipse.</param>
public static void DrawEllipse(this SpriteBatch spriteBatch, Vector2 center, Vector2 radius, int sides,
Color color, float thickness = 1f)
/// <param name="layerDepth">The depth of the layer of this shape</param>
public static void DrawEllipse(this SpriteBatch spriteBatch, Vector2 center, Vector2 radius, int sides, Color color, float thickness = 1f, int layerDepth = 0)
{
DrawPolygon(spriteBatch, center, CreateEllipse(radius.X, radius.Y, sides), color, thickness);
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;
const double max = 2.0 * Math.PI;
var points = new Vector2[sides];
var step = max/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)));
points[i] = new Vector2((float)(radius * Math.Cos(theta)), (float)(radius * Math.Sin(theta)));
theta += step;
}