"""апгрейд""" QuickDialogSystem (#133)

* It's a surprise tool that will help us later

* Are you feeling it now mr. krabs?

* ой блять

* транслате
This commit is contained in:
RedFoxIV
2024-02-27 14:58:54 +03:00
committed by GitHub
parent aeac51afe6
commit 45584a3337
6 changed files with 550 additions and 22 deletions

View File

@@ -2,6 +2,9 @@ using Content.Shared.Administration;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Sandboxing;
using Robust.Client.UserInterface;
using System.Linq;
namespace Content.Client.UserInterface.Controls;
@@ -31,7 +34,9 @@ public sealed partial class DialogWindow : FancyWindow
/// </summary>
private bool _finished;
private List<(string, LineEdit)> _promptLines;
private List<(string, Func<string>)> _promptLines;
private delegate (Control, Func<string>) ControlConstructor(Func<string, bool> verifier, string name, QuickDialogEntry entry, bool last);// are you feeling it now mr krabs?
/// <summary>
/// Create and open a new dialog with some prompts.
@@ -61,29 +66,38 @@ public sealed partial class DialogWindow : FancyWindow
var box = new BoxContainer();
box.AddChild(new Label() { Text = entry.Prompt, HorizontalExpand = true, SizeFlagsStretchRatio = 0.5f });
var edit = new LineEdit() { HorizontalExpand = true };
//var edit = new LineEdit() { HorizontalExpand = true };
(Func<string, bool>, string) pair = entry.Type switch
// 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,
// and returns another tuple:
// item 1 is the control itself
// item 2 is a Func<string>, a """generic""" function that returns whatever user has done with the control
(ControlConstructor, Func<string, bool>?, string) notapairanymore = entry.Type switch
{
QuickDialogEntryType.Integer => (VerifyInt, "integer"),
QuickDialogEntryType.Float => (VerifyFloat, "float"),
QuickDialogEntryType.ShortText => (VerifyShortText, "short-text"),
QuickDialogEntryType.LongText => (VerifyLongText, "long-text"),
QuickDialogEntryType.Integer => (SetupLineEditNumber, VerifyInt, "integer"),
QuickDialogEntryType.Float => (SetupLineEditNumber, VerifyFloat, "float"),
QuickDialogEntryType.ShortText => (SetupLineEdit, VerifyShortText, "short-text"),
QuickDialogEntryType.LongText => (SetupLineEdit, VerifyLongText, "long-text"),
QuickDialogEntryType.Hex16 => (SetupLineEditHex, VerifyHex16, "hex16"),
QuickDialogEntryType.Boolean => (SetupCheckBox, null, "boolean"),
QuickDialogEntryType.Void => (SetupVoid, null, "void"),
_ => throw new ArgumentOutOfRangeException()
};
var (valid, name) = pair;
var (setup, valid, name) = notapairanymore;
edit.IsValid += valid;
// try use placeholder from the caller, fall back to the generic one for whatever type is being validated.
edit.PlaceHolder = entry.Placeholder ?? Loc.GetString($"quick-dialog-ui-{name}");
// Last text box gets enter confirmation.
// Only the last so you don't accidentally confirm early.
if (i == entries.Count - 1)
edit.OnTextEntered += _ => Confirm();
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
_promptLines.Add((entry.FieldId, returner));
_promptLines.Add((entry.FieldId, edit));
box.AddChild(edit);
box.AddChild(control);
Prompts.AddChild(box);
}
@@ -110,9 +124,9 @@ public sealed partial class DialogWindow : FancyWindow
private void Confirm()
{
var results = new Dictionary<string, string>();
foreach (var (field, edit) in _promptLines)
foreach (var (field, returner) in _promptLines)
{
results[field] = edit.Text;
results[field] = returner();
}
_finished = true;
@@ -143,5 +157,57 @@ public sealed partial class DialogWindow : FancyWindow
return input.Length <= 2000;
}
private bool VerifyHex16(string input)
{
return input.Length <= 4 && int.TryParse(input, System.Globalization.NumberStyles.HexNumber, null, out int _);
}
private (Control, Func<string>) SetupLineEdit(Func<string, bool> valid, string name, QuickDialogEntry entry, bool last) // 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.
edit.PlaceHolder = name;
// Last text box gets enter confirmation.
// Only the last so you don't accidentally confirm early.
if (last)
edit.OnTextEntered += _ => Confirm();
return (edit, () => {return edit.Text;} );
}
private (Control, Func<string>) SetupLineEditNumber(Func<string, bool> valid, string name, QuickDialogEntry entry, bool last)
{
var (control, returner) = SetupLineEdit(valid, name, entry, last);
var le = (LineEdit) control;
le.Text = "0";
return (control, ()=> le.Text.Length > 0 ? le.Text : "0"); // Otherwise you'll get kicked for malformed data
}
private (Control, Func<string>) SetupLineEditHex(Func<string, bool> valid, string name, QuickDialogEntry entry, bool last)
{
var (control, returner) = SetupLineEditNumber(valid, name, entry, last);
((LineEdit) control).OnTextChanged += e => { e.Control.Text = e.Text.ToUpper(); };
return (control, returner);
}
private (Control, Func<string>) SetupCheckBox(Func<string, bool> _, string name, QuickDialogEntry entry, bool last)
{
var check = new CheckBox() { HorizontalExpand = true, HorizontalAlignment = HAlignment.Right };
check.Text = name;
return (check, () => { return check.Pressed ? "true" : "false"; });
}
private (Control, Func<string>) SetupVoid(Func<string, bool> _, string __, QuickDialogEntry ___, bool ____)
{
var control = new Control();
control.Visible = false;
return (control, () => "" );
}
#endregion
}