SafeAreaOverlay.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // SafeAreaOverlay.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using Microsoft.Xna.Framework;
  11. using Microsoft.Xna.Framework.Graphics;
  12. #endregion
  13. namespace SafeArea
  14. {
  15. /// <summary>
  16. /// Reusable component makes it easy to check whether your important
  17. /// graphics are positioned inside the title safe area, by superimposing
  18. /// a red border that marks the edges of the safe region.
  19. /// </summary>
  20. public class SafeAreaOverlay : DrawableGameComponent
  21. {
  22. SpriteBatch spriteBatch;
  23. Texture2D dummyTexture;
  24. /// <summary>
  25. /// Constructor.
  26. /// </summary>
  27. public SafeAreaOverlay(Game game)
  28. : base(game)
  29. {
  30. // Choose a high number, so we will draw on top of other components.
  31. DrawOrder = 1000;
  32. }
  33. /// <summary>
  34. /// Creates the graphics resources needed to draw the overlay.
  35. /// </summary>
  36. protected override void LoadContent()
  37. {
  38. spriteBatch = new SpriteBatch(GraphicsDevice);
  39. // Create a 1x1 white texture.
  40. dummyTexture = new Texture2D(GraphicsDevice, 1, 1);
  41. dummyTexture.SetData(new Color[] { Color.White });
  42. }
  43. /// <summary>
  44. /// Draws the title safe area.
  45. /// </summary>
  46. public override void Draw(GameTime gameTime)
  47. {
  48. // Look up the current viewport and safe area dimensions.
  49. Viewport viewport = GraphicsDevice.Viewport;
  50. Rectangle safeArea = viewport.TitleSafeArea;
  51. int viewportRight = viewport.X + viewport.Width;
  52. int viewportBottom = viewport.Y + viewport.Height;
  53. // Compute four border rectangles around the edges of the safe area.
  54. Rectangle leftBorder = new Rectangle(viewport.X,
  55. viewport.Y,
  56. safeArea.X - viewport.X,
  57. viewport.Height);
  58. Rectangle rightBorder = new Rectangle(safeArea.Right,
  59. viewport.Y,
  60. viewportRight - safeArea.Right,
  61. viewport.Height);
  62. Rectangle topBorder = new Rectangle(safeArea.Left,
  63. viewport.Y,
  64. safeArea.Width,
  65. safeArea.Top - viewport.Y);
  66. Rectangle bottomBorder = new Rectangle(safeArea.Left,
  67. safeArea.Bottom,
  68. safeArea.Width,
  69. viewportBottom - safeArea.Bottom);
  70. // Draw the safe area borders.
  71. Color translucentRed = Color.Red * 0.5f;
  72. spriteBatch.Begin();
  73. spriteBatch.Draw(dummyTexture, leftBorder, translucentRed);
  74. spriteBatch.Draw(dummyTexture, rightBorder, translucentRed);
  75. spriteBatch.Draw(dummyTexture, topBorder, translucentRed);
  76. spriteBatch.Draw(dummyTexture, bottomBorder, translucentRed);
  77. spriteBatch.End();
  78. }
  79. }
  80. }