MenuScreen.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // MenuScreen.cs
  4. //
  5. // 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 Microsoft.Xna.Framework;
  13. using Microsoft.Xna.Framework.Graphics;
  14. #endregion
  15. namespace RolePlaying
  16. {
  17. /// <summary>
  18. /// Base class for screens that contain a menu of options. The user can
  19. /// move up and down to select an entry, or cancel to back out of the screen.
  20. /// </summary>
  21. /// <remarks>
  22. /// Similar to a class found in the Game State Management sample on the
  23. /// XNA Creators Club Online website (http://creators.xna.com).
  24. /// </remarks>
  25. abstract class MenuScreen : GameScreen
  26. {
  27. #region Fields
  28. List<MenuEntry> menuEntries = new List<MenuEntry>();
  29. protected int selectedEntry = 0;
  30. #endregion
  31. #region Properties
  32. /// <summary>
  33. /// Gets the list of menu entries, so derived classes can add
  34. /// or change the menu contents.
  35. /// </summary>
  36. protected IList<MenuEntry> MenuEntries
  37. {
  38. get { return menuEntries; }
  39. }
  40. protected MenuEntry SelectedMenuEntry
  41. {
  42. get
  43. {
  44. if ((selectedEntry < 0) || (selectedEntry >= menuEntries.Count))
  45. {
  46. return null;
  47. }
  48. return menuEntries[selectedEntry];
  49. }
  50. }
  51. #endregion
  52. #region Initialization
  53. /// <summary>
  54. /// Constructor.
  55. /// </summary>
  56. public MenuScreen()
  57. {
  58. TransitionOnTime = TimeSpan.FromSeconds(0.5);
  59. TransitionOffTime = TimeSpan.FromSeconds(0.5);
  60. }
  61. #endregion
  62. #region Handle Input
  63. /// <summary>
  64. /// Responds to user input, changing the selected entry and accepting
  65. /// or cancelling the menu.
  66. /// </summary>
  67. public override void HandleInput()
  68. {
  69. int oldSelectedEntry = selectedEntry;
  70. // Move to the previous menu entry?
  71. if (InputManager.IsActionTriggered(InputManager.Action.CursorUp))
  72. {
  73. selectedEntry--;
  74. if (selectedEntry < 0)
  75. selectedEntry = menuEntries.Count - 1;
  76. }
  77. // Move to the next menu entry?
  78. if (InputManager.IsActionTriggered(InputManager.Action.CursorDown))
  79. {
  80. selectedEntry++;
  81. if (selectedEntry >= menuEntries.Count)
  82. selectedEntry = 0;
  83. }
  84. // Accept or cancel the menu?
  85. if (InputManager.IsActionTriggered(InputManager.Action.Ok))
  86. {
  87. AudioManager.PlayCue("Continue");
  88. OnSelectEntry(selectedEntry);
  89. }
  90. else if (InputManager.IsActionTriggered(InputManager.Action.Back) ||
  91. InputManager.IsActionTriggered(InputManager.Action.ExitGame))
  92. {
  93. OnCancel();
  94. }
  95. else if (selectedEntry != oldSelectedEntry)
  96. {
  97. AudioManager.PlayCue("MenuMove");
  98. }
  99. }
  100. /// <summary>
  101. /// Handler for when the user has chosen a menu entry.
  102. /// </summary>
  103. protected virtual void OnSelectEntry(int entryIndex)
  104. {
  105. menuEntries[selectedEntry].OnSelectEntry();
  106. }
  107. /// <summary>
  108. /// Handler for when the user has cancelled the menu.
  109. /// </summary>
  110. protected virtual void OnCancel()
  111. {
  112. ExitScreen();
  113. }
  114. /// <summary>
  115. /// Helper overload makes it easy to use OnCancel as a MenuEntry event handler.
  116. /// </summary>
  117. protected void OnCancel(object sender, EventArgs e)
  118. {
  119. OnCancel();
  120. }
  121. #endregion
  122. #region Update and Draw
  123. /// <summary>
  124. /// Updates the menu.
  125. /// </summary>
  126. public override void Update(GameTime gameTime, bool otherScreenHasFocus,
  127. bool coveredByOtherScreen)
  128. {
  129. base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
  130. // Update each nested MenuEntry object.
  131. for (int i = 0; i < menuEntries.Count; i++)
  132. {
  133. bool isSelected = IsActive && (i == selectedEntry);
  134. menuEntries[i].Update(this, isSelected, gameTime);
  135. }
  136. }
  137. /// <summary>
  138. /// Draws the menu.
  139. /// </summary>
  140. public override void Draw(GameTime gameTime)
  141. {
  142. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  143. spriteBatch.Begin();
  144. // Draw each menu entry in turn.
  145. for (int i = 0; i < menuEntries.Count; i++)
  146. {
  147. MenuEntry menuEntry = menuEntries[i];
  148. bool isSelected = IsActive && (i == selectedEntry);
  149. menuEntry.Draw(this, isSelected, gameTime);
  150. }
  151. spriteBatch.End();
  152. }
  153. #endregion
  154. }
  155. }