Skip to content

Commit 0b564fc

Browse files
committed
Priority for SettingsMenu
1 parent 6b75da7 commit 0b564fc

3 files changed

Lines changed: 72 additions & 12 deletions

File tree

LabExtended/API/Settings/Menus/SettingsMenu.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ public abstract class SettingsMenu
3131
/// </summary>
3232
public virtual bool HeaderReducedPadding { get; } = false;
3333

34+
/// <summary>
35+
/// Gets the priority of the menu. Higher priority menus are displayed first.
36+
/// </summary>
37+
public virtual int Priority { get; } = 1;
38+
3439
/// <summary>
3540
/// Gets the player assigned to this menu.
3641
/// </summary>

LabExtended/API/Settings/SettingsManager.cs

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,34 @@ public static void SyncEntries(this ExPlayer player)
6363
var list = ListPool<ServerSpecificSettingBase>.Shared.Rent();
6464
var headers = ListPool<string>.Shared.Rent();
6565

66+
foreach (var menuEntry in player.settingsMenuLookup)
67+
{
68+
if (menuEntry.Value.IsHidden)
69+
continue;
70+
71+
if (!string.IsNullOrWhiteSpace(menuEntry.Value.Header) && !headers.Contains(menuEntry.Value.CustomId))
72+
{
73+
headers.Add(menuEntry.Value.CustomId);
74+
75+
list.Add(new SSGroupHeader(menuEntry.Value.Header,
76+
menuEntry.Value.HeaderReducedPadding, menuEntry.Value.HeaderHint));
77+
}
78+
79+
foreach (var menuSetting in menuEntry.Value.Entries)
80+
{
81+
if (menuSetting?.Base == null)
82+
continue;
83+
84+
if (!menuSetting.Player)
85+
continue;
86+
87+
if (menuSetting.IsHidden)
88+
continue;
89+
90+
list.Add(menuSetting.Base);
91+
}
92+
}
93+
6694
foreach (var settingsEntry in player.settingsIdLookup)
6795
{
6896
if (settingsEntry.Value?.Base == null)
@@ -75,18 +103,7 @@ public static void SyncEntries(this ExPlayer player)
75103
continue;
76104

77105
if (settingsEntry.Value.Menu != null)
78-
{
79-
if (settingsEntry.Value.Menu.IsHidden)
80-
continue;
81-
82-
if (!string.IsNullOrWhiteSpace(settingsEntry.Value.Menu.Header) && !headers.Contains(settingsEntry.Value.Menu.CustomId))
83-
{
84-
headers.Add(settingsEntry.Value.Menu.CustomId);
85-
86-
list.Add(new SSGroupHeader(settingsEntry.Value.Menu.Header,
87-
settingsEntry.Value.Menu.HeaderReducedPadding, settingsEntry.Value.Menu.HeaderHint));
88-
}
89-
}
106+
continue;
90107

91108
list.Add(settingsEntry.Value.Base);
92109
}
@@ -494,6 +511,7 @@ public static bool AddMenu(this ExPlayer player, SettingsMenu menu)
494511
menu.Entries = ListPool<SettingsEntry>.Shared.ToArrayReturn(entries);
495512

496513
player.settingsMenuLookup.Add(menu.CustomId, menu);
514+
player.settingsMenuLookup.Order(true, m => m.Value.Priority);
497515

498516
if (!string.IsNullOrEmpty(menu.Header))
499517
{
@@ -776,6 +794,9 @@ private static void OnPlayerVerified(ExPlayer player)
776794
}
777795
}
778796

797+
if (player.settingsMenuLookup.Count > 0)
798+
player.settingsMenuLookup.Order(true, m => m.Value.Priority);
799+
779800
if (player.settingsIdLookup.Count > 0)
780801
SyncEntries(player);
781802
}

LabExtended/Extensions/CollectionExtensions.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,40 @@ public static void For<T>(this IEnumerator<T> enumerator, Action<int, T> action,
549549
#endregion
550550

551551
#region Dictionary Extensions
552+
/// <summary>
553+
/// Reorders the elements of the dictionary in place according to a specified key selector and sort direction.
554+
/// </summary>
555+
/// <remarks>This method clears and repopulates the dictionary to reflect the new order. The ordering is
556+
/// determined by the integer value returned from the selector function for each key-value pair. Note that the order
557+
/// of elements in a standard IDictionary{TKey, TValue} implementation is not guaranteed; use this method only with
558+
/// dictionary types that preserve insertion order, such as Dictionary{TKey, TValue} in .NET Core 3.0 and later, or
559+
/// OrderedDictionary.</remarks>
560+
/// <typeparam name="TKey">The type of keys in the dictionary.</typeparam>
561+
/// <typeparam name="TValue">The type of values in the dictionary.</typeparam>
562+
/// <param name="dict">The dictionary whose elements are to be reordered. Cannot be null.</param>
563+
/// <param name="descending">true to sort the dictionary in descending order; otherwise, false for ascending order.</param>
564+
/// <param name="selector">A function to extract the sort key from each key-value pair. Cannot be null.</param>
565+
/// <exception cref="ArgumentNullException">Thrown if dict or selector is null.</exception>
566+
public static void Order<TKey, TValue>(this Dictionary<TKey, TValue> dict, bool descending, Func<KeyValuePair<TKey, TValue>, int> selector)
567+
{
568+
if (dict is null)
569+
throw new ArgumentNullException(nameof(dict));
570+
571+
if (selector is null)
572+
throw new ArgumentNullException(nameof(selector));
573+
574+
var ordered = (descending
575+
? dict.OrderByDescending(selector)
576+
: dict.OrderBy(selector)).ToList();
577+
578+
dict.Clear();
579+
580+
foreach (var pair in ordered)
581+
dict[pair.Key] = pair.Value;
582+
583+
ordered.Clear();
584+
}
585+
552586
/// <summary>
553587
/// Attempts find a key by pair value.
554588
/// </summary>

0 commit comments

Comments
 (0)