MenuComponent.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // MenuComponent.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.Input;
  17. #endregion
  18. namespace XnaGraphicsDemo
  19. {
  20. /// <summary>
  21. /// Base class for all the different screens used in the demo. This provides
  22. /// a simple touch menu which can display a list of options, and detect when
  23. /// a menu item is clicked.
  24. /// </summary>
  25. class MenuComponent : DrawableGameComponent
  26. {
  27. // Properties.
  28. new public DemoGame Game { get { return (DemoGame)base.Game; } }
  29. public SpriteBatch SpriteBatch { get { return Game.SpriteBatch; } }
  30. public SpriteFont Font { get { return Game.Font; } }
  31. public SpriteFont BigFont { get { return Game.BigFont; } }
  32. protected List<MenuEntry> Entries { get; private set; }
  33. protected Vector2 LastTouchPoint { get; private set; }
  34. // Fields.
  35. bool touchDown = true;
  36. int touchSelection = -1;
  37. static TimeSpan attractTimer;
  38. static MouseState lastInputState = new MouseState(-1, -1, -1, 0, 0, 0, 0, 0);
  39. /// <summary>
  40. /// Constructor.
  41. /// </summary>
  42. public MenuComponent(DemoGame game)
  43. : base(game)
  44. {
  45. Entries = new List<MenuEntry>();
  46. }
  47. /// <summary>
  48. /// Initializes the menu, computing the screen position of each entry.
  49. /// </summary>
  50. public override void Initialize()
  51. {
  52. Vector2 pos = new Vector2(MenuEntry.Border, 800 - MenuEntry.Border - Entries.Count * MenuEntry.Height);
  53. foreach (MenuEntry entry in Entries)
  54. {
  55. entry.Position = pos;
  56. pos.Y += MenuEntry.Height;
  57. }
  58. base.Initialize();
  59. }
  60. /// <summary>
  61. /// Resets the menu, whenever we transition to or from a different screen.
  62. /// </summary>
  63. virtual public void Reset()
  64. {
  65. if (touchSelection >= 0)
  66. Entries[touchSelection].IsFocused = false;
  67. touchDown = true;
  68. touchSelection = -1;
  69. }
  70. /// <summary>
  71. /// Updates the menu state, processing user input.
  72. /// </summary>
  73. public override void Update(GameTime gameTime)
  74. {
  75. // We read input using the mouse API, which will report the first touch point
  76. // when run on the phone, but also works on Windows using a regular mouse.
  77. MouseState input = Game.IsActive ? Mouse.GetState() : new MouseState();
  78. // Scale input if we are running in an unusual screen resolution.
  79. int touchX = input.X * 480 / Game.Graphics.PreferredBackBufferWidth;
  80. int touchY = input.Y * 800 / Game.Graphics.PreferredBackBufferHeight;
  81. // Process the input.
  82. if (input.LeftButton == ButtonState.Pressed)
  83. {
  84. HandleTouchDown(touchX, touchY);
  85. }
  86. else
  87. {
  88. HandleTouchUp();
  89. }
  90. HandleAttractMode(gameTime, input);
  91. }
  92. /// <summary>
  93. /// Handles input while a touch is occurring.
  94. /// </summary>
  95. void HandleTouchDown(int touchX, int touchY)
  96. {
  97. // Hit test the touch position against the list of menu items.
  98. int currentEntry = -1;
  99. for (int i = 0; i < Entries.Count; i++)
  100. {
  101. if ((touchY >= Entries[i].Position.Y) && (touchY < Entries[i].Position.Y + MenuEntry.Height))
  102. {
  103. currentEntry = i;
  104. break;
  105. }
  106. }
  107. if (touchDown)
  108. {
  109. // Are we already processing a touch?
  110. if (touchSelection >= 0)
  111. {
  112. if (currentEntry == touchSelection || Entries[touchSelection].IsDraggable)
  113. {
  114. // Pass drag input to the currently selected item.
  115. Entries[touchSelection].IsFocused = true;
  116. Entries[touchSelection].OnDragged(touchX - LastTouchPoint.X);
  117. }
  118. else
  119. {
  120. // If the drag moves off the selected item, unfocus it.
  121. Entries[touchSelection].IsFocused = false;
  122. }
  123. }
  124. else
  125. {
  126. // If the touch was not on any menu item, process a backgroun drag.
  127. OnDrag(new Vector2(touchX, touchY) - LastTouchPoint);
  128. }
  129. }
  130. else
  131. {
  132. // We are not currently processing a touch.
  133. touchDown = true;
  134. touchSelection = currentEntry;
  135. if (touchSelection >= 0)
  136. {
  137. // Focus the menu item that has just been touched.
  138. Entries[touchSelection].IsFocused = true;
  139. }
  140. }
  141. // Store the most recent touch location.
  142. LastTouchPoint = new Vector2(touchX, touchY);
  143. }
  144. /// <summary>
  145. /// Handles input when the touch is released.
  146. /// </summary>
  147. void HandleTouchUp()
  148. {
  149. if (touchDown && touchSelection >= 0 && Entries[touchSelection].IsFocused)
  150. {
  151. // If we were touching a menu item, and just released it, process the click action.
  152. Entries[touchSelection].IsFocused = false;
  153. Entries[touchSelection].OnClicked();
  154. }
  155. touchDown = false;
  156. touchSelection = -1;
  157. }
  158. /// <summary>
  159. /// If no input is provided, we go into an automatic attract mode, which cycles
  160. /// through the various options. This was great for leaving the demo unattended
  161. /// at the kiosk during the MIX10 conference!
  162. /// </summary>
  163. void HandleAttractMode(GameTime gameTime, MouseState input)
  164. {
  165. if (input != lastInputState || touchDown)
  166. {
  167. // If input has changed, reset the timer.
  168. attractTimer = TimeSpan.FromSeconds(-15);
  169. lastInputState = input;
  170. }
  171. else
  172. {
  173. // If no input occurs, increment the timer.
  174. attractTimer += gameTime.ElapsedGameTime;
  175. if (attractTimer > AttractDelay)
  176. {
  177. // Timeout! Run the attract action.
  178. attractTimer = TimeSpan.Zero;
  179. OnAttract();
  180. }
  181. }
  182. }
  183. /// <summary>
  184. /// Allows subclasses to customize their attract behavior. The default is
  185. /// to simulate a click on the last menu entry, which is usually "back".
  186. /// </summary>
  187. protected virtual void OnAttract()
  188. {
  189. Entries[Entries.Count - 1].OnClicked();
  190. }
  191. /// <summary>
  192. /// Allows subclasses to customize how long they wait before cycling through the attract sequence.
  193. /// </summary>
  194. protected virtual TimeSpan AttractDelay { get { return TimeSpan.FromSeconds(10); } }
  195. /// <summary>
  196. /// Draws the list of menu entries.
  197. /// </summary>
  198. public override void Draw(GameTime gameTime)
  199. {
  200. SpriteBatch.Begin(0, null, null, null, null, null, Game.ScaleMatrix);
  201. foreach (MenuEntry entry in Entries)
  202. {
  203. entry.Draw(SpriteBatch, Font, Game.BlankTexture);
  204. }
  205. SpriteBatch.End();
  206. }
  207. /// <summary>
  208. /// Draws the menu title.
  209. /// </summary>
  210. protected void DrawTitle(string title, Color? backgroundColor, Color titleColor)
  211. {
  212. if (backgroundColor.HasValue)
  213. GraphicsDevice.Clear(backgroundColor.Value);
  214. SpriteBatch.Begin(0, null, null, null, null, null, Game.ScaleMatrix);
  215. SpriteBatch.DrawString(BigFont, title, new Vector2(480, 24), titleColor, MathHelper.PiOver2, Vector2.Zero, 1, 0, 0);
  216. SpriteBatch.End();
  217. }
  218. /// <summary>
  219. /// Handles a drag on the background of the screen.
  220. /// </summary>
  221. protected virtual void OnDrag(Vector2 delta)
  222. {
  223. }
  224. }
  225. }