merge cnc and ra lobby, fixes #1267, #1224, #106, others

This commit is contained in:
Chris Forbes
2011-11-06 11:50:58 +13:00
parent 4576eaadd6
commit 772734d032
44 changed files with 759 additions and 1432 deletions

View File

@@ -1,20 +1,10 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
* 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

View File

@@ -1,94 +0,0 @@
#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 System;
using OpenRA.Network;
using OpenRA.Widgets;
namespace OpenRA.Mods.Cnc.Widgets.Logic
{
public class CncConnectionLogic
{
Action onConnect, onRetry, onAbort;
string host;
int port;
void ConnectionStateChanged(OrderManager om)
{
if (om.Connection.ConnectionState == ConnectionState.Connected)
{
CloseWindow();
onConnect();
}
else if (om.Connection.ConnectionState == ConnectionState.NotConnected)
{
// Show connection failed dialog
CloseWindow();
Widget.OpenWindow("CONNECTIONFAILED_PANEL", new WidgetArgs()
{
{ "onAbort", onAbort },
{ "onRetry", onRetry },
{ "host", host },
{ "port", port }
});
}
}
void CloseWindow()
{
Game.ConnectionStateChanged -= ConnectionStateChanged;
Widget.CloseWindow();
}
[ObjectCreator.UseCtor]
public CncConnectionLogic(Widget widget, string host, int port, Action onConnect, Action onRetry, Action onAbort)
{
this.host = host;
this.port = port;
this.onConnect = onConnect;
this.onRetry = onRetry;
this.onAbort = onAbort;
Game.ConnectionStateChanged += ConnectionStateChanged;
var panel = widget.GetWidget("CONNECTING_PANEL");
panel.GetWidget<ButtonWidget>("ABORT_BUTTON").OnClick = () => { CloseWindow(); 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 WidgetArgs()
{
{ "host", host },
{ "port", port },
{ "onConnect", onConnect },
{ "onAbort", onAbort },
{ "onRetry", () => Connect(host, port, onConnect, onAbort) }
});
}
}
public class CncConnectionFailedLogic
{
[ObjectCreator.UseCtor]
public CncConnectionFailedLogic(Widget widget, string host, int port, Action onRetry, Action onAbort)
{
var panel = widget.GetWidget("CONNECTIONFAILED_PANEL");
panel.GetWidget<ButtonWidget>("ABORT_BUTTON").OnClick = () => { Widget.CloseWindow(); onAbort(); };
panel.GetWidget<ButtonWidget>("RETRY_BUTTON").OnClick = () => { Widget.CloseWindow(); onRetry(); };
widget.GetWidget<LabelWidget>("CONNECTING_DESC").GetText = () =>
"Could not connect to {0}:{1}".F(host, port);
}
}}

View File

@@ -1,43 +0,0 @@
#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 System;
using OpenRA.Widgets;
namespace OpenRA.Mods.Cnc.Widgets.Logic
{
public class CncDirectConnectLogic
{
[ObjectCreator.UseCtor]
public CncDirectConnectLogic(Widget widget, Action onExit, Action openLobby)
{
var panel = widget.GetWidget("DIRECTCONNECT_PANEL");
var ipField = panel.GetWidget<TextFieldWidget>("IP");
var portField = panel.GetWidget<TextFieldWidget>("PORT");
var last = Game.Settings.Player.LastServer.Split(':');
ipField.Text = last.Length > 1 ? last[0] : "localhost";
portField.Text = last.Length > 2 ? last[1] : "1234";
panel.GetWidget<ButtonWidget>("JOIN_BUTTON").OnClick = () =>
{
var port = Exts.WithDefault(1234, () => int.Parse(portField.Text));
Game.Settings.Player.LastServer = "{0}:{1}".F(ipField.Text, port);
Game.Settings.Save();
Widget.CloseWindow();
CncConnectionLogic.Connect(ipField.Text, port, openLobby, onExit);
};
panel.GetWidget<ButtonWidget>("BACK_BUTTON").OnClick = () => { Widget.CloseWindow(); onExit(); };
}
}
}

View File

@@ -102,7 +102,7 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
retryButton.IsDisabled = () => false;
}));
var t = new Thread( _ =>
new Thread( _ =>
{
try
{
@@ -122,8 +122,7 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
{
onError("Installation failed");
}
}) { IsBackground = true };
t.Start();
}) { IsBackground = true }.Start();
}
}
}

View File

@@ -1,446 +0,0 @@
#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 System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using OpenRA.Mods.RA;
using OpenRA.Mods.RA.Widgets.Logic;
using OpenRA.Network;
using OpenRA.Traits;
using OpenRA.Widgets;
namespace OpenRA.Mods.Cnc.Widgets.Logic
{
public class CncLobbyLogic
{
Widget EditablePlayerTemplate, NonEditablePlayerTemplate, EmptySlotTemplate,
EditableSpectatorTemplate, NonEditableSpectatorTemplate, NewSpectatorTemplate;
ScrollPanelWidget chatPanel;
Widget chatTemplate;
ScrollPanelWidget Players;
Dictionary<string, string> CountryNames;
string MapUid;
Map Map;
ColorPickerPaletteModifier PlayerPalettePreview;
readonly Action OnGameStart;
readonly Action onExit;
readonly OrderManager orderManager;
// Listen for connection failures
void ConnectionStateChanged(OrderManager om)
{
if (om.Connection.ConnectionState == ConnectionState.NotConnected)
{
// Show connection failed dialog
CloseWindow();
Action onConnect = () =>
{
Game.OpenWindow("SERVER_LOBBY", new WidgetArgs()
{
{ "onExit", onExit },
{ "onStart", OnGameStart },
{ "addBots", false }
});
};
Action onRetry = () =>
{
CloseWindow();
CncConnectionLogic.Connect(om.Host, om.Port, onConnect, onExit);
};
Widget.OpenWindow("CONNECTIONFAILED_PANEL", new WidgetArgs()
{
{ "onAbort", onExit },
{ "onRetry", onRetry },
{ "host", om.Host },
{ "port", om.Port }
});
}
}
void CloseWindow()
{
Game.LobbyInfoChanged -= UpdateCurrentMap;
Game.LobbyInfoChanged -= UpdatePlayerList;
Game.BeforeGameStart -= OnGameStart;
Game.AddChatLine -= AddChatLine;
Game.ConnectionStateChanged -= ConnectionStateChanged;
Widget.CloseWindow();
}
[ObjectCreator.UseCtor]
internal CncLobbyLogic(Widget widget, World world, OrderManager orderManager,
Action onExit, Action onStart, bool addBots)
{
var lobby = widget;
this.orderManager = orderManager;
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<ColorPickerPaletteModifier>();
PlayerPalettePreview.Ramp = Game.Settings.Player.ColorRamp;
Players = lobby.GetWidget<ScrollPanelWidget>("PLAYERS");
EditablePlayerTemplate = Players.GetWidget("TEMPLATE_EDITABLE_PLAYER");
NonEditablePlayerTemplate = Players.GetWidget("TEMPLATE_NONEDITABLE_PLAYER");
EmptySlotTemplate = Players.GetWidget("TEMPLATE_EMPTY");
EditableSpectatorTemplate = Players.GetWidget("TEMPLATE_EDITABLE_SPECTATOR");
NonEditableSpectatorTemplate = Players.GetWidget("TEMPLATE_NONEDITABLE_SPECTATOR");
NewSpectatorTemplate = Players.GetWidget("TEMPLATE_NEW_SPECTATOR");
var mapPreview = lobby.GetWidget<MapPreviewWidget>("MAP_PREVIEW");
mapPreview.IsVisible = () => Map != null;
mapPreview.Map = () => Map;
mapPreview.OnMouseDown = mi => LobbyUtils.SelectSpawnPoint( orderManager, mapPreview, Map, mi );
var mapTitle = lobby.GetWidget<LabelWidget>("MAP_TITLE");
mapTitle.IsVisible = () => Map != null;
mapTitle.GetText = () => Map.Title;
mapPreview.SpawnColors = () => LobbyUtils.GetSpawnColors( orderManager, Map );
CountryNames = Rules.Info["world"].Traits.WithInterface<CountryInfo>()
.Where(c => c.Selectable)
.ToDictionary(a => a.Race, a => a.Name);
CountryNames.Add("random", "Any");
var mapButton = lobby.GetWidget<ButtonWidget>("CHANGEMAP_BUTTON");
mapButton.OnClick = () =>
{
var onSelect = new Action<Map>(m =>
{
orderManager.IssueOrder(Order.Command("map " + m.Uid));
Game.Settings.Server.Map = m.Uid;
Game.Settings.Save();
});
Widget.OpenWindow("MAPCHOOSER_PANEL", new WidgetArgs()
{
{ "initialMap", Map.Uid },
{ "onExit", () => {} },
{ "onSelect", onSelect }
});
};
mapButton.IsVisible = () => mapButton.Visible && Game.IsHost;
var disconnectButton = lobby.GetWidget<ButtonWidget>("DISCONNECT_BUTTON");
disconnectButton.OnClick = () => { CloseWindow(); onExit(); };
var gameStarting = false;
var allowCheats = lobby.GetWidget<CheckboxWidget>("ALLOWCHEATS_CHECKBOX");
allowCheats.IsChecked = () => orderManager.LobbyInfo.GlobalSettings.AllowCheats;
allowCheats.IsDisabled = () => !Game.IsHost || gameStarting || orderManager.LocalClient == null
|| orderManager.LocalClient.IsReady;
allowCheats.OnClick = () => orderManager.IssueOrder(Order.Command(
"allowcheats {0}".F(!orderManager.LobbyInfo.GlobalSettings.AllowCheats)));
var startGameButton = lobby.GetWidget<ButtonWidget>("START_GAME_BUTTON");
startGameButton.IsVisible = () => Game.IsHost;
startGameButton.IsDisabled = () => gameStarting;
startGameButton.OnClick = () =>
{
gameStarting = true;
orderManager.IssueOrder(Order.Command("startgame"));
};
bool teamChat = false;
var chatLabel = lobby.GetWidget<LabelWidget>("LABEL_CHATTYPE");
var chatTextField = lobby.GetWidget<TextFieldWidget>("CHAT_TEXTFIELD");
chatTextField.OnEnterKey = () =>
{
if (chatTextField.Text.Length == 0)
return true;
orderManager.IssueOrder(Order.Chat(teamChat, chatTextField.Text));
chatTextField.Text = "";
return true;
};
chatTextField.OnTabKey = () =>
{
teamChat ^= true;
chatLabel.Text = (teamChat) ? "Team:" : "Chat:";
return true;
};
chatPanel = lobby.GetWidget<ScrollPanelWidget>("CHAT_DISPLAY");
chatTemplate = chatPanel.GetWidget("CHAT_TEMPLATE");
chatPanel.RemoveChildren();
var musicButton = lobby.GetWidget<ButtonWidget>("MUSIC_BUTTON");
if (musicButton != null)
musicButton.OnClick = () => Widget.OpenWindow("MUSIC_PANEL", new WidgetArgs
{ { "onExit", () => {} } });
// Add a bot on the first lobbyinfo update
if (addBots)
Game.LobbyInfoChanged += WidgetUtils.Once(() =>
{
var slot = orderManager.LobbyInfo.FirstEmptySlot();
var bot = Rules.Info["player"].Traits.WithInterface<IBotInfo>().Select(t => t.Name).FirstOrDefault();
if (slot != null && bot != null)
orderManager.IssueOrder(Order.Command("slot_bot {0} {1}".F(slot, bot)));
});
}
void AddChatLine(Color c, string from, string text)
{
var template = chatTemplate.Clone();
var nameLabel = template.GetWidget<LabelWidget>("NAME");
var timeLabel = template.GetWidget<LabelWidget>("TIME");
var textLabel = template.GetWidget<LabelWidget>("TEXT");
var name = from + ":";
var font = Game.Renderer.Fonts[nameLabel.Font];
var nameSize = font.Measure(from);
var time = DateTime.Now;
timeLabel.GetText = () => "{0:D2}:{1:D2}".F(time.Hour, time.Minute);
nameLabel.GetColor = () => c;
nameLabel.GetText = () => name;
nameLabel.Bounds.Width = nameSize.X;
textLabel.Bounds.X += nameSize.X;
textLabel.Bounds.Width -= nameSize.X;
// Hack around our hacky wordwrap behavior: need to resize the widget to fit the text
text = WidgetUtils.WrapText(text, textLabel.Bounds.Width, font);
textLabel.GetText = () => text;
var dh = font.Measure(text).Y - textLabel.Bounds.Height;
if (dh > 0)
{
textLabel.Bounds.Height += dh;
template.Bounds.Height += dh;
}
chatPanel.AddChild(template);
chatPanel.ScrollToBottom();
Sound.Play("scold1.aud");
}
void UpdateCurrentMap()
{
if (MapUid == orderManager.LobbyInfo.GlobalSettings.Map) return;
MapUid = orderManager.LobbyInfo.GlobalSettings.Map;
Map = new Map(Game.modData.AvailableMaps[MapUid].Path);
var title = Widget.RootWidget.GetWidget<LabelWidget>("TITLE");
title.Text = orderManager.LobbyInfo.GlobalSettings.ServerName;
}
void UpdatePlayerList()
{
// This causes problems for people who are in the process of editing their names (the widgets vanish from beneath them)
// Todo: handle this nicer
Players.RemoveChildren();
foreach (var kv in orderManager.LobbyInfo.Slots)
{
var key = kv.Key;
var slot = kv.Value;
var client = orderManager.LobbyInfo.ClientInSlot(key);
Widget template;
// Empty slot
if (client == null)
{
template = EmptySlotTemplate.Clone();
Func<string> getText = () => slot.Closed ? "Closed" : "Open";
var ready = orderManager.LocalClient.IsReady;
if (Game.IsHost)
{
var name = template.GetWidget<DropDownButtonWidget>("NAME_HOST");
name.IsVisible = () => true;
name.IsDisabled = () => ready;
name.GetText = getText;
name.OnMouseDown = _ => LobbyUtils.ShowSlotDropDown(name, slot, client, orderManager);
}
else
{
var name = template.GetWidget<LabelWidget>("NAME");
name.IsVisible = () => true;
name.GetText = getText;
}
var join = template.GetWidget<ButtonWidget>("JOIN");
join.IsVisible = () => !slot.Closed;
join.IsDisabled = () => ready;
join.OnClick = () => orderManager.IssueOrder(Order.Command("slot " + key));
}
// Editable player in slot
else if ((client.Index == orderManager.LocalClient.Index) ||
(client.Bot != null && Game.IsHost))
{
template = EditablePlayerTemplate.Clone();
var botReady = client.Bot != null && Game.IsHost && orderManager.LocalClient.IsReady;
var ready = botReady || client.IsReady;
if (client.Bot != null)
{
var name = template.GetWidget<DropDownButtonWidget>("BOT_DROPDOWN");
name.IsVisible = () => true;
name.IsDisabled = () => ready;
name.GetText = () => client.Name;
name.OnMouseDown = _ => LobbyUtils.ShowSlotDropDown(name, slot, client, orderManager);
}
else
{
var name = template.GetWidget<TextFieldWidget>("NAME");
name.IsVisible = () => true;
name.IsDisabled = () => ready;
LobbyUtils.SetupNameWidget(orderManager, client, name);
}
var color = template.GetWidget<DropDownButtonWidget>("COLOR");
color.IsDisabled = () => slot.LockColor || ready;
color.OnMouseDown = _ => LobbyUtils.ShowColorDropDown(color, client, orderManager, PlayerPalettePreview);
var colorBlock = color.GetWidget<ColorBlockWidget>("COLORBLOCK");
colorBlock.GetColor = () => client.ColorRamp.GetColor(0);
var faction = template.GetWidget<DropDownButtonWidget>("FACTION");
faction.IsDisabled = () => slot.LockRace || ready;
faction.OnMouseDown = _ => LobbyUtils.ShowRaceDropDown(faction, client, orderManager, CountryNames);
var factionname = faction.GetWidget<LabelWidget>("FACTIONNAME");
factionname.GetText = () => CountryNames[client.Country];
var factionflag = faction.GetWidget<ImageWidget>("FACTIONFLAG");
factionflag.GetImageName = () => client.Country;
factionflag.GetImageCollection = () => "flags";
var team = template.GetWidget<DropDownButtonWidget>("TEAM");
team.IsDisabled = () => slot.LockTeam || ready;
team.OnMouseDown = _ => LobbyUtils.ShowTeamDropDown(team, client, orderManager, Map);
team.GetText = () => (client.Team == 0) ? "-" : client.Team.ToString();
if (client.Bot == null)
{
// local player
var status = template.GetWidget<CheckboxWidget>("STATUS_CHECKBOX");
status.IsChecked = () => ready;
status.IsVisible = () => true;
status.OnClick += CycleReady;
}
else // Bot
template.GetWidget<ImageWidget>("STATUS_IMAGE").IsVisible = () => true;
}
else
{ // Non-editable player in slot
template = NonEditablePlayerTemplate.Clone();
template.GetWidget<LabelWidget>("NAME").GetText = () => client.Name;
var color = template.GetWidget<ColorBlockWidget>("COLOR");
color.GetColor = () => client.ColorRamp.GetColor(0);
var faction = template.GetWidget<LabelWidget>("FACTION");
var factionname = faction.GetWidget<LabelWidget>("FACTIONNAME");
factionname.GetText = () => CountryNames[client.Country];
var factionflag = faction.GetWidget<ImageWidget>("FACTIONFLAG");
factionflag.GetImageName = () => client.Country;
factionflag.GetImageCollection = () => "flags";
var team = template.GetWidget<LabelWidget>("TEAM");
team.GetText = () => (client.Team == 0) ? "-" : client.Team.ToString();
template.GetWidget<ImageWidget>("STATUS_IMAGE").IsVisible = () =>
client.Bot != null || client.IsReady;
var kickButton = template.GetWidget<ButtonWidget>("KICK");
kickButton.IsVisible = () => Game.IsHost && client.Index != orderManager.LocalClient.Index;
kickButton.IsDisabled = () => orderManager.LocalClient.IsReady;
kickButton.OnClick = () => orderManager.IssueOrder(Order.Command("kick " + client.Index));
}
template.IsVisible = () => true;
Players.AddChild(template);
}
// Add spectators
foreach (var client in orderManager.LobbyInfo.Clients.Where(client => client.Slot == null))
{
Widget template;
var c = client;
var ready = c.IsReady;
// Editable spectator
if (c.Index == orderManager.LocalClient.Index)
{
template = EditableSpectatorTemplate.Clone();
var name = template.GetWidget<TextFieldWidget>("NAME");
name.IsDisabled = () => ready;
LobbyUtils.SetupNameWidget(orderManager, c, name);
var color = template.GetWidget<DropDownButtonWidget>("COLOR");
color.IsDisabled = () => ready;
color.OnMouseDown = _ => LobbyUtils.ShowColorDropDown(color, c, orderManager, PlayerPalettePreview);
var colorBlock = color.GetWidget<ColorBlockWidget>("COLORBLOCK");
colorBlock.GetColor = () => c.ColorRamp.GetColor(0);
var status = template.GetWidget<CheckboxWidget>("STATUS_CHECKBOX");
status.IsChecked = () => ready;
status.OnClick += CycleReady;
}
// Non-editable spectator
else
{
template = NonEditableSpectatorTemplate.Clone();
template.GetWidget<LabelWidget>("NAME").GetText = () => c.Name;
var color = template.GetWidget<ColorBlockWidget>("COLOR");
color.GetColor = () => c.ColorRamp.GetColor(0);
template.GetWidget<ImageWidget>("STATUS_IMAGE").IsVisible = () => c.Bot != null || c.IsReady;
var kickButton = template.GetWidget<ButtonWidget>("KICK");
kickButton.IsVisible = () => Game.IsHost && c.Index != orderManager.LocalClient.Index;
kickButton.IsDisabled = () => orderManager.LocalClient.IsReady;
kickButton.OnClick = () => orderManager.IssueOrder(Order.Command("kick " + c.Index));
}
template.IsVisible = () => true;
Players.AddChild(template);
}
// Spectate button
if (orderManager.LocalClient.Slot != null)
{
var spec = NewSpectatorTemplate.Clone();
var btn = spec.GetWidget<ButtonWidget>("SPECTATE");
btn.OnClick = () => orderManager.IssueOrder(Order.Command("spectate"));
btn.IsDisabled = () => orderManager.LocalClient.IsReady;
spec.IsVisible = () => true;
Players.AddChild(spec);
}
}
bool SpawnPointAvailable(int index) { return (index == 0) || orderManager.LobbyInfo.Clients.All(c => c.SpawnPoint != index); }
void CycleReady()
{
orderManager.IssueOrder(Order.Command("ready"));
}
}
}

View File

@@ -9,6 +9,7 @@
#endregion
using System.Net;
using OpenRA.Mods.RA.Widgets.Logic;
using OpenRA.Widgets;
namespace OpenRA.Mods.Cnc.Widgets.Logic
@@ -54,35 +55,9 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
multiplayerMenu.IsVisible = () => Menu == MenuType.Multiplayer;
multiplayerMenu.GetWidget<ButtonWidget>("BACK_BUTTON").OnClick = () => Menu = MenuType.Main;
multiplayerMenu.GetWidget<ButtonWidget>("JOIN_BUTTON").OnClick = () =>
{
Menu = MenuType.None;
Widget.OpenWindow("SERVERBROWSER_PANEL", new WidgetArgs()
{
{ "onExit", () => Menu = MenuType.Multiplayer },
{ "openLobby", () => OpenLobbyPanel(MenuType.Multiplayer, false) }
});
};
multiplayerMenu.GetWidget<ButtonWidget>("CREATE_BUTTON").OnClick = () =>
{
Menu = MenuType.None;
Widget.OpenWindow("CREATESERVER_PANEL", new WidgetArgs()
{
{ "onExit", () => Menu = MenuType.Multiplayer },
{ "openLobby", () => OpenLobbyPanel(MenuType.Multiplayer, false) }
});
};
multiplayerMenu.GetWidget<ButtonWidget>("DIRECTCONNECT_BUTTON").OnClick = () =>
{
Menu = MenuType.None;
Widget.OpenWindow("DIRECTCONNECT_PANEL", new WidgetArgs()
{
{ "onExit", () => Menu = MenuType.Multiplayer },
{ "openLobby", () => OpenLobbyPanel(MenuType.Multiplayer, false) }
});
};
multiplayerMenu.GetWidget<ButtonWidget>("JOIN_BUTTON").OnClick = () => OpenGamePanel("SERVERBROWSER_PANEL");
multiplayerMenu.GetWidget<ButtonWidget>("CREATE_BUTTON").OnClick = () => OpenGamePanel("CREATESERVER_PANEL");
multiplayerMenu.GetWidget<ButtonWidget>("DIRECTCONNECT_BUTTON").OnClick = () => OpenGamePanel("DIRECTCONNECT_PANEL");
// Settings menu
var settingsMenu = widget.GetWidget("SETTINGS_MENU");
@@ -121,6 +96,16 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
rootMenu.GetWidget<ImageWidget>("RECBLOCK").IsVisible = () => world.FrameNumber / 25 % 2 == 0;
}
void OpenGamePanel(string id)
{
Menu = MenuType.None;
Widget.OpenWindow(id, new WidgetArgs()
{
{ "onExit", () => Menu = MenuType.Multiplayer },
{ "openLobby", () => OpenLobbyPanel(MenuType.Multiplayer, false) }
});
}
void RemoveShellmapUI()
{
@@ -142,10 +127,10 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
{
var map = WidgetUtils.ChooseInitialMap(Game.Settings.Server.Map);
CncConnectionLogic.Connect(IPAddress.Loopback.ToString(),
Game.CreateLocalServer(map),
() => OpenLobbyPanel(MenuType.Main, true),
() => { Game.CloseServer(); Menu = MenuType.Main; });
ConnectionLogic.Connect(IPAddress.Loopback.ToString(),
Game.CreateLocalServer(map),
() => OpenLobbyPanel(MenuType.Main, true),
() => { Game.CloseServer(); Menu = MenuType.Main; });
}
}
}

View File

@@ -1,60 +0,0 @@
#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 System;
using System.Linq;
using OpenRA.FileFormats;
using OpenRA.Widgets;
namespace OpenRA.Mods.Cnc.Widgets.Logic
{
public class CncModBrowserLogic
{
Mod currentMod;
[ObjectCreator.UseCtor]
public CncModBrowserLogic(Widget widget, Action onSwitch, Action onExit)
{
var panel = widget.GetWidget("MODS_PANEL");
var modList = panel.GetWidget<ScrollPanelWidget>("MOD_LIST");
var loadButton = panel.GetWidget<ButtonWidget>("LOAD_BUTTON");
loadButton.OnClick = () => LoadMod(currentMod.Id, onSwitch);
loadButton.IsDisabled = () => currentMod.Id == Game.CurrentMods.Keys.First();
panel.GetWidget<ButtonWidget>("BACK_BUTTON").OnClick = () => { Widget.CloseWindow(); onExit(); };
currentMod = Mod.AllMods[Game.modData.Manifest.Mods[0]];
// Mod list
var modTemplate = modList.GetWidget<ScrollItemWidget>("MOD_TEMPLATE");
foreach (var m in Mod.AllMods)
{
var mod = m.Value;
var item = ScrollItemWidget.Setup(modTemplate, () => currentMod == mod, () => currentMod = mod);
item.GetWidget<LabelWidget>("TITLE").GetText = () => mod.Title;
item.GetWidget<LabelWidget>("VERSION").GetText = () => mod.Version;
item.GetWidget<LabelWidget>("AUTHOR").GetText = () => mod.Author;
modList.AddChild(item);
}
}
void LoadMod(string mod, Action onSwitch)
{
var mods = Mod.AllMods[mod].WithPrerequisites();
Game.RunAfterTick(() =>
{
Widget.CloseWindow();
onSwitch();
Game.InitializeWithMods(mods.ToArray());
});
}
}
}

View File

@@ -11,7 +11,6 @@
using System;
using System.IO;
using System.Linq;
using System.Threading;
using OpenRA.FileFormats;
using OpenRA.GameRules;
using OpenRA.Traits;
@@ -129,7 +128,6 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
}
}
void Play()
{
if (currentSong == null)

View File

@@ -1,101 +0,0 @@
#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 System;
using System.IO;
using System.Linq;
using OpenRA.Network;
using OpenRA.Widgets;
namespace OpenRA.Mods.Cnc.Widgets.Logic
{
public class CncReplayBrowserLogic
{
Widget panel;
[ObjectCreator.UseCtor]
public CncReplayBrowserLogic(Widget widget, Action onExit, Action onStart)
{
panel = widget.GetWidget("REPLAYBROWSER_PANEL");
panel.GetWidget<ButtonWidget>("CANCEL_BUTTON").OnClick = () => { Widget.CloseWindow(); onExit(); };
var rl = panel.GetWidget<ScrollPanelWidget>("REPLAY_LIST");
var replayDir = Path.Combine(Platform.SupportDir, "Replays");
var template = panel.GetWidget<ScrollItemWidget>("REPLAY_TEMPLATE");
rl.RemoveChildren();
if (Directory.Exists(replayDir))
{
var files = Directory.GetFiles(replayDir, "*.rep").Reverse();
foreach (var replayFile in files)
AddReplay(rl, replayFile, template);
SelectReplay(files.FirstOrDefault());
}
var watch = panel.GetWidget<ButtonWidget>("WATCH_BUTTON");
watch.IsDisabled = () => currentReplay == null || currentMap == null || currentReplay.Duration == 0;
watch.OnClick = () =>
{
if (currentReplay != null)
{
Game.JoinReplay(currentReplay.Filename);
Widget.CloseWindow();
onStart();
}
};
panel.GetWidget("REPLAY_INFO").IsVisible = () => currentReplay != null;
}
Replay currentReplay;
Map currentMap;
void SelectReplay(string filename)
{
if (filename == null)
return;
try
{
currentReplay = new Replay(filename);
currentMap = currentReplay.Map();
panel.GetWidget<LabelWidget>("DURATION").GetText =
() => WidgetUtils.FormatTime(currentReplay.Duration * 3 /* todo: 3:1 ratio isnt always true. */);
panel.GetWidget<MapPreviewWidget>("MAP_PREVIEW").Map = () => currentMap;
panel.GetWidget<LabelWidget>("MAP_TITLE").GetText =
() => currentMap != null ? currentMap.Title : "(Unknown Map)";
var players = currentReplay.LobbyInfo.Slots
.Count(s => currentReplay.LobbyInfo.ClientInSlot(s.Key) != null);
panel.GetWidget<LabelWidget>("PLAYERS").GetText = () => players.ToString();
}
catch (Exception e)
{
Log.Write("debug", "Exception while parsing replay: {0}", e);
currentReplay = null;
currentMap = null;
}
}
void AddReplay(ScrollPanelWidget list, string filename, ScrollItemWidget template)
{
var item = ScrollItemWidget.Setup(template,
() => currentReplay != null && currentReplay.Filename == filename,
() => SelectReplay(filename));
var f = Path.GetFileName(filename);
item.GetWidget<LabelWidget>("TITLE").GetText = () => f;
list.AddChild(item);
}
}
}

View File

@@ -1,158 +0,0 @@
#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 System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Mods.RA.Widgets.Logic;
using OpenRA.Network;
using OpenRA.Widgets;
namespace OpenRA.Mods.Cnc.Widgets.Logic
{
public class CncServerBrowserLogic
{
GameServer currentServer;
ScrollItemWidget serverTemplate;
enum SearchStatus { Fetching, Failed, NoGames, Hidden }
SearchStatus searchStatus = SearchStatus.Fetching;
public string ProgressLabelText()
{
switch (searchStatus)
{
case SearchStatus.Fetching:
return "Fetching game list...";
case SearchStatus.Failed:
return "Failed to contact master server.";
case SearchStatus.NoGames:
return "No games found.";
default:
return "";
}
}
[ObjectCreator.UseCtor]
public CncServerBrowserLogic(Widget widget, Action openLobby, Action onExit)
{
var panel = widget.GetWidget("SERVERBROWSER_PANEL");
var sl = panel.GetWidget<ScrollPanelWidget>("SERVER_LIST");
// Menu buttons
var refreshButton = panel.GetWidget<ButtonWidget>("REFRESH_BUTTON");
refreshButton.IsDisabled = () => searchStatus == SearchStatus.Fetching;
refreshButton.OnClick = () =>
{
searchStatus = SearchStatus.Fetching;
sl.RemoveChildren();
currentServer = null;
ServerList.Query(games => RefreshServerList(panel, games));
};
var join = panel.GetWidget<ButtonWidget>("JOIN_BUTTON");
join.IsDisabled = () => currentServer == null || !currentServer.CanJoin();
join.OnClick = () =>
{
if (currentServer == null)
return;
var host = currentServer.Address.Split(':')[0];
var port = int.Parse(currentServer.Address.Split(':')[1]);
Widget.CloseWindow();
CncConnectionLogic.Connect(host, port, openLobby, onExit);
};
panel.GetWidget<ButtonWidget>("BACK_BUTTON").OnClick = () => { Widget.CloseWindow(); onExit(); };
// Server list
serverTemplate = sl.GetWidget<ScrollItemWidget>("SERVER_TEMPLATE");
// Display the progress label over the server list
// The text is only visible when the list is empty
var progressText = panel.GetWidget<LabelWidget>("PROGRESS_LABEL");
progressText.IsVisible = () => searchStatus != SearchStatus.Hidden;
progressText.GetText = ProgressLabelText;
// Map preview
var preview = panel.GetWidget<MapPreviewWidget>("MAP_PREVIEW");
preview.Map = () => CurrentMap();
preview.IsVisible = () => CurrentMap() != null;
// Server info
var infoPanel = panel.GetWidget("SERVER_INFO");
infoPanel.IsVisible = () => currentServer != null;
infoPanel.GetWidget<LabelWidget>("SERVER_IP").GetText = () => currentServer.Address;
infoPanel.GetWidget<LabelWidget>("SERVER_MODS").GetText = () => ServerBrowserLogic.GenerateModsLabel(currentServer);
infoPanel.GetWidget<LabelWidget>("MAP_TITLE").GetText = () => (CurrentMap() != null) ? CurrentMap().Title : "Unknown";
infoPanel.GetWidget<LabelWidget>("MAP_PLAYERS").GetText = () => GetPlayersLabel(currentServer);
ServerList.Query(games => RefreshServerList(panel, games));
}
string GetPlayersLabel(GameServer game)
{
if (game == null)
return "";
var map = Game.modData.FindMapByUid(game.Map);
return map == null ? "{0}".F(currentServer.Players) : "{0} / {1}".F(currentServer.Players, map.PlayerCount);
}
Map CurrentMap()
{
return (currentServer == null) ? null : Game.modData.FindMapByUid(currentServer.Map);
}
public void RefreshServerList(Widget panel, IEnumerable<GameServer> games)
{
var sl = panel.GetWidget<ScrollPanelWidget>("SERVER_LIST");
sl.RemoveChildren();
currentServer = null;
if (games == null)
{
searchStatus = SearchStatus.Failed;
return;
}
var gamesWaiting = games.Where(g => g.CanJoin());
if (gamesWaiting.Count() == 0)
{
searchStatus = SearchStatus.NoGames;
return;
}
searchStatus = SearchStatus.Hidden;
currentServer = gamesWaiting.FirstOrDefault();
foreach (var loop in gamesWaiting)
{
var game = loop;
var item = ScrollItemWidget.Setup(serverTemplate, () => currentServer == game, () => currentServer = game);
item.GetWidget<LabelWidget>("TITLE").GetText = () => game.Name;
// TODO: Use game.MapTitle once the server supports it
item.GetWidget<LabelWidget>("MAP").GetText = () =>
{
var map = Game.modData.FindMapByUid(game.Map);
return map == null ? "Unknown" : map.Title;
};
// TODO: Use game.MaxPlayers once the server supports it
item.GetWidget<LabelWidget>("PLAYERS").GetText = () => GetPlayersLabel(game);
item.GetWidget<LabelWidget>("IP").GetText = () => game.Address;
sl.AddChild(item);
}
}
}
}

View File

@@ -1,96 +0,0 @@
#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 System;
using System.Net;
using OpenRA.GameRules;
using OpenRA.Widgets;
namespace OpenRA.Mods.Cnc.Widgets.Logic
{
public class CncServerCreationLogic
{
Widget panel;
Action onCreate;
Action onExit;
Map map;
bool advertiseOnline;
[ObjectCreator.UseCtor]
public CncServerCreationLogic(Widget widget, Action onExit, Action openLobby)
{
panel = widget.GetWidget("CREATESERVER_PANEL");
onCreate = openLobby;
this.onExit = onExit;
var settings = Game.Settings;
panel.GetWidget<ButtonWidget>("BACK_BUTTON").OnClick = () => { Widget.CloseWindow(); onExit(); };
panel.GetWidget<ButtonWidget>("CREATE_BUTTON").OnClick = CreateAndJoin;
panel.GetWidget<ButtonWidget>("MAP_BUTTON").OnClick = () =>
{
Widget.OpenWindow("MAPCHOOSER_PANEL", new WidgetArgs()
{
{ "initialMap", map.Uid },
{ "onExit", () => {} },
{ "onSelect", (Action<Map>)(m => map = m) }
});
};
map = Game.modData.AvailableMaps[ WidgetUtils.ChooseInitialMap(Game.Settings.Server.Map) ];
panel.GetWidget<MapPreviewWidget>("MAP_PREVIEW").Map = () => map;
panel.GetWidget<LabelWidget>("MAP_NAME").GetText = () => map.Title;
panel.GetWidget<TextFieldWidget>("SERVER_NAME").Text = settings.Server.Name ?? "";
panel.GetWidget<TextFieldWidget>("LISTEN_PORT").Text = settings.Server.ListenPort.ToString();
advertiseOnline = Game.Settings.Server.AdvertiseOnline;
var externalPort = panel.GetWidget<TextFieldWidget>("EXTERNAL_PORT");
externalPort.Text = settings.Server.ExternalPort.ToString();
externalPort.IsDisabled = () => !advertiseOnline;
var advertiseCheckbox = panel.GetWidget<CheckboxWidget>("ADVERTISE_CHECKBOX");
advertiseCheckbox.IsChecked = () => advertiseOnline;
advertiseCheckbox.OnClick = () => advertiseOnline ^= true;
// Disable these until we have some logic behind them
panel.GetWidget<TextFieldWidget>("SERVER_DESC").IsDisabled = () => true;
panel.GetWidget<TextFieldWidget>("SERVER_PASSWORD").IsDisabled = () => true;
}
void CreateAndJoin()
{
var name = panel.GetWidget<TextFieldWidget>("SERVER_NAME").Text;
int listenPort, externalPort;
if (!int.TryParse(panel.GetWidget<TextFieldWidget>("LISTEN_PORT").Text, out listenPort))
listenPort = 1234;
if (!int.TryParse(panel.GetWidget<TextFieldWidget>("EXTERNAL_PORT").Text, out externalPort))
externalPort = 1234;
// Save new settings
Game.Settings.Server.Name = name;
Game.Settings.Server.ListenPort = listenPort;
Game.Settings.Server.ExternalPort = externalPort;
Game.Settings.Server.AdvertiseOnline = advertiseOnline;
Game.Settings.Server.Map = map.Uid;
Game.Settings.Save();
// Take a copy so that subsequent changes don't affect the server
var settings = new ServerSettings(Game.Settings.Server);
// Create and join the server
Game.CreateServer(settings);
Widget.CloseWindow();
CncConnectionLogic.Connect(IPAddress.Loopback.ToString(), Game.Settings.Server.ListenPort, onCreate, onExit);
}
}
}

View File

@@ -29,7 +29,7 @@ namespace OpenRA.Mods.Cnc.Widgets
{
public List<ProductionTab> Tabs = new List<ProductionTab>();
public string Group;
public int CumulativeCount;
public int NextQueueName = 1;
public bool Alert { get { return Tabs.Any(t => t.Queue.CurrentDone); } }
public void Update(IEnumerable<ProductionQueue> allQueues)
@@ -51,7 +51,7 @@ namespace OpenRA.Mods.Cnc.Widgets
foreach (var queue in queues)
tabs.Add(new ProductionTab()
{
Name = (++CumulativeCount).ToString(),
Name = (NextQueueName++).ToString(),
Queue = queue
});
Tabs = tabs;