Files
OldThink/Content.Client/ContextMenu/UI/EntityMenuPresenterGrouping.cs

93 lines
3.6 KiB
C#
Raw Normal View History

using Content.Shared.IdentityManagement;
using Robust.Client.GameObjects;
2022-07-29 14:13:12 +12:00
using System.Linq;
2021-06-09 22:19:39 +02:00
namespace Content.Client.ContextMenu.UI
{
public sealed partial class EntityMenuUIController
{
public const int GroupingTypesCount = 2;
private int GroupingContextMenuType { get; set; }
public void OnGroupingChanged(int obj)
{
_context.Close();
GroupingContextMenuType = obj;
}
2021-12-05 18:09:01 +01:00
private List<List<EntityUid>> GroupEntities(IEnumerable<EntityUid> entities, int depth = 0)
{
if (GroupingContextMenuType == 0)
{
var newEntities = entities.GroupBy(e => Identity.Name(e, _entityManager)).ToList();
return newEntities.Select(grp => grp.ToList()).ToList();
}
else
{
2021-12-08 12:09:43 +01:00
var newEntities = entities.GroupBy(e => e, new PrototypeAndStatesContextMenuComparer(depth, _entityManager)).ToList();
return newEntities.Select(grp => grp.ToList()).ToList();
}
}
2021-12-05 18:09:01 +01:00
private sealed class PrototypeAndStatesContextMenuComparer : IEqualityComparer<EntityUid>
{
2021-12-08 12:09:43 +01:00
private static readonly List<Func<EntityUid, EntityUid, IEntityManager, bool>> EqualsList = new()
{
2021-12-08 12:09:43 +01:00
(a, b, entMan) => entMan.GetComponent<MetaDataComponent>(a).EntityPrototype!.ID == entMan.GetComponent<MetaDataComponent>(b).EntityPrototype!.ID,
(a, b, entMan) =>
{
entMan.TryGetComponent(a, out SpriteComponent? spriteA);
entMan.TryGetComponent(b, out SpriteComponent? spriteB);
2021-11-26 18:00:28 +13:00
if (spriteA == null || spriteB == null)
return spriteA == spriteB;
var xStates = spriteA.AllLayers.Where(e => e.Visible).Select(s => s.RsiState.Name);
var yStates = spriteB.AllLayers.Where(e => e.Visible).Select(s => s.RsiState.Name);
return xStates.OrderBy(t => t).SequenceEqual(yStates.OrderBy(t => t));
},
};
2021-12-08 12:09:43 +01:00
private static readonly List<Func<EntityUid, IEntityManager, int>> GetHashCodeList = new()
{
2021-12-08 12:09:43 +01:00
(e, entMan) => EqualityComparer<string>.Default.GetHashCode(entMan.GetComponent<MetaDataComponent>(e).EntityPrototype!.ID),
(e, entMan) =>
{
var hash = 0;
2023-01-15 13:38:53 +11:00
foreach (var element in entMan.GetComponent<SpriteComponent>(e).AllLayers.Where(obj => obj.Visible).Select(s => s.RsiState.Name))
{
hash ^= EqualityComparer<string>.Default.GetHashCode(element!);
}
return hash;
},
};
private static int Count => EqualsList.Count - 1;
private readonly int _depth;
2021-12-08 12:09:43 +01:00
private readonly IEntityManager _entMan;
public PrototypeAndStatesContextMenuComparer(int step = 0, IEntityManager? entMan = null)
{
2021-12-08 12:09:43 +01:00
IoCManager.Resolve(ref entMan);
_depth = step > Count ? Count : step;
2021-12-08 12:09:43 +01:00
_entMan = entMan;
}
2021-12-05 18:09:01 +01:00
public bool Equals(EntityUid x, EntityUid y)
{
2021-12-05 18:09:01 +01:00
if (x == default)
{
2021-12-05 18:09:01 +01:00
return y == default;
}
2021-12-08 12:09:43 +01:00
return y != default && EqualsList[_depth](x, y, _entMan);
}
2021-12-05 18:09:01 +01:00
public int GetHashCode(EntityUid e)
{
2021-12-08 12:09:43 +01:00
return GetHashCodeList[_depth](e, _entMan);
}
}
}
}