From 042910bd5ef4a8e7048cb10926f5db230c6af56b Mon Sep 17 00:00:00 2001 From: Pavlos Touboulidis Date: Thu, 1 May 2014 19:37:58 +0300 Subject: [PATCH] New common dialog: TextInputPrompt to get a string from the user For both ra and cnc --- OpenRA.Mods.RA/Widgets/ConfirmationDialogs.cs | 97 +++++++++++++++++++ mods/cnc/chrome/dialogs.yaml | 46 +++++++++ mods/ra/chrome/confirmation-dialogs.yaml | 40 ++++++++ 3 files changed, 183 insertions(+) diff --git a/OpenRA.Mods.RA/Widgets/ConfirmationDialogs.cs b/OpenRA.Mods.RA/Widgets/ConfirmationDialogs.cs index a81df9295d..4545697936 100644 --- a/OpenRA.Mods.RA/Widgets/ConfirmationDialogs.cs +++ b/OpenRA.Mods.RA/Widgets/ConfirmationDialogs.cs @@ -9,6 +9,7 @@ #endregion using System; +using System.Drawing; using OpenRA.Widgets; namespace OpenRA.Mods.RA.Widgets @@ -38,5 +39,101 @@ namespace OpenRA.Mods.RA.Widgets onCancel(); }; } + + public static void TextInputPrompt( + string title, string prompt, string initialText, + Action onAccept, Action onCancel = null, + string acceptText = null, string cancelText = null, + Func inputValidator = null) + { + var panel = Ui.OpenWindow("TEXT_INPUT_PROMPT"); + Func doValidate = null; + ButtonWidget acceptButton = null, cancelButton = null; + + // + // Title + // + panel.Get("PROMPT_TITLE").GetText = () => title; + + // + // Prompt + // + panel.Get("PROMPT_TEXT").GetText = () => prompt; + + // + // Text input + // + var input = panel.Get("INPUT_TEXT"); + var isValid = false; + input.Text = initialText; + input.IsValid = () => isValid; + input.OnEnterKey = () => + { + if (acceptButton.IsDisabled()) + return false; + + acceptButton.OnClick(); + return true; + }; + input.OnEscKey = () => + { + if (cancelButton.IsDisabled()) + return false; + + cancelButton.OnClick(); + return true; + }; + input.TakeKeyboardFocus(); + input.CursorPosition = input.Text.Length; + input.OnTextEdited = () => doValidate(); + + // + // Buttons + // + acceptButton = panel.Get("ACCEPT_BUTTON"); + if (!string.IsNullOrEmpty(acceptText)) + acceptButton.GetText = () => acceptText; + + acceptButton.OnClick = () => + { + if (!doValidate()) + return; + + Ui.CloseWindow(); + onAccept(input.Text); + }; + + cancelButton = panel.Get("CANCEL_BUTTON"); + if (!string.IsNullOrEmpty(cancelText)) + cancelButton.GetText = () => cancelText; + + cancelButton.OnClick = () => + { + Ui.CloseWindow(); + if (onCancel != null) + onCancel(); + }; + + // + // Validation + // + doValidate = () => + { + if (inputValidator == null) + return true; + + isValid = inputValidator(input.Text); + if (isValid) + { + acceptButton.Disabled = false; + return true; + } + + acceptButton.Disabled = true; + return false; + }; + + doValidate(); + } } } diff --git a/mods/cnc/chrome/dialogs.yaml b/mods/cnc/chrome/dialogs.yaml index a95831d6b4..186e657262 100644 --- a/mods/cnc/chrome/dialogs.yaml +++ b/mods/cnc/chrome/dialogs.yaml @@ -154,3 +154,49 @@ Container@CONFIRM_PROMPT: Height: 35 Text: Confirm + +Container@TEXT_INPUT_PROMPT: + X: (WINDOW_RIGHT - WIDTH)/2 + Y: (WINDOW_BOTTOM - HEIGHT)/2 + Width: 370 + Height: 80 + Children: + Label@PROMPT_TITLE: + Width: PARENT_RIGHT + Y: 0-25 + Font: BigBold + Contrast: true + Align: Center + Background@bg: + Width: PARENT_RIGHT + Height: 80 + Background: panel-black + Children: + Label@PROMPT_TEXT: + X: 20 + Y: 10 + Width: PARENT_RIGHT - 40 + Height: 25 + Font: Bold + Align: Center + TextField@INPUT_TEXT: + X: 20 + Y: 40 + Width: PARENT_RIGHT - 40 + Height: 25 + Button@ACCEPT_BUTTON: + X: PARENT_RIGHT - 160 + Y: PARENT_BOTTOM - 1 + Width: 160 + Height: 30 + Text: OK + Font: Bold + Key: return + Button@CANCEL_BUTTON: + X: 0 + Y: PARENT_BOTTOM - 1 + Width: 160 + Height: 30 + Text: Cancel + Font: Bold + Key: escape diff --git a/mods/ra/chrome/confirmation-dialogs.yaml b/mods/ra/chrome/confirmation-dialogs.yaml index 972067af83..35522917d2 100644 --- a/mods/ra/chrome/confirmation-dialogs.yaml +++ b/mods/ra/chrome/confirmation-dialogs.yaml @@ -32,3 +32,43 @@ Background@CONFIRM_PROMPT: Text: Cancel Font: Bold Key: escape + +Background@TEXT_INPUT_PROMPT: + X: (WINDOW_RIGHT - WIDTH)/2 + Y: (WINDOW_BOTTOM - HEIGHT)/2 + Width: 370 + Height: 175 + Children: + Label@PROMPT_TITLE: + Width: PARENT_RIGHT + Y: 20 + Height: 25 + Font: Bold + Align: Center + Label@PROMPT_TEXT: + X: 20 + Y: 50 + Width: PARENT_RIGHT - 40 + Height: 25 + Align: Center + TextField@INPUT_TEXT: + X: 20 + Y: 80 + Width: PARENT_RIGHT - 40 + Height: 25 + Button@ACCEPT_BUTTON: + X: 20 + Y: PARENT_BOTTOM - 45 + Width: 160 + Height: 25 + Text: OK + 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