MainMenuScreen.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // MainMenuScreen.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 Microsoft.Xna.Framework;
  11. #endregion
  12. namespace GameStateManagement
  13. {
  14. /// <summary>
  15. /// The main menu screen is the first thing displayed when the game starts up.
  16. /// </summary>
  17. class MainMenuScreen : MenuScreen
  18. {
  19. #region Initialization
  20. /// <summary>
  21. /// Constructor fills in the menu contents.
  22. /// </summary>
  23. public MainMenuScreen()
  24. : base("Main Menu")
  25. {
  26. // Create our menu entries.
  27. MenuEntry playGameMenuEntry = new MenuEntry("Play Game");
  28. MenuEntry optionsMenuEntry = new MenuEntry("Options");
  29. MenuEntry exitMenuEntry = new MenuEntry("Exit");
  30. // Hook up menu event handlers.
  31. playGameMenuEntry.Selected += PlayGameMenuEntrySelected;
  32. optionsMenuEntry.Selected += OptionsMenuEntrySelected;
  33. exitMenuEntry.Selected += OnCancel;
  34. // Add entries to the menu.
  35. MenuEntries.Add(playGameMenuEntry);
  36. MenuEntries.Add(optionsMenuEntry);
  37. MenuEntries.Add(exitMenuEntry);
  38. }
  39. #endregion
  40. #region Handle Input
  41. /// <summary>
  42. /// Event handler for when the Play Game menu entry is selected.
  43. /// </summary>
  44. void PlayGameMenuEntrySelected(object sender, PlayerIndexEventArgs e)
  45. {
  46. LoadingScreen.Load(ScreenManager, true, e.PlayerIndex,
  47. new GameplayScreen());
  48. }
  49. /// <summary>
  50. /// Event handler for when the Options menu entry is selected.
  51. /// </summary>
  52. void OptionsMenuEntrySelected(object sender, PlayerIndexEventArgs e)
  53. {
  54. ScreenManager.AddScreen(new OptionsMenuScreen(), e.PlayerIndex);
  55. }
  56. /// <summary>
  57. /// When the user cancels the main menu, ask if they want to exit the sample.
  58. /// </summary>
  59. protected override void OnCancel(PlayerIndex playerIndex)
  60. {
  61. const string message = "Are you sure you want to exit this sample?";
  62. MessageBoxScreen confirmExitMessageBox = new MessageBoxScreen(message);
  63. confirmExitMessageBox.Accepted += ConfirmExitMessageBoxAccepted;
  64. ScreenManager.AddScreen(confirmExitMessageBox, playerIndex);
  65. }
  66. /// <summary>
  67. /// Event handler for when the user selects ok on the "are you sure
  68. /// you want to exit" message box.
  69. /// </summary>
  70. void ConfirmExitMessageBoxAccepted(object sender, PlayerIndexEventArgs e)
  71. {
  72. ScreenManager.Game.Exit();
  73. }
  74. #endregion
  75. }
  76. }