diff --git a/OpenRA.Mods.Cnc/Widgets/Logic/CncConnectionLogic.cs b/OpenRA.Mods.Cnc/Widgets/Logic/CncConnectionLogic.cs index a05645e62f..a2ea1ee084 100644 --- a/OpenRA.Mods.Cnc/Widgets/Logic/CncConnectionLogic.cs +++ b/OpenRA.Mods.Cnc/Widgets/Logic/CncConnectionLogic.cs @@ -20,35 +20,17 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic string host; int port; - static CncConnectingLogic() - { - Game.ConnectionStateChanged += ConnectionStateChangedStub; - } - - static void ConnectionStateChangedStub(OrderManager om) - { - var panel = Widget.RootWidget.GetWidget("CONNECTING_PANEL"); - if (panel == null) - return; - - var handler = panel.LogicObject as CncConnectingLogic; - if (handler == null) - return; - - handler.ConnectionStateChanged(om); - } - void ConnectionStateChanged(OrderManager om) { if (om.Connection.ConnectionState == ConnectionState.Connected) { - Widget.CloseWindow(); + CloseWindow(); onConnect(); } else if (om.Connection.ConnectionState == ConnectionState.NotConnected) { // Show connection failed dialog - Widget.CloseWindow(); + CloseWindow(); Widget.OpenWindow("CONNECTIONFAILED_PANEL", new WidgetArgs() { { "onAbort", onAbort }, @@ -59,6 +41,12 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic } } + public void CloseWindow() + { + Game.ConnectionStateChanged -= ConnectionStateChanged; + Widget.CloseWindow(); + } + [ObjectCreator.UseCtor] public CncConnectingLogic([ObjectCreator.Param] Widget widget, [ObjectCreator.Param] string host, @@ -73,8 +61,10 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic this.onRetry = onRetry; this.onAbort = onAbort; + Game.ConnectionStateChanged += ConnectionStateChanged; + var panel = widget.GetWidget("CONNECTING_PANEL"); - panel.GetWidget("ABORT_BUTTON").OnClick = () => { Widget.CloseWindow(); onAbort(); }; + panel.GetWidget("ABORT_BUTTON").OnClick = () => { CloseWindow(); onAbort(); }; widget.GetWidget("CONNECTING_DESC").GetText = () => "Connecting to {0}:{1}...".F(host, port); diff --git a/OpenRA.Mods.Cnc/Widgets/Logic/CncIngameChromeLogic.cs b/OpenRA.Mods.Cnc/Widgets/Logic/CncIngameChromeLogic.cs index c3c34d9538..82df56dfb9 100644 --- a/OpenRA.Mods.Cnc/Widgets/Logic/CncIngameChromeLogic.cs +++ b/OpenRA.Mods.Cnc/Widgets/Logic/CncIngameChromeLogic.cs @@ -21,30 +21,17 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic Widget ingameRoot; - static CncIngameChromeLogic() - { - Game.AddChatLine += AddChatLineStub; - } - - static void AddChatLineStub(Color c, string from, string text) - { - var panel = Widget.RootWidget.GetWidget("INGAME_ROOT"); - if (panel == null) - return; - - var handler = panel.LogicObject as CncIngameChromeLogic; - if (handler == null) - return; - - handler.AddChatLine(c, from, text); - } - - void AddChatLine(Color c, string from, string text) { ingameRoot.GetWidget("CHAT_DISPLAY").AddLine(c, from, text); } + public void UnregisterEvents() + { + Game.AddChatLine -= AddChatLine; + Game.BeforeGameStart -= UnregisterEvents; + } + [ObjectCreator.UseCtor] public CncIngameChromeLogic([ObjectCreator.Param] Widget widget, [ObjectCreator.Param] World world ) @@ -52,6 +39,10 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic world.WorldActor.Trait() .Fade(CncMenuPaletteEffect.EffectType.None); + + Game.AddChatLine += AddChatLine; + Game.BeforeGameStart += UnregisterEvents; + ingameRoot = widget.GetWidget("INGAME_ROOT"); if (world.LocalPlayer != null) diff --git a/OpenRA.Mods.Cnc/Widgets/Logic/CncLobbyLogic.cs b/OpenRA.Mods.Cnc/Widgets/Logic/CncLobbyLogic.cs index 967c207179..833ea0470d 100644 --- a/OpenRA.Mods.Cnc/Widgets/Logic/CncLobbyLogic.cs +++ b/OpenRA.Mods.Cnc/Widgets/Logic/CncLobbyLogic.cs @@ -36,60 +36,6 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic readonly Action OnGameStart; readonly Action onExit; readonly OrderManager orderManager; - - static CncLobbyLogic() - { - Game.LobbyInfoChanged += LobbyInfoChangedStub; - Game.BeforeGameStart += BeforeGameStartStub; - Game.AddChatLine += AddChatLineStub; - Game.ConnectionStateChanged += ConnectionStateChangedStub; - } - - public static CncLobbyLogic GetHandler() - { - var panel = Widget.RootWidget.GetWidget("SERVER_LOBBY"); - if (panel == null) - return null; - - return panel.LogicObject as CncLobbyLogic; - } - - static void LobbyInfoChangedStub() - { - var handler = GetHandler(); - if (handler == null) - return; - - 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) @@ -97,7 +43,7 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic if (om.Connection.ConnectionState == ConnectionState.NotConnected) { // Show connection failed dialog - Widget.CloseWindow(); + CloseWindow(); Action onConnect = () => { @@ -110,7 +56,7 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic Action onRetry = () => { - Widget.CloseWindow(); + CloseWindow(); CncConnectingLogic.Connect(om.Host, om.Port, onConnect, onExit); }; @@ -123,6 +69,17 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic }); } } + + public void CloseWindow() + { + Game.LobbyInfoChanged -= UpdateCurrentMap; + Game.LobbyInfoChanged -= UpdatePlayerList; + Game.BeforeGameStart -= OnGameStart; + Game.AddChatLine -= AddChatLine; + Game.ConnectionStateChanged -= ConnectionStateChanged; + + Widget.CloseWindow(); + } [ObjectCreator.UseCtor] internal CncLobbyLogic([ObjectCreator.Param( "widget" )] Widget lobby, @@ -132,8 +89,14 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic [ObjectCreator.Param] Action onStart) { this.orderManager = orderManager; - this.OnGameStart = () => { Widget.CloseWindow(); onStart(); }; + this.OnGameStart = () => { CloseWindow(); onStart(); }; this.onExit = onExit; + + Game.LobbyInfoChanged += UpdateCurrentMap; + Game.LobbyInfoChanged += UpdatePlayerList; + Game.BeforeGameStart += OnGameStart; + Game.AddChatLine += AddChatLine; + Game.ConnectionStateChanged += ConnectionStateChanged; UpdateCurrentMap(); PlayerPalettePreview = world.WorldActor.Trait(); @@ -204,7 +167,7 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic mapButton.IsVisible = () => mapButton.Visible && Game.IsHost; var disconnectButton = lobby.GetWidget("DISCONNECT_BUTTON"); - disconnectButton.OnClick = () => { Widget.CloseWindow(); onExit(); }; + disconnectButton.OnClick = () => { CloseWindow(); onExit(); }; var gameStarting = false; var lockTeamsCheckbox = lobby.GetWidget("LOCKTEAMS_CHECKBOX"); diff --git a/OpenRA.Mods.RA/Widgets/Logic/IngameChromeLogic.cs b/OpenRA.Mods.RA/Widgets/Logic/IngameChromeLogic.cs index 7989bd101c..f9918055dd 100644 --- a/OpenRA.Mods.RA/Widgets/Logic/IngameChromeLogic.cs +++ b/OpenRA.Mods.RA/Widgets/Logic/IngameChromeLogic.cs @@ -10,16 +10,22 @@ using OpenRA.Traits; using OpenRA.Widgets; +using System.Drawing; namespace OpenRA.Mods.RA.Widgets.Logic { public class IngameChromeLogic { + Widget gameRoot; + [ObjectCreator.UseCtor] public IngameChromeLogic( [ObjectCreator.Param] World world ) { + Game.AddChatLine += AddChatLine; + Game.BeforeGameStart += UnregisterEvents; + var r = Widget.RootWidget; - var gameRoot = r.GetWidget("INGAME_ROOT"); + gameRoot = r.GetWidget("INGAME_ROOT"); var optionsBG = gameRoot.GetWidget("INGAME_OPTIONS_BG"); r.GetWidget("INGAME_OPTIONS_BUTTON").OnMouseUp = mi => { @@ -64,10 +70,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic Game.Exit(); return true; }; - - Game.AddChatLine += gameRoot.GetWidget("CHAT_DISPLAY").AddLine; - - + var postgameBG = gameRoot.GetWidget("POSTGAME_BG"); var postgameText = postgameBG.GetWidget("TEXT"); postgameBG.IsVisible = () => @@ -82,5 +85,16 @@ namespace OpenRA.Mods.RA.Widgets.Logic ((state == WinState.Lost)? "YOU ARE DEFEATED" : "YOU ARE VICTORIOUS"); }; } + + public void UnregisterEvents() + { + Game.AddChatLine -= AddChatLine; + Game.BeforeGameStart -= UnregisterEvents; + } + + void AddChatLine(Color c, string from, string text) + { + gameRoot.GetWidget("CHAT_DISPLAY").AddLine(c, from, text); + } } } diff --git a/OpenRA.Mods.RA/Widgets/Logic/IngameObserverChromeLogic.cs b/OpenRA.Mods.RA/Widgets/Logic/IngameObserverChromeLogic.cs index 88c1150338..2d73b14c0a 100644 --- a/OpenRA.Mods.RA/Widgets/Logic/IngameObserverChromeLogic.cs +++ b/OpenRA.Mods.RA/Widgets/Logic/IngameObserverChromeLogic.cs @@ -10,16 +10,22 @@ using OpenRA.Traits; using OpenRA.Widgets; +using System.Drawing; namespace OpenRA.Mods.RA.Widgets.Logic { public class IngameObserverChromeLogic { + Widget gameRoot; + [ObjectCreator.UseCtor] public IngameObserverChromeLogic([ObjectCreator.Param] World world) { + Game.AddChatLine += AddChatLine; + Game.BeforeGameStart += UnregisterEvents; + var r = Widget.RootWidget; - var gameRoot = r.GetWidget("OBSERVER_ROOT"); + gameRoot = r.GetWidget("OBSERVER_ROOT"); var optionsBG = gameRoot.GetWidget("INGAME_OPTIONS_BG"); r.GetWidget("INGAME_OPTIONS_BUTTON").OnMouseUp = mi => { @@ -51,38 +57,24 @@ namespace OpenRA.Mods.RA.Widgets.Logic optionsBG.Visible = false; return true; }; - /* - optionsBG.GetWidget("SURRENDER").OnMouseUp = mi => - { - world.IssueOrder(new Order("Surrender", world.LocalPlayer.PlayerActor)); - return true; - }; - */ + optionsBG.GetWidget("SURRENDER").IsVisible = () => false; optionsBG.GetWidget("QUIT").OnMouseUp = mi => { Game.Exit(); return true; }; - - Game.AddChatLine += gameRoot.GetWidget("CHAT_DISPLAY").AddLine; - - /* - var postgameBG = gameRoot.GetWidget("POSTGAME_BG"); - var postgameText = postgameBG.GetWidget("TEXT"); - postgameBG.IsVisible = () => - { - return world.LocalPlayer != null && world.LocalPlayer.WinState != WinState.Undefined; - }; - - postgameText.GetText = () => - { - if (world.LocalPlayer == null) - return ""; - var state = world.LocalPlayer.WinState; - return (state == WinState.Undefined)? "" : - ((state == WinState.Lost)? "YOU ARE DEFEATED" : "YOU ARE VICTORIOUS"); - };*/ + } + + public void UnregisterEvents() + { + Game.AddChatLine -= AddChatLine; + Game.BeforeGameStart -= UnregisterEvents; + } + + void AddChatLine(Color c, string from, string text) + { + gameRoot.GetWidget("CHAT_DISPLAY").AddLine(c, from, text); } } } diff --git a/OpenRA.Mods.RA/Widgets/Logic/LobbyLogic.cs b/OpenRA.Mods.RA/Widgets/Logic/LobbyLogic.cs index 59f524aac4..dbc286db20 100644 --- a/OpenRA.Mods.RA/Widgets/Logic/LobbyLogic.cs +++ b/OpenRA.Mods.RA/Widgets/Logic/LobbyLogic.cs @@ -22,7 +22,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic { public class LobbyLogic { - Widget LocalPlayerTemplate, RemotePlayerTemplate, EmptySlotTemplate, EmptySlotTemplateHost; + Widget lobby, LocalPlayerTemplate, RemotePlayerTemplate, EmptySlotTemplate, EmptySlotTemplateHost; ScrollPanelWidget Players; Dictionary CountryNames; string MapUid; @@ -37,8 +37,10 @@ namespace OpenRA.Mods.RA.Widgets.Logic { this.orderManager = orderManager; this.worldRenderer = worldRenderer; - + this.lobby = lobby; + Game.BeforeGameStart += CloseWindow; Game.LobbyInfoChanged += UpdateCurrentMap; + Game.LobbyInfoChanged += UpdatePlayerList; UpdateCurrentMap(); CurrentColorPreview = Game.Settings.Player.ColorRamp; @@ -101,9 +103,9 @@ namespace OpenRA.Mods.RA.Widgets.Logic var disconnectButton = lobby.GetWidget("DISCONNECT_BUTTON"); disconnectButton.OnMouseUp = mi => { + CloseWindow(); Game.Disconnect(); Game.LoadShellMap(); - Widget.CloseWindow(); Widget.OpenWindow("MAINMENU_BG"); return true; }; @@ -137,10 +139,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic // Todo: Only show if the map requirements are met for player slots startGameButton.IsVisible = () => Game.IsHost; - Game.LobbyInfoChanged += UpdatePlayerList; - Game.AddChatLine += lobby.GetWidget("CHAT_DISPLAY").AddLine; - bool teamChat = false; var chatLabel = lobby.GetWidget("LABEL_CHATTYPE"); var chatTextField = lobby.GetWidget("CHAT_TEXTFIELD"); @@ -161,6 +160,23 @@ namespace OpenRA.Mods.RA.Widgets.Logic chatLabel.Text = (teamChat) ? "Team:" : "Chat:"; return true; }; + + Game.AddChatLine += AddChatLine; + } + + public void CloseWindow() + { + Game.LobbyInfoChanged -= UpdateCurrentMap; + Game.LobbyInfoChanged -= UpdatePlayerList; + Game.AddChatLine -= AddChatLine; + Game.BeforeGameStart -= CloseWindow; + + Widget.CloseWindow(); + } + + void AddChatLine(Color c, string from, string text) + { + lobby.GetWidget("CHAT_DISPLAY").AddLine(c, from, text); } void UpdatePlayerColor(float hf, float sf, float lf, float r)