MenuScreen.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //-----------------------------------------------------------------------------
  2. // MenuScreen.cs
  3. //
  4. // XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using System.Collections.Generic;
  9. using Microsoft.Xna.Framework;
  10. using Microsoft.Xna.Framework.Content;
  11. using Microsoft.Xna.Framework.Graphics;
  12. namespace AlienGameSample
  13. {
  14. /// <summary>
  15. /// Base class for screens that contain a menu of options. The user can
  16. /// move up and down to select an entry, or cancel to back out of the screen.
  17. /// </summary>
  18. abstract class MenuScreen : GameScreen
  19. {
  20. List<MenuEntry> menuEntries = new List<MenuEntry>();
  21. int selectedEntry;
  22. string menuTitle;
  23. /// <summary>
  24. /// Gets the list of menu entries, so derived classes can add
  25. /// or change the menu contents.
  26. /// </summary>
  27. protected IList<MenuEntry> MenuEntries
  28. {
  29. get { return menuEntries; }
  30. }
  31. /// <summary>
  32. /// Constructor.
  33. /// </summary>
  34. public MenuScreen(string menuTitle)
  35. {
  36. this.menuTitle = menuTitle;
  37. TransitionOnTime = TimeSpan.FromSeconds(1.0);
  38. TransitionOffTime = TimeSpan.FromSeconds(1.0);
  39. }
  40. public override void LoadContent()
  41. {
  42. base.LoadContent();
  43. }
  44. /// <summary>
  45. /// Responds to user input, changing the selected entry and accepting
  46. /// or cancelling the menu.
  47. /// </summary>
  48. public override void HandleInput(InputState input)
  49. {
  50. // Accept or cancel the menu?
  51. if (input.MenuSelect)
  52. {
  53. OnSelectEntry(selectedEntry);
  54. }
  55. else if (input.MenuCancel)
  56. {
  57. OnCancel();
  58. }
  59. }
  60. /// <summary>
  61. /// Handler for when the user has chosen a menu entry.
  62. /// </summary>
  63. protected virtual void OnSelectEntry(int entryIndex)
  64. {
  65. menuEntries[selectedEntry].OnSelectEntry();
  66. }
  67. /// <summary>
  68. /// Handler for when the user has cancelled the menu.
  69. /// </summary>
  70. protected virtual void OnCancel()
  71. {
  72. ExitScreen();
  73. }
  74. /// <summary>
  75. /// Helper overload makes it easy to use OnCancel as a MenuEntry event handler.
  76. /// </summary>
  77. protected void OnCancel(object sender, EventArgs e)
  78. {
  79. OnCancel();
  80. }
  81. /// <summary>
  82. /// Updates the menu.
  83. /// </summary>
  84. public override void Update(GameTime gameTime, bool otherScreenHasFocus,
  85. bool coveredByOtherScreen)
  86. {
  87. base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
  88. // Update each nested MenuEntry object.
  89. for (int i = 0; i < menuEntries.Count; i++)
  90. {
  91. bool isSelected = IsActive && (i == selectedEntry);
  92. menuEntries[i].Update(this, isSelected, gameTime);
  93. }
  94. }
  95. /// <summary>
  96. /// Draws the menu. Tweaked a bit from the sample so that it draws menus on the bottom left corner and transitions
  97. /// on and off from the bottom.
  98. /// </summary>
  99. public override void Draw(GameTime gameTime)
  100. {
  101. if (menuEntries.Count > 0)
  102. {
  103. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  104. SpriteFont font = ScreenManager.Font;
  105. Vector2 position = new Vector2(40, 420 - menuEntries[0].GetHeight(this));
  106. // Make the menu slide into place during transitions, using a
  107. // power curve to make things look more interesting (this makes
  108. // the movement slow down as it nears the end).
  109. float transitionOffset = (float)Math.Pow(TransitionPosition, 2);
  110. position.Y += transitionOffset * 512;
  111. spriteBatch.Begin();
  112. // Draw each menu entry in turn.
  113. for (int i = menuEntries.Count - 1; i >= 0; --i)
  114. {
  115. MenuEntry menuEntry = menuEntries[i];
  116. position.X = 160 - font.MeasureString(menuEntry.Text).X / 2;
  117. bool isSelected = IsActive && (i == selectedEntry);
  118. menuEntry.Draw(this, position, isSelected, gameTime);
  119. position.Y -= menuEntry.GetHeight(this);
  120. }
  121. spriteBatch.End();
  122. }
  123. }
  124. }
  125. }