From 5c59f7703d8dead9f9cd2e79a84e2e240c4a795e Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sun, 8 May 2011 16:12:24 +1200 Subject: [PATCH] Reimplement connecting / connection failed dialogs. --- OpenRA.Game/Game.cs | 7 +- OpenRA.Mods.Cnc/OpenRA.Mods.Cnc.csproj | 1 + OpenRA.Mods.Cnc/Widgets/CncConnectionLogic.cs | 103 ++++++++++++++++++ OpenRA.Mods.Cnc/Widgets/CncLobbyLogic.cs | 84 ++++++++++---- .../Widgets/CncServerBrowserLogic.cs | 31 +++--- .../Widgets/CncServerCreationLogic.cs | 6 +- .../Widgets/Delegates/GameInitDelegate.cs | 33 +----- mods/cnc/chrome/connection.yaml | 77 +++++++++++++ mods/cnc/chrome/unused/connection.yaml | 74 ------------- mods/cnc/mod.yaml | 1 + 10 files changed, 270 insertions(+), 147 deletions(-) create mode 100644 OpenRA.Mods.Cnc/Widgets/CncConnectionLogic.cs create mode 100644 mods/cnc/chrome/connection.yaml delete mode 100644 mods/cnc/chrome/unused/connection.yaml diff --git a/OpenRA.Game/Game.cs b/OpenRA.Game/Game.cs index 2afa0f000e..bead1df213 100755 --- a/OpenRA.Game/Game.cs +++ b/OpenRA.Game/Game.cs @@ -356,9 +356,14 @@ namespace OpenRA return modData.ObjectCreator.CreateObject( name ); } - public static void CreateAndJoinServer(Settings settings, string map) + public static void CreateServer(Settings settings, string map) { server = new Server.Server(modData, settings, map); + } + + public static void CreateAndJoinServer(Settings settings, string map) + { + CreateServer(settings, map); JoinServer(IPAddress.Loopback.ToString(), settings.Server.ListenPort); } diff --git a/OpenRA.Mods.Cnc/OpenRA.Mods.Cnc.csproj b/OpenRA.Mods.Cnc/OpenRA.Mods.Cnc.csproj index 8efa9f7037..e95ab6b598 100644 --- a/OpenRA.Mods.Cnc/OpenRA.Mods.Cnc.csproj +++ b/OpenRA.Mods.Cnc/OpenRA.Mods.Cnc.csproj @@ -77,6 +77,7 @@ + diff --git a/OpenRA.Mods.Cnc/Widgets/CncConnectionLogic.cs b/OpenRA.Mods.Cnc/Widgets/CncConnectionLogic.cs new file mode 100644 index 0000000000..7ae2f0d5a7 --- /dev/null +++ b/OpenRA.Mods.Cnc/Widgets/CncConnectionLogic.cs @@ -0,0 +1,103 @@ +#region Copyright & License Information +/* + * Copyright 2007-2011 The OpenRA Developers (see AUTHORS) + * This file is part of OpenRA, which is free software. It is made + * available to you under the terms of the GNU General Public License + * as published by the Free Software Foundation. For more information, + * see COPYING. + */ +#endregion + +using OpenRA.Network; +using OpenRA.Widgets; +using System; +using System.Collections.Generic; + +namespace OpenRA.Mods.Cnc.Widgets +{ + public class CncConnectingLogic : IWidgetDelegate + { + static bool staticSetup; + Action onConnect, onRetry, onAbort; + + static void ConnectionStateChangedStub(OrderManager om) + { + var panel = Widget.RootWidget.GetWidget("CONNECTING_PANEL"); + if (panel == null) + return; + + var handler = panel.DelegateObject as CncConnectingLogic; + if (handler == null) + return; + + handler.ConnectionStateChanged(om); + } + + void ConnectionStateChanged(OrderManager om) + { + if (om.Connection.ConnectionState == ConnectionState.Connected) + { + Widget.CloseWindow(); + onConnect(); + } + else if (om.Connection.ConnectionState == ConnectionState.NotConnected) + { + // Show connection failed dialog + Widget.CloseWindow(); + Widget.OpenWindow("CONNECTIONFAILED_PANEL", new Dictionary() + { + { "onAbort", onAbort }, + { "onRetry", onRetry } + }); + } + } + + [ObjectCreator.UseCtor] + public CncConnectingLogic([ObjectCreator.Param] Widget widget, + [ObjectCreator.Param] Action onConnect, + [ObjectCreator.Param] Action onRetry, + [ObjectCreator.Param] Action onAbort) + { + this.onConnect = onConnect; + this.onRetry = onRetry; + this.onAbort = onAbort; + if (!staticSetup) + { + staticSetup = true; + Game.ConnectionStateChanged += ConnectionStateChangedStub; + } + + var panel = widget.GetWidget("CONNECTING_PANEL"); + panel.GetWidget("ABORT_BUTTON").OnClick = onAbort; + + //widget.GetWidget("CONNECTING_DESC").GetText = () => + // "Connecting to {0}:{1}...".F(host, port); + } + + public static void Connect(string host, int port, Action onConnect, Action onAbort) + { + Game.JoinServer(host, port); + Widget.OpenWindow("CONNECTING_PANEL", new Dictionary() + { + { "onConnect", onConnect }, + { "onAbort", onAbort }, + { "onRetry", new Action(() => { Widget.CloseWindow(); Connect(host, port, onConnect, onAbort); }) } + }); + } + } + + public class CncConnectionFailedLogic : IWidgetDelegate + { + [ObjectCreator.UseCtor] + public CncConnectionFailedLogic([ObjectCreator.Param] Widget widget, + [ObjectCreator.Param] Action onRetry, + [ObjectCreator.Param] Action onAbort) + { + var panel = widget.GetWidget("CONNECTIONFAILED_PANEL"); + panel.GetWidget("ABORT_BUTTON").OnClick = onAbort; + panel.GetWidget("RETRY_BUTTON").OnClick = onRetry; + + //widget.GetWidget("CONNECTING_DESC").GetText = () => + // "Connecting to {0}:{1}...".F(host, port); + } + }} diff --git a/OpenRA.Mods.Cnc/Widgets/CncLobbyLogic.cs b/OpenRA.Mods.Cnc/Widgets/CncLobbyLogic.cs index 1f66642ab9..9572bf5108 100755 --- a/OpenRA.Mods.Cnc/Widgets/CncLobbyLogic.cs +++ b/OpenRA.Mods.Cnc/Widgets/CncLobbyLogic.cs @@ -35,38 +35,72 @@ namespace OpenRA.Mods.Cnc.Widgets // Must be set only once on game start // TODO: This is stupid - static bool staticSetup; - enum StaticCrap { LobbyInfo, BeforeGameStart, AddChatLine } - static void StaticCrapChanged(StaticCrap type, Color a, string b, string c) + static bool staticSetup; + + public static CncLobbyLogic GetHandler() { var panel = Widget.RootWidget.GetWidget("SERVER_LOBBY"); - // The panel may not be open anymore if (panel == null) - return; + return null; - var lobbyLogic = panel.DelegateObject as CncLobbyLogic; - if (lobbyLogic == null) + return panel.DelegateObject as CncLobbyLogic; + } + + static void LobbyInfoChangedStub() + { + var handler = GetHandler(); + if (handler == null) return; - switch (type) - { - case StaticCrap.LobbyInfo: - lobbyLogic.UpdateCurrentMap(); - lobbyLogic.UpdatePlayerList(); - break; - case StaticCrap.BeforeGameStart: - lobbyLogic.onGameStart(); - break; - case StaticCrap.AddChatLine: - lobbyLogic.AddChatLine(a,b,c); - break; - } + handler.UpdateCurrentMap(); + handler.UpdatePlayerList(); } - + + static void BeforeGameStartStub() + { + var handler = GetHandler(); + if (handler == null) + return; + + handler.onGameStart(); + } + + static void AddChatLineStub(Color c, string from, string text) + { + var handler = GetHandler(); + if (handler == null) + return; + + handler.AddChatLine(c, from, text); + } + + static void ConnectionStateChangedStub(OrderManager om) + { + var handler = GetHandler(); + if (handler == null) + return; + + handler.ConnectionStateChanged(om); + } + + + // Listen for connection failures + void ConnectionStateChanged(OrderManager om) + { + // TODO: Show a connection failed dialog + if (om.Connection.ConnectionState == ConnectionState.NotConnected) + onExit(); + + //Widget.CloseWindow(); + //Widget.OpenWindow("CONNECTION_FAILED_BG", new Dictionary { { "orderManager", orderManager } }); + } + readonly Action onGameStart; + readonly Action onExit; readonly OrderManager orderManager; readonly WorldRenderer worldRenderer; + [ObjectCreator.UseCtor] internal CncLobbyLogic([ObjectCreator.Param( "widget" )] Widget lobby, [ObjectCreator.Param] OrderManager orderManager, @@ -77,13 +111,15 @@ namespace OpenRA.Mods.Cnc.Widgets this.orderManager = orderManager; this.worldRenderer = worldRenderer; this.onGameStart = onStart; + this.onExit = onExit; if (!staticSetup) { staticSetup = true; - Game.LobbyInfoChanged += () => StaticCrapChanged(StaticCrap.LobbyInfo, Color.Beige, null, null); - Game.BeforeGameStart += () => StaticCrapChanged(StaticCrap.BeforeGameStart, Color.PapayaWhip, null, null); - Game.AddChatLine += (a,b,c) => StaticCrapChanged(StaticCrap.AddChatLine, a, b, c); + Game.LobbyInfoChanged += LobbyInfoChangedStub; + Game.BeforeGameStart += BeforeGameStartStub; + Game.AddChatLine += AddChatLineStub; + Game.ConnectionStateChanged += ConnectionStateChangedStub; } UpdateCurrentMap(); diff --git a/OpenRA.Mods.Cnc/Widgets/CncServerBrowserLogic.cs b/OpenRA.Mods.Cnc/Widgets/CncServerBrowserLogic.cs index 1cc74c418f..4a8be65cd6 100644 --- a/OpenRA.Mods.Cnc/Widgets/CncServerBrowserLogic.cs +++ b/OpenRA.Mods.Cnc/Widgets/CncServerBrowserLogic.cs @@ -49,9 +49,10 @@ namespace OpenRA.Mods.Cnc.Widgets return ""; } } - + [ObjectCreator.UseCtor] public CncServerBrowserLogic([ObjectCreator.Param] Widget widget, + [ObjectCreator.Param] Action openLobby, [ObjectCreator.Param] Action onExit) { var panel = widget.GetWidget("SERVERBROWSER_PANEL"); @@ -74,8 +75,10 @@ namespace OpenRA.Mods.Cnc.Widgets if (currentServer == null) return; - Widget.CloseWindow(); - Game.JoinServer(currentServer.Address.Split(':')[0], int.Parse(currentServer.Address.Split(':')[1])); + string host = currentServer.Address.Split(':')[0]; + int port = int.Parse(currentServer.Address.Split(':')[1]); + + CncConnectingLogic.Connect(host, port, openLobby, onExit); }; panel.GetWidget("BACK_BUTTON").OnClick = onExit; @@ -189,7 +192,7 @@ namespace OpenRA.Mods.Cnc.Widgets browserLogic.RefreshServerList(games); } } - + public class CncDirectConnectLogic : IWidgetDelegate { [ObjectCreator.UseCtor] @@ -198,24 +201,24 @@ namespace OpenRA.Mods.Cnc.Widgets [ObjectCreator.Param] Action openLobby) { var panel = widget.GetWidget("DIRECTCONNECT_PANEL"); - var ip = panel.GetWidget("IP"); - var port = panel.GetWidget("PORT"); + var ipField = panel.GetWidget("IP"); + var portField = panel.GetWidget("PORT"); var last = Game.Settings.Player.LastServer.Split(':').ToArray(); - ip.Text = last.Length > 1 ? last[0] : "localhost"; - port.Text = last.Length > 2 ? last[1] : "1234"; + ipField.Text = last.Length > 1 ? last[0] : "localhost"; + portField.Text = last.Length > 2 ? last[1] : "1234"; panel.GetWidget("JOIN_BUTTON").OnClick = () => { - int p; - if (!int.TryParse(port.Text, out p)) - p = 1234; + int port; + if (!int.TryParse(portField.Text, out port)) + port = 1234; - Game.Settings.Player.LastServer = "{0}:{1}".F(ip.Text, p); + Game.Settings.Player.LastServer = "{0}:{1}".F(ipField.Text, port); Game.Settings.Save(); - Game.JoinServer(ip.Text,p); - openLobby(); + Widget.CloseWindow(); + CncConnectingLogic.Connect(ipField.Text, port, openLobby, onExit); }; panel.GetWidget("BACK_BUTTON").OnClick = onExit; diff --git a/OpenRA.Mods.Cnc/Widgets/CncServerCreationLogic.cs b/OpenRA.Mods.Cnc/Widgets/CncServerCreationLogic.cs index c05051802d..bf305d9185 100644 --- a/OpenRA.Mods.Cnc/Widgets/CncServerCreationLogic.cs +++ b/OpenRA.Mods.Cnc/Widgets/CncServerCreationLogic.cs @@ -20,6 +20,7 @@ namespace OpenRA.Mods.Cnc.Widgets { Widget panel; Action onCreate; + Action onExit; Map map; bool advertiseOnline; [ObjectCreator.UseCtor] @@ -29,6 +30,7 @@ namespace OpenRA.Mods.Cnc.Widgets { panel = widget.GetWidget("CREATESERVER_PANEL"); onCreate = openLobby; + this.onExit = onExit; var settings = Game.Settings; panel.GetWidget("BACK_BUTTON").OnClick = onExit; @@ -68,9 +70,9 @@ namespace OpenRA.Mods.Cnc.Widgets Game.Settings.Server.AdvertiseOnline = advertiseOnline; Game.Settings.Save(); - Game.CreateAndJoinServer(Game.Settings, map.Uid); + Game.CreateServer(Game.Settings, map.Uid); Widget.CloseWindow(); - onCreate(); + CncConnectingLogic.Connect(IPAddress.Loopback.ToString(), Game.Settings.Server.ListenPort, onCreate, onExit); } } } diff --git a/OpenRA.Mods.RA/Widgets/Delegates/GameInitDelegate.cs b/OpenRA.Mods.RA/Widgets/Delegates/GameInitDelegate.cs index 9201495ffb..65ae348b9b 100755 --- a/OpenRA.Mods.RA/Widgets/Delegates/GameInitDelegate.cs +++ b/OpenRA.Mods.RA/Widgets/Delegates/GameInitDelegate.cs @@ -34,38 +34,7 @@ namespace OpenRA.Mods.RA.Widgets.Delegates public void Init() { - if (Info.InstallMode == "cnc") - { - /* - Game.ConnectionStateChanged += orderManager => - { - Widget.CloseWindow(); - switch (orderManager.Connection.ConnectionState) - { - case ConnectionState.PreConnecting: - Widget.LoadWidget("MENU_BACKGROUND"); - break; - case ConnectionState.Connecting: - Widget.OpenWindow("CONNECTING_BG", - new Dictionary { { "host", orderManager.Host }, { "port", orderManager.Port } }); - break; - case ConnectionState.NotConnected: - Widget.OpenWindow("CONNECTION_FAILED_BG", - new Dictionary { { "orderManager", orderManager } }); - break; - case ConnectionState.Connected: - var lobby = Game.OpenWindow(orderManager.world, "SERVER_LOBBY"); - lobby.GetWidget("CHAT_DISPLAY").ClearChat(); - lobby.GetWidget("CHANGEMAP_BUTTON").Visible = true; - lobby.GetWidget("LOCKTEAMS_CHECKBOX").Visible = true; - lobby.GetWidget("ALLOWCHEATS_CHECKBOX").Visible = true; - lobby.GetWidget("DISCONNECT_BUTTON").Visible = true; - break; - } - }; - */ - } - else + if (Info.InstallMode != "cnc") { Game.ConnectionStateChanged += orderManager => { diff --git a/mods/cnc/chrome/connection.yaml b/mods/cnc/chrome/connection.yaml new file mode 100644 index 0000000000..1eb3f9cea6 --- /dev/null +++ b/mods/cnc/chrome/connection.yaml @@ -0,0 +1,77 @@ +Container@CONNECTING_PANEL: + Id:CONNECTING_PANEL + Delegate:CncConnectingLogic + X:(WINDOW_RIGHT - WIDTH)/2 + Y:(WINDOW_BOTTOM - 90)/2 + Width:370 + Height:125 + Children: + Label@TITLE: + Width:PARENT_RIGHT + Y:0-25 + Font:BigBold + Contrast:true + Align:Center + Text:Connecting + Background@bg: + Width:370 + Height:90 + Background:panel-black + Children: + Label@CONNECTING_DESC: + Id:CONNECTING_DESC + Y:(PARENT_BOTTOM-HEIGHT)/2 + Width:PARENT_RIGHT + Height:25 + Text:Connecting... + Font:Bold + Align:Center + CncMenuButton@ABORT_BUTTON: + Id:ABORT_BUTTON + X:230 + Y:89 + Width:140 + Height:35 + Text:Abort + +Container@CONNECTIONFAILED_PANEL: + Id:CONNECTIONFAILED_PANEL + Delegate:CncConnectionFailedLogic + X:(WINDOW_RIGHT - WIDTH)/2 + Y:(WINDOW_BOTTOM - 90)/2 + Width:370 + Height:125 + Children: + Label@TITLE: + Width:PARENT_RIGHT + Y:0-25 + Font:BigBold + Contrast:true + Align:Center + Text:Connection Failed + Background@bg: + Width:370 + Height:90 + Background:panel-black + Children: + Label@CONNECTING_DESC: + Id:CONNECTING_DESC + Y:(PARENT_BOTTOM-HEIGHT)/2 + Width:PARENT_RIGHT + Height:25 + Text:Failed to connect + Font:Bold + Align:Center + CncMenuButton@RETRY_BUTTON: + Id:RETRY_BUTTON + Y:89 + Width:140 + Height:35 + Text:Retry + CncMenuButton@ABORT_BUTTON: + Id:ABORT_BUTTON + X:230 + Y:89 + Width:140 + Height:35 + Text:Abort \ No newline at end of file diff --git a/mods/cnc/chrome/unused/connection.yaml b/mods/cnc/chrome/unused/connection.yaml deleted file mode 100644 index e24b83bb1c..0000000000 --- a/mods/cnc/chrome/unused/connection.yaml +++ /dev/null @@ -1,74 +0,0 @@ -Background@CONNECTION_FAILED_BG: - Id:CONNECTION_FAILED_BG - Delegate:ConnectionFailedDelegate - X:(WINDOW_RIGHT - WIDTH)/2 - Y:(WINDOW_BOTTOM - HEIGHT)/2 - Width:450 - Height:150 - Children: - Label@CONNECTION_FAILED_TITLE: - Id:CONNECTION_FAILED_TITLE - X:0 - Y:20 - Width:450 - Height:25 - Text:Connection Failed - Align:Center - Bold:True - Label@CONNECTION_FAILED_DESC: - Id:CONNECTION_FAILED_DESC - X:0 - Y:60 - Width:PARENT_RIGHT - Height:25 - Text:Could not connect to AAA.BBB.CCC.DDD:EEEE - Align:Center - Button@CONNECTION_BUTTON_RETRY: - Id:CONNECTION_BUTTON_RETRY - X:PARENT_RIGHT - 360 - Y:PARENT_BOTTOM - 45 - Width:160 - Height:25 - Text:Retry - Bold:True - Button@CONNECTION_BUTTON_CANCEL: - Id:CONNECTION_BUTTON_CANCEL - X:PARENT_RIGHT - 180 - Y:PARENT_BOTTOM - 45 - Width:160 - Height:25 - Text:Cancel - Bold:True -Background@CONNECTING_BG: - Id:CONNECTING_BG - Delegate:ConnectionDialogsDelegate - X:(WINDOW_RIGHT - WIDTH)/2 - Y:(WINDOW_BOTTOM - HEIGHT)/2 - Width:450 - Height:150 - Children: - Label@CONNECTING_TITLE: - Id:CONNECTING_TITLE - X:0 - Y:20 - Width:450 - Height:25 - Text:Connecting - Align:Center - Bold:True - Label@CONNECTING_DESC: - Id:CONNECTING_DESC - X:0 - Y:60 - Width:PARENT_RIGHT - Height:25 - Text:Connecting to AAA.BBB.CCC.DDD:EEEE... - Align:Center - Button@CONNECTION_BUTTON_ABORT: - Id:CONNECTION_BUTTON_ABORT - X:PARENT_RIGHT - 180 - Y:PARENT_BOTTOM - 45 - Width:160 - Height:25 - Text:Abort - Bold:True \ No newline at end of file diff --git a/mods/cnc/mod.yaml b/mods/cnc/mod.yaml index 8500f01988..658abee239 100644 --- a/mods/cnc/mod.yaml +++ b/mods/cnc/mod.yaml @@ -65,6 +65,7 @@ ChromeLayout: mods/cnc/chrome/createserver.yaml mods/cnc/chrome/directconnect.yaml mods/cnc/chrome/lobby.yaml + mods/cnc/chrome/connection.yaml mods/cnc/chrome/mapchooser.yaml mods/cnc/chrome/replaybrowser.yaml mods/cnc/chrome/ingame.yaml