Add a Select button to ProtoId VV editor (#6097)

* Add a Select button to ProtoId VV editor

* Changelog

* Fix ftl string name

---------

Co-authored-by: PJB3005 <pieterjan.briers+git@gmail.com>
This commit is contained in:
Tayrtahn
2025-08-02 19:08:24 +02:00
committed by GitHub
co-authored by PJB3005
parent 121b58ee9a
commit e316649fd1
3 changed files with 66 additions and 12 deletions
+1
View File
@@ -39,6 +39,7 @@ END TEMPLATE-->
### New features
* ViewVariables editors for `ProtoId` fields now have a Select button which opens a window listing all available prototypes of the appropriate type.
* added **IConfigurationManager**.*SubscribeMultiple* ext. method to provide simpler way to unsubscribe from multiple cvar at once
* Added `SharedMapSystem.QueueDeleteMap`, which deletes a map with the specified MapId in the next tick.
* Added generic version of `ComponentRegistry.TryGetComponent`.
@@ -25,3 +25,9 @@ vv-sound-reference-distance = Reference Distance
vv-sound-loop = Loop
vv-sound-play-offset = Play Offset (s)
vv-sound-variation = Pitch variation
## ProtoId
vv-protoid-id-placeholder = Prototype ID
vv-protoid-select-button-label = Select
vv-protoid-addwindow-title = Set Prototype
@@ -1,38 +1,85 @@
using System.Linq;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Prototypes;
namespace Robust.Client.ViewVariables.Editors;
internal sealed class VVPropEditorProtoId<T> : VVPropEditor where T : class, IPrototype
{
[Dependency] private readonly ILocalizationManager _loc = default!;
[Dependency] private readonly IPrototypeManager _protoManager = default!;
private ViewVariablesAddWindow? _addWindow;
private LineEdit? _lineEdit;
protected override Control MakeUI(object? value)
{
var lineEdit = new LineEdit
// ID LineEdit
_lineEdit = new LineEdit
{
Text = (ProtoId<T>) (value ?? ""),
Text = (ProtoId<T>)(value ?? ""),
PlaceHolder = _loc.GetString("vv-protoid-id-placeholder"),
Editable = !ReadOnly,
HorizontalExpand = true,
};
if (!ReadOnly)
{
lineEdit.OnTextEntered += e =>
_lineEdit.OnTextEntered += e =>
{
var id = (ProtoId<T>)e.Text;
if (!_protoManager.HasIndex(id))
{
return;
}
ValueChanged(id);
SetValue(e.Text);
};
}
return lineEdit;
// Select button
var selectButton = new Button
{
Text = _loc.GetString("vv-protoid-select-button-label"),
Disabled = ReadOnly,
};
selectButton.OnPressed += OnListButtonPressed;
// Container
var hBox = new BoxContainer
{
Orientation = BoxContainer.LayoutOrientation.Horizontal,
HorizontalExpand = true,
Children =
{
_lineEdit,
selectButton,
}
};
return hBox;
}
private void OnListButtonPressed(BaseButton.ButtonEventArgs args)
{
_addWindow?.Close();
var list = _protoManager.EnumeratePrototypes<T>().Select(p => p.ID);
_addWindow = new ViewVariablesAddWindow(list, _loc.GetString("vv-protoid-addwindow-title"));
_addWindow.AddButtonPressed += OnAddButtonPressed;
_addWindow.OpenCentered();
}
private void OnAddButtonPressed(ViewVariablesAddWindow.AddButtonPressedEventArgs args)
{
_lineEdit?.SetText(args.Entry);
_addWindow?.Close();
SetValue(args.Entry);
}
private void SetValue(string value)
{
var proto = (ProtoId<T>)value;
if (_protoManager.HasIndex(proto))
ValueChanged(proto, false);
}
}