Skip to content

Commit

Permalink
Moved Overlay sorting to OverlayManager
Browse files Browse the repository at this point in the history
  • Loading branch information
Tornado-Technology committed Jan 17, 2025
1 parent 9bb7af3 commit 37001de
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 17 deletions.
14 changes: 0 additions & 14 deletions Robust.Client/Graphics/Clyde/Clyde.HLR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,6 @@ private List<Overlay> GetOverlaysForSpace(OverlaySpace space)
}
}

_overlays.Sort(OverlayComparer.Instance);

return _overlays;
}

Expand Down Expand Up @@ -574,17 +572,5 @@ private static Box2Rotated CalcWorldBounds(Viewport viewport)

return new Box2Rotated(aabb, rotation, aabb.Center);
}

private sealed class OverlayComparer : IComparer<Overlay>
{
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);
}
}
}
}
46 changes: 43 additions & 3 deletions Robust.Client/Graphics/Overlays/OverlayManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,22 @@ internal sealed class OverlayManager : IOverlayManagerInternal, IPostInjectInit
[Dependency] private readonly ILogManager _logMan = default!;

[ViewVariables]
private readonly Dictionary<Type, Overlay> _overlays = new Dictionary<Type, Overlay>();
private readonly Dictionary<Type, Overlay> _overlays = new();

/// <summary>
/// A list that duplicates a value from <see cref="_overlays"/>,
/// but already sorted, by invoking <see cref="Sort"/>
/// in <see cref="AddOverlay"/> and <see cref="RemoveOverlay(System.Type)"/>.
/// </summary>
[ViewVariables]
private readonly List<Overlay> _sortedOverlays = [];

private ISawmill _logger = default!;

public IEnumerable<Overlay> AllOverlays => _overlays.Values;
/// <summary>
/// Returns a list of all overlays sorted by <see cref="Overlay.ZIndex"/>
/// </summary>
public IEnumerable<Overlay> AllOverlays => _sortedOverlays;

public void FrameUpdate(FrameEventArgs args)
{
Expand All @@ -30,7 +42,9 @@ public bool AddOverlay(Overlay overlay)
{
if (_overlays.ContainsKey(overlay.GetType()))
return false;

_overlays.Add(overlay.GetType(), overlay);
Sort();
return true;
}

Expand All @@ -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<T>() where T : Overlay
Expand Down Expand Up @@ -104,8 +120,32 @@ public bool HasOverlay<T>() where T : Overlay
return _overlays.ContainsKey(typeof(T));
}

private void Sort()
{
_sortedOverlays.Clear();

foreach (var overlay in _overlays.Values)
{
_sortedOverlays.Add(overlay);
}

_sortedOverlays.Sort(OverlayComparer.Instance);
}

void IPostInjectInit.PostInject()
{
_logger = _logMan.GetSawmill("overlay");
}

private sealed class OverlayComparer : IComparer<Overlay>
{
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);
}
}
}

0 comments on commit 37001de

Please sign in to comment.