DecalPlacerWindow.xaml.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using System.Linq;
  2. using Content.Client.Stylesheets;
  3. using Content.Shared.Decals;
  4. using Robust.Client.AutoGenerated;
  5. using Robust.Client.Graphics;
  6. using Robust.Client.UserInterface.Controls;
  7. using Robust.Client.UserInterface.CustomControls;
  8. using Robust.Client.UserInterface.XAML;
  9. using Robust.Client.Utility;
  10. using Robust.Shared.Prototypes;
  11. using static Robust.Client.UserInterface.Controls.BaseButton;
  12. namespace Content.Client.Decals.UI;
  13. [GenerateTypedNameReferences]
  14. public sealed partial class DecalPlacerWindow : DefaultWindow
  15. {
  16. [Dependency] private readonly IPrototypeManager _prototype = default!;
  17. [Dependency] private readonly IEntityManager _e = default!;
  18. private readonly DecalPlacementSystem _decalPlacementSystem;
  19. public FloatSpinBox RotationSpinBox;
  20. private PaletteColorPicker? _picker;
  21. private SortedDictionary<string, Texture>? _decals;
  22. private string? _selected;
  23. private Color _color = Color.White;
  24. private bool _useColor;
  25. private bool _snap;
  26. private float _rotation;
  27. private bool _cleanable;
  28. private int _zIndex;
  29. private bool _auto;
  30. public DecalPlacerWindow()
  31. {
  32. RobustXamlLoader.Load(this);
  33. IoCManager.InjectDependencies(this);
  34. _decalPlacementSystem = _e.System<DecalPlacementSystem>();
  35. // This needs to be done in C# so we can have custom stuff passed in the constructor
  36. // and thus have a proper step size
  37. RotationSpinBox = new FloatSpinBox(90.0f, 0)
  38. {
  39. HorizontalExpand = true
  40. };
  41. SpinBoxContainer.AddChild(RotationSpinBox);
  42. Search.OnTextChanged += _ => RefreshList();
  43. ColorPicker.OnColorChanged += OnColorPicked;
  44. PickerOpen.OnPressed += _ =>
  45. {
  46. if (_picker is null)
  47. {
  48. _picker = new PaletteColorPicker();
  49. _picker.OpenToLeft();
  50. _picker.PaletteList.OnItemSelected += args =>
  51. {
  52. var color = (args.ItemList.GetSelected().First().Metadata as Color?)!.Value;
  53. ColorPicker.Color = color;
  54. OnColorPicked(color);
  55. };
  56. }
  57. else
  58. {
  59. if (_picker.IsOpen)
  60. {
  61. _picker.Close();
  62. }
  63. else
  64. {
  65. _picker.Open();
  66. }
  67. }
  68. };
  69. RotationSpinBox.OnValueChanged += args =>
  70. {
  71. _rotation = args.Value;
  72. UpdateDecalPlacementInfo();
  73. };
  74. EnableAuto.OnToggled += args =>
  75. {
  76. _auto = args.Pressed;
  77. if (_selected != null)
  78. SelectDecal(_selected);
  79. };
  80. EnableColor.OnToggled += args =>
  81. {
  82. _useColor = args.Pressed;
  83. UpdateDecalPlacementInfo();
  84. RefreshList();
  85. };
  86. EnableSnap.OnToggled += args =>
  87. {
  88. _snap = args.Pressed;
  89. UpdateDecalPlacementInfo();
  90. };
  91. EnableCleanable.OnToggled += args =>
  92. {
  93. _cleanable = args.Pressed;
  94. UpdateDecalPlacementInfo();
  95. };
  96. ZIndexSpinBox.ValueChanged += args =>
  97. {
  98. _zIndex = args.Value;
  99. UpdateDecalPlacementInfo();
  100. };
  101. }
  102. private void OnColorPicked(Color color)
  103. {
  104. _color = color;
  105. UpdateDecalPlacementInfo();
  106. RefreshList();
  107. }
  108. private void UpdateDecalPlacementInfo()
  109. {
  110. if (_selected is null)
  111. return;
  112. var color = _useColor ? _color : Color.White;
  113. _decalPlacementSystem.UpdateDecalInfo(_selected, color, _rotation, _snap, _zIndex, _cleanable);
  114. }
  115. private void RefreshList()
  116. {
  117. // Clear
  118. Grid.RemoveAllChildren();
  119. if (_decals == null)
  120. return;
  121. var filter = Search.Text;
  122. foreach (var (decal, tex) in _decals)
  123. {
  124. if (!decal.ToLowerInvariant().Contains(filter.ToLowerInvariant()))
  125. continue;
  126. var button = new TextureButton
  127. {
  128. TextureNormal = tex,
  129. Name = decal,
  130. ToolTip = decal,
  131. Modulate = _useColor ? _color : Color.White
  132. };
  133. button.OnPressed += ButtonOnPressed;
  134. if (_selected == decal)
  135. {
  136. var panelContainer = new PanelContainer
  137. {
  138. PanelOverride = new StyleBoxFlat
  139. {
  140. BackgroundColor = StyleNano.ButtonColorDefault
  141. },
  142. Children =
  143. {
  144. button
  145. }
  146. };
  147. Grid.AddChild(panelContainer);
  148. }
  149. else
  150. Grid.AddChild(button);
  151. }
  152. }
  153. private void ButtonOnPressed(ButtonEventArgs obj)
  154. {
  155. if (obj.Button.Name == null)
  156. return;
  157. SelectDecal(obj.Button.Name);
  158. }
  159. private void SelectDecal(string decalId)
  160. {
  161. if (!_prototype.TryIndex<DecalPrototype>(decalId, out var decal))
  162. return;
  163. _selected = decalId;
  164. if (_auto)
  165. {
  166. EnableCleanable.Pressed = decal.DefaultCleanable;
  167. EnableColor.Pressed = decal.DefaultCustomColor;
  168. EnableSnap.Pressed = decal.DefaultSnap;
  169. _cleanable = decal.DefaultCleanable;
  170. _useColor = decal.DefaultCustomColor;
  171. _snap = decal.DefaultSnap;
  172. }
  173. UpdateDecalPlacementInfo();
  174. RefreshList();
  175. }
  176. public void Populate(IEnumerable<DecalPrototype> prototypes)
  177. {
  178. _decals = new SortedDictionary<string, Texture>();
  179. foreach (var decalPrototype in prototypes)
  180. {
  181. if (decalPrototype.ShowMenu)
  182. _decals.Add(decalPrototype.ID, decalPrototype.Sprite.Frame0());
  183. }
  184. RefreshList();
  185. }
  186. protected override void Opened()
  187. {
  188. base.Opened();
  189. _decalPlacementSystem.SetActive(true);
  190. }
  191. public override void Close()
  192. {
  193. base.Close();
  194. _decalPlacementSystem.SetActive(false);
  195. }
  196. }