Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved Overlay sorting to OverlayManager #5614

Merged
merged 2 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}
}
41 changes: 38 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,27 @@ public bool HasOverlay<T>() 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<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);
}
}
}
Loading