Remove static handler crap from widget delegates - Register/unregister events manually (ra & cnc).

This commit is contained in:
Paul Chote
2011-05-30 20:48:19 +12:00
parent add29e845e
commit 2ebb2ae921
6 changed files with 102 additions and 136 deletions

View File

@@ -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<ButtonWidget>("ABORT_BUTTON").OnClick = () => { Widget.CloseWindow(); onAbort(); };
panel.GetWidget<ButtonWidget>("ABORT_BUTTON").OnClick = () => { CloseWindow(); onAbort(); };
widget.GetWidget<LabelWidget>("CONNECTING_DESC").GetText = () =>
"Connecting to {0}:{1}...".F(host, port);

View File

@@ -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<ChatDisplayWidget>("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<CncMenuPaletteEffect>()
.Fade(CncMenuPaletteEffect.EffectType.None);
Game.AddChatLine += AddChatLine;
Game.BeforeGameStart += UnregisterEvents;
ingameRoot = widget.GetWidget("INGAME_ROOT");
if (world.LocalPlayer != null)

View File

@@ -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<CncColorPickerPaletteModifier>();
@@ -204,7 +167,7 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
mapButton.IsVisible = () => mapButton.Visible && Game.IsHost;
var disconnectButton = lobby.GetWidget<ButtonWidget>("DISCONNECT_BUTTON");
disconnectButton.OnClick = () => { Widget.CloseWindow(); onExit(); };
disconnectButton.OnClick = () => { CloseWindow(); onExit(); };
var gameStarting = false;
var lockTeamsCheckbox = lobby.GetWidget<CheckboxWidget>("LOCKTEAMS_CHECKBOX");