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");

View File

@@ -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<ChatDisplayWidget>("CHAT_DISPLAY").AddLine;
var postgameBG = gameRoot.GetWidget("POSTGAME_BG");
var postgameText = postgameBG.GetWidget<LabelWidget>("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<ChatDisplayWidget>("CHAT_DISPLAY").AddLine(c, from, text);
}
}
}

View File

@@ -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<ChatDisplayWidget>("CHAT_DISPLAY").AddLine;
/*
var postgameBG = gameRoot.GetWidget("POSTGAME_BG");
var postgameText = postgameBG.GetWidget<LabelWidget>("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<ChatDisplayWidget>("CHAT_DISPLAY").AddLine(c, from, text);
}
}
}

View File

@@ -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<string, string> 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<ChatDisplayWidget>("CHAT_DISPLAY").AddLine;
bool teamChat = false;
var chatLabel = lobby.GetWidget<LabelWidget>("LABEL_CHATTYPE");
var chatTextField = lobby.GetWidget<TextFieldWidget>("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<ChatDisplayWidget>("CHAT_DISPLAY").AddLine(c, from, text);
}
void UpdatePlayerColor(float hf, float sf, float lf, float r)