PageRenderTime 48ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Rendering/Basics/Drawing/Rect.cs

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