Layout.cs 6.5 KB

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