Changes to the RestartGame function to make it more streamlined

Removal of PromptAbortAction and inclusion into PromptConfirmAction
Changes to prevent a restart button being required for all mods
ConfirmAction
Addtion of named parameters to PromptConfirmAction
Moved StartGame from MissionBrowserLogic.cs to Game.cs
This commit is contained in:
Whinis
2016-01-14 22:24:30 -05:00
parent e0c033fe99
commit 9059e3e2c8
11 changed files with 166 additions and 190 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

@@ -177,8 +177,39 @@ namespace OpenRA
public static void RestartGame() public static void RestartGame()
{ {
OrderManager.World.EndGame(); var replay = OrderManager.Connection as ReplayConnection;
StartGame(OrderManager.World.Map.Uid, WorldType.Regular); 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

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,28 +15,60 @@ 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)
{ {
Ui.CloseWindow(); cancelButton.IsVisible = () => true;
if (onCancel != null) cancelButton.OnClick = () =>
onCancel(); {
}; Ui.CloseWindow();
if (onCancel != null)
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)
@@ -55,39 +87,6 @@ namespace OpenRA.Mods.Common.Widgets
onCancel(); onCancel();
}; };
} }
/**
* open confirmation dialog for mission / game restart
*/
public static void PromptAbortMission(World world, string title, string text, Action onAbort, Action onCancel = null, Action closeMenu = null)
{
var isMultiplayer = !world.LobbyInfo.IsSinglePlayer && !world.IsReplay;
var prompt = Ui.OpenWindow("ABORT_MISSION_PROMPT");
prompt.Get<LabelWidget>("PROMPT_TITLE").GetText = () => title;
prompt.Get<LabelWidget>("PROMPT_TEXT").GetText = () => text;
prompt.Get<ButtonWidget>("ABORT_BUTTON").OnClick = () =>
{
Ui.CloseWindow();
onAbort();
};
var restartButton = prompt.Get<ButtonWidget>("RESTART_BUTTON");
restartButton.IsVisible = () => !isMultiplayer;
restartButton.OnClick = () =>
{
if (closeMenu != null)
closeMenu();
Ui.CloseWindow();
Game.RestartGame();
};
prompt.Get<ButtonWidget>("CANCEL_BUTTON").OnClick = () =>
{
Ui.CloseWindow();
if (onCancel != null)
onCancel();
};
}
public static void TextInputPrompt( public static void TextInputPrompt(
string title, string prompt, string initialText, string title, string prompt, string initialText,

View File

@@ -88,14 +88,42 @@ namespace OpenRA.Mods.Common.Widgets.Logic
abortMissionButton.OnClick = () => abortMissionButton.OnClick = () =>
{ {
if (world.IsGameOver)
{
onQuit();
return;
}
hideMenu = true; hideMenu = true;
ConfirmationDialogs.PromptAbortMission(world, "Abort Mission", "Leave this game and return to the menu?", onQuit, showMenu, closeMenu);
if (world.LocalPlayer == null || (world.LocalPlayer.WinState != WinState.Won &&
(!world.IsGameOver || world.Map.Visibility == MapVisibility.MissionSelector)))
{
Action restartAction = null;
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;
}
Game.RunAfterDelay(exitDelay, Game.RestartGame);
};
}
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
@@ -200,50 +207,6 @@ Container@CANCEL_PROMPT:
Height: 35 Height: 35
Text: Cancel Text: Cancel
Container@ABORT_MISSION_PROMPT:
X: (WINDOW_RIGHT - WIDTH)/2
Y: (WINDOW_BOTTOM - 90)/2
Width: 500
Height: 125
Children:
Label@PROMPT_TITLE:
Width: PARENT_RIGHT
Y: 0-25
Font: BigBold
Contrast: true
Align: Center
Background@bg:
Width: 500
Height: 90
Background: panel-black
Children:
Label@PROMPT_TEXT:
Y: (PARENT_BOTTOM-HEIGHT)/2
Width: PARENT_RIGHT
Height: 25
Font: Bold
Align: Center
Button@ABORT_BUTTON:
Key: return
X: 360
Y: 89
Width: 140
Height: 35
Text: Abort
Button@RESTART_BUTTON:
Key: r
X: 180
Y: 89
Width: 140
Height: 35
Text: Restart
Button@CANCEL_BUTTON:
Key: escape
Y: 89
Width: 140
Height: 35
Text: Cancel
Container@TEXT_INPUT_PROMPT: Container@TEXT_INPUT_PROMPT:
X: (WINDOW_RIGHT - WIDTH)/2 X: (WINDOW_RIGHT - WIDTH)/2
Y: (WINDOW_BOTTOM - HEIGHT)/2 Y: (WINDOW_BOTTOM - HEIGHT)/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:
@@ -17,41 +17,6 @@ Background@CONFIRM_PROMPT:
Height: 65 Height: 65
Align: Center Align: Center
Button@CONFIRM_BUTTON: Button@CONFIRM_BUTTON:
X: 20
Y: PARENT_BOTTOM - 45
Width: 160
Height: 25
Text: Confirm
Font: Bold
Key: return
Button@CANCEL_BUTTON:
X: PARENT_RIGHT - 180
Y: PARENT_BOTTOM - 45
Width: 160
Height: 25
Text: Cancel
Font: Bold
Key: escape
Background@ABORT_MISSION_PROMPT:
X: (WINDOW_RIGHT - WIDTH)/2
Y: (WINDOW_BOTTOM - 90)/2
Width: 600
Height: 175
Children:
Label@PROMPT_TITLE:
Width: PARENT_RIGHT
Y: 20
Height: 25
Font: Bold
Align: Center
Label@PROMPT_TEXT:
X: 15
Y: 50
Width: PARENT_RIGHT - 30
Height: 65
Align: Center
Button@ABORT_BUTTON:
X: 20 X: 20
Y: PARENT_BOTTOM - 45 Y: PARENT_BOTTOM - 45
Width: 160 Width: 160
@@ -59,7 +24,7 @@ Background@ABORT_MISSION_PROMPT:
Text: Abort Text: Abort
Font: Bold Font: Bold
Key: return Key: return
Button@RESTART_BUTTON: Button@OTHER_BUTTON:
X: PARENT_RIGHT - 380 X: PARENT_RIGHT - 380
Y: PARENT_BOTTOM - 45 Y: PARENT_BOTTOM - 45
Width: 160 Width: 160