From 5aaf6d09946cf333bc525d44a08e0c633da8e8f1 Mon Sep 17 00:00:00 2001 From: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Date: Fri, 15 Mar 2024 20:16:09 +1100 Subject: [PATCH] Fix VV for entity prototypes (#4956) * Fix VV for entity prototypes * Fix ProtoId --- RELEASE-NOTES.md | 2 +- .../ClientViewVariablesManager.cs | 16 ++++++++ .../Editors/VVPropEditorEntProtoId.cs | 28 ++++++++++++++ .../Editors/VVPropEditorProtoId.cs | 38 +++++++++++++++++++ 4 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 Robust.Client/ViewVariables/Editors/VVPropEditorEntProtoId.cs create mode 100644 Robust.Client/ViewVariables/Editors/VVPropEditorProtoId.cs diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md index f8e739c1abf..b67bcabbd31 100644 --- a/RELEASE-NOTES.md +++ b/RELEASE-NOTES.md @@ -43,7 +43,7 @@ END TEMPLATE--> ### Bugfixes -*None yet* +* Fix VV for prototype structs. ### Other diff --git a/Robust.Client/ViewVariables/ClientViewVariablesManager.cs b/Robust.Client/ViewVariables/ClientViewVariablesManager.cs index 9bf6c3916b1..f8045ec94d0 100644 --- a/Robust.Client/ViewVariables/ClientViewVariablesManager.cs +++ b/Robust.Client/ViewVariables/ClientViewVariablesManager.cs @@ -126,6 +126,22 @@ public VVPropEditor PropertyFor(Type? type) return new VVPropEditorString(); } + if (type == typeof(EntProtoId) || + type == typeof(EntProtoId?)) + { + return new VVPropEditorEntProtoId(); + } + + if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(ProtoId<>)) + { + var editor = + (VVPropEditor)Activator.CreateInstance( + typeof(VVPropEditorProtoId<>).MakeGenericType(type.GenericTypeArguments[0]))!; + + IoCManager.InjectDependencies(editor); + return editor; + } + if (typeof(IPrototype).IsAssignableFrom(type) || typeof(ViewVariablesBlobMembers.PrototypeReferenceToken).IsAssignableFrom(type)) { return (VVPropEditor)Activator.CreateInstance(typeof(VVPropEditorIPrototype<>).MakeGenericType(type))!; diff --git a/Robust.Client/ViewVariables/Editors/VVPropEditorEntProtoId.cs b/Robust.Client/ViewVariables/Editors/VVPropEditorEntProtoId.cs new file mode 100644 index 00000000000..512c5f8f7df --- /dev/null +++ b/Robust.Client/ViewVariables/Editors/VVPropEditorEntProtoId.cs @@ -0,0 +1,28 @@ +using Robust.Client.UserInterface; +using Robust.Client.UserInterface.Controls; +using Robust.Shared.Prototypes; + +namespace Robust.Client.ViewVariables.Editors; + +internal sealed class VVPropEditorEntProtoId : VVPropEditor +{ + protected override Control MakeUI(object? value) + { + var lineEdit = new LineEdit + { + Text = (EntProtoId) (value ?? ""), + Editable = !ReadOnly, + HorizontalExpand = true, + }; + + if (!ReadOnly) + { + lineEdit.OnTextEntered += e => + { + ValueChanged((EntProtoId) e.Text); + }; + } + + return lineEdit; + } +} diff --git a/Robust.Client/ViewVariables/Editors/VVPropEditorProtoId.cs b/Robust.Client/ViewVariables/Editors/VVPropEditorProtoId.cs new file mode 100644 index 00000000000..bf288191e60 --- /dev/null +++ b/Robust.Client/ViewVariables/Editors/VVPropEditorProtoId.cs @@ -0,0 +1,38 @@ +using Robust.Client.UserInterface; +using Robust.Client.UserInterface.Controls; +using Robust.Shared.IoC; +using Robust.Shared.Prototypes; + +namespace Robust.Client.ViewVariables.Editors; + +internal sealed class VVPropEditorProtoId : VVPropEditor where T : class, IPrototype +{ + [Dependency] private readonly IPrototypeManager _protoManager = default!; + + protected override Control MakeUI(object? value) + { + var lineEdit = new LineEdit + { + Text = (ProtoId) (value ?? ""), + Editable = !ReadOnly, + HorizontalExpand = true, + }; + + if (!ReadOnly) + { + lineEdit.OnTextEntered += e => + { + var id = (ProtoId)e.Text; + + if (!_protoManager.HasIndex(id)) + { + return; + } + + ValueChanged(id); + }; + } + + return lineEdit; + } +}