diff --git a/OpenRA.Game/Game.cs b/OpenRA.Game/Game.cs index 4ac8770c17..a0057f247f 100644 --- a/OpenRA.Game/Game.cs +++ b/OpenRA.Game/Game.cs @@ -163,7 +163,7 @@ namespace OpenRA Timer.Time("----end LoadMap"); Debug("Map change {0} -> {1}".F(Game.mapName, mapName)); } - + public static void MoveViewport(int2 loc) { viewport.Center(loc); @@ -255,7 +255,10 @@ namespace OpenRA Log.Write("sync", "Sync for net frame {0} -------------", f.First); Log.Write("sync", "{0}", f.Second); } - + + public static event Action ConnectionStateChanged = () => { }; + static ConnectionState lastConnectionState = ConnectionState.PreConnecting; + static void Tick() { if (packageChangePending) @@ -273,8 +276,14 @@ namespace OpenRA mapName = LobbyInfo.GlobalSettings.Map; mapChangePending = false; return; - } - + } + + if (orderManager.Connection.ConnectionState != lastConnectionState) + { + lastConnectionState = orderManager.Connection.ConnectionState; + ConnectionStateChanged(); + } + int t = Environment.TickCount; int dt = t - lastTime; if (dt >= Settings.Timestep) @@ -397,7 +406,8 @@ namespace OpenRA gs.GameStarted(world); orderManager.StartGame(); } - + + public static event Action OnGameStart = () => { }; internal static void StartGame() { LoadMap(LobbyInfo.GlobalSettings.Map); @@ -410,7 +420,8 @@ namespace OpenRA gs.GameStarted(world); viewport.GoToStartLocation(world.LocalPlayer); - orderManager.StartGame(); + orderManager.StartGame(); + OnGameStart(); } public static Stance ChooseInitialStance(Player p, Player q) diff --git a/OpenRA.Game/Graphics/Viewport.cs b/OpenRA.Game/Graphics/Viewport.cs index 455a7402a1..6c2ebba374 100644 --- a/OpenRA.Game/Graphics/Viewport.cs +++ b/OpenRA.Game/Graphics/Viewport.cs @@ -64,8 +64,6 @@ namespace OpenRA.Graphics this.scrollPosition = Game.CellSize* mapStart; } - ConnectionState lastConnectionState = ConnectionState.PreConnecting; - bool gameWasStarted = false; public void DrawRegions( World world ) { Timer.Time( "DrawRegions start" ); @@ -79,55 +77,7 @@ namespace OpenRA.Graphics renderer.BeginFrame(r1, r2, scrollPosition.ToInt2()); world.WorldRenderer.Draw(); Timer.Time( "worldRenderer: {0}" ); - if( Game.orderManager.GameStarted && world.LocalPlayer != null) - { - if (!gameWasStarted) - { - Chrome.rootWidget.OpenWindow("INGAME_ROOT"); - gameWasStarted = true; - } - } - else - { - // Still hacky, but at least it uses widgets - // TODO: Clean up the logic of this beast - // TODO: Have a proper "In main menu" state - ConnectionState state = Game.orderManager.Connection.ConnectionState; - if (state != lastConnectionState) - { - switch( Game.orderManager.Connection.ConnectionState ) - { - case ConnectionState.PreConnecting: - Chrome.rootWidget.GetWidget("MAINMENU_BG").Visible = true; - Chrome.rootWidget.GetWidget("CONNECTING_BG").Visible = false; - Chrome.rootWidget.GetWidget("CONNECTION_FAILED_BG").Visible = false; - break; - case ConnectionState.Connecting: - Chrome.rootWidget.GetWidget("MAINMENU_BG").Visible = false; - Chrome.rootWidget.GetWidget("CONNECTING_BG").Visible = true; - Chrome.rootWidget.GetWidget("CONNECTION_FAILED_BG").Visible = false; - break; - case ConnectionState.NotConnected: - Chrome.rootWidget.GetWidget("MAINMENU_BG").Visible = false; - Chrome.rootWidget.GetWidget("CONNECTING_BG").Visible = false; - Chrome.rootWidget.GetWidget("CONNECTION_FAILED_BG").Visible = true; - break; - case ConnectionState.Connected: - Chrome.rootWidget.GetWidget("MAINMENU_BG").Visible = false; - Chrome.rootWidget.GetWidget("CONNECTING_BG").Visible = false; - Chrome.rootWidget.GetWidget("CONNECTION_FAILED_BG").Visible = false; - break; - } - - // TODO: Kill this (hopefully!) soon - if (state == ConnectionState.Connected) - Chrome.rootWidget.OpenWindow( "SERVER_LOBBY" ); - } - - lastConnectionState = state; - Timer.Time( "connectionState: {0}" ); - } Game.chrome.Draw(world); Timer.Time( "widgets: {0}" ); diff --git a/OpenRA.Game/Widgets/Delegates/ConnectionDialogsDelegate.cs b/OpenRA.Game/Widgets/Delegates/ConnectionDialogsDelegate.cs index 6af772931c..5c82953be5 100644 --- a/OpenRA.Game/Widgets/Delegates/ConnectionDialogsDelegate.cs +++ b/OpenRA.Game/Widgets/Delegates/ConnectionDialogsDelegate.cs @@ -18,6 +18,8 @@ */ #endregion +using OpenRA.Network; + namespace OpenRA.Widgets.Delegates { public class ConnectionDialogsDelegate : IWidgetDelegate @@ -45,6 +47,26 @@ namespace OpenRA.Widgets.Delegates r.GetWidget("CONNECTION_FAILED_DESC").GetText = () => "Could not connect to {0}:{1}".F(Game.CurrentHost, Game.CurrentPort); + + Game.ConnectionStateChanged += () => + { + r.CloseWindow(); + switch( Game.orderManager.Connection.ConnectionState ) + { + case ConnectionState.PreConnecting: + r.OpenWindow("MAINMENU_BG"); + break; + case ConnectionState.Connecting: + r.OpenWindow("CONNECTING_BG"); + break; + case ConnectionState.NotConnected: + r.OpenWindow("CONNECTION_FAILED_BG"); + break; + case ConnectionState.Connected: + r.OpenWindow("SERVER_LOBBY"); + break; + } + }; } } } diff --git a/OpenRA.Game/Widgets/Delegates/IngameChromeDelegate.cs b/OpenRA.Game/Widgets/Delegates/IngameChromeDelegate.cs index 7ec4b112bb..7b2f29aa77 100644 --- a/OpenRA.Game/Widgets/Delegates/IngameChromeDelegate.cs +++ b/OpenRA.Game/Widgets/Delegates/IngameChromeDelegate.cs @@ -30,6 +30,8 @@ namespace OpenRA.Widgets.Delegates var r = Chrome.rootWidget; var gameRoot = r.GetWidget("INGAME_ROOT"); var optionsBG = gameRoot.GetWidget("INGAME_OPTIONS_BG"); + + Game.OnGameStart += () => r.OpenWindow("INGAME_ROOT"); r.GetWidget("INGAME_OPTIONS_BUTTON").OnMouseUp = mi => { optionsBG.Visible = !optionsBG.Visible;