Layout.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Layout.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 System;
  11. using Microsoft.Xna.Framework;
  12. using Microsoft.Xna.Framework.Graphics;
  13. #endregion
  14. namespace PerformanceMeasuring.GameDebugTools
  15. {
  16. /// <summary>
  17. /// Alignment for layout.
  18. /// </summary>
  19. [Flags]
  20. public enum Alignment
  21. {
  22. None = 0,
  23. // Horizontal layouts
  24. Left = 1,
  25. Right = 2,
  26. HorizontalCenter = 4,
  27. // Vertical layouts
  28. Top = 8,
  29. Bottom = 16,
  30. VerticalCenter = 32,
  31. // Combinations
  32. TopLeft = Top | Left,
  33. TopRight = Top | Right,
  34. TopCenter = Top | HorizontalCenter,
  35. BottomLeft = Bottom | Left,
  36. BottomRight = Bottom | Right,
  37. BottomCenter = Bottom | HorizontalCenter,
  38. CenterLeft = VerticalCenter | Left,
  39. CenterRight = VerticalCenter | Right,
  40. Center = VerticalCenter | HorizontalCenter
  41. }
  42. /// <summary>
  43. /// Layout class that supports title safe area.
  44. /// </summary>
  45. /// <remarks>
  46. /// You have to support various resolutions when you develop multi-platform
  47. /// games. Also, you have to support title safe area for Xbox 360 games.
  48. ///
  49. /// This structure places given rectangle with specified alignment and margin
  50. /// based on layout area (client area) with safe area.
  51. ///
  52. /// Margin is percentage of client area size.
  53. ///
  54. /// Example:
  55. ///
  56. /// Place( region, 0.1f, 0.2f, Aligment.TopLeft );
  57. ///
  58. /// Place region at 10% from left side of the client area,
  59. /// 20% from top of the client area.
  60. ///
  61. ///
  62. /// Place( region, 0.3f, 0.4f, Aligment.BottomRight );
  63. ///
  64. /// Place region at 30% from right side of client,
  65. /// 40% from the bottom of the client area.
  66. ///
  67. ///
  68. /// You can individually specify client area and safe area.
  69. /// So, it is useful when you have split screen game which layout happens based
  70. /// on client and it takes care of the safe at same time.
  71. ///
  72. /// </remarks>
  73. public struct Layout
  74. {
  75. #region Fields
  76. /// <summary>
  77. /// Gets/Sets client area.
  78. /// </summary>
  79. public Rectangle ClientArea;
  80. /// <summary>
  81. /// Gets/Sets safe area.
  82. /// </summary>
  83. public Rectangle SafeArea;
  84. #endregion
  85. #region Initialization
  86. /// <summary>
  87. /// Construct layout object by specify both client area and safe area.
  88. /// </summary>
  89. /// <param name="client">Client area</param>
  90. /// <param name="safeArea">safe area</param>
  91. public Layout(Rectangle clientArea, Rectangle safeArea)
  92. {
  93. ClientArea = clientArea;
  94. SafeArea = safeArea;
  95. }
  96. /// <summary>
  97. /// Construct layout object by specify client area.
  98. /// Safe area becomes same size as client area.
  99. /// </summary>
  100. /// <param name="client">Client area</param>
  101. public Layout(Rectangle clientArea)
  102. : this(clientArea, clientArea)
  103. {
  104. }
  105. /// <summary>
  106. /// Construct layout object by specify viewport.
  107. /// Safe area becomes same as Viewpoert.TItleSafeArea.
  108. /// </summary>
  109. public Layout(Viewport viewport)
  110. {
  111. ClientArea = new Rectangle((int)viewport.X, (int)viewport.Y,
  112. (int)viewport.Width, (int)viewport.Height);
  113. SafeArea = viewport.TitleSafeArea;
  114. }
  115. #endregion
  116. /// <summary>
  117. /// Layouting specified region
  118. /// </summary>
  119. /// <param name="region">placing region</param>
  120. /// <returns>Placed position</returns>
  121. public Vector2 Place(Vector2 size, float horizontalMargin,
  122. float verticalMargine, Alignment alignment)
  123. {
  124. Rectangle rc = new Rectangle(0, 0, (int)size.X, (int)size.Y);
  125. rc = Place(rc, horizontalMargin, verticalMargine, alignment);
  126. return new Vector2(rc.X, rc.Y);
  127. }
  128. /// <summary>
  129. /// Layouting specified region
  130. /// </summary>
  131. /// <param name="region">placing rectangle</param>
  132. /// <returns>placed rectangle</returns>
  133. public Rectangle Place(Rectangle region, float horizontalMargin,
  134. float verticalMargine, Alignment alignment)
  135. {
  136. // Horizontal layout.
  137. if ((alignment & Alignment.Left) != 0)
  138. {
  139. region.X = ClientArea.X + (int)(ClientArea.Width * horizontalMargin);
  140. }
  141. else if ((alignment & Alignment.Right) != 0)
  142. {
  143. region.X = ClientArea.X +
  144. (int)(ClientArea.Width * (1.0f - horizontalMargin)) -
  145. region.Width;
  146. }
  147. else if ((alignment & Alignment.HorizontalCenter) != 0)
  148. {
  149. region.X = ClientArea.X + (ClientArea.Width - region.Width) / 2 +
  150. (int)(horizontalMargin * ClientArea.Width);
  151. }
  152. else
  153. {
  154. // Don't do layout.
  155. }
  156. // Vertical layout.
  157. if ((alignment & Alignment.Top) != 0)
  158. {
  159. region.Y = ClientArea.Y + (int)(ClientArea.Height * verticalMargine);
  160. }
  161. else if ((alignment & Alignment.Bottom) != 0)
  162. {
  163. region.Y = ClientArea.Y +
  164. (int)(ClientArea.Height * (1.0f - verticalMargine)) -
  165. region.Height;
  166. }
  167. else if ((alignment & Alignment.VerticalCenter) != 0)
  168. {
  169. region.Y = ClientArea.Y + (ClientArea.Height - region.Height) / 2 +
  170. (int)(verticalMargine * ClientArea.Height);
  171. }
  172. else
  173. {
  174. // Don't do layout.
  175. }
  176. // Make sure layout region is in the safe area.
  177. if (region.Left < SafeArea.Left)
  178. region.X = SafeArea.Left;
  179. if (region.Right > SafeArea.Right)
  180. region.X = SafeArea.Right - region.Width;
  181. if (region.Top < SafeArea.Top)
  182. region.Y = SafeArea.Top;
  183. if (region.Bottom > SafeArea.Bottom)
  184. region.Y = SafeArea.Bottom - region.Height;
  185. return region;
  186. }
  187. }
  188. }