craftworkgames / MonoGame.Extended

Extensions to make MonoGame more awesome
http://www.monogameextended.net/
Other
1.44k stars 325 forks source link

PrimitiveDrawing DrawSolidRectangle() and DrawSolidCircle() should have bool outline parameters #727

Open ericrrichards opened 3 years ago

ericrrichards commented 3 years ago

It would be helpful if the MonoGame.Extended.VectorDraw.PrimitiveDrawing class had optional parameters on the DrawSolidRectangle and DrawSolidCircle methods to fill the shape with the same color that is specified, along the pattern of the DrawSolidPolygon and DrawSolidEllipse methods. Currently there's no way to do this, and so you get a shape with the desired color only on the outline, and a 50% alpha fill color.

For example:

public void DrawSolidRectangle(Vector2 location, float width, float height, Color color, /*added optional parameter*/ bool outline=true)
{
    if (!_primitiveBatch.IsReady())
        throw new InvalidOperationException("BeginCustomDraw must be called before drawing anything.");

    Vector2[] rectVerts = new Vector2[4]
    {
        new Vector2(0, 0),
        new Vector2(width, 0),
        new Vector2(width, height),
        new Vector2(0, height)
    };

    DrawSolidPolygon(location, rectVerts, color, /*pass parameter through*/ outline);
}
public void DrawSolidCircle(Vector2 center, float radius, Color color, /*add optional parameter*/ bool outline=true)
{
    if (!_primitiveBatch.IsReady())
        throw new InvalidOperationException("BeginCustomDraw must be called before drawing anything.");

    const double increment = Math.PI * 2.0 / CircleSegments;
    double theta = 0.0;

    Color colorFill = color * (outline ? 0.5f : 1.0f); // same fill color logic from DrawSolidPolygon()

    Vector2 v0 = center + radius * new Vector2((float)Math.Cos(theta), (float)Math.Sin(theta));
    theta += increment;

    for (int i = 1; i < CircleSegments - 1; i++)
    {
        Vector2 v1 = center + radius * new Vector2((float)Math.Cos(theta), (float)Math.Sin(theta));
        Vector2 v2 = center + radius * new Vector2((float)Math.Cos(theta + increment), (float)Math.Sin(theta + increment));

        _primitiveBatch.AddVertex(v0, colorFill, PrimitiveType.TriangleList);
        _primitiveBatch.AddVertex(v1, colorFill, PrimitiveType.TriangleList);
        _primitiveBatch.AddVertex(v2, colorFill, PrimitiveType.TriangleList);

        theta += increment;
    }
    if (outline) // skip if not outlining, as in DrawSolidPolygon()
        DrawCircle(center, radius, color);            
}