Add searching by component name to View Variables (#1417)

Co-authored-by: Pieter-Jan Briers <pieterjan.briers+git@gmail.com>
This commit is contained in:
DrSmugleaf
2020-11-26 02:01:27 +01:00
committed by GitHub
parent 3767a7d285
commit 3ad65ca63f

View File

@@ -1,4 +1,5 @@
using System.Linq;
using System;
using System.Linq;
using Robust.Client.Interfaces.GameObjects.Components;
using Robust.Client.Interfaces.ResourceManagement;
using Robust.Client.UserInterface;
@@ -7,9 +8,12 @@ using Robust.Client.UserInterface.CustomControls;
using Robust.Client.ViewVariables.Traits;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Localization;
using Robust.Shared.Maths;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
using static Robust.Client.UserInterface.Control;
using static Robust.Client.UserInterface.Controls.LineEdit;
namespace Robust.Client.ViewVariables.Instances
{
@@ -37,9 +41,14 @@ namespace Robust.Client.ViewVariables.Instances
private ViewVariablesBlobMembers? _membersBlob;
private VBoxContainer _clientComponents = default!;
private VBoxContainer _serverVariables = default!;
private VBoxContainer _serverComponents = default!;
private LineEdit _clientComponentsSearchBar = default!;
private LineEdit _serverComponentsSearchBar = default!;
private bool _serverLoaded;
public ViewVariablesInstanceEntity(IViewVariablesManagerInternal vvm, IResourceCache resCache, IEntityManager entityManager) : base(vvm, resCache)
@@ -51,15 +60,13 @@ namespace Robust.Client.ViewVariables.Instances
{
_entity = (IEntity) obj;
var type = obj.GetType();
var scrollContainer = new ScrollContainer();
//scrollContainer.SetAnchorPreset(Control.LayoutPreset.Wide, true);
window.Contents.AddChild(scrollContainer);
var vBoxContainer = new VBoxContainer
{
SizeFlagsHorizontal = Control.SizeFlags.FillExpand,
SizeFlagsVertical = Control.SizeFlags.FillExpand,
SizeFlagsHorizontal = SizeFlags.FillExpand,
SizeFlagsVertical = SizeFlags.FillExpand,
};
scrollContainer.AddChild(vBoxContainer);
@@ -90,7 +97,7 @@ namespace Robust.Client.ViewVariables.Instances
if (_entity.TryGetComponent(out ISpriteComponent? sprite))
{
var hBox = new HBoxContainer();
top.SizeFlagsHorizontal = Control.SizeFlags.FillExpand;
top.SizeFlagsHorizontal = SizeFlags.FillExpand;
hBox.AddChild(top);
hBox.AddChild(new SpriteView {Sprite = sprite});
vBoxContainer.AddChild(hBox);
@@ -123,17 +130,26 @@ namespace Robust.Client.ViewVariables.Instances
}
}
var clientComponents = new VBoxContainer {SeparationOverride = 0};
_tabs.AddChild(clientComponents);
_clientComponents = new VBoxContainer {SeparationOverride = 0};
_tabs.AddChild(_clientComponents);
_tabs.SetTabTitle(TabClientComponents, "Client Components");
_clientComponents.AddChild(_clientComponentsSearchBar = new LineEdit
{
PlaceHolder = Loc.GetString("Search"),
SizeFlagsHorizontal = SizeFlags.FillExpand
});
_clientComponentsSearchBar.OnTextChanged += OnClientComponentsSearchBarChanged;
// See engine#636 for why the Distinct() call.
var componentList = _entity.GetAllComponents().OrderBy(c => c.GetType().ToString());
foreach (var component in componentList)
{
var button = new Button {Text = TypeAbbreviation.Abbreviate(component.GetType()), TextAlign = Label.AlignMode.Left};
button.OnPressed += args => { ViewVariablesManager.OpenVV(component); };
clientComponents.AddChild(button);
_clientComponents.AddChild(button);
}
if (!_entity.Uid.IsClientSide())
@@ -145,9 +161,97 @@ namespace Robust.Client.ViewVariables.Instances
_serverComponents = new VBoxContainer {SeparationOverride = 0};
_tabs.AddChild(_serverComponents);
_tabs.SetTabTitle(TabServerComponents, "Server Components");
_serverComponents.AddChild(_serverComponentsSearchBar = new LineEdit
{
PlaceHolder = Loc.GetString("Search"),
SizeFlagsHorizontal = SizeFlags.FillExpand
});
_serverComponentsSearchBar.OnTextChanged += OnServerComponentsSearchBarChanged;
}
}
private void UpdateClientComponentListVisibility(string? searchStr = null)
{
if (string.IsNullOrEmpty(searchStr))
{
foreach (var child in _clientComponents.Children)
{
child.Visible = true;
}
return;
}
foreach (var child in _clientComponents.Children)
{
if (child is not Button button)
{
continue;
}
if (button.Text == null)
{
button.Visible = false;
continue;
}
if (!button.Text.Contains(searchStr, StringComparison.InvariantCultureIgnoreCase))
{
button.Visible = false;
continue;
}
button.Visible = true;
}
}
private void UpdateServerComponentListVisibility(string? searchStr = null)
{
if (string.IsNullOrEmpty(searchStr))
{
foreach (var child in _serverComponents.Children)
{
child.Visible = true;
}
return;
}
foreach (var child in _serverComponents.Children)
{
if (child is not Button button)
{
continue;
}
if (button.Text == null)
{
button.Visible = false;
continue;
}
if (!button.Text.Contains(searchStr, StringComparison.InvariantCultureIgnoreCase))
{
button.Visible = false;
continue;
}
button.Visible = true;
}
}
private void OnClientComponentsSearchBarChanged(LineEditEventArgs args)
{
UpdateClientComponentListVisibility(args.Text);
}
private void OnServerComponentsSearchBarChanged(LineEditEventArgs args)
{
UpdateServerComponentListVisibility(args.Text);
}
public override async void Initialize(SS14Window window, ViewVariablesBlobMetadata blob, ViewVariablesRemoteSession session)
{
// TODO: this is pretty poorly implemented right now.
@@ -224,8 +328,29 @@ namespace Robust.Client.ViewVariables.Instances
var componentsBlob = await ViewVariablesManager.RequestData<ViewVariablesBlobEntityComponents>(_entitySession, new ViewVariablesRequestEntityComponents());
_serverComponents.DisposeAllChildren();
_serverComponents.AddChild(_serverComponentsSearchBar = new LineEdit
{
PlaceHolder = Loc.GetString("Search"),
SizeFlagsHorizontal = SizeFlags.FillExpand
});
_serverComponentsSearchBar.OnTextChanged += OnServerComponentsSearchBarChanged;
componentsBlob.ComponentTypes.Sort();
foreach (var componentType in componentsBlob.ComponentTypes.OrderBy(t => t.Stringified))
var componentTypes = componentsBlob.ComponentTypes.AsEnumerable();
if (!string.IsNullOrEmpty(_serverComponentsSearchBar.Text))
{
componentTypes = componentTypes
.Where(t => t.Stringified.Contains(_serverComponentsSearchBar.Text,
StringComparison.InvariantCultureIgnoreCase));
}
componentTypes = componentTypes.OrderBy(t => t.Stringified);
foreach (var componentType in componentTypes)
{
var button = new Button {Text = componentType.Stringified, TextAlign = Label.AlignMode.Left};
button.OnPressed += args =>