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

@@ -7,17 +7,6 @@ namespace Content.Client.Clothing
[ComponentReference(typeof(SharedClothingComponent))]
public sealed class ClothingComponent : SharedClothingComponent
{
[ViewVariables(VVAccess.ReadWrite)]
[DataField("femaleMask")]
public FemaleClothingMask FemaleMask = FemaleClothingMask.UniformFull;
public string? InSlot;
}
public enum FemaleClothingMask : byte
{
NoMask = 0,
UniformFull,
UniformTop
}
}

View File

@@ -0,0 +1,110 @@
using System.Linq;
using Content.Shared.Clothing.Components;
using Content.Shared.Clothing.EntitySystems;
using Content.Shared.Inventory;
using Robust.Client.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.Prototypes;
namespace Content.Client.Clothing.Systems;
// All valid items for chameleon are calculated on client startup and stored in dictionary.
public sealed class ChameleonClothingSystem : SharedChameleonClothingSystem
{
[Dependency] private readonly IPrototypeManager _proto = default!;
[Dependency] private readonly IComponentFactory _factory = default!;
private static readonly SlotFlags[] IgnoredSlots =
{
SlotFlags.All,
SlotFlags.PREVENTEQUIP,
SlotFlags.NONE
};
private static readonly SlotFlags[] Slots = Enum.GetValues<SlotFlags>().Except(IgnoredSlots).ToArray();
private readonly Dictionary<SlotFlags, List<string>> _data = new();
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ChameleonClothingComponent, ComponentHandleState>(HandleState);
PrepareAllVariants();
_proto.PrototypesReloaded += OnProtoReloaded;
}
public override void Shutdown()
{
base.Shutdown();
_proto.PrototypesReloaded -= OnProtoReloaded;
}
private void OnProtoReloaded(PrototypesReloadedEventArgs _)
{
PrepareAllVariants();
}
private void HandleState(EntityUid uid, ChameleonClothingComponent component, ref ComponentHandleState args)
{
if (args.Current is not ChameleonClothingComponentState state)
return;
component.SelectedId = state.SelectedId;
UpdateVisuals(uid, component);
}
protected override void UpdateSprite(EntityUid uid, EntityPrototype proto)
{
base.UpdateSprite(uid, proto);
if (TryComp(uid, out SpriteComponent? sprite)
&& proto.TryGetComponent(out SpriteComponent? otherSprite, _factory))
{
sprite.CopyFrom(otherSprite);
}
}
/// <summary>
/// Get a list of valid chameleon targets for these slots.
/// </summary>
public IEnumerable<string> GetValidTargets(SlotFlags slot)
{
var set = new HashSet<string>();
foreach (var availableSlot in _data.Keys)
{
if (slot.HasFlag(availableSlot))
{
set.UnionWith(_data[availableSlot]);
}
}
return set;
}
private void PrepareAllVariants()
{
_data.Clear();
var prototypes = _proto.EnumeratePrototypes<EntityPrototype>();
foreach (var proto in prototypes)
{
// check if this is valid clothing
if (!IsValidTarget(proto))
continue;
if (!proto.TryGetComponent(out ClothingComponent? item, _factory))
continue;
// sort item by their slot flags
// one item can be placed in several buckets
foreach (var slot in Slots)
{
if (!item.Slots.HasFlag(slot))
continue;
if (!_data.ContainsKey(slot))
{
_data.Add(slot, new List<string>());
}
_data[slot].Add(proto.ID);
}
}
}
}

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();
}
}