1
0

MappingScreen.xaml.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using System.Linq;
  2. using System.Numerics;
  3. using Content.Client.Decals;
  4. using Content.Client.Decals.UI;
  5. using Content.Client.UserInterface.Screens;
  6. using Content.Client.UserInterface.Systems.Chat.Widgets;
  7. using Content.Shared.Decals;
  8. using Robust.Client.AutoGenerated;
  9. using Robust.Client.UserInterface;
  10. using Robust.Client.UserInterface.Controls;
  11. using Robust.Client.UserInterface.CustomControls;
  12. using Robust.Client.UserInterface.XAML;
  13. using Robust.Shared.Prototypes;
  14. using static Robust.Client.UserInterface.Controls.BaseButton;
  15. namespace Content.Client.Mapping;
  16. [GenerateTypedNameReferences]
  17. public sealed partial class MappingScreen : InGameScreen
  18. {
  19. [Dependency] private readonly IPrototypeManager _prototype = default!;
  20. public DecalPlacementSystem DecalSystem = default!;
  21. private PaletteColorPicker? _picker;
  22. private ProtoId<DecalPrototype>? _id;
  23. private Color _decalColor = Color.White;
  24. private float _decalRotation;
  25. private bool _decalSnap;
  26. private int _decalZIndex;
  27. private bool _decalCleanable;
  28. private bool _decalAuto;
  29. public override ChatBox ChatBox => GetWidget<ChatBox>()!;
  30. public event Func<MappingSpawnButton, bool>? IsDecalVisible;
  31. public MappingScreen()
  32. {
  33. RobustXamlLoader.Load(this);
  34. IoCManager.InjectDependencies(this);
  35. AutoscaleMaxResolution = new Vector2i(1080, 770);
  36. SetAnchorPreset(ScreenContainer, LayoutPreset.Wide);
  37. SetAnchorPreset(ViewportContainer, LayoutPreset.Wide);
  38. SetAnchorPreset(SpawnContainer, LayoutPreset.Wide);
  39. SetAnchorPreset(MainViewport, LayoutPreset.Wide);
  40. SetAnchorAndMarginPreset(Hotbar, LayoutPreset.BottomWide, margin: 5);
  41. SetAnchorAndMarginPreset(Actions, LayoutPreset.TopWide, margin: 5);
  42. ScreenContainer.OnSplitResizeFinished += () =>
  43. OnChatResized?.Invoke(new Vector2(ScreenContainer.SplitFraction, 0));
  44. var rotationSpinBox = new FloatSpinBox(90.0f, 0)
  45. {
  46. HorizontalExpand = true
  47. };
  48. DecalSpinBoxContainer.AddChild(rotationSpinBox);
  49. DecalColorPicker.OnColorChanged += OnDecalColorPicked;
  50. DecalPickerOpen.OnPressed += OnDecalPickerOpenPressed;
  51. rotationSpinBox.OnValueChanged += args =>
  52. {
  53. _decalRotation = args.Value;
  54. UpdateDecal();
  55. };
  56. DecalEnableAuto.OnToggled += args =>
  57. {
  58. _decalAuto = args.Pressed;
  59. if (_id is { } id)
  60. SelectDecal(id);
  61. };
  62. DecalEnableSnap.OnToggled += args =>
  63. {
  64. _decalSnap = args.Pressed;
  65. UpdateDecal();
  66. };
  67. DecalEnableCleanable.OnToggled += args =>
  68. {
  69. _decalCleanable = args.Pressed;
  70. UpdateDecal();
  71. };
  72. DecalZIndexSpinBox.ValueChanged += args =>
  73. {
  74. _decalZIndex = args.Value;
  75. UpdateDecal();
  76. };
  77. for (var i = 0; i < EntitySpawnWindow.InitOpts.Length; i++)
  78. {
  79. EntityPlacementMode.AddItem(EntitySpawnWindow.InitOpts[i], i);
  80. }
  81. Pick.Texture.TexturePath = "/Textures/Interface/eyedropper.svg.png";
  82. Delete.Texture.TexturePath = "/Textures/Interface/eraser.svg.png";
  83. Flip.Texture.TexturePath = "/Textures/Interface/VerbIcons/rotate_cw.svg.192dpi.png";
  84. Flip.OnPressed += args => FlipSides();
  85. }
  86. public void FlipSides()
  87. {
  88. ScreenContainer.Flip();
  89. if (SpawnContainer.GetPositionInParent() == 0)
  90. {
  91. Flip.Texture.TexturePath = "/Textures/Interface/VerbIcons/rotate_cw.svg.192dpi.png";
  92. }
  93. else
  94. {
  95. Flip.Texture.TexturePath = "/Textures/Interface/VerbIcons/rotate_ccw.svg.192dpi.png";
  96. }
  97. }
  98. private void OnDecalColorPicked(Color color)
  99. {
  100. _decalColor = color;
  101. DecalColorPicker.Color = color;
  102. UpdateDecal();
  103. }
  104. private void OnDecalPickerOpenPressed(ButtonEventArgs obj)
  105. {
  106. if (_picker == null)
  107. {
  108. _picker = new PaletteColorPicker();
  109. _picker.OpenToLeft();
  110. _picker.PaletteList.OnItemSelected += args =>
  111. {
  112. var color = ((Color?) args.ItemList.GetSelected().First().Metadata)!.Value;
  113. OnDecalColorPicked(color);
  114. };
  115. return;
  116. }
  117. if (_picker.IsOpen)
  118. _picker.Close();
  119. else
  120. _picker.Open();
  121. }
  122. private void UpdateDecal()
  123. {
  124. if (_id is not { } id)
  125. return;
  126. DecalSystem.UpdateDecalInfo(id, _decalColor, _decalRotation, _decalSnap, _decalZIndex, _decalCleanable);
  127. }
  128. public void SelectDecal(string decalId)
  129. {
  130. if (!_prototype.TryIndex<DecalPrototype>(decalId, out var decal))
  131. return;
  132. _id = decalId;
  133. if (_decalAuto)
  134. {
  135. _decalColor = Color.White;
  136. _decalCleanable = decal.DefaultCleanable;
  137. _decalSnap = decal.DefaultSnap;
  138. DecalColorPicker.Color = _decalColor;
  139. DecalEnableCleanable.Pressed = _decalCleanable;
  140. DecalEnableSnap.Pressed = _decalSnap;
  141. }
  142. UpdateDecal();
  143. RefreshList();
  144. }
  145. private void RefreshList()
  146. {
  147. foreach (var control in Prototypes.Children)
  148. {
  149. if (control is not MappingSpawnButton button ||
  150. button.Prototype?.Prototype is not DecalPrototype)
  151. {
  152. continue;
  153. }
  154. foreach (var child in button.Children)
  155. {
  156. if (child is not MappingSpawnButton { Prototype.Prototype: DecalPrototype } childButton)
  157. {
  158. continue;
  159. }
  160. childButton.Texture.Modulate = _decalColor;
  161. childButton.Visible = IsDecalVisible?.Invoke(childButton) ?? true;
  162. }
  163. }
  164. }
  165. public override void SetChatSize(Vector2 size)
  166. {
  167. ScreenContainer.ResizeMode = SplitContainer.SplitResizeMode.RespectChildrenMinSize;
  168. }
  169. public void UnPressActionsExcept(Control except)
  170. {
  171. Add.Pressed = Add == except;
  172. Fill.Pressed = Fill == except;
  173. Grab.Pressed = Grab == except;
  174. Move.Pressed = Move == except;
  175. Pick.Pressed = Pick == except;
  176. Delete.Pressed = Delete == except;
  177. }
  178. }