SprayPainterWindow.xaml.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using Robust.Client.AutoGenerated;
  2. using Robust.Client.GameObjects;
  3. using Robust.Client.UserInterface.Controls;
  4. using Robust.Client.UserInterface.CustomControls;
  5. using Robust.Client.UserInterface.XAML;
  6. using Robust.Shared.Utility;
  7. namespace Content.Client.SprayPainter.UI;
  8. [GenerateTypedNameReferences]
  9. public sealed partial class SprayPainterWindow : DefaultWindow
  10. {
  11. [Dependency] private readonly IEntitySystemManager _sysMan = default!;
  12. private readonly SpriteSystem _spriteSystem;
  13. public Action<ItemList.ItemListSelectedEventArgs>? OnSpritePicked;
  14. public Action<ItemList.ItemListSelectedEventArgs>? OnColorPicked;
  15. public Dictionary<string, int> ItemColorIndex = new();
  16. private Dictionary<string, Color> currentPalette = new();
  17. private const string colorLocKeyPrefix = "pipe-painter-color-";
  18. private List<SprayPainterEntry> CurrentEntries = new List<SprayPainterEntry>();
  19. private readonly SpriteSpecifier _colorEntryIconTexture = new SpriteSpecifier.Rsi(
  20. new ResPath("Structures/Piping/Atmospherics/pipe.rsi"),
  21. "pipeStraight");
  22. public SprayPainterWindow()
  23. {
  24. RobustXamlLoader.Load(this);
  25. IoCManager.InjectDependencies(this);
  26. _spriteSystem = _sysMan.GetEntitySystem<SpriteSystem>();
  27. }
  28. private static string GetColorLocString(string? colorKey)
  29. {
  30. if (string.IsNullOrEmpty(colorKey))
  31. return Loc.GetString("pipe-painter-no-color-selected");
  32. var locKey = colorLocKeyPrefix + colorKey;
  33. if (!Loc.TryGetString(locKey, out var locString))
  34. locString = colorKey;
  35. return locString;
  36. }
  37. public string? IndexToColorKey(int index)
  38. {
  39. return (string?) ColorList[index].Metadata;
  40. }
  41. public void Populate(List<SprayPainterEntry> entries, int selectedStyle, string? selectedColorKey, Dictionary<string, Color> palette)
  42. {
  43. // Only clear if the entries change. Otherwise the list would "jump" after selecting an item
  44. if (!CurrentEntries.Equals(entries))
  45. {
  46. CurrentEntries = entries;
  47. SpriteList.Clear();
  48. foreach (var entry in entries)
  49. {
  50. SpriteList.AddItem(entry.Name, entry.Icon);
  51. }
  52. }
  53. if (!currentPalette.Equals(palette))
  54. {
  55. currentPalette = palette;
  56. ItemColorIndex.Clear();
  57. ColorList.Clear();
  58. foreach (var color in palette)
  59. {
  60. var locString = GetColorLocString(color.Key);
  61. var item = ColorList.AddItem(locString, _spriteSystem.Frame0(_colorEntryIconTexture));
  62. item.IconModulate = color.Value;
  63. item.Metadata = color.Key;
  64. ItemColorIndex.Add(color.Key, ColorList.IndexOf(item));
  65. }
  66. }
  67. // Disable event so we don't send a new event for pre-selectedStyle entry and end up in a loop
  68. if (selectedColorKey != null)
  69. {
  70. var index = ItemColorIndex[selectedColorKey];
  71. ColorList.OnItemSelected -= OnColorPicked;
  72. ColorList[index].Selected = true;
  73. ColorList.OnItemSelected += OnColorPicked;
  74. }
  75. SpriteList.OnItemSelected -= OnSpritePicked;
  76. SpriteList[selectedStyle].Selected = true;
  77. SpriteList.OnItemSelected += OnSpritePicked;
  78. }
  79. }