diff --git a/Content.Client/Administration/QuickDialogSystem.cs b/Content.Client/Administration/QuickDialogSystem.cs index 84236c5a3e..d015b7769b 100644 --- a/Content.Client/Administration/QuickDialogSystem.cs +++ b/Content.Client/Administration/QuickDialogSystem.cs @@ -1,13 +1,8 @@ -using System.Linq; using Content.Client.UserInterface.Controls; using Content.Shared.Administration; -using Robust.Client.UserInterface; -using Robust.Client.UserInterface.Controls; namespace Content.Client.Administration; -// mfw they ported input() from BYOND - /// /// This handles the client portion of quick dialogs. /// @@ -21,149 +16,22 @@ public sealed class QuickDialogSystem : EntitySystem private void OpenDialog(QuickDialogOpenEvent ev) { - var window = new FancyWindow() - { - Title = ev.Title - }; + var ok = (ev.Buttons & QuickDialogButtonFlag.OkButton) != 0; + var cancel = (ev.Buttons & QuickDialogButtonFlag.CancelButton) != 0; + var window = new DialogWindow(ev.Title, ev.Prompts, ok: ok, cancel: cancel); - var entryContainer = new BoxContainer() - { - Orientation = BoxContainer.LayoutOrientation.Vertical, - Margin = new Thickness(8), - }; - - var promptsDict = new Dictionary(); - - for (var index = 0; index < ev.Prompts.Count; index++) - { - var entry = ev.Prompts[index]; - var entryBox = new BoxContainer() - { - Orientation = BoxContainer.LayoutOrientation.Horizontal - }; - - entryBox.AddChild(new Label { Text = entry.Prompt, HorizontalExpand = true, SizeFlagsStretchRatio = 0.5f }); - var edit = new LineEdit() { HorizontalExpand = true }; - entryBox.AddChild(edit); - switch (entry.Type) - { - case QuickDialogEntryType.Integer: - edit.IsValid += VerifyInt; - edit.PlaceHolder = Loc.GetString("quick-dialog-ui-integer"); - break; - case QuickDialogEntryType.Float: - edit.IsValid += VerifyFloat; - edit.PlaceHolder = Loc.GetString("quick-dialog-ui-float"); - break; - case QuickDialogEntryType.ShortText: - edit.IsValid += VerifyShortText; - edit.PlaceHolder = Loc.GetString("quick-dialog-ui-short-text"); - break; - case QuickDialogEntryType.LongText: - edit.IsValid += VerifyLongText; - edit.PlaceHolder = Loc.GetString("quick-dialog-ui-long-text"); - break; - default: - throw new ArgumentOutOfRangeException(); - } - - promptsDict.Add(entry.FieldId, edit); - entryContainer.AddChild(entryBox); - - if (index == ev.Prompts.Count - 1) - { - // Last text box gets enter confirmation. - // Only the last so you don't accidentally confirm early. - edit.OnTextEntered += _ => Confirm(); - } - } - - var buttonsBox = new BoxContainer() - { - Orientation = BoxContainer.LayoutOrientation.Horizontal, - HorizontalAlignment = Control.HAlignment.Center, - }; - - var alreadyReplied = false; - - if ((ev.Buttons & QuickDialogButtonFlag.OkButton) != 0) - { - var okButton = new Button() - { - Text = Loc.GetString("quick-dialog-ui-ok"), - }; - - okButton.OnPressed += _ => Confirm(); - - buttonsBox.AddChild(okButton); - } - - if ((ev.Buttons & QuickDialogButtonFlag.OkButton) != 0) - { - var cancelButton = new Button() - { - Text = Loc.GetString("quick-dialog-ui-cancel"), - }; - - cancelButton.OnPressed += _ => - { - RaiseNetworkEvent(new QuickDialogResponseEvent(ev.DialogId, - new(), - QuickDialogButtonFlag.CancelButton)); - alreadyReplied = true; - window.Close(); - }; - - buttonsBox.AddChild(cancelButton); - } - - window.OnClose += () => - { - if (!alreadyReplied) - { - RaiseNetworkEvent(new QuickDialogResponseEvent(ev.DialogId, - new(), - QuickDialogButtonFlag.CancelButton)); - } - }; - - entryContainer.AddChild(buttonsBox); - - window.ContentsContainer.AddChild(entryContainer); - - window.MinWidth *= 2; // Just double it. - - window.OpenCentered(); - - return; - - void Confirm() + window.OnConfirmed += responses => { RaiseNetworkEvent(new QuickDialogResponseEvent(ev.DialogId, - promptsDict.Select(x => (x.Key, x.Value.Text)).ToDictionary(x => x.Key, x => x.Text), + responses, QuickDialogButtonFlag.OkButton)); - alreadyReplied = true; - window.Close(); - } - } + }; - private bool VerifyInt(string input) - { - return int.TryParse(input, out var _); - } - - private bool VerifyFloat(string input) - { - return float.TryParse(input, out var _); - } - - private bool VerifyShortText(string input) - { - return input.Length <= 100; - } - - private bool VerifyLongText(string input) - { - return input.Length <= 2000; + window.OnCancelled += () => + { + RaiseNetworkEvent(new QuickDialogResponseEvent(ev.DialogId, + new(), + QuickDialogButtonFlag.CancelButton)); + }; } } diff --git a/Content.Client/UserInterface/Controls/DialogWindow.xaml b/Content.Client/UserInterface/Controls/DialogWindow.xaml new file mode 100644 index 0000000000..4f9a8a49f9 --- /dev/null +++ b/Content.Client/UserInterface/Controls/DialogWindow.xaml @@ -0,0 +1,9 @@ + + + + +