Prompt before quit / surrender

This commit is contained in:
Paul Chote
2011-05-08 20:49:24 +12:00
parent 33b35f768e
commit 6fa9eeb5bb
2 changed files with 80 additions and 5 deletions

View File

@@ -80,13 +80,15 @@ namespace OpenRA.Mods.Cnc.Widgets
public class CncIngameMenuLogic : IWidgetDelegate
{
Widget menu;
[ObjectCreator.UseCtor]
public CncIngameMenuLogic([ObjectCreator.Param] Widget widget,
[ObjectCreator.Param] World world,
[ObjectCreator.Param] Action onExit)
{
var menu = widget.GetWidget("INGAME_MENU");
menu.GetWidget<CncMenuButtonWidget>("QUIT_BUTTON").OnClick = () =>
menu = widget.GetWidget("INGAME_MENU");
var onQuit = (Action)(() =>
{
Game.DisconnectOnly();
@@ -94,13 +96,45 @@ namespace OpenRA.Mods.Cnc.Widgets
Game.LoadShellMap();
Widget.RootWidget.RemoveChildren();
Widget.LoadWidget("MENU_BACKGROUND", new Dictionary<string, object>());
};
});
var doNothing = (Action)(() => {});
menu.GetWidget<CncMenuButtonWidget>("QUIT_BUTTON").OnClick = () =>
PromptConfirmAction("Exit Game", "Are you sure you want to leave the game?", onQuit, doNothing);
var onSurrender = (Action)(() => world.IssueOrder(new Order("Surrender", world.LocalPlayer.PlayerActor, false)));
var surrenderButton = menu.GetWidget<CncMenuButtonWidget>("SURRENDER_BUTTON");
surrenderButton.IsDisabled = () => (world.LocalPlayer == null || world.LocalPlayer.WinState != WinState.Undefined);
surrenderButton.OnClick = () =>
PromptConfirmAction("Surrender", "Are you sure you want to surrender?", onSurrender, doNothing);
menu.GetWidget<CncMenuButtonWidget>("RESUME_BUTTON").OnClick = () =>
{
Widget.RootWidget.RemoveChild(menu);
onExit();
onExit();
};
}
public void PromptConfirmAction(string title, string text, Action onConfirm, Action onCancel)
{
var prompt = menu.GetWidget("CONFIRM_PROMPT");
prompt.GetWidget<LabelWidget>("PROMPT_TITLE").GetText = () => title;
prompt.GetWidget<LabelWidget>("PROMPT_TEXT").GetText = () => text;
prompt.GetWidget<CncMenuButtonWidget>("CONFIRM_BUTTON").OnClick = () =>
{
prompt.IsVisible = () => false;
onConfirm();
};
prompt.GetWidget<CncMenuButtonWidget>("CANCEL_BUTTON").OnClick = () =>
{
prompt.IsVisible = () => false;
onCancel();
};
prompt.IsVisible = () => true;
}
}
}

View File

@@ -51,4 +51,45 @@ Container@INGAME_MENU:
Y:0
Width:140
Height:35
Text:Resume
Text:Resume
Container@CONFIRM_PROMPT:
Id:CONFIRM_PROMPT
X:(WINDOW_RIGHT - WIDTH)/2
Y:(WINDOW_BOTTOM - 90)/2
Width:370
Height:125
Visible:false
Children:
Label@PROMPT_TITLE:
Id:PROMPT_TITLE
Width:PARENT_RIGHT
Y:0-25
Font:BigBold
Contrast:true
Align:Center
Background@bg:
Width:370
Height:90
Background:panel-black
Children:
Label@PROMPT_TEXT:
Id:PROMPT_TEXT
Y:(PARENT_BOTTOM-HEIGHT)/2
Width:PARENT_RIGHT
Height:25
Font:Bold
WordWrap:true
Align:Center
CncMenuButton@CANCEL_BUTTON:
Id:CANCEL_BUTTON
Y:89
Width:140
Height:35
Text:Cancel
CncMenuButton@CONFIRM_BUTTON:
Id:CONFIRM_BUTTON
X:230
Y:89
Width:140
Height:35
Text:Confirm