PhoneMenuScreen.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // PhoneMenuScreen.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. using System;
  10. using System.Collections.Generic;
  11. using GameStateManagement;
  12. using Microsoft.Xna.Framework;
  13. using Microsoft.Xna.Framework.Graphics;
  14. using Microsoft.Xna.Framework.Input;
  15. using Microsoft.Xna.Framework.Input.Touch;
  16. namespace GameStateManagementSample
  17. {
  18. /// <summary>
  19. /// Provides a basic base screen for menus on Windows Phone leveraging the Button class.
  20. /// </summary>
  21. class PhoneMenuScreen : GameScreen
  22. {
  23. List<Button> menuButtons = new List<Button>();
  24. string menuTitle;
  25. InputAction menuCancel;
  26. /// <summary>
  27. /// Gets the list of buttons, so derived classes can add or change the menu contents.
  28. /// </summary>
  29. protected IList<Button> MenuButtons
  30. {
  31. get { return menuButtons; }
  32. }
  33. /// <summary>
  34. /// Creates the PhoneMenuScreen with a particular title.
  35. /// </summary>
  36. /// <param name="title">The title of the screen</param>
  37. public PhoneMenuScreen(string title)
  38. {
  39. menuTitle = title;
  40. TransitionOnTime = TimeSpan.FromSeconds(0.5);
  41. TransitionOffTime = TimeSpan.FromSeconds(0.5);
  42. // Create the menuCancel action
  43. menuCancel = new InputAction(new Buttons[] { Buttons.Back }, null, true);
  44. // We need tap gestures to hit the buttons
  45. EnabledGestures = GestureType.Tap;
  46. }
  47. public override void Activate(bool instancePreserved)
  48. {
  49. // When the screen is activated, we have a valid ScreenManager so we can arrange
  50. // our buttons on the screen
  51. float y = 140f;
  52. float center = ScreenManager.GraphicsDevice.Viewport.Bounds.Center.X;
  53. for (int i = 0; i < MenuButtons.Count; i++)
  54. {
  55. Button b = MenuButtons[i];
  56. b.Position = new Vector2(center - b.Size.X / 2, y);
  57. y += b.Size.Y * 1.5f;
  58. }
  59. base.Activate(instancePreserved);
  60. }
  61. public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
  62. {
  63. // Update opacity of the buttons
  64. foreach (Button b in menuButtons)
  65. {
  66. b.Alpha = TransitionAlpha;
  67. }
  68. base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
  69. }
  70. /// <summary>
  71. /// An overrideable method called whenever the menuCancel action is triggered
  72. /// </summary>
  73. protected virtual void OnCancel() { }
  74. public override void HandleInput(GameTime gameTime, InputState input)
  75. {
  76. // Test for the menuCancel action
  77. PlayerIndex player;
  78. if (menuCancel.Evaluate(input, ControllingPlayer, out player))
  79. {
  80. OnCancel();
  81. }
  82. // Read in our gestures
  83. foreach (GestureSample gesture in input.Gestures)
  84. {
  85. // If we have a tap
  86. if (gesture.GestureType == GestureType.Tap)
  87. {
  88. // Test the tap against the buttons until one of the buttons handles the tap
  89. foreach (Button b in menuButtons)
  90. {
  91. if (b.HandleTap(gesture.Position))
  92. break;
  93. }
  94. }
  95. }
  96. base.HandleInput(gameTime, input);
  97. }
  98. public override void Draw(GameTime gameTime)
  99. {
  100. GraphicsDevice graphics = ScreenManager.GraphicsDevice;
  101. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  102. SpriteFont font = ScreenManager.Font;
  103. spriteBatch.Begin();
  104. // Draw all of the buttons
  105. foreach (Button b in menuButtons)
  106. b.Draw(this);
  107. // Make the menu slide into place during transitions, using a
  108. // power curve to make things look more interesting (this makes
  109. // the movement slow down as it nears the end).
  110. float transitionOffset = (float)Math.Pow(TransitionPosition, 2);
  111. // Draw the menu title centered on the screen
  112. Vector2 titlePosition = new Vector2(graphics.Viewport.Width / 2, 80);
  113. Vector2 titleOrigin = font.MeasureString(menuTitle) / 2;
  114. Color titleColor = new Color(192, 192, 192) * TransitionAlpha;
  115. float titleScale = 1.25f;
  116. titlePosition.Y -= transitionOffset * 100;
  117. spriteBatch.DrawString(font, menuTitle, titlePosition, titleColor, 0,
  118. titleOrigin, titleScale, SpriteEffects.None, 0);
  119. spriteBatch.End();
  120. base.Draw(gameTime);
  121. }
  122. }
  123. }