MainMenuScreen.cs 2.8 KB

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