From 3ac42e1643cc1bc5e489dc5af302dd96c1c9b275 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Thu, 16 Jun 2016 18:21:55 +0100 Subject: [PATCH] Generalise and combine cancel/confirm prompts. --- .../Widgets/ConfirmationDialogs.cs | 80 +++++++++-------- .../Widgets/Logic/Ingame/IngameMenuLogic.cs | 6 +- .../Widgets/Logic/MapChooserLogic.cs | 4 +- .../Widgets/Logic/ReplayBrowserLogic.cs | 4 +- .../Widgets/Logic/ReplayUtils.cs | 2 +- .../Widgets/Logic/ServerCreationLogic.cs | 8 +- .../Widgets/Logic/SettingsLogic.cs | 2 +- mods/cnc/chrome/dialogs.yaml | 90 ++++++------------- mods/ra/chrome/confirmation-dialogs.yaml | 44 +++------ 9 files changed, 94 insertions(+), 146 deletions(-) diff --git a/OpenRA.Mods.Common/Widgets/ConfirmationDialogs.cs b/OpenRA.Mods.Common/Widgets/ConfirmationDialogs.cs index 57e11c9981..6233fdf2f5 100644 --- a/OpenRA.Mods.Common/Widgets/ConfirmationDialogs.cs +++ b/OpenRA.Mods.Common/Widgets/ConfirmationDialogs.cs @@ -16,78 +16,82 @@ namespace OpenRA.Mods.Common.Widgets { public static class ConfirmationDialogs { - public static void PromptConfirmAction( + public static void ButtonPrompt( string title, string text, - Action onConfirm, + Action onConfirm = null, Action onCancel = null, Action onOther = null, string confirmText = null, string cancelText = null, string otherText = null) { - var promptName = onOther != null ? "CONFIRM_PROMPT_THREEBUTTON" : "CONFIRM_PROMPT_TWOBUTTON"; + var promptName = onOther != null ? "THREEBUTTON_PROMPT" : "TWOBUTTON_PROMPT"; var prompt = Ui.OpenWindow(promptName); - var confirmButton = prompt.Get("CONFIRM_BUTTON"); + var confirmButton = prompt.GetOrNull("CONFIRM_BUTTON"); var cancelButton = prompt.GetOrNull("CANCEL_BUTTON"); var otherButton = prompt.GetOrNull("OTHER_BUTTON"); prompt.Get("PROMPT_TITLE").GetText = () => title; - prompt.Get("PROMPT_TEXT").GetText = () => text; - if (!string.IsNullOrEmpty(confirmText)) - confirmButton.GetText = () => confirmText; - if (!string.IsNullOrEmpty(otherText) && otherButton != null) - otherButton.GetText = () => otherText; - if (!string.IsNullOrEmpty(cancelText) && cancelButton != null) - cancelButton.GetText = () => cancelText; - confirmButton.OnClick = () => + var headerTemplate = prompt.Get("PROMPT_TEXT"); + var headerLines = text.Replace("\\n", "\n").Split('\n'); + var headerHeight = 0; + foreach (var l in headerLines) { - Ui.CloseWindow(); - onConfirm(); - }; + var line = (LabelWidget)headerTemplate.Clone(); + line.GetText = () => l; + line.Bounds.Y += headerHeight; + prompt.AddChild(line); + + headerHeight += headerTemplate.Bounds.Height; + } + + prompt.Bounds.Height += headerHeight; + prompt.Bounds.Y -= headerHeight / 2; + + if (onConfirm != null && confirmButton != null) + { + confirmButton.Visible = true; + confirmButton.Bounds.Y += headerHeight; + confirmButton.OnClick = () => + { + Ui.CloseWindow(); + onConfirm(); + }; + + if (!string.IsNullOrEmpty(confirmText)) + confirmButton.GetText = () => confirmText; + } if (onCancel != null && cancelButton != null) { - cancelButton.IsVisible = () => true; + cancelButton.Visible = true; + cancelButton.Bounds.Y += headerHeight; cancelButton.OnClick = () => { Ui.CloseWindow(); if (onCancel != null) onCancel(); }; + + if (!string.IsNullOrEmpty(cancelText) && cancelButton != null) + cancelButton.GetText = () => cancelText; } - else if (cancelButton != null) - cancelButton.IsVisible = () => false; if (onOther != null && otherButton != null) { - otherButton.IsVisible = () => true; + otherButton.Visible = true; + otherButton.Bounds.Y += headerHeight; otherButton.OnClick = () => { if (onOther != null) onOther(); }; + + if (!string.IsNullOrEmpty(otherText) && otherButton != null) + otherButton.GetText = () => otherText; } - else if (otherButton != null) - otherButton.IsVisible = () => false; - } - - public static void CancelPrompt(string title, string text, Action onCancel = null, string cancelText = null) - { - var prompt = Ui.OpenWindow("CANCEL_PROMPT"); - prompt.Get("PROMPT_TITLE").GetText = () => title; - prompt.Get("PROMPT_TEXT").GetText = () => text; - - if (!string.IsNullOrEmpty(cancelText)) - prompt.Get("CANCEL_BUTTON").GetText = () => cancelText; - - prompt.Get("CANCEL_BUTTON").OnClick = () => - { - Ui.CloseWindow(); - if (onCancel != null) - onCancel(); - }; } public static void TextInputPrompt( diff --git a/OpenRA.Mods.Common/Widgets/Logic/Ingame/IngameMenuLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/Ingame/IngameMenuLogic.cs index fd96c28d10..c30fa9b229 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/Ingame/IngameMenuLogic.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/Ingame/IngameMenuLogic.cs @@ -113,7 +113,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic }; } - ConfirmationDialogs.PromptConfirmAction( + ConfirmationDialogs.ButtonPrompt( title: "Leave Mission", text: "Leave this game and return to the menu?", onConfirm: onQuit, @@ -132,7 +132,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic exitEditorButton.OnClick = () => { hideMenu = true; - ConfirmationDialogs.PromptConfirmAction( + ConfirmationDialogs.ButtonPrompt( title: "Exit Map Editor", text: "Exit and lose all unsaved changes?", onConfirm: onQuit, @@ -152,7 +152,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic surrenderButton.OnClick = () => { hideMenu = true; - ConfirmationDialogs.PromptConfirmAction( + ConfirmationDialogs.ButtonPrompt( title: "Surrender", text: "Are you sure you want to surrender?", onConfirm: onSurrender, diff --git a/OpenRA.Mods.Common/Widgets/Logic/MapChooserLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/MapChooserLogic.cs index f87535b3fc..bf0fa56490 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/MapChooserLogic.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/MapChooserLogic.cs @@ -328,7 +328,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic void DeleteOneMap(string map, Action after) { - ConfirmationDialogs.PromptConfirmAction( + ConfirmationDialogs.ButtonPrompt( title: "Delete map", text: "Delete the map '{0}'?".F(modData.MapCache[map].Title), onConfirm: () => @@ -343,7 +343,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic void DeleteAllMaps(string[] maps, Action after) { - ConfirmationDialogs.PromptConfirmAction( + ConfirmationDialogs.ButtonPrompt( title: "Delete maps", text: "Delete all maps on this page?", onConfirm: () => diff --git a/OpenRA.Mods.Common/Widgets/Logic/ReplayBrowserLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/ReplayBrowserLogic.cs index 8998ad6b55..3ac585e5ed 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/ReplayBrowserLogic.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/ReplayBrowserLogic.cs @@ -409,7 +409,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic Action onDeleteReplay = (r, after) => { - ConfirmationDialogs.PromptConfirmAction( + ConfirmationDialogs.ButtonPrompt( title: "Delete selected replay?", text: "Delete replay '{0}'?".F(Path.GetFileNameWithoutExtension(r.FilePath)), onConfirm: () => @@ -450,7 +450,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic return; } - ConfirmationDialogs.PromptConfirmAction( + ConfirmationDialogs.ButtonPrompt( title: "Delete all selected replays?", text: "Delete {0} replays?".F(list.Count), onConfirm: () => diff --git a/OpenRA.Mods.Common/Widgets/Logic/ReplayUtils.cs b/OpenRA.Mods.Common/Widgets/Logic/ReplayUtils.cs index a79c3efbe4..37b7466a99 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/ReplayUtils.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/ReplayUtils.cs @@ -51,7 +51,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic var error = "It was recorded with an " + type; error += string.IsNullOrEmpty(name) ? "." : ":\n{0}".F(name); - ConfirmationDialogs.CancelPrompt("Incompatible Replay", error, onCancel); + ConfirmationDialogs.ButtonPrompt("Incompatible Replay", error, onCancel: onCancel); return false; } diff --git a/OpenRA.Mods.Common/Widgets/Logic/ServerCreationLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/ServerCreationLogic.cs index 1ced376947..1bd1af347d 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/ServerCreationLogic.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/ServerCreationLogic.cs @@ -128,14 +128,14 @@ namespace OpenRA.Mods.Common.Widgets.Logic } catch (System.Net.Sockets.SocketException e) { - var err_msg = "Could not listen on port {0}.".F(Game.Settings.Server.ListenPort); + var message = "Could not listen on port {0}.".F(Game.Settings.Server.ListenPort); if (e.ErrorCode == 10048) { // AddressAlreadyInUse (WSAEADDRINUSE) - err_msg += "\n\nCheck if the port is already being used."; + message += "\nCheck if the port is already being used."; } else { - err_msg += "\n\nError is: \"{0}\" ({1})".F(e.Message, e.ErrorCode); + message += "\nError is: \"{0}\" ({1})".F(e.Message, e.ErrorCode); } - ConfirmationDialogs.CancelPrompt("Server Creation Failed", err_msg, cancelText: "OK"); + ConfirmationDialogs.ButtonPrompt("Server Creation Failed", message, onCancel: () => { }, cancelText: "Back"); return; } diff --git a/OpenRA.Mods.Common/Widgets/Logic/SettingsLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/SettingsLogic.cs index 4eb047f390..c37f74ce9e 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/SettingsLogic.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/SettingsLogic.cs @@ -77,7 +77,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic OriginalGraphicsRenderer != current.Graphics.Renderer || OriginalGraphicsWindowedSize != current.Graphics.WindowedSize || OriginalGraphicsFullscreenSize != current.Graphics.FullscreenSize) - ConfirmationDialogs.PromptConfirmAction( + ConfirmationDialogs.ButtonPrompt( title: "Restart Now?", text: "Some changes will not be applied until\nthe game is restarted. Restart now?", onConfirm: Game.Restart, diff --git a/mods/cnc/chrome/dialogs.yaml b/mods/cnc/chrome/dialogs.yaml index f1363d7d91..37d28bea49 100644 --- a/mods/cnc/chrome/dialogs.yaml +++ b/mods/cnc/chrome/dialogs.yaml @@ -133,11 +133,12 @@ ScrollPanel@SPECTATOR_DROPDOWN_TEMPLATE: Width: PARENT_RIGHT Height: 25 -Container@CONFIRM_PROMPT_THREEBUTTON: +Background@THREEBUTTON_PROMPT: X: (WINDOW_RIGHT - WIDTH)/2 Y: (WINDOW_BOTTOM - 90)/2 Width: 500 - Height: 125 + Height: 41 + Background: panel-black Children: Label@PROMPT_TITLE: Width: PARENT_RIGHT @@ -145,44 +146,42 @@ Container@CONFIRM_PROMPT_THREEBUTTON: 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 + Label@PROMPT_TEXT: + Y: 20 + Width: PARENT_RIGHT + Height: 20 + Font: Bold + Align: Center Button@CONFIRM_BUTTON: Key: return X: 360 - Y: 89 + Y: 40 Width: 140 Height: 35 Text: Confirm + Visible: false Button@OTHER_BUTTON: Key: r X: 210 - Y: 89 + Y: 40 Width: 140 Height: 35 Text: Restart + Visible: false Button@CANCEL_BUTTON: Key: escape - Y: 89 + Y: 40 Width: 140 Height: 35 Text: Cancel + Visible: false - -Container@CONFIRM_PROMPT_TWOBUTTON: +Background@TWOBUTTON_PROMPT: X: (WINDOW_RIGHT - WIDTH)/2 Y: (WINDOW_BOTTOM - 90)/2 Width: 370 - Height: 125 + Height: 31 + Background: panel-black Children: Label@PROMPT_TITLE: Width: PARENT_RIGHT @@ -190,60 +189,27 @@ Container@CONFIRM_PROMPT_TWOBUTTON: Font: BigBold Contrast: true Align: Center - Background@bg: - Width: 370 - Height: 90 - Background: panel-black - Children: - Label@PROMPT_TEXT: - Y: (PARENT_BOTTOM-HEIGHT)/2 - Width: PARENT_RIGHT - Height: 25 - Font: Bold - Align: Center + Label@PROMPT_TEXT: + Y: 15 + Width: PARENT_RIGHT + Height: 20 + Font: Bold + Align: Center Button@CANCEL_BUTTON: Key: escape - Y: 89 + Y: 30 Width: 140 Height: 35 Text: Cancel + Visible: false Button@CONFIRM_BUTTON: Key: return X: 230 - Y: 89 + Y: 30 Width: 140 Height: 35 Text: Confirm - -Container@CANCEL_PROMPT: - X: (WINDOW_RIGHT - WIDTH)/2 - Y: (WINDOW_BOTTOM - 90)/2 - Width: 370 - Height: 125 - Children: - Label@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: - Y: (PARENT_BOTTOM-HEIGHT)/2 - Width: PARENT_RIGHT - Height: 25 - Font: Bold - Align: Center - Button@CANCEL_BUTTON: - Key: escape - Y: 89 - Width: 140 - Height: 35 - Text: Cancel + Visible: false Container@TEXT_INPUT_PROMPT: X: (WINDOW_RIGHT - WIDTH)/2 diff --git a/mods/ra/chrome/confirmation-dialogs.yaml b/mods/ra/chrome/confirmation-dialogs.yaml index 73e28c7573..3783a6b86d 100644 --- a/mods/ra/chrome/confirmation-dialogs.yaml +++ b/mods/ra/chrome/confirmation-dialogs.yaml @@ -1,8 +1,8 @@ -Background@CONFIRM_PROMPT_THREEBUTTON: +Background@THREEBUTTON_PROMPT: X: (WINDOW_RIGHT - WIDTH)/2 Y: (WINDOW_BOTTOM - 90)/2 Width: 600 - Height: 175 + Height: 110 Children: Label@PROMPT_TITLE: Width: PARENT_RIGHT @@ -14,7 +14,7 @@ Background@CONFIRM_PROMPT_THREEBUTTON: X: 15 Y: 50 Width: PARENT_RIGHT - 30 - Height: 65 + Height: 20 Align: Center Button@CONFIRM_BUTTON: X: 20 @@ -24,6 +24,7 @@ Background@CONFIRM_PROMPT_THREEBUTTON: Text: Confirm Font: Bold Key: return + Visible: false Button@OTHER_BUTTON: X: PARENT_RIGHT - 380 Y: PARENT_BOTTOM - 45 @@ -31,6 +32,7 @@ Background@CONFIRM_PROMPT_THREEBUTTON: Height: 25 Text: Restart Font: Bold + Visible: false Button@CANCEL_BUTTON: X: PARENT_RIGHT - 180 Y: PARENT_BOTTOM - 45 @@ -39,12 +41,13 @@ Background@CONFIRM_PROMPT_THREEBUTTON: Text: Cancel Font: Bold Key: escape + Visible: false -Background@CONFIRM_PROMPT_TWOBUTTON: +Background@TWOBUTTON_PROMPT: X: (WINDOW_RIGHT - WIDTH)/2 Y: (WINDOW_BOTTOM - 90)/2 Width: 370 - Height: 175 + Height: 110 Children: Label@PROMPT_TITLE: Width: PARENT_RIGHT @@ -56,7 +59,7 @@ Background@CONFIRM_PROMPT_TWOBUTTON: X: 15 Y: 50 Width: PARENT_RIGHT - 30 - Height: 65 + Height: 20 Align: Center Button@CONFIRM_BUTTON: X: 20 @@ -66,6 +69,7 @@ Background@CONFIRM_PROMPT_TWOBUTTON: Text: Confirm Font: Bold Key: return + Visible: false Button@CANCEL_BUTTON: X: PARENT_RIGHT - 180 Y: PARENT_BOTTOM - 45 @@ -74,33 +78,7 @@ Background@CONFIRM_PROMPT_TWOBUTTON: Text: Cancel Font: Bold Key: escape - -Background@CANCEL_PROMPT: - X: (WINDOW_RIGHT - WIDTH)/2 - Y: (WINDOW_BOTTOM - 90)/2 - Width: 370 - 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@CANCEL_BUTTON: - X: PARENT_RIGHT/2 - WIDTH/2 - Y: PARENT_BOTTOM - 45 - Width: 160 - Height: 25 - Text: Cancel - Font: Bold - Key: escape + Visible: false Background@TEXT_INPUT_PROMPT: X: (WINDOW_RIGHT - WIDTH)/2