Skip to content

Commit

Permalink
Fix VV for entity prototypes (#4956)
Browse files Browse the repository at this point in the history
* Fix VV for entity prototypes

* Fix ProtoId
  • Loading branch information
metalgearsloth committed Mar 15, 2024
1 parent 15f4da5 commit 5aaf6d0
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
2 changes: 1 addition & 1 deletion RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ END TEMPLATE-->

### Bugfixes

*None yet*
* Fix VV for prototype structs.

### Other

Expand Down
16 changes: 16 additions & 0 deletions Robust.Client/ViewVariables/ClientViewVariablesManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))!;
Expand Down
28 changes: 28 additions & 0 deletions Robust.Client/ViewVariables/Editors/VVPropEditorEntProtoId.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
38 changes: 38 additions & 0 deletions Robust.Client/ViewVariables/Editors/VVPropEditorProtoId.cs
Original file line number Diff line number Diff line change
@@ -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<T> : VVPropEditor where T : class, IPrototype
{
[Dependency] private readonly IPrototypeManager _protoManager = default!;

protected override Control MakeUI(object? value)
{
var lineEdit = new LineEdit
{
Text = (ProtoId<T>) (value ?? ""),
Editable = !ReadOnly,
HorizontalExpand = true,
};

if (!ReadOnly)
{
lineEdit.OnTextEntered += e =>
{
var id = (ProtoId<T>)e.Text;
if (!_protoManager.HasIndex(id))
{
return;
}
ValueChanged(id);
};
}

return lineEdit;
}
}

0 comments on commit 5aaf6d0

Please sign in to comment.