Reimplement connecting / connection failed dialogs.

This commit is contained in:
Paul Chote
2011-05-08 16:12:24 +12:00
parent a607a60b8f
commit 5c59f7703d
10 changed files with 270 additions and 147 deletions

View File

@@ -77,6 +77,7 @@
<Compile Include="Widgets\CncReplayBrowserLogic.cs" />
<Compile Include="Widgets\CncServerCreationLogic.cs" />
<Compile Include="Widgets\CncMapChooserLogic.cs" />
<Compile Include="Widgets\CncConnectionLogic.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj">

View File

@@ -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<string, object>()
{
{ "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<CncMenuButtonWidget>("ABORT_BUTTON").OnClick = onAbort;
//widget.GetWidget<LabelWidget>("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<string, object>()
{
{ "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<CncMenuButtonWidget>("ABORT_BUTTON").OnClick = onAbort;
panel.GetWidget<CncMenuButtonWidget>("RETRY_BUTTON").OnClick = onRetry;
//widget.GetWidget<LabelWidget>("CONNECTING_DESC").GetText = () =>
// "Connecting to {0}:{1}...".F(host, port);
}
}}

View File

@@ -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<string, object> { { "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();

View File

@@ -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<CncMenuButtonWidget>("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<TextFieldWidget>("IP");
var port = panel.GetWidget<TextFieldWidget>("PORT");
var ipField = panel.GetWidget<TextFieldWidget>("IP");
var portField = panel.GetWidget<TextFieldWidget>("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<CncMenuButtonWidget>("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<CncMenuButtonWidget>("BACK_BUTTON").OnClick = onExit;

View File

@@ -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<CncMenuButtonWidget>("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);
}
}
}