diff --git a/Content.Client/UserInterface/Controls/DialogWindow.xaml.cs b/Content.Client/UserInterface/Controls/DialogWindow.xaml.cs index 4260bec463..22b988b005 100644 --- a/Content.Client/UserInterface/Controls/DialogWindow.xaml.cs +++ b/Content.Client/UserInterface/Controls/DialogWindow.xaml.cs @@ -36,7 +36,7 @@ public sealed partial class DialogWindow : FancyWindow private List<(string, Func)> _promptLines; - private delegate (Control, Func) ControlConstructor(Func verifier, string name, QuickDialogEntry entry, bool last);// are you feeling it now mr krabs? + private delegate (Control, Func) ControlConstructor(Func verifier, string name, QuickDialogEntry entry, bool last, object? value);// are you feeling it now mr krabs? /// /// Create and open a new dialog with some prompts. @@ -62,7 +62,7 @@ public sealed partial class DialogWindow : FancyWindow for (int i = 0; i < entries.Count; i++) { var entry = entries[i]; - + var value = entry.Value; var box = new BoxContainer(); box.AddChild(new Label() { Text = entry.Prompt, HorizontalExpand = true, SizeFlagsStretchRatio = 0.5f }); @@ -72,7 +72,7 @@ public sealed partial class DialogWindow : FancyWindow // walkthrough: // second item in this tuple is a verifier function for controls who have that option // third item is a backup name in case we have not been provided with one from the server - // first item is a function that takes the other two, the QuickDialogEntry for it, a bool of whether it's the last control in the window, + // first item is a function that takes the other two, the QuickDialogEntry for it, a bool of whether it's the last control in the window, the default value for the control // and returns another tuple: // item 1 is the control itself // item 2 is a Func, a """generic""" function that returns whatever user has done with the control @@ -90,11 +90,11 @@ public sealed partial class DialogWindow : FancyWindow }; var (setup, valid, name) = notapairanymore; - - var (control, returner) = setup(valid!, entry.Placeholder ?? Loc.GetString($"quick-dialog-ui-{name}"), entry, i == entries.Count - 1); // ARE YOU FEELING IT NOW MR KRABS? - // yes, valid can be null - // yes, i am just going to ignore that - // go fuck yourself + // try use placeholder from the caller, fall back to the generic one for whatever type is being validated. + var (control, returner) = setup(valid!, entry.Placeholder ?? Loc.GetString($"quick-dialog-ui-{name}"), entry, i == entries.Count - 1, value); // ARE YOU FEELING IT NOW MR KRABS? + // yes, valid can be null + // yes, i am just going to ignore that + // go fuck yourself _promptLines.Add((entry.FieldId, returner)); box.AddChild(control); @@ -165,12 +165,14 @@ public sealed partial class DialogWindow : FancyWindow - private (Control, Func) SetupLineEdit(Func valid, string name, QuickDialogEntry entry, bool last) // oh shit i'm feeling it + private (Control, Func) SetupLineEdit(Func valid, string name, QuickDialogEntry entry, bool last, object? value) // oh shit i'm feeling it { var edit = new LineEdit() { HorizontalExpand = true }; edit.IsValid += valid; - // try use placeholder from the caller, fall back to the generic one for whatever type is being validated. + if(value != null) + edit.Text = value.ToString() ?? ""; edit.PlaceHolder = name; + // Last text box gets enter confirmation. // Only the last so you don't accidentally confirm early. if (last) @@ -178,28 +180,34 @@ public sealed partial class DialogWindow : FancyWindow return (edit, () => {return edit.Text;} ); } - private (Control, Func) SetupLineEditNumber(Func valid, string name, QuickDialogEntry entry, bool last) + private (Control, Func) SetupLineEditNumber(Func valid, string name, QuickDialogEntry entry, bool last, object? value) { - var (control, returner) = SetupLineEdit(valid, name, entry, last); + var (control, returner) = SetupLineEdit(valid, name, entry, last, value); var le = (LineEdit) control; return (control, ()=> le.Text.Length > 0 ? le.Text : "0"); // Otherwise you'll get kicked for malformed data } - private (Control, Func) SetupLineEditHex(Func valid, string name, QuickDialogEntry entry, bool last) + private (Control, Func) SetupLineEditHex(Func valid, string name, QuickDialogEntry entry, bool last, object? value) { - var (control, returner) = SetupLineEditNumber(valid, name, entry, last); - ((LineEdit) control).OnTextChanged += e => { e.Control.Text = e.Text.ToUpper(); }; + var (control, returner) = SetupLineEditNumber(valid, name, entry, last, value); + var le = (LineEdit) control; + if(value is int) + { + le.Text = ((int)value).ToString("X"); + } + le.OnTextChanged += e => { e.Control.Text = e.Text.ToUpper(); }; return (control, returner); } - private (Control, Func) SetupCheckBox(Func _, string name, QuickDialogEntry entry, bool last) + private (Control, Func) SetupCheckBox(Func _, string name, QuickDialogEntry entry, bool last, object? value) { var check = new CheckBox() { HorizontalExpand = true, HorizontalAlignment = HAlignment.Right }; check.Text = name; - + value ??= false; + check.Pressed = (bool)value; return (check, () => { return check.Pressed ? "true" : "false"; }); } - private (Control, Func) SetupVoid(Func _, string __, QuickDialogEntry ___, bool ____) + private (Control, Func) SetupVoid(Func _, string __, QuickDialogEntry ___, bool ____, object? _____) { var control = new Control(); control.Visible = false; diff --git a/Content.Server/Administration/QuickDialogSystem.OpenDialog.cs b/Content.Server/Administration/QuickDialogSystem.OpenDialog.cs index c98e152cd2..2cb63ffbf4 100644 --- a/Content.Server/Administration/QuickDialogSystem.OpenDialog.cs +++ b/Content.Server/Administration/QuickDialogSystem.OpenDialog.cs @@ -5,6 +5,9 @@ using Robust.Shared.Player; namespace Content.Server.Administration; //res().System().TryGetSession(new EntityUid(X), out var seks);res().System().OpenDialog(seks, "Заголовок", "Серийный код твоей матери", "Селёдкой пахнет?", "Сосал?", "Сколько ванотян жрал хуёв:", "тыгыдык тыгыдык тыгыдык тыгыдык" ," ", "Вскрываем байты", (_,_,_,_,_,_,_)=>{}); +// +//List<(Type, string, object)> entries = new();entries.Add((typeof(string), "shitass", "faggot"));entries.Add((typeof(int), "cunt", 2254)) +//res().System().TryGetSession(new EntityUid(X), out var seks);res().System().OpenDialog(seks, "Заголовок", entries, (_)=>{}); public sealed partial class QuickDialogSystem { /// @@ -549,4 +552,53 @@ public sealed partial class QuickDialogSystem cancelAction ?? (() => { }) ); } + + + /// + /// Opens a dialog for the given client, with any amount of entries, allowing them to enter in the desired data. For the truly unhinged. + /// + /// Client to show a dialog for. + /// Title of the dialog. + /// List of tuples, not QuickDialogEntries. + /// The action to execute upon Ok being pressed. + /// The action to execute upon the dialog being cancelled. + /// + /// Tuple structure for dialogEntries argument: + /// Type - int/float/string/LongString/Hex16/VoidOption/null (VoidOption) + /// string - prompt text + /// object - default value. No checks are performed whether or not it matches the specified type. + /// + + [PublicAPI] + public void OpenDialog(ICommonSession session, string title, List<(Type, string, object)> dialogEntries, Action okAction, Action? cancelAction = null) + { + List _dialogEntries = new(); + + for(int i = 0; i < dialogEntries.Count; i++) + { + var (type, prompt, defaultValue) = dialogEntries[i]; + _dialogEntries.Add(new QuickDialogEntry((i+1).ToString(), TypeToEntryType(type), prompt??" ", null, defaultValue)); + } // ^^^ these "indexes" start with 1, for some reason + + + OpenDialogInternal( + session, + title, + _dialogEntries, + QuickDialogButtonFlag.OkButton | QuickDialogButtonFlag.CancelButton, + (ev => + { + if (TryParseQuickDialogList(_dialogEntries, ev.Responses, out var results)) + { + okAction.Invoke(results!); + } + else + { + session.Channel.Disconnect("Replied with invalid quick dialog data."); + cancelAction?.Invoke(); + } + }), + cancelAction ?? (() => { }) + ); + } } diff --git a/Content.Server/Administration/QuickDialogSystem.cs b/Content.Server/Administration/QuickDialogSystem.cs index 89f0db3c16..4b21f19588 100644 --- a/Content.Server/Administration/QuickDialogSystem.cs +++ b/Content.Server/Administration/QuickDialogSystem.cs @@ -105,6 +105,27 @@ public sealed partial class QuickDialogSystem : EntitySystem _openDialogsByUser[session.UserId].Add(did); } + private bool TryParseQuickDialogList(List entries, Dictionary responces, out object[]? output) + { + if(entries.Count != responces.Count) + { + output = null; + return false; + } + + output = new object[entries.Count]; + for(int i = 0; i < entries.Count; i++) + { + var entryType = entries[i].Type; + var input = responces[(i+1).ToString()]; //starts with "1" + if(!TryParseQuickDialog(entryType, input, out object? o)) + { + return false; + } + output[i] = o; + } + return true; + } private bool TryParseQuickDialog(QuickDialogEntryType entryType, string input, [NotNullWhen(true)] out T? output) { @@ -181,7 +202,7 @@ public sealed partial class QuickDialogSystem : EntitySystem } } - private QuickDialogEntryType TypeToEntryType(Type T) + public QuickDialogEntryType TypeToEntryType(Type T) { // yandere station much? if (T == typeof(int) || T == typeof(uint) || T == typeof(long) || T == typeof(ulong)) @@ -202,7 +223,7 @@ public sealed partial class QuickDialogSystem : EntitySystem if (T == typeof(bool)) return QuickDialogEntryType.Boolean; - if (T == typeof(VoidOption)) + if (T == typeof(VoidOption) || T == null) return QuickDialogEntryType.Void; diff --git a/Content.Shared/Administration/QuickDialogOpenEvent.cs b/Content.Shared/Administration/QuickDialogOpenEvent.cs index 71a292474b..656d14fea8 100644 --- a/Content.Shared/Administration/QuickDialogOpenEvent.cs +++ b/Content.Shared/Administration/QuickDialogOpenEvent.cs @@ -1,4 +1,5 @@ using Robust.Shared.Serialization; +using System.ComponentModel; namespace Content.Shared.Administration; @@ -87,17 +88,22 @@ public sealed class QuickDialogEntry /// public string Prompt; + /// + /// Default value in the window. + /// + public object? Value; /// /// String to replace the type-specific placeholder with. /// public string? Placeholder; - public QuickDialogEntry(string fieldId, QuickDialogEntryType type, string prompt, string? placeholder = null) + public QuickDialogEntry(string fieldId, QuickDialogEntryType type, string prompt, string? placeholder = null, object? defaultValue = null) { FieldId = fieldId; Type = type; Prompt = prompt; Placeholder = placeholder; + Value = defaultValue; } }