PhoneScreen.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #region File Information
  2. //-----------------------------------------------------------------------------
  3. // PhoneScreen.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 System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using Microsoft.Xna.Framework;
  15. using Microsoft.Xna.Framework.Graphics;
  16. using Microsoft.Xna.Framework.Content;
  17. using Microsoft.Xna.Framework.Input.Touch;
  18. #endregion
  19. namespace DynamicMenu.Controls
  20. {
  21. /// <summary>
  22. /// Provides an abstraction that allows a dynamic menu to be shown both vertically and horizontally
  23. /// by breaking the control space into two containers. When held vertically, the two containers
  24. /// are on top of each other, and when held horizontally, the containers are side by side.
  25. /// </summary>
  26. public class PhoneScreen : Control
  27. {
  28. #region Fields
  29. private const int CONTAINER_WIDTH = 400;
  30. private const int CONTAINER_HEIGHT = 400;
  31. // Assuming orientation of 480 x 800
  32. private const int VERTICAL_CONTAINER1_LEFT = 40;
  33. private const int VERTICAL_CONTAINER1_TOP = 0;
  34. private const int VERTICAL_CONTAINER2_LEFT = 40;
  35. private const int VERTICAL_CONTAINER2_TOP = 400;
  36. // Assuming orientation of 800 x 480
  37. private const int HORIZONTAL_CONTAINER1_LEFT = 0;
  38. private const int HORIZONTAL_CONTAINER1_TOP = 40;
  39. private const int HORIZONTAL_CONTAINER2_LEFT = 400;
  40. private const int HORIZONTAL_CONTAINER2_TOP = 40;
  41. private Container container1 = new Container();
  42. private Container container2 = new Container();
  43. private DisplayOrientation currentOrientation = DisplayOrientation.Portrait;
  44. #endregion
  45. #region Properties
  46. /// <summary>
  47. /// The first container
  48. /// </summary>
  49. public Container Container1
  50. {
  51. get { return container1; }
  52. }
  53. /// <summary>
  54. /// The second container
  55. /// </summary>
  56. public Container Container2
  57. {
  58. get { return container2; }
  59. }
  60. /// <summary>
  61. /// The current orientation of the screen
  62. /// </summary>
  63. public DisplayOrientation CurrentOrientation
  64. {
  65. get
  66. {
  67. return currentOrientation;
  68. }
  69. set
  70. {
  71. currentOrientation = value;
  72. UpdateOrientation();
  73. }
  74. }
  75. #endregion
  76. #region Initialization
  77. /// <summary>
  78. /// Initializes the control and its containers
  79. /// </summary>
  80. public override void Initialize()
  81. {
  82. base.Initialize();
  83. container1.Initialize();
  84. container2.Initialize();
  85. // Initialize the orientation
  86. UpdateOrientation();
  87. }
  88. /// <summary>
  89. /// Loads the control and its containers
  90. /// </summary>
  91. public override void LoadContent(GraphicsDevice _graphics, ContentManager _content)
  92. {
  93. base.LoadContent(_graphics, _content);
  94. container1.LoadContent(_graphics, _content);
  95. container2.LoadContent(_graphics, _content);
  96. }
  97. #endregion
  98. #region Update
  99. /// <summary>
  100. /// Updates the control
  101. /// </summary>
  102. public override void Update(GameTime gameTime, List<GestureSample> gestures)
  103. {
  104. base.Update(gameTime, gestures);
  105. if (container1.Visible)
  106. {
  107. container1.Update(gameTime, gestures);
  108. }
  109. if (container2.Visible)
  110. {
  111. container2.Update(gameTime, gestures);
  112. }
  113. }
  114. #endregion
  115. #region Draw
  116. /// <summary>
  117. /// Draws the control
  118. /// </summary>
  119. public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
  120. {
  121. base.Draw(gameTime, spriteBatch);
  122. if (container1.Visible)
  123. {
  124. container1.Draw(gameTime, spriteBatch);
  125. }
  126. if (container2.Visible)
  127. {
  128. container2.Draw(gameTime, spriteBatch);
  129. }
  130. }
  131. #endregion
  132. #region Private Methods
  133. /// <summary>
  134. /// Changes the position of the containers according to the orientation of
  135. /// the phone
  136. /// </summary>
  137. private void UpdateOrientation()
  138. {
  139. Container1.Width = CONTAINER_WIDTH;
  140. Container1.Height = CONTAINER_HEIGHT;
  141. Container2.Width = CONTAINER_WIDTH;
  142. Container2.Height = CONTAINER_HEIGHT;
  143. switch (currentOrientation)
  144. {
  145. case DisplayOrientation.Portrait:
  146. Container1.Left = VERTICAL_CONTAINER1_LEFT;
  147. Container1.Top = VERTICAL_CONTAINER1_TOP;
  148. Container2.Left = VERTICAL_CONTAINER2_LEFT;
  149. Container2.Top = VERTICAL_CONTAINER2_TOP;
  150. break;
  151. case DisplayOrientation.LandscapeLeft:
  152. case DisplayOrientation.LandscapeRight:
  153. Container1.Left = HORIZONTAL_CONTAINER1_LEFT;
  154. Container1.Top = HORIZONTAL_CONTAINER1_TOP;
  155. Container2.Left = HORIZONTAL_CONTAINER2_LEFT;
  156. Container2.Top = HORIZONTAL_CONTAINER2_TOP;
  157. break;
  158. }
  159. }
  160. #endregion
  161. }
  162. }