Chameleon clothing (#8444)

Co-authored-by: Moony <moonheart08@users.noreply.github.com>
Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
This commit is contained in:
Alex Evgrashin
2022-09-14 10:42:14 +02:00
committed by GitHub
parent 54947c137c
commit 9ce3a18e3f
33 changed files with 868 additions and 29 deletions

View File

@@ -0,0 +1,57 @@
using Content.Client.Clothing.Systems;
using Content.Shared.Clothing.Components;
using JetBrains.Annotations;
using Robust.Client.GameObjects;
namespace Content.Client.Clothing.UI;
[UsedImplicitly]
public sealed class ChameleonBoundUserInterface : BoundUserInterface
{
[Dependency] private readonly IEntityManager _entityManager = default!;
private readonly ChameleonClothingSystem _chameleon;
private ChameleonMenu? _menu;
public ChameleonBoundUserInterface(ClientUserInterfaceComponent owner, Enum uiKey) : base(owner, uiKey)
{
IoCManager.InjectDependencies(this);
_chameleon = _entityManager.System<ChameleonClothingSystem>();
}
protected override void Open()
{
base.Open();
_menu = new ChameleonMenu();
_menu.OnClose += Close;
_menu.OnIdSelected += OnIdSelected;
_menu.OpenCentered();
}
protected override void UpdateState(BoundUserInterfaceState state)
{
base.UpdateState(state);
if (state is not ChameleonBoundUserInterfaceState st)
return;
var targets = _chameleon.GetValidTargets(st.Slot);
_menu?.UpdateState(targets, st.SelectedId);
}
private void OnIdSelected(string selectedId)
{
SendMessage(new ChameleonPrototypeSelectedMessage(selectedId));
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing)
{
_menu?.Close();
_menu = null;
}
}
}

View File

@@ -0,0 +1,12 @@
<DefaultWindow xmlns="https://spacestation14.io"
Title="{Loc 'chameleon-component-ui-window-name'}"
MinSize="250 300"
SetSize="250 300">
<BoxContainer Orientation="Vertical">
<LineEdit Name="Search" PlaceHolder = "{Loc 'chameleon-component-ui-search-placeholder'}"/>
<ScrollContainer VerticalExpand="True">
<GridContainer Name="Grid" Columns="3" Margin="0 5" >
</GridContainer>
</ScrollContainer>
</BoxContainer>
</DefaultWindow>

View File

@@ -0,0 +1,90 @@
using System.Linq;
using Content.Client.Clothing.Systems;
using Content.Client.Stylesheets;
using Robust.Client.AutoGenerated;
using Robust.Client.GameObjects;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Prototypes;
namespace Content.Client.Clothing.UI;
[GenerateTypedNameReferences]
public sealed partial class ChameleonMenu : DefaultWindow
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IEntityManager _entityManager = default!;
private readonly SpriteSystem _sprite;
public event Action<string>? OnIdSelected;
private IEnumerable<string> _possibleIds = Enumerable.Empty<string>();
private string? _selectedId;
private string _searchFilter = "";
public ChameleonMenu()
{
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);
_sprite = _entityManager.System<SpriteSystem>();
Search.OnTextChanged += OnSearchEntered;
}
public void UpdateState(IEnumerable<string> possibleIds, string? selectedId)
{
_possibleIds = possibleIds;
_selectedId = selectedId;
UpdateGrid();
}
private void OnSearchEntered(LineEdit.LineEditEventArgs obj)
{
_searchFilter = obj.Text;
UpdateGrid();
}
private void UpdateGrid()
{
ClearGrid();
var group = new ButtonGroup();
var searchFilterLow = _searchFilter.ToLowerInvariant();
foreach (var id in _possibleIds)
{
if (!_prototypeManager.TryIndex(id, out EntityPrototype? proto))
continue;
var lowId = id.ToLowerInvariant();
var lowName = proto.Name.ToLowerInvariant();
if (!lowId.Contains(searchFilterLow) && !lowName.Contains(_searchFilter))
continue;
var button = new Button
{
MinSize = new Vector2(48, 48),
HorizontalExpand = true,
Group = group,
StyleClasses = {StyleBase.ButtonSquare},
ToggleMode = true,
Pressed = _selectedId == id,
ToolTip = proto.Name
};
button.OnPressed += _ => OnIdSelected?.Invoke(id);
Grid.AddChild(button);
var texture = _sprite.GetPrototypeIcon(proto);
button.AddChild(new TextureRect
{
Stretch = TextureRect.StretchMode.KeepAspectCentered,
Texture = texture.Default
});
}
}
private void ClearGrid()
{
Grid.RemoveAllChildren();
}
}