Re: Re: """апгрейд""" QuickDialogSystem (#150)

* Removed Herobrine

* Added Herobrine
This commit is contained in:
RedFoxIV
2024-02-29 23:16:53 +03:00
committed by GitHub
parent 8761ce249d
commit 9b4af878b5
4 changed files with 108 additions and 21 deletions

View File

@@ -36,7 +36,7 @@ public sealed partial class DialogWindow : FancyWindow
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?
private delegate (Control, Func<string>) ControlConstructor(Func<string, bool> verifier, string name, QuickDialogEntry entry, bool last, object? value);// are you feeling it now mr krabs?
/// <summary>
/// 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<string>, 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<string>) SetupLineEdit(Func<string, bool> valid, string name, QuickDialogEntry entry, bool last) // oh shit i'm feeling it
private (Control, Func<string>) SetupLineEdit(Func<string, bool> 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<string>) SetupLineEditNumber(Func<string, bool> valid, string name, QuickDialogEntry entry, bool last)
private (Control, Func<string>) SetupLineEditNumber(Func<string, bool> 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<string>) SetupLineEditHex(Func<string, bool> valid, string name, QuickDialogEntry entry, bool last)
private (Control, Func<string>) SetupLineEditHex(Func<string, bool> 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<string>) SetupCheckBox(Func<string, bool> _, string name, QuickDialogEntry entry, bool last)
private (Control, Func<string>) SetupCheckBox(Func<string, bool> _, 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<string>) SetupVoid(Func<string, bool> _, string __, QuickDialogEntry ___, bool ____)
private (Control, Func<string>) SetupVoid(Func<string, bool> _, string __, QuickDialogEntry ___, bool ____, object? _____)
{
var control = new Control();
control.Visible = false;

View File

@@ -5,6 +5,9 @@ using Robust.Shared.Player;
namespace Content.Server.Administration;
//res<IEntityManager>().System<MindSystem>().TryGetSession(new EntityUid(X), out var seks);res<IEntityManager>().System<QuickDialogSystem>().OpenDialog<string, bool, bool, int, VoidOption, VoidOption, Hex16>(seks, "Заголовок", "Серийный код твоей матери", "Селёдкой пахнет?", "Сосал?", "Сколько ванотян жрал хуёв:", "тыгыдык тыгыдык тыгыдык тыгыдык" ," ", "Вскрываем байты", (_,_,_,_,_,_,_)=>{});
//
//List<(Type, string, object)> entries = new();entries.Add((typeof(string), "shitass", "faggot"));entries.Add((typeof(int), "cunt", 2254))
//res<IEntityManager>().System<MindSystem>().TryGetSession(new EntityUid(X), out var seks);res<IEntityManager>().System<QuickDialogSystem>().OpenDialog(seks, "Заголовок", entries, (_)=>{});
public sealed partial class QuickDialogSystem
{
/// <summary>
@@ -549,4 +552,53 @@ public sealed partial class QuickDialogSystem
cancelAction ?? (() => { })
);
}
/// <summary>
/// Opens a dialog for the given client, with any amount of entries, allowing them to enter in the desired data. For the truly unhinged.
/// </summary>
/// <param name="session">Client to show a dialog for.</param>
/// <param name="title">Title of the dialog.</param>
/// <param name="dialogEntries">List of tuples, not QuickDialogEntries.</param>
/// <param name="okAction">The action to execute upon Ok being pressed.</param>
/// <param name="cancelAction">The action to execute upon the dialog being cancelled.</param>
/// <remarks>
/// 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.
/// </remarks>
[PublicAPI]
public void OpenDialog(ICommonSession session, string title, List<(Type, string, object)> dialogEntries, Action<object[]> okAction, Action? cancelAction = null)
{
List<QuickDialogEntry> _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 ?? (() => { })
);
}
}

View File

@@ -105,6 +105,27 @@ public sealed partial class QuickDialogSystem : EntitySystem
_openDialogsByUser[session.UserId].Add(did);
}
private bool TryParseQuickDialogList(List<QuickDialogEntry> entries, Dictionary<string, string> 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<object>(entryType, input, out object? o))
{
return false;
}
output[i] = o;
}
return true;
}
private bool TryParseQuickDialog<T>(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;

View File

@@ -1,4 +1,5 @@
using Robust.Shared.Serialization;
using System.ComponentModel;
namespace Content.Shared.Administration;
@@ -87,17 +88,22 @@ public sealed class QuickDialogEntry
/// </summary>
public string Prompt;
/// <summary>
/// Default value in the window.
/// </summary>
public object? Value;
/// <summary>
/// String to replace the type-specific placeholder with.
/// </summary>
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;
}
}