ProfileSignInScreen.cs 6.9 KB

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