2
0

MainMenuScreen.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. using Microsoft.Xna.Framework.Net;
  12. #endregion
  13. namespace NetworkStateManagement
  14. {
  15. /// <summary>
  16. /// The main menu screen is the first thing displayed when the game starts up.
  17. /// </summary>
  18. class MainMenuScreen : MenuScreen
  19. {
  20. #region Initialization
  21. /// <summary>
  22. /// Constructor fills in the menu contents.
  23. /// </summary>
  24. public MainMenuScreen ()
  25. : base(Resources.MainMenu)
  26. {
  27. // Create our menu entries.
  28. MenuEntry singlePlayerMenuEntry = new MenuEntry (Resources.SinglePlayer);
  29. MenuEntry liveMenuEntry = new MenuEntry (Resources.PlayerMatch);
  30. MenuEntry systemLinkMenuEntry = new MenuEntry (Resources.SystemLink);
  31. MenuEntry exitMenuEntry = new MenuEntry (Resources.Exit);
  32. // Hook up menu event handlers.
  33. singlePlayerMenuEntry.Selected += SinglePlayerMenuEntrySelected;
  34. liveMenuEntry.Selected += LiveMenuEntrySelected;
  35. systemLinkMenuEntry.Selected += SystemLinkMenuEntrySelected;
  36. exitMenuEntry.Selected += OnCancel;
  37. // Add entries to the menu.
  38. MenuEntries.Add (singlePlayerMenuEntry);
  39. MenuEntries.Add (liveMenuEntry);
  40. MenuEntries.Add (systemLinkMenuEntry);
  41. MenuEntries.Add (exitMenuEntry);
  42. }
  43. #endregion
  44. #region Handle Input
  45. /// <summary>
  46. /// Event handler for when the Single Player menu entry is selected.
  47. /// </summary>
  48. void SinglePlayerMenuEntrySelected (object sender, PlayerIndexEventArgs e)
  49. {
  50. LoadingScreen.Load (ScreenManager, true, e.PlayerIndex,
  51. new GameplayScreen (null));
  52. }
  53. /// <summary>
  54. /// Event handler for when the Live menu entry is selected.
  55. /// </summary>
  56. void LiveMenuEntrySelected (object sender, PlayerIndexEventArgs e)
  57. {
  58. CreateOrFindSession (NetworkSessionType.PlayerMatch, e.PlayerIndex);
  59. }
  60. /// <summary>
  61. /// Event handler for when the System Link menu entry is selected.
  62. /// </summary>
  63. void SystemLinkMenuEntrySelected (object sender, PlayerIndexEventArgs e)
  64. {
  65. CreateOrFindSession (NetworkSessionType.SystemLink, e.PlayerIndex);
  66. }
  67. /// <summary>
  68. /// Helper method shared by the Live and System Link menu event handlers.
  69. /// </summary>
  70. void CreateOrFindSession (NetworkSessionType sessionType,
  71. PlayerIndex playerIndex)
  72. {
  73. // First, we need to make sure a suitable gamer profile is signed in.
  74. ProfileSignInScreen profileSignIn = new ProfileSignInScreen (sessionType);
  75. // Hook up an event so once the ProfileSignInScreen is happy,
  76. // it will activate the CreateOrFindSessionScreen.
  77. profileSignIn.ProfileSignedIn += delegate
  78. {
  79. GameScreen createOrFind = new CreateOrFindSessionScreen (sessionType);
  80. ScreenManager.AddScreen (createOrFind, playerIndex);
  81. };
  82. // Activate the ProfileSignInScreen.
  83. ScreenManager.AddScreen (profileSignIn, playerIndex);
  84. }
  85. /// <summary>
  86. /// When the user cancels the main menu, ask if they want to exit the sample.
  87. /// </summary>
  88. protected override void OnCancel (PlayerIndex playerIndex)
  89. {
  90. MessageBoxScreen confirmExitMessageBox =
  91. new MessageBoxScreen (Resources.ConfirmExitSample);
  92. confirmExitMessageBox.Accepted += ConfirmExitMessageBoxAccepted;
  93. ScreenManager.AddScreen (confirmExitMessageBox, playerIndex);
  94. }
  95. /// <summary>
  96. /// Event handler for when the user selects ok on the "are you sure
  97. /// you want to exit" message box.
  98. /// </summary>
  99. void ConfirmExitMessageBoxAccepted (object sender, PlayerIndexEventArgs e)
  100. {
  101. ScreenManager.Game.Exit ();
  102. }
  103. #endregion
  104. }
  105. }