mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-02-15 11:40:52 +01:00
98 lines
3.0 KiB
C#
98 lines
3.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Robust.Client.AutoGenerated;
|
|
using Robust.Client.UserInterface.Controls;
|
|
using Robust.Client.UserInterface.CustomControls;
|
|
using Robust.Client.UserInterface.XAML;
|
|
using Robust.Shared.Localization;
|
|
using Robust.Shared.Maths;
|
|
|
|
namespace Robust.Client.ViewVariables
|
|
{
|
|
[GenerateTypedNameReferences]
|
|
public partial class ViewVariablesAddComponentWindow : SS14Window
|
|
{
|
|
protected override Vector2? CustomSize => (200f, 300f);
|
|
|
|
private string? _lastSearch;
|
|
private string[] _components = Array.Empty<string>();
|
|
|
|
public event Action<AddComponentButtonPressedEventArgs>? AddComponentButtonPressed;
|
|
|
|
public ViewVariablesAddComponentWindow(IEnumerable<string> components, bool serverside)
|
|
{
|
|
RobustXamlLoader.Load(this);
|
|
|
|
Title = Loc.GetString(serverside ? "Add Component [S]" : "Add Component [C]");
|
|
|
|
ComponentItemList.OnItemSelected += _ => RefreshAddButton();
|
|
ComponentItemList.OnItemDeselected += _ => RefreshAddButton();
|
|
SearchLineEdit.OnTextChanged += OnSearchTextChanged;
|
|
AddButton.OnPressed += OnAddButtonPressed;
|
|
|
|
Populate(components);
|
|
}
|
|
|
|
private void RefreshAddButton()
|
|
{
|
|
AddButton.Disabled = !ComponentItemList.GetSelected().Any();
|
|
}
|
|
|
|
public void Populate(IEnumerable<string> components)
|
|
{
|
|
_components = components.ToArray();
|
|
Array.Sort(_components);
|
|
Populate(_lastSearch);
|
|
}
|
|
|
|
private void Populate(string? search = null)
|
|
{
|
|
_lastSearch = search;
|
|
ComponentItemList.ClearSelected();
|
|
ComponentItemList.Clear();
|
|
AddButton.Disabled = true;
|
|
|
|
foreach (var component in _components)
|
|
{
|
|
if(!string.IsNullOrEmpty(search) && !component.Contains(search, StringComparison.InvariantCultureIgnoreCase))
|
|
continue;
|
|
|
|
ComponentItemList.AddItem(component);
|
|
}
|
|
}
|
|
|
|
private void OnSearchTextChanged(LineEdit.LineEditEventArgs obj)
|
|
{
|
|
Populate(obj.Text);
|
|
}
|
|
|
|
private void OnAddButtonPressed(BaseButton.ButtonEventArgs obj)
|
|
{
|
|
var selected = ComponentItemList.GetSelected().ToArray();
|
|
|
|
// Nothing to do here!
|
|
if (selected.Length == 0)
|
|
return;
|
|
|
|
var comp = selected[0];
|
|
|
|
// This shouldn't really happen.
|
|
if (comp.Text == null)
|
|
return;
|
|
|
|
AddComponentButtonPressed?.Invoke(new AddComponentButtonPressedEventArgs(comp.Text));
|
|
}
|
|
|
|
public class AddComponentButtonPressedEventArgs : EventArgs
|
|
{
|
|
public string Component { get; }
|
|
|
|
public AddComponentButtonPressedEventArgs(string component)
|
|
{
|
|
Component = component;
|
|
}
|
|
}
|
|
}
|
|
}
|