MenuScreen.cs 5.0 KB

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