New common dialog: TextInputPrompt to get a string from the user
For both ra and cnc
This commit is contained in:
@@ -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<string> onAccept, Action onCancel = null,
|
||||
string acceptText = null, string cancelText = null,
|
||||
Func<string, bool> inputValidator = null)
|
||||
{
|
||||
var panel = Ui.OpenWindow("TEXT_INPUT_PROMPT");
|
||||
Func<bool> doValidate = null;
|
||||
ButtonWidget acceptButton = null, cancelButton = null;
|
||||
|
||||
//
|
||||
// Title
|
||||
//
|
||||
panel.Get<LabelWidget>("PROMPT_TITLE").GetText = () => title;
|
||||
|
||||
//
|
||||
// Prompt
|
||||
//
|
||||
panel.Get<LabelWidget>("PROMPT_TEXT").GetText = () => prompt;
|
||||
|
||||
//
|
||||
// Text input
|
||||
//
|
||||
var input = panel.Get<TextFieldWidget>("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<ButtonWidget>("ACCEPT_BUTTON");
|
||||
if (!string.IsNullOrEmpty(acceptText))
|
||||
acceptButton.GetText = () => acceptText;
|
||||
|
||||
acceptButton.OnClick = () =>
|
||||
{
|
||||
if (!doValidate())
|
||||
return;
|
||||
|
||||
Ui.CloseWindow();
|
||||
onAccept(input.Text);
|
||||
};
|
||||
|
||||
cancelButton = panel.Get<ButtonWidget>("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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user