Merge pull request #10264 from whinis/bleed

Restart button for missions
This commit is contained in:
Oliver Brakmann
2016-01-15 21:55:02 +01:00
11 changed files with 177 additions and 76 deletions

View File

@@ -75,6 +75,7 @@ Also thanks to:
* Jeff Harris (jeff_1amstudios) * Jeff Harris (jeff_1amstudios)
* Jes * Jes
* Joakim Lindberg (booom3) * Joakim Lindberg (booom3)
* John Turner (whinis)
* Joppy Furr * Joppy Furr
* Kanar * Kanar
* Kenny Hoxworth (hoxworth) * Kenny Hoxworth (hoxworth)

View File

@@ -175,6 +175,43 @@ namespace OpenRA
GC.Collect(); GC.Collect();
} }
public static void RestartGame()
{
var replay = OrderManager.Connection as ReplayConnection;
var replayName = replay != null ? replay.Filename : null;
var uid = OrderManager.World.Map.Uid;
var globalSettings = OrderManager.LobbyInfo.GlobalSettings;
// Disconnect from the current game
Disconnect();
Ui.ResetAll();
// Restart the game with the same replay/mission
if (replay != null)
JoinReplay(replayName);
else
StartMission(uid, globalSettings.GameSpeedType, globalSettings.Difficulty);
}
public static void StartMission(string mapUID, string gameSpeed, string difficulty, Action onStart = null)
{
OrderManager om = null;
Action lobbyReady = null;
lobbyReady = () =>
{
LobbyInfoChanged -= lobbyReady;
om.IssueOrder(Order.Command("gamespeed {0}".F(gameSpeed)));
om.IssueOrder(Order.Command("difficulty {0}".F(difficulty)));
om.IssueOrder(Order.Command("state {0}".F(Session.ClientState.Ready)));
if (onStart != null)
onStart();
};
LobbyInfoChanged += lobbyReady;
om = JoinServer(IPAddress.Loopback.ToString(), CreateLocalServer(mapUID), "");
}
public static bool IsHost public static bool IsHost
{ {
get get

View File

@@ -69,6 +69,9 @@ namespace OpenRA.Network
public void Receive(int clientID, byte[] data) public void Receive(int clientID, byte[] data)
{ {
if (disposed) // TODO: This can be removed once NetworkConnection is fixed to dispose properly.
return;
if (preStartBuffer != null && IsGameStart(data)) if (preStartBuffer != null && IsGameStart(data))
{ {
writer.Flush(); writer.Flush();

View File

@@ -15,29 +15,61 @@ namespace OpenRA.Mods.Common.Widgets
{ {
public static class ConfirmationDialogs public static class ConfirmationDialogs
{ {
public static void PromptConfirmAction(string title, string text, Action onConfirm, Action onCancel = null, string confirmText = null, string cancelText = null) public static void PromptConfirmAction(
string title,
string text,
Action onConfirm,
Action onCancel = null,
Action onOther = null,
string confirmText = null,
string cancelText = null,
string otherText = null)
{ {
var prompt = Ui.OpenWindow("CONFIRM_PROMPT"); var prompt = Ui.OpenWindow("CONFIRM_PROMPT");
var confirmButton = prompt.Get<ButtonWidget>("CONFIRM_BUTTON");
var cancelButton = prompt.GetOrNull<ButtonWidget>("CANCEL_BUTTON");
var otherButton = prompt.GetOrNull<ButtonWidget>("OTHER_BUTTON");
prompt.Get<LabelWidget>("PROMPT_TITLE").GetText = () => title; prompt.Get<LabelWidget>("PROMPT_TITLE").GetText = () => title;
prompt.Get<LabelWidget>("PROMPT_TEXT").GetText = () => text; prompt.Get<LabelWidget>("PROMPT_TEXT").GetText = () => text;
if (!string.IsNullOrEmpty(confirmText)) if (!string.IsNullOrEmpty(confirmText))
prompt.Get<ButtonWidget>("CONFIRM_BUTTON").GetText = () => confirmText; confirmButton.GetText = () => confirmText;
if (!string.IsNullOrEmpty(cancelText)) if (!string.IsNullOrEmpty(otherText) && otherButton != null)
prompt.Get<ButtonWidget>("CANCEL_BUTTON").GetText = () => cancelText; otherButton.GetText = () => otherText;
if (!string.IsNullOrEmpty(cancelText) && cancelButton != null)
cancelButton.GetText = () => cancelText;
prompt.Get<ButtonWidget>("CONFIRM_BUTTON").OnClick = () => confirmButton.OnClick = () =>
{ {
Ui.CloseWindow(); Ui.CloseWindow();
onConfirm(); onConfirm();
}; };
prompt.Get<ButtonWidget>("CANCEL_BUTTON").OnClick = () => if (onCancel != null && cancelButton != null)
{
cancelButton.IsVisible = () => true;
cancelButton.OnClick = () =>
{ {
Ui.CloseWindow(); Ui.CloseWindow();
if (onCancel != null) if (onCancel != null)
onCancel(); onCancel();
}; };
} }
else if (cancelButton != null)
cancelButton.IsVisible = () => false;
if (onOther != null && otherButton != null)
{
otherButton.IsVisible = () => true;
otherButton.OnClick = () =>
{
if (onOther != null)
onOther();
};
}
else if (otherButton != null)
otherButton.IsVisible = () => false;
}
public static void CancelPrompt(string title, string text, Action onCancel = null, string cancelText = null) public static void CancelPrompt(string title, string text, Action onCancel = null, string cancelText = null)
{ {

View File

@@ -88,14 +88,42 @@ namespace OpenRA.Mods.Common.Widgets.Logic
abortMissionButton.OnClick = () => abortMissionButton.OnClick = () =>
{ {
if (world.IsGameOver) hideMenu = true;
if (world.LocalPlayer == null || (world.LocalPlayer.WinState != WinState.Won &&
(!world.IsGameOver || world.Map.Visibility == MapVisibility.MissionSelector)))
{ {
onQuit(); Action restartAction = null;
return; if (world.IsReplay || world.Map.Visibility == MapVisibility.MissionSelector)
{
var iop = world.WorldActor.TraitsImplementing<IObjectivesPanel>().FirstOrDefault();
var exitDelay = iop != null ? iop.ExitDelay : 0;
restartAction = () =>
{
Ui.CloseWindow();
if (mpe != null)
{
if (Game.IsCurrentWorld(world))
mpe.Fade(MenuPaletteEffect.EffectType.Black);
exitDelay += 40 * mpe.Info.FadeLength;
} }
hideMenu = true; Game.RunAfterDelay(exitDelay, Game.RestartGame);
ConfirmationDialogs.PromptConfirmAction("Abort Mission", "Leave this game and return to the menu?", onQuit, showMenu); };
}
ConfirmationDialogs.PromptConfirmAction(
title: "Leave Mission",
text: "Leave this game and return to the menu?",
onConfirm: onQuit,
onCancel: showMenu,
confirmText: "Leave",
cancelText: "Stay",
otherText: "Restart",
onOther: restartAction);
}
else
onQuit();
}; };
var exitEditorButton = menu.Get<ButtonWidget>("EXIT_EDITOR"); var exitEditorButton = menu.Get<ButtonWidget>("EXIT_EDITOR");
@@ -103,7 +131,11 @@ namespace OpenRA.Mods.Common.Widgets.Logic
exitEditorButton.OnClick = () => exitEditorButton.OnClick = () =>
{ {
hideMenu = true; hideMenu = true;
ConfirmationDialogs.PromptConfirmAction("Exit Map Editor", "Exit and lose all unsaved changes?", onQuit, showMenu); ConfirmationDialogs.PromptConfirmAction(
title: "Exit Map Editor",
text: "Exit and lose all unsaved changes?",
onConfirm: onQuit,
onCancel: showMenu);
}; };
Action onSurrender = () => Action onSurrender = () =>
@@ -117,7 +149,11 @@ namespace OpenRA.Mods.Common.Widgets.Logic
surrenderButton.OnClick = () => surrenderButton.OnClick = () =>
{ {
hideMenu = true; hideMenu = true;
ConfirmationDialogs.PromptConfirmAction("Surrender", "Are you sure you want to surrender?", onSurrender, showMenu); ConfirmationDialogs.PromptConfirmAction(
title: "Surrender",
text: "Are you sure you want to surrender?",
onConfirm: onSurrender,
onCancel: showMenu);
}; };
var saveMapButton = menu.Get<ButtonWidget>("SAVE_MAP"); var saveMapButton = menu.Get<ButtonWidget>("SAVE_MAP");

View File

@@ -309,31 +309,29 @@ namespace OpenRA.Mods.Common.Widgets.Logic
void DeleteOneMap(string map, Action<string> after) void DeleteOneMap(string map, Action<string> after)
{ {
ConfirmationDialogs.PromptConfirmAction( ConfirmationDialogs.PromptConfirmAction(
"Delete map", title: "Delete map",
"Delete the map '{0}'?".F(Game.ModData.MapCache[map].Title), text: "Delete the map '{0}'?".F(Game.ModData.MapCache[map].Title),
() => onConfirm: () =>
{ {
var newUid = DeleteMap(map); var newUid = DeleteMap(map);
if (after != null) if (after != null)
after(newUid); after(newUid);
}, },
null, confirmText: "Delete");
"Delete");
} }
void DeleteAllMaps(string[] maps, Action<string> after) void DeleteAllMaps(string[] maps, Action<string> after)
{ {
ConfirmationDialogs.PromptConfirmAction( ConfirmationDialogs.PromptConfirmAction(
"Delete maps", title: "Delete maps",
"Delete all maps on this page?", text: "Delete all maps on this page?",
() => onConfirm: () =>
{ {
maps.Do(m => DeleteMap(m)); maps.Do(m => DeleteMap(m));
if (after != null) if (after != null)
after(WidgetUtils.ChooseInitialMap(null)); after(WidgetUtils.ChooseInitialMap(null));
}, },
null, confirmText: "Delete");
"Delete");
} }
} }
} }

View File

@@ -301,29 +301,11 @@ namespace OpenRA.Mods.Common.Widgets.Logic
PlayVideo(fsPlayer, gameStartVideo, PlayingVideo.GameStart, () => PlayVideo(fsPlayer, gameStartVideo, PlayingVideo.GameStart, () =>
{ {
StopVideo(fsPlayer); StopVideo(fsPlayer);
StartMission(); Game.StartMission(selectedMapPreview.Uid, gameSpeed, difficulty, onStart);
}); });
} }
else else
StartMission(); Game.StartMission(selectedMapPreview.Uid, gameSpeed, difficulty, onStart);
}
void StartMission()
{
OrderManager om = null;
Action lobbyReady = null;
lobbyReady = () =>
{
om.IssueOrder(Order.Command("gamespeed {0}".F(gameSpeed)));
om.IssueOrder(Order.Command("difficulty {0}".F(difficulty)));
Game.LobbyInfoChanged -= lobbyReady;
onStart();
om.IssueOrder(Order.Command("state {0}".F(Session.ClientState.Ready)));
};
Game.LobbyInfoChanged += lobbyReady;
om = Game.JoinServer(IPAddress.Loopback.ToString(), Game.CreateLocalServer(selectedMapPreview.Uid), "");
} }
class DropDownOption class DropDownOption

View File

@@ -403,16 +403,15 @@ namespace OpenRA.Mods.Common.Widgets.Logic
Action<ReplayMetadata, Action> onDeleteReplay = (r, after) => Action<ReplayMetadata, Action> onDeleteReplay = (r, after) =>
{ {
ConfirmationDialogs.PromptConfirmAction( ConfirmationDialogs.PromptConfirmAction(
"Delete selected replay?", title: "Delete selected replay?",
"Delete replay '{0}'?".F(Path.GetFileNameWithoutExtension(r.FilePath)), text: "Delete replay '{0}'?".F(Path.GetFileNameWithoutExtension(r.FilePath)),
() => onConfirm: () =>
{ {
DeleteReplay(r); DeleteReplay(r);
if (after != null) if (after != null)
after.Invoke(); after.Invoke();
}, },
null, confirmText: "Delete");
"Delete");
}; };
{ {
@@ -444,16 +443,15 @@ namespace OpenRA.Mods.Common.Widgets.Logic
} }
ConfirmationDialogs.PromptConfirmAction( ConfirmationDialogs.PromptConfirmAction(
"Delete all selected replays?", title: "Delete all selected replays?",
"Delete {0} replays?".F(list.Count), text: "Delete {0} replays?".F(list.Count),
() => onConfirm: () =>
{ {
list.ForEach(DeleteReplay); list.ForEach(DeleteReplay);
if (selectedReplay == null) if (selectedReplay == null)
SelectFirstVisibleReplay(); SelectFirstVisibleReplay();
}, },
null, confirmText: "Delete All");
"Delete All");
}; };
} }
} }

View File

@@ -73,12 +73,12 @@ namespace OpenRA.Mods.Common.Widgets.Logic
OriginalGraphicsWindowedSize != current.Graphics.WindowedSize || OriginalGraphicsWindowedSize != current.Graphics.WindowedSize ||
OriginalGraphicsFullscreenSize != current.Graphics.FullscreenSize) OriginalGraphicsFullscreenSize != current.Graphics.FullscreenSize)
ConfirmationDialogs.PromptConfirmAction( ConfirmationDialogs.PromptConfirmAction(
"Restart Now?", title: "Restart Now?",
"Some changes will not be applied until\nthe game is restarted. Restart now?", text: "Some changes will not be applied until\nthe game is restarted. Restart now?",
Game.Restart, onConfirm: Game.Restart,
closeAndExit, onCancel: closeAndExit,
"Restart Now", confirmText: "Restart Now",
"Restart Later"); cancelText: "Restart Later");
else else
closeAndExit(); closeAndExit();
}; };

View File

@@ -136,7 +136,7 @@ ScrollPanel@SPECTATOR_DROPDOWN_TEMPLATE:
Container@CONFIRM_PROMPT: Container@CONFIRM_PROMPT:
X: (WINDOW_RIGHT - WIDTH)/2 X: (WINDOW_RIGHT - WIDTH)/2
Y: (WINDOW_BOTTOM - 90)/2 Y: (WINDOW_BOTTOM - 90)/2
Width: 370 Width: 500
Height: 125 Height: 125
Children: Children:
Label@PROMPT_TITLE: Label@PROMPT_TITLE:
@@ -146,7 +146,7 @@ Container@CONFIRM_PROMPT:
Contrast: true Contrast: true
Align: Center Align: Center
Background@bg: Background@bg:
Width: 370 Width: 500
Height: 90 Height: 90
Background: panel-black Background: panel-black
Children: Children:
@@ -156,19 +156,26 @@ Container@CONFIRM_PROMPT:
Height: 25 Height: 25
Font: Bold Font: Bold
Align: Center Align: Center
Button@CONFIRM_BUTTON:
Key: return
X: 360
Y: 89
Width: 140
Height: 35
Text: Abort
Button@OTHER_BUTTON:
Key: r
X: 210
Y: 89
Width: 140
Height: 35
Text: Restart
Button@CANCEL_BUTTON: Button@CANCEL_BUTTON:
Key: escape Key: escape
Y: 89 Y: 89
Width: 140 Width: 140
Height: 35 Height: 35
Text: Cancel Text: Cancel
Button@CONFIRM_BUTTON:
Key: return
X: 230
Y: 89
Width: 140
Height: 35
Text: Confirm
Container@CANCEL_PROMPT: Container@CANCEL_PROMPT:
X: (WINDOW_RIGHT - WIDTH)/2 X: (WINDOW_RIGHT - WIDTH)/2

View File

@@ -1,7 +1,7 @@
Background@CONFIRM_PROMPT: Background@CONFIRM_PROMPT:
X: (WINDOW_RIGHT - WIDTH)/2 X: (WINDOW_RIGHT - WIDTH)/2
Y: (WINDOW_BOTTOM - 90)/2 Y: (WINDOW_BOTTOM - 90)/2
Width: 370 Width: 600
Height: 175 Height: 175
Children: Children:
Label@PROMPT_TITLE: Label@PROMPT_TITLE:
@@ -21,9 +21,16 @@ Background@CONFIRM_PROMPT:
Y: PARENT_BOTTOM - 45 Y: PARENT_BOTTOM - 45
Width: 160 Width: 160
Height: 25 Height: 25
Text: Confirm Text: Abort
Font: Bold Font: Bold
Key: return Key: return
Button@OTHER_BUTTON:
X: PARENT_RIGHT - 380
Y: PARENT_BOTTOM - 45
Width: 160
Height: 25
Text: Restart
Font: Bold
Button@CANCEL_BUTTON: Button@CANCEL_BUTTON:
X: PARENT_RIGHT - 180 X: PARENT_RIGHT - 180
Y: PARENT_BOTTOM - 45 Y: PARENT_BOTTOM - 45