diff --git a/Projects/UOContent/Gumps/WhoGump.cs b/Projects/UOContent/Gumps/WhoGump.cs index 3852414eb..979239aa9 100644 --- a/Projects/UOContent/Gumps/WhoGump.cs +++ b/Projects/UOContent/Gumps/WhoGump.cs @@ -5,297 +5,289 @@ using static Server.Gumps.PropsConfig; -namespace Server.Gumps +namespace Server.Gumps; +public class WhoGump : DynamicGump { - public class WhoGump : Gump - { - private static readonly int PrevLabelOffsetX = PrevWidth + 1; - private static readonly int PrevLabelOffsetY = 0; + private static readonly int PrevLabelOffsetX = PrevWidth + 1; + private static readonly int PrevLabelOffsetY = 0; - private static readonly int NextLabelOffsetX = -29; - private static readonly int NextLabelOffsetY = 0; + private static readonly int NextLabelOffsetX = -29; + private static readonly int NextLabelOffsetY = 0; - private static readonly int EntryWidth = 180; - private static readonly int EntryCount = 15; + private static readonly int EntryWidth = 180; + private static readonly int EntryCount = 15; - private static readonly int TotalWidth = OffsetSize + EntryWidth + OffsetSize + SetWidth + OffsetSize; + private static readonly int TotalWidth = OffsetSize + EntryWidth + OffsetSize + SetWidth + OffsetSize; - private static readonly int BackWidth = BorderSize + TotalWidth + BorderSize; + private static readonly int BackWidth = BorderSize + TotalWidth + BorderSize; - private readonly List m_Mobiles; + private readonly List _mobiles; + private readonly int _page; - private Mobile m_Owner; - private int m_Page; + public WhoGump(Mobile owner, string filter) : this(BuildList(owner, filter)) + { + } - public WhoGump(Mobile owner, string filter) : this(owner, BuildList(owner, filter)) - { - } + public WhoGump(List list, int page = 0) : base(GumpOffsetX, GumpOffsetY) + { + _mobiles = list; + _page = page; - public WhoGump(Mobile owner, List list, int page = 0) : base(GumpOffsetX, GumpOffsetY) - { - owner.CloseGump(); + // Initialize(page); + } - m_Owner = owner; - m_Mobiles = list; + public static void Configure() + { + CommandSystem.Register("WhoList", AccessLevel.Counselor, WhoList_OnCommand); + } - Initialize(page); - } + [Usage("WhoList [filter]")] + [Aliases("Who")] + [Description("Lists all connected clients. Optionally filters results by name.")] + private static void WhoList_OnCommand(CommandEventArgs e) + { + e.Mobile.SendGump(new WhoGump(e.Mobile, e.ArgString)); + } - public static void Configure() - { - CommandSystem.Register("WhoList", AccessLevel.Counselor, WhoList_OnCommand); - } + public static List BuildList(Mobile owner, string rawFilter) + { + var filter = rawFilter.Trim().ToLower().DefaultIfNullOrEmpty(null); - [Usage("WhoList [filter]")] - [Aliases("Who")] - [Description("Lists all connected clients. Optionally filters results by name.")] - private static void WhoList_OnCommand(CommandEventArgs e) - { - e.Mobile.SendGump(new WhoGump(e.Mobile, e.ArgString)); - } + var list = new List(); - public static List BuildList(Mobile owner, string rawFilter) + foreach (var ns in NetState.Instances) { - var filter = rawFilter.Trim().ToLower().DefaultIfNullOrEmpty(null); + var m = ns.Mobile; - var list = new List(); - - foreach (var ns in NetState.Instances) + if (m != null && (m == owner || !m.Hidden || owner.AccessLevel >= m.AccessLevel || + m is PlayerMobile mobile && mobile.VisibilityList.Contains(owner))) { - var m = ns.Mobile; - - if (m != null && (m == owner || !m.Hidden || owner.AccessLevel >= m.AccessLevel || - m is PlayerMobile mobile && mobile.VisibilityList.Contains(owner))) + if (filter != null && !m.Name.InsensitiveContains(filter)) { - if (filter != null && !m.Name.InsensitiveContains(filter)) - { - continue; - } - - list.Add(m); + continue; } - } - list.Sort(InternalComparer.Instance); - - return list; + list.Add(m); + } } - public void Initialize(int page) - { - m_Page = page; + list.Sort(InternalComparer.Instance); - var count = Math.Clamp(m_Mobiles.Count - page * EntryCount, 0, EntryCount); + return list; + } - var totalHeight = OffsetSize + (EntryHeight + OffsetSize) * (count + 1); + protected override void BuildLayout(ref DynamicGumpBuilder builder) + { + var count = Math.Clamp(_mobiles.Count - _page * EntryCount, 0, EntryCount); - AddPage(0); + var totalHeight = OffsetSize + (EntryHeight + OffsetSize) * (count + 1); - AddBackground(0, 0, BackWidth, BorderSize + totalHeight + BorderSize, BackGumpID); - AddImageTiled( - BorderSize, - BorderSize, - TotalWidth - (OldStyle ? SetWidth + OffsetSize : 0), - totalHeight, - OffsetGumpID - ); + builder.AddPage(); - var x = BorderSize + OffsetSize; - var y = BorderSize + OffsetSize; + builder.AddBackground(0, 0, BackWidth, BorderSize + totalHeight + BorderSize, BackGumpID); + builder.AddImageTiled( + BorderSize, + BorderSize, + TotalWidth - (OldStyle ? SetWidth + OffsetSize : 0), + totalHeight, + OffsetGumpID + ); - var emptyWidth = TotalWidth - PrevWidth - NextWidth - OffsetSize * 4 - (OldStyle ? SetWidth + OffsetSize : 0); + var x = BorderSize + OffsetSize; + var y = BorderSize + OffsetSize; - if (!OldStyle) - { - AddImageTiled( - x - (OldStyle ? OffsetSize : 0), - y, - emptyWidth + (OldStyle ? OffsetSize * 2 : 0), - EntryHeight, - EntryGumpID - ); - } + var emptyWidth = TotalWidth - PrevWidth - NextWidth - OffsetSize * 4 - (OldStyle ? SetWidth + OffsetSize : 0); - AddLabel( - x + TextOffsetX, + if (!OldStyle) + { + builder.AddImageTiled( + x - (OldStyle ? OffsetSize : 0), y, - TextHue, - $"Page {page + 1} of {(m_Mobiles.Count + EntryCount - 1) / EntryCount} ({m_Mobiles.Count})" + emptyWidth + (OldStyle ? OffsetSize * 2 : 0), + EntryHeight, + EntryGumpID ); + } - x += emptyWidth + OffsetSize; + builder.AddLabel( + x + TextOffsetX, + y, + TextHue, + $"Page {_page + 1} of {(_mobiles.Count + EntryCount - 1) / EntryCount} ({_mobiles.Count})" + ); - if (OldStyle) - { - AddImageTiled(x, y, TotalWidth - OffsetSize * 3 - SetWidth, EntryHeight, HeaderGumpID); - } - else + x += emptyWidth + OffsetSize; + + if (OldStyle) + { + builder.AddImageTiled(x, y, TotalWidth - OffsetSize * 3 - SetWidth, EntryHeight, HeaderGumpID); + } + else + { + builder.AddImageTiled(x, y, PrevWidth, EntryHeight, HeaderGumpID); + } + + if (_page > 0) + { + builder.AddButton(x + PrevOffsetX, y + PrevOffsetY, PrevButtonID1, PrevButtonID2, 1); + + if (PrevLabel) { - AddImageTiled(x, y, PrevWidth, EntryHeight, HeaderGumpID); + builder.AddLabel(x + PrevLabelOffsetX, y + PrevLabelOffsetY, TextHue, "Previous"); } + } - if (page > 0) - { - AddButton(x + PrevOffsetX, y + PrevOffsetY, PrevButtonID1, PrevButtonID2, 1); + x += PrevWidth + OffsetSize; - if (PrevLabel) - { - AddLabel(x + PrevLabelOffsetX, y + PrevLabelOffsetY, TextHue, "Previous"); - } - } + if (!OldStyle) + { + builder.AddImageTiled(x, y, NextWidth, EntryHeight, HeaderGumpID); + } - x += PrevWidth + OffsetSize; + if ((_page + 1) * EntryCount < _mobiles.Count) + { + builder.AddButton(x + NextOffsetX, y + NextOffsetY, NextButtonID1, NextButtonID2, 2, GumpButtonType.Reply, 1); - if (!OldStyle) + if (NextLabel) { - AddImageTiled(x, y, NextWidth, EntryHeight, HeaderGumpID); + builder.AddLabel(x + NextLabelOffsetX, y + NextLabelOffsetY, TextHue, "Next"); } + } - if ((page + 1) * EntryCount < m_Mobiles.Count) - { - AddButton(x + NextOffsetX, y + NextOffsetY, NextButtonID1, NextButtonID2, 2, GumpButtonType.Reply, 1); - - if (NextLabel) - { - AddLabel(x + NextLabelOffsetX, y + NextLabelOffsetY, TextHue, "Next"); - } - } + for (int i = 0, index = _page * EntryCount; i < EntryCount && index < _mobiles.Count; ++i, ++index) + { + x = BorderSize + OffsetSize; + y += EntryHeight + OffsetSize; - for (int i = 0, index = page * EntryCount; i < EntryCount && index < m_Mobiles.Count; ++i, ++index) - { - x = BorderSize + OffsetSize; - y += EntryHeight + OffsetSize; + var m = _mobiles[index]; - var m = m_Mobiles[index]; + builder.AddImageTiled(x, y, EntryWidth, EntryHeight, EntryGumpID); + builder.AddLabelCropped( + x + TextOffsetX, + y, + EntryWidth - TextOffsetX, + EntryHeight, + GetHueFor(m), + m.Deleted ? "(deleted)" : m.Name + ); - AddImageTiled(x, y, EntryWidth, EntryHeight, EntryGumpID); - AddLabelCropped( - x + TextOffsetX, - y, - EntryWidth - TextOffsetX, - EntryHeight, - GetHueFor(m), - m.Deleted ? "(deleted)" : m.Name - ); + x += EntryWidth + OffsetSize; - x += EntryWidth + OffsetSize; + if (SetGumpID != 0) + { + builder.AddImageTiled(x, y, SetWidth, EntryHeight, SetGumpID); + } - if (SetGumpID != 0) - { - AddImageTiled(x, y, SetWidth, EntryHeight, SetGumpID); - } + if (m.NetState != null && !m.Deleted) + { + builder.AddButton(x + SetOffsetX, y + SetOffsetY, SetButtonID1, SetButtonID2, i + 3); + } + } + } - if (m.NetState != null && !m.Deleted) + private static int GetHueFor(Mobile m) + { + switch (m.AccessLevel) + { + case AccessLevel.Owner: + case AccessLevel.Developer: + case AccessLevel.Administrator: return 0x516; + case AccessLevel.Seer: return 0x144; + case AccessLevel.GameMaster: return 0x21; + case AccessLevel.Counselor: return 0x2; + default: { - AddButton(x + SetOffsetX, y + SetOffsetY, SetButtonID1, SetButtonID2, i + 3); + return m.Kills >= 5 ? 0x21 : + m.Criminal ? 0x3B1 : 0x58; } - } } + } + + public override void OnResponse(NetState state, in RelayInfo info) + { + var from = state.Mobile; - private static int GetHueFor(Mobile m) + switch (info.ButtonID) { - switch (m.AccessLevel) - { - case AccessLevel.Owner: - case AccessLevel.Developer: - case AccessLevel.Administrator: return 0x516; - case AccessLevel.Seer: return 0x144; - case AccessLevel.GameMaster: return 0x21; - case AccessLevel.Counselor: return 0x2; - default: + case 0: // Closed + { + return; + } + case 1: // Previous + { + if (_page > 0) { - return m.Kills >= 5 ? 0x21 : - m.Criminal ? 0x3B1 : 0x58; + from.SendGump(new WhoGump(_mobiles, _page - 1)); } - } - } - - public override void OnResponse(NetState state, in RelayInfo info) - { - var from = state.Mobile; - switch (info.ButtonID) - { - case 0: // Closed + break; + } + case 2: // Next + { + if ((_page + 1) * EntryCount < _mobiles.Count) { - return; + from.SendGump(new WhoGump(_mobiles, _page + 1)); } - case 1: // Previous + + break; + } + default: + { + var index = _page * EntryCount + (info.ButtonID - 3); + + if (index >= 0 && index < _mobiles.Count) { - if (m_Page > 0) + var m = _mobiles[index]; + + if (m.Deleted) { - from.SendGump(new WhoGump(from, m_Mobiles, m_Page - 1)); + from.SendMessage("That player has deleted their character."); + from.SendGump(new WhoGump(_mobiles, _page)); } - - break; - } - case 2: // Next - { - if ((m_Page + 1) * EntryCount < m_Mobiles.Count) + else if (m.NetState == null) { - from.SendGump(new WhoGump(from, m_Mobiles, m_Page + 1)); + from.SendMessage("That player is no longer online."); + from.SendGump(new WhoGump(_mobiles, _page)); } - - break; - } - default: - { - var index = m_Page * EntryCount + (info.ButtonID - 3); - - if (index >= 0 && index < m_Mobiles.Count) + else if (m == from || !m.Hidden || from.AccessLevel >= m.AccessLevel || + m is PlayerMobile mobile && mobile.VisibilityList.Contains(from)) { - var m = m_Mobiles[index]; - - if (m.Deleted) - { - from.SendMessage("That player has deleted their character."); - from.SendGump(new WhoGump(from, m_Mobiles, m_Page)); - } - else if (m.NetState == null) - { - from.SendMessage("That player is no longer online."); - from.SendGump(new WhoGump(from, m_Mobiles, m_Page)); - } - else if (m == from || !m.Hidden || from.AccessLevel >= m.AccessLevel || - m is PlayerMobile mobile && mobile.VisibilityList.Contains(from)) - { - from.SendGump(new ClientGump(from, m.NetState)); - } - else - { - from.SendMessage("You cannot see them."); - from.SendGump(new WhoGump(from, m_Mobiles, m_Page)); - } + from.SendGump(new ClientGump(from, m.NetState)); + } + else + { + from.SendMessage("You cannot see them."); + from.SendGump(new WhoGump(_mobiles, _page)); } - - break; } - } + + break; + } } + } - private class InternalComparer : IComparer - { - public static readonly IComparer Instance = new InternalComparer(); + private class InternalComparer : IComparer + { + public static readonly IComparer Instance = new InternalComparer(); - public int Compare(Mobile x, Mobile y) + public int Compare(Mobile x, Mobile y) + { + if (x == null) { - if (x == null) - { - throw new ArgumentNullException(nameof(x)); - } - - if (y == null) - { - throw new ArgumentNullException(nameof(y)); - } + throw new ArgumentNullException(nameof(x)); + } - if (x.AccessLevel > y.AccessLevel) - { - return -1; - } + if (y == null) + { + throw new ArgumentNullException(nameof(y)); + } - return x.AccessLevel < y.AccessLevel ? 1 : x.Name.InsensitiveCompare(y.Name); + if (x.AccessLevel > y.AccessLevel) + { + return -1; } + + return x.AccessLevel < y.AccessLevel ? 1 : x.Name.InsensitiveCompare(y.Name); } } }