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; string host;
int port; 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) void ConnectionStateChanged(OrderManager om)
{ {
if (om.Connection.ConnectionState == ConnectionState.Connected) if (om.Connection.ConnectionState == ConnectionState.Connected)
{ {
Widget.CloseWindow(); CloseWindow();
onConnect(); onConnect();
} }
else if (om.Connection.ConnectionState == ConnectionState.NotConnected) else if (om.Connection.ConnectionState == ConnectionState.NotConnected)
{ {
// Show connection failed dialog // Show connection failed dialog
Widget.CloseWindow(); CloseWindow();
Widget.OpenWindow("CONNECTIONFAILED_PANEL", new WidgetArgs() Widget.OpenWindow("CONNECTIONFAILED_PANEL", new WidgetArgs()
{ {
{ "onAbort", onAbort }, { "onAbort", onAbort },
@@ -59,6 +41,12 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
} }
} }
public void CloseWindow()
{
Game.ConnectionStateChanged -= ConnectionStateChanged;
Widget.CloseWindow();
}
[ObjectCreator.UseCtor] [ObjectCreator.UseCtor]
public CncConnectingLogic([ObjectCreator.Param] Widget widget, public CncConnectingLogic([ObjectCreator.Param] Widget widget,
[ObjectCreator.Param] string host, [ObjectCreator.Param] string host,
@@ -73,8 +61,10 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
this.onRetry = onRetry; this.onRetry = onRetry;
this.onAbort = onAbort; this.onAbort = onAbort;
Game.ConnectionStateChanged += ConnectionStateChanged;
var panel = widget.GetWidget("CONNECTING_PANEL"); 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 = () => widget.GetWidget<LabelWidget>("CONNECTING_DESC").GetText = () =>
"Connecting to {0}:{1}...".F(host, port); "Connecting to {0}:{1}...".F(host, port);

View File

@@ -21,30 +21,17 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
Widget ingameRoot; 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) void AddChatLine(Color c, string from, string text)
{ {
ingameRoot.GetWidget<ChatDisplayWidget>("CHAT_DISPLAY").AddLine(c, from, text); ingameRoot.GetWidget<ChatDisplayWidget>("CHAT_DISPLAY").AddLine(c, from, text);
} }
public void UnregisterEvents()
{
Game.AddChatLine -= AddChatLine;
Game.BeforeGameStart -= UnregisterEvents;
}
[ObjectCreator.UseCtor] [ObjectCreator.UseCtor]
public CncIngameChromeLogic([ObjectCreator.Param] Widget widget, public CncIngameChromeLogic([ObjectCreator.Param] Widget widget,
[ObjectCreator.Param] World world ) [ObjectCreator.Param] World world )
@@ -52,6 +39,10 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
world.WorldActor.Trait<CncMenuPaletteEffect>() world.WorldActor.Trait<CncMenuPaletteEffect>()
.Fade(CncMenuPaletteEffect.EffectType.None); .Fade(CncMenuPaletteEffect.EffectType.None);
Game.AddChatLine += AddChatLine;
Game.BeforeGameStart += UnregisterEvents;
ingameRoot = widget.GetWidget("INGAME_ROOT"); ingameRoot = widget.GetWidget("INGAME_ROOT");
if (world.LocalPlayer != null) if (world.LocalPlayer != null)

View File

@@ -37,67 +37,13 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
readonly Action onExit; readonly Action onExit;
readonly OrderManager orderManager; 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 // Listen for connection failures
void ConnectionStateChanged(OrderManager om) void ConnectionStateChanged(OrderManager om)
{ {
if (om.Connection.ConnectionState == ConnectionState.NotConnected) if (om.Connection.ConnectionState == ConnectionState.NotConnected)
{ {
// Show connection failed dialog // Show connection failed dialog
Widget.CloseWindow(); CloseWindow();
Action onConnect = () => Action onConnect = () =>
{ {
@@ -110,7 +56,7 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
Action onRetry = () => Action onRetry = () =>
{ {
Widget.CloseWindow(); CloseWindow();
CncConnectingLogic.Connect(om.Host, om.Port, onConnect, onExit); CncConnectingLogic.Connect(om.Host, om.Port, onConnect, onExit);
}; };
@@ -124,6 +70,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] [ObjectCreator.UseCtor]
internal CncLobbyLogic([ObjectCreator.Param( "widget" )] Widget lobby, internal CncLobbyLogic([ObjectCreator.Param( "widget" )] Widget lobby,
[ObjectCreator.Param] World world, // Shellmap world [ObjectCreator.Param] World world, // Shellmap world
@@ -132,9 +89,15 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
[ObjectCreator.Param] Action onStart) [ObjectCreator.Param] Action onStart)
{ {
this.orderManager = orderManager; this.orderManager = orderManager;
this.OnGameStart = () => { Widget.CloseWindow(); onStart(); }; this.OnGameStart = () => { CloseWindow(); onStart(); };
this.onExit = onExit; this.onExit = onExit;
Game.LobbyInfoChanged += UpdateCurrentMap;
Game.LobbyInfoChanged += UpdatePlayerList;
Game.BeforeGameStart += OnGameStart;
Game.AddChatLine += AddChatLine;
Game.ConnectionStateChanged += ConnectionStateChanged;
UpdateCurrentMap(); UpdateCurrentMap();
PlayerPalettePreview = world.WorldActor.Trait<CncColorPickerPaletteModifier>(); PlayerPalettePreview = world.WorldActor.Trait<CncColorPickerPaletteModifier>();
PlayerPalettePreview.Ramp = Game.Settings.Player.ColorRamp; PlayerPalettePreview.Ramp = Game.Settings.Player.ColorRamp;
@@ -204,7 +167,7 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
mapButton.IsVisible = () => mapButton.Visible && Game.IsHost; mapButton.IsVisible = () => mapButton.Visible && Game.IsHost;
var disconnectButton = lobby.GetWidget<ButtonWidget>("DISCONNECT_BUTTON"); var disconnectButton = lobby.GetWidget<ButtonWidget>("DISCONNECT_BUTTON");
disconnectButton.OnClick = () => { Widget.CloseWindow(); onExit(); }; disconnectButton.OnClick = () => { CloseWindow(); onExit(); };
var gameStarting = false; var gameStarting = false;
var lockTeamsCheckbox = lobby.GetWidget<CheckboxWidget>("LOCKTEAMS_CHECKBOX"); var lockTeamsCheckbox = lobby.GetWidget<CheckboxWidget>("LOCKTEAMS_CHECKBOX");

View File

@@ -10,16 +10,22 @@
using OpenRA.Traits; using OpenRA.Traits;
using OpenRA.Widgets; using OpenRA.Widgets;
using System.Drawing;
namespace OpenRA.Mods.RA.Widgets.Logic namespace OpenRA.Mods.RA.Widgets.Logic
{ {
public class IngameChromeLogic public class IngameChromeLogic
{ {
Widget gameRoot;
[ObjectCreator.UseCtor] [ObjectCreator.UseCtor]
public IngameChromeLogic( [ObjectCreator.Param] World world ) public IngameChromeLogic( [ObjectCreator.Param] World world )
{ {
Game.AddChatLine += AddChatLine;
Game.BeforeGameStart += UnregisterEvents;
var r = Widget.RootWidget; var r = Widget.RootWidget;
var gameRoot = r.GetWidget("INGAME_ROOT"); gameRoot = r.GetWidget("INGAME_ROOT");
var optionsBG = gameRoot.GetWidget("INGAME_OPTIONS_BG"); var optionsBG = gameRoot.GetWidget("INGAME_OPTIONS_BG");
r.GetWidget("INGAME_OPTIONS_BUTTON").OnMouseUp = mi => { r.GetWidget("INGAME_OPTIONS_BUTTON").OnMouseUp = mi => {
@@ -65,9 +71,6 @@ namespace OpenRA.Mods.RA.Widgets.Logic
return true; return true;
}; };
Game.AddChatLine += gameRoot.GetWidget<ChatDisplayWidget>("CHAT_DISPLAY").AddLine;
var postgameBG = gameRoot.GetWidget("POSTGAME_BG"); var postgameBG = gameRoot.GetWidget("POSTGAME_BG");
var postgameText = postgameBG.GetWidget<LabelWidget>("TEXT"); var postgameText = postgameBG.GetWidget<LabelWidget>("TEXT");
postgameBG.IsVisible = () => postgameBG.IsVisible = () =>
@@ -82,5 +85,16 @@ namespace OpenRA.Mods.RA.Widgets.Logic
((state == WinState.Lost)? "YOU ARE DEFEATED" : "YOU ARE VICTORIOUS"); ((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.Traits;
using OpenRA.Widgets; using OpenRA.Widgets;
using System.Drawing;
namespace OpenRA.Mods.RA.Widgets.Logic namespace OpenRA.Mods.RA.Widgets.Logic
{ {
public class IngameObserverChromeLogic public class IngameObserverChromeLogic
{ {
Widget gameRoot;
[ObjectCreator.UseCtor] [ObjectCreator.UseCtor]
public IngameObserverChromeLogic([ObjectCreator.Param] World world) public IngameObserverChromeLogic([ObjectCreator.Param] World world)
{ {
Game.AddChatLine += AddChatLine;
Game.BeforeGameStart += UnregisterEvents;
var r = Widget.RootWidget; var r = Widget.RootWidget;
var gameRoot = r.GetWidget("OBSERVER_ROOT"); gameRoot = r.GetWidget("OBSERVER_ROOT");
var optionsBG = gameRoot.GetWidget("INGAME_OPTIONS_BG"); var optionsBG = gameRoot.GetWidget("INGAME_OPTIONS_BG");
r.GetWidget("INGAME_OPTIONS_BUTTON").OnMouseUp = mi => { r.GetWidget("INGAME_OPTIONS_BUTTON").OnMouseUp = mi => {
@@ -51,38 +57,24 @@ namespace OpenRA.Mods.RA.Widgets.Logic
optionsBG.Visible = false; optionsBG.Visible = false;
return true; return true;
}; };
/*
optionsBG.GetWidget("SURRENDER").OnMouseUp = mi =>
{
world.IssueOrder(new Order("Surrender", world.LocalPlayer.PlayerActor));
return true;
};
*/
optionsBG.GetWidget("SURRENDER").IsVisible = () => false; optionsBG.GetWidget("SURRENDER").IsVisible = () => false;
optionsBG.GetWidget("QUIT").OnMouseUp = mi => { optionsBG.GetWidget("QUIT").OnMouseUp = mi => {
Game.Exit(); Game.Exit();
return true; return true;
}; };
}
Game.AddChatLine += gameRoot.GetWidget<ChatDisplayWidget>("CHAT_DISPLAY").AddLine; public void UnregisterEvents()
/*
var postgameBG = gameRoot.GetWidget("POSTGAME_BG");
var postgameText = postgameBG.GetWidget<LabelWidget>("TEXT");
postgameBG.IsVisible = () =>
{ {
return world.LocalPlayer != null && world.LocalPlayer.WinState != WinState.Undefined; Game.AddChatLine -= AddChatLine;
}; Game.BeforeGameStart -= UnregisterEvents;
}
postgameText.GetText = () => void AddChatLine(Color c, string from, string text)
{ {
if (world.LocalPlayer == null) gameRoot.GetWidget<ChatDisplayWidget>("CHAT_DISPLAY").AddLine(c, from, text);
return "";
var state = world.LocalPlayer.WinState;
return (state == WinState.Undefined)? "" :
((state == WinState.Lost)? "YOU ARE DEFEATED" : "YOU ARE VICTORIOUS");
};*/
} }
} }
} }

View File

@@ -22,7 +22,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic
{ {
public class LobbyLogic public class LobbyLogic
{ {
Widget LocalPlayerTemplate, RemotePlayerTemplate, EmptySlotTemplate, EmptySlotTemplateHost; Widget lobby, LocalPlayerTemplate, RemotePlayerTemplate, EmptySlotTemplate, EmptySlotTemplateHost;
ScrollPanelWidget Players; ScrollPanelWidget Players;
Dictionary<string, string> CountryNames; Dictionary<string, string> CountryNames;
string MapUid; string MapUid;
@@ -37,8 +37,10 @@ namespace OpenRA.Mods.RA.Widgets.Logic
{ {
this.orderManager = orderManager; this.orderManager = orderManager;
this.worldRenderer = worldRenderer; this.worldRenderer = worldRenderer;
this.lobby = lobby;
Game.BeforeGameStart += CloseWindow;
Game.LobbyInfoChanged += UpdateCurrentMap; Game.LobbyInfoChanged += UpdateCurrentMap;
Game.LobbyInfoChanged += UpdatePlayerList;
UpdateCurrentMap(); UpdateCurrentMap();
CurrentColorPreview = Game.Settings.Player.ColorRamp; CurrentColorPreview = Game.Settings.Player.ColorRamp;
@@ -101,9 +103,9 @@ namespace OpenRA.Mods.RA.Widgets.Logic
var disconnectButton = lobby.GetWidget("DISCONNECT_BUTTON"); var disconnectButton = lobby.GetWidget("DISCONNECT_BUTTON");
disconnectButton.OnMouseUp = mi => disconnectButton.OnMouseUp = mi =>
{ {
CloseWindow();
Game.Disconnect(); Game.Disconnect();
Game.LoadShellMap(); Game.LoadShellMap();
Widget.CloseWindow();
Widget.OpenWindow("MAINMENU_BG"); Widget.OpenWindow("MAINMENU_BG");
return true; return true;
}; };
@@ -137,9 +139,6 @@ namespace OpenRA.Mods.RA.Widgets.Logic
// Todo: Only show if the map requirements are met for player slots // Todo: Only show if the map requirements are met for player slots
startGameButton.IsVisible = () => Game.IsHost; startGameButton.IsVisible = () => Game.IsHost;
Game.LobbyInfoChanged += UpdatePlayerList;
Game.AddChatLine += lobby.GetWidget<ChatDisplayWidget>("CHAT_DISPLAY").AddLine;
bool teamChat = false; bool teamChat = false;
var chatLabel = lobby.GetWidget<LabelWidget>("LABEL_CHATTYPE"); var chatLabel = lobby.GetWidget<LabelWidget>("LABEL_CHATTYPE");
@@ -161,6 +160,23 @@ namespace OpenRA.Mods.RA.Widgets.Logic
chatLabel.Text = (teamChat) ? "Team:" : "Chat:"; chatLabel.Text = (teamChat) ? "Team:" : "Chat:";
return true; 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) void UpdatePlayerColor(float hf, float sf, float lf, float r)