diff --git a/Robust.Client/Graphics/Clyde/Clyde.HLR.cs b/Robust.Client/Graphics/Clyde/Clyde.HLR.cs index ea9271562a3..11ffc62737f 100644 --- a/Robust.Client/Graphics/Clyde/Clyde.HLR.cs +++ b/Robust.Client/Graphics/Clyde/Clyde.HLR.cs @@ -215,8 +215,6 @@ private List GetOverlaysForSpace(OverlaySpace space) } } - _overlays.Sort(OverlayComparer.Instance); - return _overlays; } @@ -574,17 +572,5 @@ private static Box2Rotated CalcWorldBounds(Viewport viewport) return new Box2Rotated(aabb, rotation, aabb.Center); } - - private sealed class OverlayComparer : IComparer - { - public static readonly OverlayComparer Instance = new(); - - public int Compare(Overlay? x, Overlay? y) - { - var zX = x?.ZIndex ?? 0; - var zY = y?.ZIndex ?? 0; - return zX.CompareTo(zY); - } - } } } diff --git a/Robust.Client/Graphics/Overlays/OverlayManager.cs b/Robust.Client/Graphics/Overlays/OverlayManager.cs index e0022802bb7..79357083fc5 100644 --- a/Robust.Client/Graphics/Overlays/OverlayManager.cs +++ b/Robust.Client/Graphics/Overlays/OverlayManager.cs @@ -13,10 +13,22 @@ internal sealed class OverlayManager : IOverlayManagerInternal, IPostInjectInit [Dependency] private readonly ILogManager _logMan = default!; [ViewVariables] - private readonly Dictionary _overlays = new Dictionary(); + private readonly Dictionary _overlays = new(); + + /// + /// A list that duplicates a value from , + /// but already sorted, by invoking + /// in and . + /// + [ViewVariables] + private readonly List _sortedOverlays = []; + private ISawmill _logger = default!; - public IEnumerable AllOverlays => _overlays.Values; + /// + /// Returns a list of all overlays sorted by + /// + public IEnumerable AllOverlays => _sortedOverlays; public void FrameUpdate(FrameEventArgs args) { @@ -30,7 +42,9 @@ public bool AddOverlay(Overlay overlay) { if (_overlays.ContainsKey(overlay.GetType())) return false; + _overlays.Add(overlay.GetType(), overlay); + Sort(); return true; } @@ -42,7 +56,9 @@ public bool RemoveOverlay(Type overlayClass) return false; } - return _overlays.Remove(overlayClass); + var result = _overlays.Remove(overlayClass); + Sort(); + return result; } public bool RemoveOverlay() where T : Overlay @@ -104,8 +120,27 @@ public bool HasOverlay() where T : Overlay return _overlays.ContainsKey(typeof(T)); } + private void Sort() + { + _sortedOverlays.Clear(); + _sortedOverlays.AddRange(_overlays.Values); + _sortedOverlays.Sort(OverlayComparer.Instance); + } + void IPostInjectInit.PostInject() { _logger = _logMan.GetSawmill("overlay"); } + + private sealed class OverlayComparer : IComparer + { + public static readonly OverlayComparer Instance = new(); + + public int Compare(Overlay? x, Overlay? y) + { + var zX = x?.ZIndex ?? 0; + var zY = y?.ZIndex ?? 0; + return zX.CompareTo(zY); + } + } }