ProfileSignInScreen.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // ProfileSignInScreen.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 System;
  11. using Microsoft.Xna.Framework;
  12. using Microsoft.Xna.Framework.Net;
  13. using Microsoft.Xna.Framework.GamerServices;
  14. #endregion
  15. namespace NetworkStateManagement
  16. {
  17. /// <summary>
  18. /// In order to play a networked game, you must have a player profile signed in.
  19. /// If you want to play on Live, that has to be a Live profile. Rather than just
  20. /// failing with an error message, it is nice if we can automatically bring up the
  21. /// Guide screen when we detect that no suitable profiles are currently signed in,
  22. /// so the user can easily correct the problem. This screen checks the sign in
  23. /// state, and brings up the Guide user interface if there is a problem with it.
  24. /// It then raises an event as soon as a valid profile has been signed in.
  25. ///
  26. /// There are two scenarios for how this can work. If no good profile is signed in:
  27. ///
  28. /// - MainMenuScreen activates the ProfileSignInScreen
  29. /// - ProfileSignInScreen activates the Guide user interface
  30. /// - User signs in a profile
  31. /// - ProfileSignInScreen raises the ProfileSignedIn event
  32. /// - This advances to the CreateOrFindSessionScreen
  33. ///
  34. /// Alternatively, there might already be a valid profile signed in. In this case:
  35. ///
  36. /// - MainMenuScreen activates the ProfileSignInScreen
  37. /// - ProfileSignInScreen notices everything is already good
  38. /// - ProfileSignInScreen raises the ProfileSignedIn event
  39. /// - This advances to the CreateOrFindSessionScreen
  40. ///
  41. /// In this second case, the ProfileSignInScreen is only active for a single
  42. /// Update, so the user just sees a transition directly from the MainMenuScreen
  43. /// to the CreateOrFindSessionScreen.
  44. /// </summary>
  45. class ProfileSignInScreen : GameScreen
  46. {
  47. #region Fields
  48. NetworkSessionType sessionType;
  49. bool haveShownGuide;
  50. bool haveShownMarketplace;
  51. #endregion
  52. #region Events
  53. public event EventHandler<EventArgs> ProfileSignedIn;
  54. #endregion
  55. #region Initialization
  56. /// <summary>
  57. /// Constructs a new profile sign in screen.
  58. /// </summary>
  59. public ProfileSignInScreen(NetworkSessionType sessionType)
  60. {
  61. this.sessionType = sessionType;
  62. IsPopup = true;
  63. }
  64. #endregion
  65. #region Update
  66. /// <summary>
  67. /// Updates the profile sign in screen.
  68. /// </summary>
  69. public override void Update(GameTime gameTime, bool otherScreenHasFocus,
  70. bool coveredByOtherScreen)
  71. {
  72. base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
  73. if (ValidProfileSignedIn())
  74. {
  75. // As soon as we detect a suitable profile is signed in,
  76. // we raise the profile signed in event, then go away.
  77. if (ProfileSignedIn != null)
  78. ProfileSignedIn(this, EventArgs.Empty);
  79. ExitScreen();
  80. }
  81. else if (IsActive && !Guide.IsVisible)
  82. {
  83. // If we are in trial mode, and they want to play online, and a profile
  84. // is signed in, take them to marketplace so they can purchase the game.
  85. if ((Guide.IsTrialMode) &&
  86. (NetworkSessionComponent.IsOnlineSessionType(sessionType)) &&
  87. (Gamer.SignedInGamers[ControllingPlayer.Value] != null) &&
  88. (!haveShownMarketplace))
  89. {
  90. ShowMarketplace();
  91. haveShownMarketplace = true;
  92. }
  93. else if (!haveShownGuide && !haveShownMarketplace)
  94. {
  95. // No suitable profile is signed in, and we haven't already shown
  96. // the Guide. Let's show it now, so they can sign in a profile.
  97. Guide.ShowSignIn(1,
  98. NetworkSessionComponent.IsOnlineSessionType(sessionType));
  99. haveShownGuide = true;
  100. }
  101. else
  102. {
  103. // Hmm. No suitable profile is signed in, but we already showed
  104. // the Guide, and the Guide isn't still visible. There is only
  105. // one thing that can explain this: they must have cancelled the
  106. // Guide without signing in a profile. We'd better just exit,
  107. // which will leave us on the same menu as before.
  108. ExitScreen();
  109. }
  110. }
  111. }
  112. /// <summary>
  113. /// Helper checks whether a valid player profile is signed in.
  114. /// </summary>
  115. bool ValidProfileSignedIn()
  116. {
  117. // If there is no profile signed in, that is never good.
  118. SignedInGamer gamer = Gamer.SignedInGamers[ControllingPlayer.Value];
  119. if (gamer == null)
  120. return false;
  121. // If we want to play in a Live session, also make sure the profile is
  122. // signed in to Live, and that it has the privilege for online gameplay.
  123. if (NetworkSessionComponent.IsOnlineSessionType(sessionType))
  124. {
  125. if (!gamer.IsSignedInToLive)
  126. return false;
  127. if (!gamer.Privileges.AllowOnlineSessions)
  128. return false;
  129. }
  130. // Okeydokey, this looks good.
  131. return true;
  132. }
  133. /// <summary>
  134. /// LIVE networking is not supported in trial mode. Rather than just giving
  135. /// the user an error, this function asks if they want to purchase the full
  136. /// game, then takes them to Marketplace where they can do that. Once the
  137. /// Guide is active, the user can either make the purchase, or cancel it.
  138. /// When the Guide closes, ProfileSignInScreen.Update will notice that
  139. /// Guide.IsVisible has gone back to false, at which point it will check if
  140. /// the game is still in trial mode, and either exit the screen or proceed
  141. /// forward accordingly.
  142. /// </summary>
  143. void ShowMarketplace()
  144. {
  145. MessageBoxScreen confirmMarketplaceMessageBox =
  146. new MessageBoxScreen(Resources.ConfirmMarketplace);
  147. confirmMarketplaceMessageBox.Accepted += delegate
  148. {
  149. Guide.ShowMarketplace(ControllingPlayer.Value);
  150. };
  151. ScreenManager.AddScreen(confirmMarketplaceMessageBox, ControllingPlayer);
  152. }
  153. #endregion
  154. }
  155. }