/Rendering/Basics/Drawing/Rect.cs
# · C# · 148 lines · 88 code · 9 blank · 51 comment · 4 complexity · 7c672915987ffd063ee86b156db8f5b8 MD5 · raw file
- using Delta.Utilities.Datatypes;
-
- namespace Delta.Rendering.Basics.Drawing
- {
- /// <summary>
- /// Draw 2D rectangles with help of this class. They can be drawn as only
- /// outline, filled shape and with rotation. This has the advantage of
- /// optimizing it better.
- /// </summary>
- public static class Rect
- {
- #region Constants
- /// <summary>
- /// Helper for the Rectangle method that allows rotated rectangle drawing.
- /// </summary>
- private static readonly Point[] rotatedEdgePoints = new Point[4];
- #endregion
-
- #region DrawOutline (Static)
- /// <summary>
- /// Draws a rectangle box with lines
- /// </summary>
- /// <param name="rect">Rectangle to draw</param>
- /// <param name="lineColor">Line color</param>
- public static void DrawOutline(Rectangle rect, Color lineColor)
- {
- // Skip if the drawArea is certainly outside of the quadratic space.
- // Checking the screen area would work too, but is a little slower and
- // won't exclude much more anyway.
- if (rect.Left >= 1.0f ||
- rect.Top >= 1.0f ||
- rect.Left < -rect.Width ||
- rect.Top < -rect.Height)
- {
- // Skip this material rendering, not visible this frame.
- return;
- }
-
- DrawManager drawInstance = DrawManager.Instance;
- Point topRight = rect.TopRight;
- Point bottomRight = rect.BottomRight;
- Point bottomLeft = rect.BottomLeft;
- drawInstance.Draw2DLine(ref rect.TopLeft, ref topRight, ref lineColor);
- drawInstance.Draw2DLine(ref topRight, ref bottomRight, ref lineColor);
- drawInstance.Draw2DLine(ref bottomRight, ref bottomLeft, ref lineColor);
- drawInstance.Draw2DLine(ref bottomLeft, ref rect.TopLeft, ref lineColor);
- }
-
- /// <summary>
- /// Draws a rotated rectangle box with lines.
- /// </summary>
- /// <param name="rect">Rectangle to draw</param>
- /// <param name="lineColor">Line color</param>
- /// <param name="rotation">Rotation of the rectangle</param>
- public static void DrawOutline(Rectangle rect, Color lineColor,
- float rotation)
- {
- // Skip if the drawArea is certainly outside of the quadratic space.
- // Checking the screen area would work too, but is a little slower and
- // won't exclude much more anyway.
- if (rect.Left >= 1.0f ||
- rect.Top >= 1.0f ||
- rect.Left < -rect.Width ||
- rect.Top < -rect.Height)
- {
- // Skip this material rendering, not visible this frame.
- return;
- }
-
- // Rotate the original rectangle
- rect.Rotate(rotation, rotatedEdgePoints);
- // And draw it out same way as above: top left, right, bottom right, left
- DrawManager drawInstance = DrawManager.Instance;
- drawInstance.Draw2DLine(ref rotatedEdgePoints[0],
- ref rotatedEdgePoints[1], ref lineColor);
- drawInstance.Draw2DLine(ref rotatedEdgePoints[1],
- ref rotatedEdgePoints[2], ref lineColor);
- drawInstance.Draw2DLine(ref rotatedEdgePoints[2],
- ref rotatedEdgePoints[3], ref lineColor);
- drawInstance.Draw2DLine(ref rotatedEdgePoints[3],
- ref rotatedEdgePoints[0], ref lineColor);
- }
- #endregion
-
- #region DrawFilled (Static)
- /// <summary>
- /// Draw filled rectangle at the given position with the given color.
- /// </summary>
- /// <param name="rect">The rect.</param>
- /// <param name="color">The color.</param>
- public static void DrawFilled(Rectangle rect, Color color)
- {
- // Skip if the drawArea is certainly outside of the quadratic space.
- // Checking the screen area would work too, but is a little slower and
- // won't exclude much more anyway.
- if (rect.Left >= 1.0f ||
- rect.Top >= 1.0f ||
- rect.Left < -rect.Width ||
- rect.Top < -rect.Height)
- {
- // Skip this material rendering, not visible this frame.
- return;
- }
-
- // A rectangle is just 2 polygons
- DrawManager drawInstance = DrawManager.Instance;
- Point bottomRight = rect.BottomRight;
- Point topRight = rect.TopRight;
- Point bottomLeft = rect.BottomLeft;
- drawInstance.DrawPolygon(ref rect.TopLeft, ref bottomRight,
- ref topRight, ref color);
- drawInstance.DrawPolygon(ref rect.TopLeft, ref bottomLeft,
- ref bottomRight, ref color);
- }
-
- /// <summary>
- /// Draw filled rectangle at the given position with the given color.
- /// </summary>
- /// <param name="rect">The rect.</param>
- /// <param name="color">The color.</param>
- /// <param name="rotation">The rotation.</param>
- public static void DrawFilled(Rectangle rect, Color color,
- float rotation)
- {
- // Skip if the drawArea is certainly outside of the quadratic space.
- // Checking the screen area would work too, but is a little slower and
- // won't exclude much more anyway.
- if (rect.Left >= 1.0f ||
- rect.Top >= 1.0f ||
- rect.Left < -rect.Width ||
- rect.Top < -rect.Height)
- {
- // Skip this material rendering, not visible this frame.
- return;
- }
-
- // A rectangle is 2 polygons, but rotate the original rectangle first
- rect.Rotate(rotation, rotatedEdgePoints);
- // And draw it out same way as above
- DrawManager drawInstance = DrawManager.Instance;
- drawInstance.DrawPolygon(ref rotatedEdgePoints[0],
- ref rotatedEdgePoints[2], ref rotatedEdgePoints[1], ref color);
- drawInstance.DrawPolygon(ref rotatedEdgePoints[0],
- ref rotatedEdgePoints[3], ref rotatedEdgePoints[2], ref color);
- }
- #endregion
- }
- }