1
0

CrayonWindow.xaml.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Content.Client.Stylesheets;
  4. using Content.Shared.Crayon;
  5. using Content.Shared.Decals;
  6. using Robust.Client.AutoGenerated;
  7. using Robust.Client.GameObjects;
  8. using Robust.Client.Graphics;
  9. using Robust.Client.UserInterface.Controls;
  10. using Robust.Client.UserInterface.CustomControls;
  11. using Robust.Client.UserInterface.XAML;
  12. using Robust.Client.Utility;
  13. using Robust.Shared.Graphics;
  14. using Robust.Shared.Maths;
  15. using Robust.Shared.Utility;
  16. using static Robust.Client.UserInterface.Controls.BaseButton;
  17. namespace Content.Client.Crayon.UI
  18. {
  19. [GenerateTypedNameReferences]
  20. public sealed partial class CrayonWindow : DefaultWindow
  21. {
  22. [Dependency] private readonly IEntitySystemManager _entitySystem = default!;
  23. private readonly SpriteSystem _spriteSystem = default!;
  24. private Dictionary<string, List<(string Name, Texture Texture)>>? _decals;
  25. private List<string>? _allDecals;
  26. private string? _autoSelected;
  27. private string? _selected;
  28. private Color _color;
  29. public event Action<Color>? OnColorSelected;
  30. public event Action<string>? OnSelected;
  31. public CrayonWindow()
  32. {
  33. RobustXamlLoader.Load(this);
  34. IoCManager.InjectDependencies(this);
  35. _spriteSystem = _entitySystem.GetEntitySystem<SpriteSystem>();
  36. Search.OnTextChanged += SearchChanged;
  37. ColorSelector.OnColorChanged += SelectColor;
  38. }
  39. private void SelectColor(Color color)
  40. {
  41. _color = color;
  42. OnColorSelected?.Invoke(color);
  43. RefreshList();
  44. }
  45. private void RefreshList()
  46. {
  47. // Clear
  48. Grids.DisposeAllChildren();
  49. if (_decals == null || _allDecals == null)
  50. return;
  51. var filter = Search.Text;
  52. var comma = filter.IndexOf(',');
  53. var first = (comma == -1 ? filter : filter[..comma]).Trim();
  54. var names = _decals.Keys.ToList();
  55. names.Sort((a, b) => a == "random" ? 1 : b == "random" ? -1 : a.CompareTo(b));
  56. if (_autoSelected != null && first != _autoSelected && _allDecals.Contains(first))
  57. {
  58. _selected = first;
  59. _autoSelected = _selected;
  60. OnSelected?.Invoke(_selected);
  61. }
  62. foreach (var categoryName in names)
  63. {
  64. var locName = Loc.GetString("crayon-category-" + categoryName);
  65. var category = _decals[categoryName].Where(d => locName.Contains(first) || d.Name.Contains(first)).ToList();
  66. if (category.Count == 0)
  67. continue;
  68. var label = new Label
  69. {
  70. Text = locName
  71. };
  72. var grid = new GridContainer
  73. {
  74. Columns = 6,
  75. Margin = new Thickness(0, 0, 0, 16)
  76. };
  77. Grids.AddChild(label);
  78. Grids.AddChild(grid);
  79. foreach (var (name, texture) in category)
  80. {
  81. var button = new TextureButton()
  82. {
  83. TextureNormal = texture,
  84. Name = name,
  85. ToolTip = name,
  86. Modulate = _color,
  87. Scale = new System.Numerics.Vector2(2, 2)
  88. };
  89. button.OnPressed += ButtonOnPressed;
  90. if (_selected == name)
  91. {
  92. var panelContainer = new PanelContainer()
  93. {
  94. PanelOverride = new StyleBoxFlat()
  95. {
  96. BackgroundColor = StyleNano.ButtonColorDefault,
  97. },
  98. Children =
  99. {
  100. button,
  101. },
  102. };
  103. grid.AddChild(panelContainer);
  104. }
  105. else
  106. {
  107. grid.AddChild(button);
  108. }
  109. }
  110. }
  111. }
  112. private void SearchChanged(LineEdit.LineEditEventArgs obj)
  113. {
  114. _autoSelected = ""; // Placeholder to kick off the auto-select in refreshlist()
  115. RefreshList();
  116. }
  117. private void ButtonOnPressed(ButtonEventArgs obj)
  118. {
  119. if (obj.Button.Name == null) return;
  120. _selected = obj.Button.Name;
  121. _autoSelected = null;
  122. OnSelected?.Invoke(_selected);
  123. RefreshList();
  124. }
  125. public void UpdateState(CrayonBoundUserInterfaceState state)
  126. {
  127. _selected = state.Selected;
  128. ColorSelector.Visible = state.SelectableColor;
  129. _color = state.Color;
  130. if (ColorSelector.Visible)
  131. {
  132. ColorSelector.Color = state.Color;
  133. }
  134. RefreshList();
  135. }
  136. public void AdvanceState(string drawnDecal)
  137. {
  138. var filter = Search.Text;
  139. if (!filter.Contains(',') || !filter.Contains(drawnDecal))
  140. return;
  141. var first = filter[..filter.IndexOf(',')].Trim();
  142. if (first.Equals(drawnDecal, StringComparison.InvariantCultureIgnoreCase))
  143. {
  144. Search.Text = filter[(filter.IndexOf(',') + 1)..].Trim();
  145. _autoSelected = first;
  146. }
  147. RefreshList();
  148. }
  149. public void Populate(List<DecalPrototype> prototypes)
  150. {
  151. _decals = [];
  152. _allDecals = [];
  153. prototypes.Sort((a, b) => a.ID.CompareTo(b.ID));
  154. foreach (var decalPrototype in prototypes)
  155. {
  156. var category = "random";
  157. if (decalPrototype.Tags.Count > 1 && decalPrototype.Tags[1].StartsWith("crayon-"))
  158. category = decalPrototype.Tags[1].Replace("crayon-", "");
  159. var list = _decals.GetOrNew(category);
  160. list.Add((decalPrototype.ID, _spriteSystem.Frame0(decalPrototype.Sprite)));
  161. _allDecals.Add(decalPrototype.ID);
  162. }
  163. RefreshList();
  164. }
  165. }
  166. }