/Rendering/Basics/Drawing/Rect.cs
C# | 148 lines | 88 code | 9 blank | 51 comment | 4 complexity | 7c672915987ffd063ee86b156db8f5b8 MD5 | raw file
1using Delta.Utilities.Datatypes; 2 3namespace Delta.Rendering.Basics.Drawing 4{ 5 /// <summary> 6 /// Draw 2D rectangles with help of this class. They can be drawn as only 7 /// outline, filled shape and with rotation. This has the advantage of 8 /// optimizing it better. 9 /// </summary> 10 public static class Rect 11 { 12 #region Constants 13 /// <summary> 14 /// Helper for the Rectangle method that allows rotated rectangle drawing. 15 /// </summary> 16 private static readonly Point[] rotatedEdgePoints = new Point[4]; 17 #endregion 18 19 #region DrawOutline (Static) 20 /// <summary> 21 /// Draws a rectangle box with lines 22 /// </summary> 23 /// <param name="rect">Rectangle to draw</param> 24 /// <param name="lineColor">Line color</param> 25 public static void DrawOutline(Rectangle rect, Color lineColor) 26 { 27 // Skip if the drawArea is certainly outside of the quadratic space. 28 // Checking the screen area would work too, but is a little slower and 29 // won't exclude much more anyway. 30 if (rect.Left >= 1.0f || 31 rect.Top >= 1.0f || 32 rect.Left < -rect.Width || 33 rect.Top < -rect.Height) 34 { 35 // Skip this material rendering, not visible this frame. 36 return; 37 } 38 39 DrawManager drawInstance = DrawManager.Instance; 40 Point topRight = rect.TopRight; 41 Point bottomRight = rect.BottomRight; 42 Point bottomLeft = rect.BottomLeft; 43 drawInstance.Draw2DLine(ref rect.TopLeft, ref topRight, ref lineColor); 44 drawInstance.Draw2DLine(ref topRight, ref bottomRight, ref lineColor); 45 drawInstance.Draw2DLine(ref bottomRight, ref bottomLeft, ref lineColor); 46 drawInstance.Draw2DLine(ref bottomLeft, ref rect.TopLeft, ref lineColor); 47 } 48 49 /// <summary> 50 /// Draws a rotated rectangle box with lines. 51 /// </summary> 52 /// <param name="rect">Rectangle to draw</param> 53 /// <param name="lineColor">Line color</param> 54 /// <param name="rotation">Rotation of the rectangle</param> 55 public static void DrawOutline(Rectangle rect, Color lineColor, 56 float rotation) 57 { 58 // Skip if the drawArea is certainly outside of the quadratic space. 59 // Checking the screen area would work too, but is a little slower and 60 // won't exclude much more anyway. 61 if (rect.Left >= 1.0f || 62 rect.Top >= 1.0f || 63 rect.Left < -rect.Width || 64 rect.Top < -rect.Height) 65 { 66 // Skip this material rendering, not visible this frame. 67 return; 68 } 69 70 // Rotate the original rectangle 71 rect.Rotate(rotation, rotatedEdgePoints); 72 // And draw it out same way as above: top left, right, bottom right, left 73 DrawManager drawInstance = DrawManager.Instance; 74 drawInstance.Draw2DLine(ref rotatedEdgePoints[0], 75 ref rotatedEdgePoints[1], ref lineColor); 76 drawInstance.Draw2DLine(ref rotatedEdgePoints[1], 77 ref rotatedEdgePoints[2], ref lineColor); 78 drawInstance.Draw2DLine(ref rotatedEdgePoints[2], 79 ref rotatedEdgePoints[3], ref lineColor); 80 drawInstance.Draw2DLine(ref rotatedEdgePoints[3], 81 ref rotatedEdgePoints[0], ref lineColor); 82 } 83 #endregion 84 85 #region DrawFilled (Static) 86 /// <summary> 87 /// Draw filled rectangle at the given position with the given color. 88 /// </summary> 89 /// <param name="rect">The rect.</param> 90 /// <param name="color">The color.</param> 91 public static void DrawFilled(Rectangle rect, Color color) 92 { 93 // Skip if the drawArea is certainly outside of the quadratic space. 94 // Checking the screen area would work too, but is a little slower and 95 // won't exclude much more anyway. 96 if (rect.Left >= 1.0f || 97 rect.Top >= 1.0f || 98 rect.Left < -rect.Width || 99 rect.Top < -rect.Height) 100 { 101 // Skip this material rendering, not visible this frame. 102 return; 103 } 104 105 // A rectangle is just 2 polygons 106 DrawManager drawInstance = DrawManager.Instance; 107 Point bottomRight = rect.BottomRight; 108 Point topRight = rect.TopRight; 109 Point bottomLeft = rect.BottomLeft; 110 drawInstance.DrawPolygon(ref rect.TopLeft, ref bottomRight, 111 ref topRight, ref color); 112 drawInstance.DrawPolygon(ref rect.TopLeft, ref bottomLeft, 113 ref bottomRight, ref color); 114 } 115 116 /// <summary> 117 /// Draw filled rectangle at the given position with the given color. 118 /// </summary> 119 /// <param name="rect">The rect.</param> 120 /// <param name="color">The color.</param> 121 /// <param name="rotation">The rotation.</param> 122 public static void DrawFilled(Rectangle rect, Color color, 123 float rotation) 124 { 125 // Skip if the drawArea is certainly outside of the quadratic space. 126 // Checking the screen area would work too, but is a little slower and 127 // won't exclude much more anyway. 128 if (rect.Left >= 1.0f || 129 rect.Top >= 1.0f || 130 rect.Left < -rect.Width || 131 rect.Top < -rect.Height) 132 { 133 // Skip this material rendering, not visible this frame. 134 return; 135 } 136 137 // A rectangle is 2 polygons, but rotate the original rectangle first 138 rect.Rotate(rotation, rotatedEdgePoints); 139 // And draw it out same way as above 140 DrawManager drawInstance = DrawManager.Instance; 141 drawInstance.DrawPolygon(ref rotatedEdgePoints[0], 142 ref rotatedEdgePoints[2], ref rotatedEdgePoints[1], ref color); 143 drawInstance.DrawPolygon(ref rotatedEdgePoints[0], 144 ref rotatedEdgePoints[3], ref rotatedEdgePoints[2], ref color); 145 } 146 #endregion 147 } 148}