JoinSessionScreen.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // JoinSessionScreen.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. /// This menu screen displays a list of available network sessions,
  20. /// and lets the user choose which one to join.
  21. /// </summary>
  22. class JoinSessionScreen : MenuScreen
  23. {
  24. #region Fields
  25. const int MaxSearchResults = 8;
  26. AvailableNetworkSessionCollection availableSessions;
  27. #endregion
  28. #region Initialization
  29. /// <summary>
  30. /// Constructs a menu screen listing the available network sessions.
  31. /// </summary>
  32. public JoinSessionScreen(AvailableNetworkSessionCollection availableSessions)
  33. : base(Resources.JoinSession)
  34. {
  35. this.availableSessions = availableSessions;
  36. foreach (AvailableNetworkSession availableSession in availableSessions)
  37. {
  38. // Create menu entries for each available session.
  39. MenuEntry menuEntry = new AvailableSessionMenuEntry(availableSession);
  40. menuEntry.Selected += AvailableSessionMenuEntrySelected;
  41. MenuEntries.Add(menuEntry);
  42. // Matchmaking can return up to 25 available sessions at a time, but
  43. // we don't have room to fit that many on the screen. In a perfect
  44. // world we should make the menu scroll if there are too many, but it
  45. // is easier to just not bother displaying more than we have room for.
  46. if (MenuEntries.Count >= MaxSearchResults)
  47. break;
  48. }
  49. // Add the Back menu entry.
  50. MenuEntry backMenuEntry = new MenuEntry(Resources.Back);
  51. backMenuEntry.Selected += BackMenuEntrySelected;
  52. MenuEntries.Add(backMenuEntry);
  53. }
  54. #endregion
  55. #region Event Handlers
  56. /// <summary>
  57. /// Event handler for when an available session menu entry is selected.
  58. /// </summary>
  59. void AvailableSessionMenuEntrySelected(object sender, PlayerIndexEventArgs e)
  60. {
  61. // Which menu entry was selected?
  62. AvailableSessionMenuEntry menuEntry = (AvailableSessionMenuEntry)sender;
  63. AvailableNetworkSession availableSession = menuEntry.AvailableSession;
  64. try
  65. {
  66. // Begin an asynchronous join network session operation.
  67. IAsyncResult asyncResult = NetworkSession.BeginJoin(availableSession,
  68. null, null);
  69. // Activate the network busy screen, which will display
  70. // an animation until this operation has completed.
  71. NetworkBusyScreen busyScreen = new NetworkBusyScreen(asyncResult);
  72. busyScreen.OperationCompleted += JoinSessionOperationCompleted;
  73. ScreenManager.AddScreen(busyScreen, ControllingPlayer);
  74. }
  75. catch (Exception exception)
  76. {
  77. NetworkErrorScreen errorScreen = new NetworkErrorScreen(exception);
  78. ScreenManager.AddScreen(errorScreen, ControllingPlayer);
  79. }
  80. }
  81. /// <summary>
  82. /// Event handler for when the asynchronous join network session
  83. /// operation has completed.
  84. /// </summary>
  85. void JoinSessionOperationCompleted(object sender, OperationCompletedEventArgs e)
  86. {
  87. try
  88. {
  89. // End the asynchronous join network session operation.
  90. NetworkSession networkSession = NetworkSession.EndJoin(e.AsyncResult);
  91. // Create a component that will manage the session we just joined.
  92. NetworkSessionComponent.Create(ScreenManager, networkSession);
  93. // Go to the lobby screen. We pass null as the controlling player,
  94. // because the lobby screen accepts input from all local players
  95. // who are in the session, not just a single controlling player.
  96. ScreenManager.AddScreen(new LobbyScreen(networkSession), null);
  97. availableSessions.Dispose();
  98. }
  99. catch (Exception exception)
  100. {
  101. NetworkErrorScreen errorScreen = new NetworkErrorScreen(exception);
  102. ScreenManager.AddScreen(errorScreen, ControllingPlayer);
  103. }
  104. }
  105. /// <summary>
  106. /// Event handler for when the Back menu entry is selected.
  107. /// </summary>
  108. void BackMenuEntrySelected(object sender, PlayerIndexEventArgs e)
  109. {
  110. availableSessions.Dispose();
  111. ExitScreen();
  112. }
  113. #endregion
  114. }
  115. }