ChameleonBoundUserInterface.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using Content.Client.Clothing.Systems;
  2. using Content.Shared.Clothing.Components;
  3. using Content.Shared.Tag;
  4. using Content.Shared.Prototypes;
  5. using JetBrains.Annotations;
  6. using Robust.Client.GameObjects;
  7. using Robust.Client.UserInterface;
  8. using Robust.Shared.Prototypes;
  9. namespace Content.Client.Clothing.UI;
  10. [UsedImplicitly]
  11. public sealed class ChameleonBoundUserInterface : BoundUserInterface
  12. {
  13. [Dependency] private readonly IComponentFactory _factory = default!;
  14. [Dependency] private readonly IPrototypeManager _proto = default!;
  15. private readonly ChameleonClothingSystem _chameleon;
  16. private readonly TagSystem _tag;
  17. [ViewVariables]
  18. private ChameleonMenu? _menu;
  19. public ChameleonBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey)
  20. {
  21. _chameleon = EntMan.System<ChameleonClothingSystem>();
  22. _tag = EntMan.System<TagSystem>();
  23. }
  24. protected override void Open()
  25. {
  26. base.Open();
  27. _menu = this.CreateWindow<ChameleonMenu>();
  28. _menu.OnIdSelected += OnIdSelected;
  29. }
  30. protected override void UpdateState(BoundUserInterfaceState state)
  31. {
  32. base.UpdateState(state);
  33. if (state is not ChameleonBoundUserInterfaceState st)
  34. return;
  35. var targets = _chameleon.GetValidTargets(st.Slot);
  36. if (st.RequiredTag != null)
  37. {
  38. var newTargets = new List<string>();
  39. foreach (var target in targets)
  40. {
  41. if (string.IsNullOrEmpty(target) || !_proto.TryIndex(target, out EntityPrototype? proto))
  42. continue;
  43. if (!proto.TryGetComponent(out TagComponent? tag, _factory) || !_tag.HasTag(tag, st.RequiredTag))
  44. continue;
  45. newTargets.Add(target);
  46. }
  47. _menu?.UpdateState(newTargets, st.SelectedId);
  48. } else
  49. {
  50. _menu?.UpdateState(targets, st.SelectedId);
  51. }
  52. }
  53. private void OnIdSelected(string selectedId)
  54. {
  55. SendMessage(new ChameleonPrototypeSelectedMessage(selectedId));
  56. }
  57. }