1
0

MappingState.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  1. using System.Linq;
  2. using System.Numerics;
  3. using Content.Client.Administration.Managers;
  4. using Content.Client.ContextMenu.UI;
  5. using Content.Client.Decals;
  6. using Content.Client.Gameplay;
  7. using Content.Client.UserInterface.Controls;
  8. using Content.Client.UserInterface.Systems.Gameplay;
  9. using Content.Client.Verbs;
  10. using Content.Shared.Administration;
  11. using Content.Shared.Decals;
  12. using Content.Shared.Input;
  13. using Content.Shared.Maps;
  14. using Robust.Client.GameObjects;
  15. using Robust.Client.Graphics;
  16. using Robust.Client.Input;
  17. using Robust.Client.Placement;
  18. using Robust.Client.ResourceManagement;
  19. using Robust.Client.UserInterface;
  20. using Robust.Client.UserInterface.CustomControls;
  21. using Robust.Shared.Enums;
  22. using Robust.Shared.Input.Binding;
  23. using Robust.Shared.Map;
  24. using Robust.Shared.Player;
  25. using Robust.Shared.Prototypes;
  26. using Robust.Shared.Serialization.Markdown.Sequence;
  27. using Robust.Shared.Serialization.Markdown.Value;
  28. using Robust.Shared.Timing;
  29. using Robust.Shared.Utility;
  30. using static System.StringComparison;
  31. using static Robust.Client.UserInterface.Controls.BaseButton;
  32. using static Robust.Client.UserInterface.Controls.LineEdit;
  33. using static Robust.Client.UserInterface.Controls.OptionButton;
  34. using static Robust.Shared.Input.Binding.PointerInputCmdHandler;
  35. namespace Content.Client.Mapping;
  36. public sealed class MappingState : GameplayStateBase
  37. {
  38. [Dependency] private readonly IClientAdminManager _admin = default!;
  39. [Dependency] private readonly IEntityManager _entityManager = default!;
  40. [Dependency] private readonly IEntityNetworkManager _entityNetwork = default!;
  41. [Dependency] private readonly IInputManager _input = default!;
  42. [Dependency] private readonly ILogManager _log = default!;
  43. [Dependency] private readonly IMapManager _mapMan = default!;
  44. [Dependency] private readonly MappingManager _mapping = default!;
  45. [Dependency] private readonly IOverlayManager _overlays = default!;
  46. [Dependency] private readonly IPlacementManager _placement = default!;
  47. [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
  48. [Dependency] private readonly IResourceCache _resources = default!;
  49. [Dependency] private readonly IGameTiming _timing = default!;
  50. private EntityMenuUIController _entityMenuController = default!;
  51. private DecalPlacementSystem _decal = default!;
  52. private SpriteSystem _sprite = default!;
  53. private TransformSystem _transform = default!;
  54. private VerbSystem _verbs = default!;
  55. private readonly ISawmill _sawmill;
  56. private readonly GameplayStateLoadController _loadController;
  57. private bool _setup;
  58. private readonly List<MappingPrototype> _allPrototypes = new();
  59. private readonly Dictionary<IPrototype, MappingPrototype> _allPrototypesDict = new();
  60. private readonly Dictionary<Type, Dictionary<string, MappingPrototype>> _idDict = new();
  61. private readonly List<MappingPrototype> _prototypes = new();
  62. private (TimeSpan At, MappingSpawnButton Button)? _lastClicked;
  63. private Control? _scrollTo;
  64. private bool _updatePlacement;
  65. private bool _updateEraseDecal;
  66. private MappingScreen Screen => (MappingScreen) UserInterfaceManager.ActiveScreen!;
  67. private MainViewport Viewport => UserInterfaceManager.ActiveScreen!.GetWidget<MainViewport>()!;
  68. public CursorState State { get; set; }
  69. public MappingState()
  70. {
  71. IoCManager.InjectDependencies(this);
  72. _sawmill = _log.GetSawmill("mapping");
  73. _loadController = UserInterfaceManager.GetUIController<GameplayStateLoadController>();
  74. }
  75. protected override void Startup()
  76. {
  77. EnsureSetup();
  78. base.Startup();
  79. UserInterfaceManager.LoadScreen<MappingScreen>();
  80. _loadController.LoadScreen();
  81. var context = _input.Contexts.GetContext("common");
  82. context.AddFunction(ContentKeyFunctions.MappingUnselect);
  83. context.AddFunction(ContentKeyFunctions.SaveMap);
  84. context.AddFunction(ContentKeyFunctions.MappingEnablePick);
  85. context.AddFunction(ContentKeyFunctions.MappingEnableDelete);
  86. context.AddFunction(ContentKeyFunctions.MappingPick);
  87. context.AddFunction(ContentKeyFunctions.MappingRemoveDecal);
  88. context.AddFunction(ContentKeyFunctions.MappingCancelEraseDecal);
  89. context.AddFunction(ContentKeyFunctions.MappingOpenContextMenu);
  90. Screen.DecalSystem = _decal;
  91. Screen.Prototypes.SearchBar.OnTextChanged += OnSearch;
  92. Screen.Prototypes.CollapseAllButton.OnPressed += OnCollapseAll;
  93. Screen.Prototypes.ClearSearchButton.OnPressed += OnClearSearch;
  94. Screen.Prototypes.GetPrototypeData += OnGetData;
  95. Screen.Prototypes.SelectionChanged += OnSelected;
  96. Screen.Prototypes.CollapseToggled += OnCollapseToggled;
  97. Screen.Pick.OnPressed += OnPickPressed;
  98. Screen.Delete.OnPressed += OnDeletePressed;
  99. Screen.EntityReplaceButton.OnToggled += OnEntityReplacePressed;
  100. Screen.EntityPlacementMode.OnItemSelected += OnEntityPlacementSelected;
  101. Screen.EraseEntityButton.OnToggled += OnEraseEntityPressed;
  102. Screen.EraseDecalButton.OnToggled += OnEraseDecalPressed;
  103. _placement.PlacementChanged += OnPlacementChanged;
  104. CommandBinds.Builder
  105. .Bind(ContentKeyFunctions.MappingUnselect, new PointerInputCmdHandler(HandleMappingUnselect, outsidePrediction: true))
  106. .Bind(ContentKeyFunctions.SaveMap, new PointerInputCmdHandler(HandleSaveMap, outsidePrediction: true))
  107. .Bind(ContentKeyFunctions.MappingEnablePick, new PointerStateInputCmdHandler(HandleEnablePick, HandleDisablePick, outsidePrediction: true))
  108. .Bind(ContentKeyFunctions.MappingEnableDelete, new PointerStateInputCmdHandler(HandleEnableDelete, HandleDisableDelete, outsidePrediction: true))
  109. .Bind(ContentKeyFunctions.MappingPick, new PointerInputCmdHandler(HandlePick, outsidePrediction: true))
  110. .Bind(ContentKeyFunctions.MappingRemoveDecal, new PointerInputCmdHandler(HandleEditorCancelPlace, outsidePrediction: true))
  111. .Bind(ContentKeyFunctions.MappingCancelEraseDecal, new PointerInputCmdHandler(HandleCancelEraseDecal, outsidePrediction: true))
  112. .Bind(ContentKeyFunctions.MappingOpenContextMenu, new PointerInputCmdHandler(HandleOpenContextMenu, outsidePrediction: true))
  113. .Register<MappingState>();
  114. _overlays.AddOverlay(new MappingOverlay(this));
  115. _prototypeManager.PrototypesReloaded += OnPrototypesReloaded;
  116. Screen.Prototypes.UpdateVisible(_prototypes);
  117. }
  118. private void OnPrototypesReloaded(PrototypesReloadedEventArgs obj)
  119. {
  120. if (!obj.WasModified<EntityPrototype>() &&
  121. !obj.WasModified<ContentTileDefinition>() &&
  122. !obj.WasModified<DecalPrototype>())
  123. {
  124. return;
  125. }
  126. ReloadPrototypes();
  127. }
  128. private bool HandleOpenContextMenu(in PointerInputCmdArgs args)
  129. {
  130. Deselect();
  131. var coords = args.Coordinates.ToMap(_entityManager, _transform);
  132. if (_verbs.TryGetEntityMenuEntities(coords, out var entities))
  133. _entityMenuController.OpenRootMenu(entities);
  134. return true;
  135. }
  136. protected override void Shutdown()
  137. {
  138. CommandBinds.Unregister<MappingState>();
  139. Screen.Prototypes.SearchBar.OnTextChanged -= OnSearch;
  140. Screen.Prototypes.CollapseAllButton.OnPressed -= OnCollapseAll;
  141. Screen.Prototypes.ClearSearchButton.OnPressed -= OnClearSearch;
  142. Screen.Prototypes.GetPrototypeData -= OnGetData;
  143. Screen.Prototypes.SelectionChanged -= OnSelected;
  144. Screen.Prototypes.CollapseToggled -= OnCollapseToggled;
  145. Screen.Pick.OnPressed -= OnPickPressed;
  146. Screen.Delete.OnPressed -= OnDeletePressed;
  147. Screen.EntityReplaceButton.OnToggled -= OnEntityReplacePressed;
  148. Screen.EntityPlacementMode.OnItemSelected -= OnEntityPlacementSelected;
  149. Screen.EraseEntityButton.OnToggled -= OnEraseEntityPressed;
  150. Screen.EraseDecalButton.OnToggled -= OnEraseDecalPressed;
  151. _placement.PlacementChanged -= OnPlacementChanged;
  152. _prototypeManager.PrototypesReloaded -= OnPrototypesReloaded;
  153. UserInterfaceManager.ClearWindows();
  154. _loadController.UnloadScreen();
  155. UserInterfaceManager.UnloadScreen();
  156. var context = _input.Contexts.GetContext("common");
  157. context.RemoveFunction(ContentKeyFunctions.MappingUnselect);
  158. context.RemoveFunction(ContentKeyFunctions.SaveMap);
  159. context.RemoveFunction(ContentKeyFunctions.MappingEnablePick);
  160. context.RemoveFunction(ContentKeyFunctions.MappingEnableDelete);
  161. context.RemoveFunction(ContentKeyFunctions.MappingPick);
  162. context.RemoveFunction(ContentKeyFunctions.MappingRemoveDecal);
  163. context.RemoveFunction(ContentKeyFunctions.MappingCancelEraseDecal);
  164. context.RemoveFunction(ContentKeyFunctions.MappingOpenContextMenu);
  165. _overlays.RemoveOverlay<MappingOverlay>();
  166. base.Shutdown();
  167. }
  168. private void EnsureSetup()
  169. {
  170. if (_setup)
  171. return;
  172. _setup = true;
  173. _entityMenuController = UserInterfaceManager.GetUIController<EntityMenuUIController>();
  174. _decal = _entityManager.System<DecalPlacementSystem>();
  175. _sprite = _entityManager.System<SpriteSystem>();
  176. _transform = _entityManager.System<TransformSystem>();
  177. _verbs = _entityManager.System<VerbSystem>();
  178. ReloadPrototypes();
  179. }
  180. private void ReloadPrototypes()
  181. {
  182. var entities = new MappingPrototype(null, Loc.GetString("mapping-entities")) { Children = new List<MappingPrototype>() };
  183. _prototypes.Add(entities);
  184. var mappings = new Dictionary<string, MappingPrototype>();
  185. foreach (var entity in _prototypeManager.EnumeratePrototypes<EntityPrototype>())
  186. {
  187. Register(entity, entity.ID, entities);
  188. }
  189. Sort(mappings, entities);
  190. mappings.Clear();
  191. var tiles = new MappingPrototype(null, Loc.GetString("mapping-tiles")) { Children = new List<MappingPrototype>() };
  192. _prototypes.Add(tiles);
  193. foreach (var tile in _prototypeManager.EnumeratePrototypes<ContentTileDefinition>())
  194. {
  195. Register(tile, tile.ID, tiles);
  196. }
  197. Sort(mappings, tiles);
  198. mappings.Clear();
  199. var decals = new MappingPrototype(null, Loc.GetString("mapping-decals")) { Children = new List<MappingPrototype>() };
  200. _prototypes.Add(decals);
  201. foreach (var decal in _prototypeManager.EnumeratePrototypes<DecalPrototype>())
  202. {
  203. Register(decal, decal.ID, decals);
  204. }
  205. Sort(mappings, decals);
  206. mappings.Clear();
  207. }
  208. private void Sort(Dictionary<string, MappingPrototype> prototypes, MappingPrototype topLevel)
  209. {
  210. static int Compare(MappingPrototype a, MappingPrototype b)
  211. {
  212. return string.Compare(a.Name, b.Name, OrdinalIgnoreCase);
  213. }
  214. topLevel.Children ??= new List<MappingPrototype>();
  215. foreach (var prototype in prototypes.Values)
  216. {
  217. if (prototype.Parents == null && prototype != topLevel)
  218. {
  219. prototype.Parents = new List<MappingPrototype> { topLevel };
  220. topLevel.Children.Add(prototype);
  221. }
  222. prototype.Parents?.Sort(Compare);
  223. prototype.Children?.Sort(Compare);
  224. }
  225. topLevel.Children.Sort(Compare);
  226. }
  227. private MappingPrototype? Register<T>(T? prototype, string id, MappingPrototype topLevel) where T : class, IPrototype, IInheritingPrototype
  228. {
  229. {
  230. if (prototype == null &&
  231. _prototypeManager.TryIndex(id, out prototype) &&
  232. prototype is EntityPrototype entity)
  233. {
  234. if (entity.HideSpawnMenu || entity.Abstract)
  235. prototype = null;
  236. }
  237. }
  238. if (prototype == null)
  239. {
  240. if (!_prototypeManager.TryGetMapping(typeof(T), id, out var node))
  241. {
  242. _sawmill.Error($"No {nameof(T)} found with id {id}");
  243. return null;
  244. }
  245. var ids = _idDict.GetOrNew(typeof(T));
  246. if (ids.TryGetValue(id, out var mapping))
  247. {
  248. return mapping;
  249. }
  250. else
  251. {
  252. var name = node.TryGet("name", out ValueDataNode? nameNode)
  253. ? nameNode.Value
  254. : id;
  255. if (node.TryGet("suffix", out ValueDataNode? suffix))
  256. name = $"{name} [{suffix.Value}]";
  257. mapping = new MappingPrototype(prototype, name);
  258. _allPrototypes.Add(mapping);
  259. ids.Add(id, mapping);
  260. if (node.TryGet("parent", out ValueDataNode? parentValue))
  261. {
  262. var parent = Register<T>(null, parentValue.Value, topLevel);
  263. if (parent != null)
  264. {
  265. mapping.Parents ??= new List<MappingPrototype>();
  266. mapping.Parents.Add(parent);
  267. parent.Children ??= new List<MappingPrototype>();
  268. parent.Children.Add(mapping);
  269. }
  270. }
  271. else if (node.TryGet("parent", out SequenceDataNode? parentSequence))
  272. {
  273. foreach (var parentNode in parentSequence.Cast<ValueDataNode>())
  274. {
  275. var parent = Register<T>(null, parentNode.Value, topLevel);
  276. if (parent != null)
  277. {
  278. mapping.Parents ??= new List<MappingPrototype>();
  279. mapping.Parents.Add(parent);
  280. parent.Children ??= new List<MappingPrototype>();
  281. parent.Children.Add(mapping);
  282. }
  283. }
  284. }
  285. else
  286. {
  287. topLevel.Children ??= new List<MappingPrototype>();
  288. topLevel.Children.Add(mapping);
  289. mapping.Parents ??= new List<MappingPrototype>();
  290. mapping.Parents.Add(topLevel);
  291. }
  292. return mapping;
  293. }
  294. }
  295. else
  296. {
  297. var ids = _idDict.GetOrNew(typeof(T));
  298. if (ids.TryGetValue(id, out var mapping))
  299. {
  300. return mapping;
  301. }
  302. else
  303. {
  304. var entity = prototype as EntityPrototype;
  305. var name = entity?.Name ?? prototype.ID;
  306. if (!string.IsNullOrWhiteSpace(entity?.EditorSuffix))
  307. name = $"{name} [{entity.EditorSuffix}]";
  308. mapping = new MappingPrototype(prototype, name);
  309. _allPrototypes.Add(mapping);
  310. _allPrototypesDict.Add(prototype, mapping);
  311. ids.Add(prototype.ID, mapping);
  312. }
  313. if (prototype.Parents == null)
  314. {
  315. topLevel.Children ??= new List<MappingPrototype>();
  316. topLevel.Children.Add(mapping);
  317. mapping.Parents ??= new List<MappingPrototype>();
  318. mapping.Parents.Add(topLevel);
  319. return mapping;
  320. }
  321. foreach (var parentId in prototype.Parents)
  322. {
  323. var parent = Register<T>(null, parentId, topLevel);
  324. if (parent != null)
  325. {
  326. mapping.Parents ??= new List<MappingPrototype>();
  327. mapping.Parents.Add(parent);
  328. parent.Children ??= new List<MappingPrototype>();
  329. parent.Children.Add(mapping);
  330. }
  331. }
  332. return mapping;
  333. }
  334. }
  335. private void OnPlacementChanged(object? sender, EventArgs e)
  336. {
  337. _updatePlacement = true;
  338. }
  339. protected override void OnKeyBindStateChanged(ViewportBoundKeyEventArgs args)
  340. {
  341. if (args.Viewport == null)
  342. base.OnKeyBindStateChanged(new ViewportBoundKeyEventArgs(args.KeyEventArgs, Viewport.Viewport));
  343. else
  344. base.OnKeyBindStateChanged(args);
  345. }
  346. private void OnSearch(LineEditEventArgs args)
  347. {
  348. if (string.IsNullOrEmpty(args.Text))
  349. {
  350. Screen.Prototypes.PrototypeList.Visible = true;
  351. Screen.Prototypes.SearchList.Visible = false;
  352. return;
  353. }
  354. var matches = new List<MappingPrototype>();
  355. foreach (var prototype in _allPrototypes)
  356. {
  357. if (prototype.Name.Contains(args.Text, OrdinalIgnoreCase))
  358. matches.Add(prototype);
  359. }
  360. matches.Sort(static (a, b) => string.Compare(a.Name, b.Name, OrdinalIgnoreCase));
  361. Screen.Prototypes.PrototypeList.Visible = false;
  362. Screen.Prototypes.SearchList.Visible = true;
  363. Screen.Prototypes.Search(matches);
  364. }
  365. private void OnCollapseAll(ButtonEventArgs args)
  366. {
  367. foreach (var child in Screen.Prototypes.PrototypeList.Children)
  368. {
  369. if (child is not MappingSpawnButton button)
  370. continue;
  371. Collapse(button);
  372. }
  373. Screen.Prototypes.ScrollContainer.SetScrollValue(new Vector2(0, 0));
  374. }
  375. private void OnClearSearch(ButtonEventArgs obj)
  376. {
  377. Screen.Prototypes.SearchBar.Text = string.Empty;
  378. OnSearch(new LineEditEventArgs(Screen.Prototypes.SearchBar, string.Empty));
  379. }
  380. private void OnGetData(IPrototype prototype, List<Texture> textures)
  381. {
  382. switch (prototype)
  383. {
  384. case EntityPrototype entity:
  385. textures.AddRange(SpriteComponent.GetPrototypeTextures(entity, _resources).Select(t => t.Default));
  386. break;
  387. case DecalPrototype decal:
  388. textures.Add(_sprite.Frame0(decal.Sprite));
  389. break;
  390. case ContentTileDefinition tile:
  391. if (tile.Sprite?.ToString() is { } sprite)
  392. textures.Add(_resources.GetResource<TextureResource>(sprite).Texture);
  393. break;
  394. }
  395. }
  396. private void OnSelected(MappingPrototype mapping)
  397. {
  398. if (mapping.Prototype == null)
  399. return;
  400. var chain = new Stack<MappingPrototype>();
  401. chain.Push(mapping);
  402. var parent = mapping.Parents?.FirstOrDefault();
  403. while (parent != null)
  404. {
  405. chain.Push(parent);
  406. parent = parent.Parents?.FirstOrDefault();
  407. }
  408. _lastClicked = null;
  409. Control? last = null;
  410. var children = Screen.Prototypes.PrototypeList.Children;
  411. foreach (var prototype in chain)
  412. {
  413. foreach (var child in children)
  414. {
  415. if (child is MappingSpawnButton button &&
  416. button.Prototype == prototype)
  417. {
  418. UnCollapse(button);
  419. OnSelected(button, prototype.Prototype);
  420. children = button.ChildrenPrototypes.Children;
  421. last = child;
  422. break;
  423. }
  424. }
  425. }
  426. if (last != null && Screen.Prototypes.PrototypeList.Visible)
  427. _scrollTo = last;
  428. }
  429. private void OnSelected(MappingSpawnButton button, IPrototype? prototype)
  430. {
  431. var time = _timing.CurTime;
  432. if (prototype is DecalPrototype)
  433. Screen.SelectDecal(prototype.ID);
  434. // Double-click functionality if it's collapsible.
  435. if (_lastClicked is { } lastClicked &&
  436. lastClicked.Button == button &&
  437. lastClicked.At > time - TimeSpan.FromSeconds(0.333) &&
  438. string.IsNullOrEmpty(Screen.Prototypes.SearchBar.Text) &&
  439. button.CollapseButton.Visible)
  440. {
  441. button.CollapseButton.Pressed = !button.CollapseButton.Pressed;
  442. ToggleCollapse(button);
  443. button.Button.Pressed = true;
  444. Screen.Prototypes.Selected = button;
  445. _lastClicked = null;
  446. return;
  447. }
  448. // Toggle if it's the same button (at least if we just unclicked it).
  449. if (!button.Button.Pressed && button.Prototype?.Prototype != null && _lastClicked?.Button == button)
  450. {
  451. _lastClicked = null;
  452. Deselect();
  453. return;
  454. }
  455. _lastClicked = (time, button);
  456. if (button.Prototype == null)
  457. return;
  458. if (Screen.Prototypes.Selected is { } oldButton &&
  459. oldButton != button)
  460. {
  461. Deselect();
  462. }
  463. Screen.EntityContainer.Visible = false;
  464. Screen.DecalContainer.Visible = false;
  465. switch (prototype)
  466. {
  467. case EntityPrototype entity:
  468. {
  469. var placementId = Screen.EntityPlacementMode.SelectedId;
  470. var placement = new PlacementInformation
  471. {
  472. PlacementOption = placementId > 0 ? EntitySpawnWindow.InitOpts[placementId] : entity.PlacementMode,
  473. EntityType = entity.ID,
  474. IsTile = false
  475. };
  476. Screen.EntityContainer.Visible = true;
  477. _decal.SetActive(false);
  478. _placement.BeginPlacing(placement);
  479. break;
  480. }
  481. case DecalPrototype decal:
  482. _placement.Clear();
  483. _decal.SetActive(true);
  484. _decal.UpdateDecalInfo(decal.ID, Color.White, 0, true, 0, false);
  485. Screen.DecalContainer.Visible = true;
  486. break;
  487. case ContentTileDefinition tile:
  488. {
  489. var placement = new PlacementInformation
  490. {
  491. PlacementOption = "AlignTileAny",
  492. TileType = tile.TileId,
  493. IsTile = true
  494. };
  495. _decal.SetActive(false);
  496. _placement.BeginPlacing(placement);
  497. break;
  498. }
  499. default:
  500. _placement.Clear();
  501. break;
  502. }
  503. Screen.Prototypes.Selected = button;
  504. button.Button.Pressed = true;
  505. }
  506. private void Deselect()
  507. {
  508. if (Screen.Prototypes.Selected is { } selected)
  509. {
  510. selected.Button.Pressed = false;
  511. Screen.Prototypes.Selected = null;
  512. if (selected.Prototype?.Prototype is DecalPrototype)
  513. {
  514. _decal.SetActive(false);
  515. Screen.DecalContainer.Visible = false;
  516. }
  517. if (selected.Prototype?.Prototype is EntityPrototype)
  518. {
  519. _placement.Clear();
  520. }
  521. if (selected.Prototype?.Prototype is ContentTileDefinition)
  522. {
  523. _placement.Clear();
  524. }
  525. }
  526. }
  527. private void OnCollapseToggled(MappingSpawnButton button, ButtonToggledEventArgs args)
  528. {
  529. ToggleCollapse(button);
  530. }
  531. private void OnPickPressed(ButtonEventArgs args)
  532. {
  533. if (args.Button.Pressed)
  534. EnablePick();
  535. else
  536. DisablePick();
  537. }
  538. private void OnDeletePressed(ButtonEventArgs obj)
  539. {
  540. if (obj.Button.Pressed)
  541. EnableDelete();
  542. else
  543. DisableDelete();
  544. }
  545. private void OnEntityReplacePressed(ButtonToggledEventArgs args)
  546. {
  547. _placement.Replacement = args.Pressed;
  548. }
  549. private void OnEntityPlacementSelected(ItemSelectedEventArgs args)
  550. {
  551. Screen.EntityPlacementMode.SelectId(args.Id);
  552. if (_placement.CurrentMode != null)
  553. {
  554. var placement = new PlacementInformation
  555. {
  556. PlacementOption = EntitySpawnWindow.InitOpts[args.Id],
  557. EntityType = _placement.CurrentPermission!.EntityType,
  558. TileType = _placement.CurrentPermission.TileType,
  559. Range = 2,
  560. IsTile = _placement.CurrentPermission.IsTile,
  561. };
  562. _placement.BeginPlacing(placement);
  563. }
  564. }
  565. private void OnEraseEntityPressed(ButtonEventArgs args)
  566. {
  567. if (args.Button.Pressed == _placement.Eraser)
  568. return;
  569. if (args.Button.Pressed)
  570. EnableEraser();
  571. else
  572. DisableEraser();
  573. }
  574. private void OnEraseDecalPressed(ButtonToggledEventArgs args)
  575. {
  576. _placement.Clear();
  577. Deselect();
  578. Screen.EraseEntityButton.Pressed = false;
  579. _updatePlacement = true;
  580. _updateEraseDecal = args.Pressed;
  581. }
  582. private void EnableEraser()
  583. {
  584. if (_placement.Eraser)
  585. return;
  586. _placement.Clear();
  587. _placement.ToggleEraser();
  588. Screen.EntityPlacementMode.Disabled = true;
  589. Screen.EraseDecalButton.Pressed = false;
  590. Deselect();
  591. }
  592. private void DisableEraser()
  593. {
  594. if (!_placement.Eraser)
  595. return;
  596. _placement.ToggleEraser();
  597. Screen.EntityPlacementMode.Disabled = false;
  598. }
  599. private void EnablePick()
  600. {
  601. Screen.UnPressActionsExcept(Screen.Pick);
  602. State = CursorState.Pick;
  603. }
  604. private void DisablePick()
  605. {
  606. Screen.Pick.Pressed = false;
  607. State = CursorState.None;
  608. }
  609. private void EnableDelete()
  610. {
  611. Screen.UnPressActionsExcept(Screen.Delete);
  612. State = CursorState.Delete;
  613. EnableEraser();
  614. }
  615. private void DisableDelete()
  616. {
  617. Screen.Delete.Pressed = false;
  618. State = CursorState.None;
  619. DisableEraser();
  620. }
  621. private bool HandleMappingUnselect(in PointerInputCmdArgs args)
  622. {
  623. if (Screen.Prototypes.Selected is not { Prototype.Prototype: DecalPrototype })
  624. return false;
  625. Deselect();
  626. return true;
  627. }
  628. private bool HandleSaveMap(in PointerInputCmdArgs args)
  629. {
  630. #if FULL_RELEASE
  631. return false;
  632. #endif
  633. if (!_admin.IsAdmin(true) || !_admin.HasFlag(AdminFlags.Host))
  634. return false;
  635. SaveMap();
  636. return true;
  637. }
  638. private bool HandleEnablePick(ICommonSession? session, EntityCoordinates coords, EntityUid uid)
  639. {
  640. EnablePick();
  641. return true;
  642. }
  643. private bool HandleDisablePick(ICommonSession? session, EntityCoordinates coords, EntityUid uid)
  644. {
  645. DisablePick();
  646. return true;
  647. }
  648. private bool HandleEnableDelete(ICommonSession? session, EntityCoordinates coords, EntityUid uid)
  649. {
  650. EnableDelete();
  651. return true;
  652. }
  653. private bool HandleDisableDelete(ICommonSession? session, EntityCoordinates coords, EntityUid uid)
  654. {
  655. DisableDelete();
  656. return true;
  657. }
  658. private bool HandlePick(ICommonSession? session, EntityCoordinates coords, EntityUid uid)
  659. {
  660. if (State != CursorState.Pick)
  661. return false;
  662. MappingPrototype? button = null;
  663. // Try and get tile under it
  664. // TODO: Separate mode for decals.
  665. if (!uid.IsValid())
  666. {
  667. var mapPos = _transform.ToMapCoordinates(coords);
  668. if (_mapMan.TryFindGridAt(mapPos, out var gridUid, out var grid) &&
  669. _entityManager.System<SharedMapSystem>().TryGetTileRef(gridUid, grid, coords, out var tileRef) &&
  670. _allPrototypesDict.TryGetValue(tileRef.GetContentTileDefinition(), out button))
  671. {
  672. OnSelected(button);
  673. return true;
  674. }
  675. }
  676. if (button == null)
  677. {
  678. if (uid == EntityUid.Invalid ||
  679. _entityManager.GetComponentOrNull<MetaDataComponent>(uid) is not { EntityPrototype: { } prototype } ||
  680. !_allPrototypesDict.TryGetValue(prototype, out button))
  681. {
  682. // we always block other input handlers if pick mode is enabled
  683. // this makes you not accidentally place something in space because you
  684. // miss-clicked while holding down the pick hotkey
  685. return true;
  686. }
  687. // Selected an entity
  688. OnSelected(button);
  689. // Match rotation
  690. _placement.Direction = _entityManager.GetComponent<TransformComponent>(uid).LocalRotation.GetDir();
  691. }
  692. return true;
  693. }
  694. private bool HandleEditorCancelPlace(ICommonSession? session, EntityCoordinates coords, EntityUid uid)
  695. {
  696. if (!Screen.EraseDecalButton.Pressed)
  697. return false;
  698. _entityNetwork.SendSystemNetworkMessage(new RequestDecalRemovalEvent(_entityManager.GetNetCoordinates(coords)));
  699. return true;
  700. }
  701. private bool HandleCancelEraseDecal(in PointerInputCmdArgs args)
  702. {
  703. if (!Screen.EraseDecalButton.Pressed)
  704. return false;
  705. Screen.EraseDecalButton.Pressed = false;
  706. return true;
  707. }
  708. private async void SaveMap()
  709. {
  710. await _mapping.SaveMap();
  711. }
  712. private void ToggleCollapse(MappingSpawnButton button)
  713. {
  714. if (button.CollapseButton.Pressed)
  715. {
  716. if (button.Prototype?.Children != null)
  717. {
  718. foreach (var child in button.Prototype.Children)
  719. {
  720. Screen.Prototypes.Insert(button.ChildrenPrototypes, child, true);
  721. }
  722. }
  723. button.CollapseButton.Label.Text = "▼";
  724. }
  725. else
  726. {
  727. button.ChildrenPrototypes.DisposeAllChildren();
  728. button.CollapseButton.Label.Text = "▶";
  729. }
  730. }
  731. private void Collapse(MappingSpawnButton button)
  732. {
  733. if (!button.CollapseButton.Pressed)
  734. return;
  735. button.CollapseButton.Pressed = false;
  736. ToggleCollapse(button);
  737. }
  738. private void UnCollapse(MappingSpawnButton button)
  739. {
  740. if (button.CollapseButton.Pressed)
  741. return;
  742. button.CollapseButton.Pressed = true;
  743. ToggleCollapse(button);
  744. }
  745. public EntityUid? GetHoveredEntity()
  746. {
  747. if (UserInterfaceManager.CurrentlyHovered is not IViewportControl viewport ||
  748. _input.MouseScreenPosition is not { IsValid: true } position)
  749. {
  750. return null;
  751. }
  752. var mapPos = viewport.PixelToMap(position.Position);
  753. return GetClickedEntity(mapPos);
  754. }
  755. public override void FrameUpdate(FrameEventArgs e)
  756. {
  757. if (_updatePlacement)
  758. {
  759. _updatePlacement = false;
  760. if (!_placement.IsActive && _decal.GetActiveDecal().Decal == null)
  761. Deselect();
  762. Screen.EraseEntityButton.Pressed = _placement.Eraser;
  763. Screen.EraseDecalButton.Pressed = _updateEraseDecal;
  764. Screen.EntityPlacementMode.Disabled = _placement.Eraser;
  765. }
  766. if (_scrollTo is not { } scrollTo)
  767. return;
  768. // this is not ideal but we wait until the control's height is computed to use
  769. // its position to scroll to
  770. if (scrollTo.Height > 0 && Screen.Prototypes.PrototypeList.Visible)
  771. {
  772. var y = scrollTo.GlobalPosition.Y - Screen.Prototypes.ScrollContainer.Height / 2 + scrollTo.Height;
  773. var scroll = Screen.Prototypes.ScrollContainer;
  774. scroll.SetScrollValue(scroll.GetScrollValue() + new Vector2(0, y));
  775. _scrollTo = null;
  776. }
  777. }
  778. // TODO this doesn't handle pressing down multiple state hotkeys at the moment
  779. public enum CursorState
  780. {
  781. None,
  782. Pick,
  783. Delete
  784. }
  785. }