ProfileSignInScreen.cs 7.1 KB

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