Skip to content

Commit

Permalink
settings api fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
marchellc committed Jan 22, 2025
1 parent d1b25df commit 93668bf
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private static void ApplyTestMenu(List<SettingsMenu> menus)
public class TestMenu : SettingsMenu
{
public override string CustomId { get; } = "testMenu";
public override string MenuLabel { get; } = "Test Menu";
public override string Header { get; } = "Test Menu";

public override void BuildMenu(List<SettingsEntry> settings)
{
Expand Down
7 changes: 7 additions & 0 deletions LabExtended/API/Settings/Entries/Dropdown/SettingsDropdown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public SettingsDropdown(
SSDropdownSetting.DropdownEntryType dropdownEntryType,
Action<SettingsDropdown> dropdownBuilder = null,
string dropdownHint = null)

: base(new SSDropdownSetting(
SettingsManager.GetIntegerId(customId),

Expand All @@ -31,6 +32,9 @@ public SettingsDropdown(
customId)
{
Base = (SSDropdownSetting)base.Base;

dropdownBuilder.InvokeSafe(this);

Base.Options = Options.Select(x => x.Text).ToArray();

_prevSelectedIndex = Base.DefaultOptionIndex;
Expand Down Expand Up @@ -72,6 +76,9 @@ public SettingsDropdownOption SelectedOption
return Options[SelectedIndex];
}
}

public void SyncOptions()
=> Base.Options = Options.Select(x => x.Text).ToArray();

public SettingsDropdown AddOption<T>(T optionValue, string optionText)
=> AddOption(new SettingsDropdownOption<T>(optionValue, optionText));
Expand Down
8 changes: 6 additions & 2 deletions LabExtended/API/Settings/Menus/SettingsMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ namespace LabExtended.API.Settings.Menus
public abstract class SettingsMenu
{
public abstract string CustomId { get; }
public abstract string MenuLabel { get; }


public abstract string Header { get; }

public virtual string HeaderHint { get; }
public virtual bool HeaderReducedPadding { get; }

public ExPlayer Player { get; internal set; }

public LockedList<SettingsEntry> Settings { get; } = new LockedList<SettingsEntry>();
Expand Down
9 changes: 9 additions & 0 deletions LabExtended/API/Settings/SettingsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ namespace LabExtended.API.Settings;

public static class SettingsExtensions
{
public static List<SettingsEntry> WithEntry<T>(this List<SettingsEntry> entries) where T : SettingsEntry, new()
{
if (entries is null)
throw new ArgumentNullException(nameof(entries));

entries.Add(Activator.CreateInstance<T>());
return entries;
}

public static List<SettingsEntry> WithEntry(this List<SettingsEntry> entries, SettingsEntry entry)
{
if (entries is null)
Expand Down
10 changes: 6 additions & 4 deletions LabExtended/API/Settings/SettingsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,15 +284,17 @@ private static void OnPlayerJoined(ExPlayer player)

builtMenu.Player = player;

if (!string.IsNullOrWhiteSpace(builtMenu.MenuLabel))
builtList.Add(new SSGroupHeader(builtMenu.MenuLabel));

builtMenu.BuildMenu(menuList);

builtMenu.Settings.Clear();
builtMenu.Settings.AddRange(menuList);

ListPool<SettingsEntry>.Shared.Return(menuList);

if (!idEntries.TryGetValue(builtMenu.CustomId, out var menuHeader))
idEntries.Add(builtMenu.CustomId, menuHeader = new LockedDictionary<ExPlayer, SettingsEntry>());

menuHeader[player] = new SettingsGroup(builtMenu.Header, builtMenu.HeaderReducedPadding, builtMenu.HeaderHint);

for (int y = 0; y < builtMenu.Settings.Count; y++)
{
Expand Down Expand Up @@ -461,7 +463,7 @@ internal static void OnResponseMessage(NetworkConnection connection, SSSClientRe
break;

case SettingsDropdown dropdown:
entry.Menu.OnDropdownSelected(dropdown, dropdown.TryGetOption(dropdown.SelectedIndex, out var option) ? option : null);
entry.Menu.OnDropdownSelected(dropdown, dropdown.SelectedOption);
break;
}
}
Expand Down

0 comments on commit 93668bf

Please sign in to comment.