Skip to content

Commit 856aeb5

Browse files
committed
Settings API cleanup & docs
1 parent 4c32827 commit 856aeb5

16 files changed

Lines changed: 999 additions & 168 deletions

LabExtended/API/ExPlayer.cs

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,62 @@
11
using CentralAuth;
22
using CommandSystem;
3+
34
using Footprinting;
5+
46
using Hints;
7+
58
using InventorySystem.Disarming;
9+
610
using InventorySystem.Items;
711
using InventorySystem.Items.Pickups;
12+
813
using LabApi.Features.Wrappers;
14+
15+
using LabExtended.API.Enums;
916
using LabExtended.API.Containers;
1017
using LabExtended.API.CustomVoice;
11-
using LabExtended.API.Enums;
1218
using LabExtended.API.FileStorage;
19+
1320
using LabExtended.API.Hints;
1421
using LabExtended.API.Hints.Elements.Personal;
22+
1523
using LabExtended.API.RemoteAdmin;
24+
1625
using LabExtended.API.Settings.Entries;
1726
using LabExtended.API.Settings.Menus;
27+
1828
using LabExtended.Commands.Attributes;
1929
using LabExtended.Commands.Interfaces;
30+
2031
using LabExtended.Core;
2132
using LabExtended.Core.Pooling.Pools;
33+
2234
using LabExtended.Events;
23-
using LabExtended.Extensions;
2435
using LabExtended.Utilities;
36+
using LabExtended.Extensions;
37+
2538
using LiteNetLib;
39+
2640
using Mirror;
2741
using Mirror.LiteNetLib4Mirror;
42+
2843
using NetworkManagerUtils.Dummies;
44+
2945
using NorthwoodLib.Pools;
46+
3047
using PlayerRoles;
31-
using PlayerRoles.FirstPersonControl;
3248
using PlayerRoles.Spectating;
49+
using PlayerRoles.FirstPersonControl;
50+
3351
using RemoteAdmin;
3452
using RemoteAdmin.Communication;
35-
using System.Collections.Generic;
36-
using System.Collections.ObjectModel;
53+
3754
using System.Reflection;
55+
3856
using UnityEngine;
57+
3958
using UserSettings.ServerSpecific;
59+
4060
using VoiceChat;
4161

4262
#pragma warning disable CS8602 // Dereference of a possibly null reference.
@@ -1473,21 +1493,6 @@ public void Dispose()
14731493
Rotation = null!;
14741494
}
14751495

1476-
internal void SyncSettingsByAssembly(Assembly assembly, ServerSpecificSettingBase[] collection) {
1477-
if (!this)
1478-
return;
1479-
1480-
if (settingsByAssembly == null) {
1481-
ApiLog.Warn($"Player's {nameof(settingsByAssembly)} is null");
1482-
return;
1483-
}
1484-
1485-
if (collection == null || collection.Length == 0)
1486-
settingsByAssembly.Remove(assembly);
1487-
else
1488-
settingsByAssembly[assembly] = collection;
1489-
}
1490-
14911496
private static ReferenceHub SpawnHiddenDummy(string nick)
14921497
{
14931498
var hubGo = UnityEngine.Object.Instantiate(NetworkManager.singleton.playerPrefab);

LabExtended/API/Settings/Entries/Buttons/SettingsButton.cs

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,26 @@
77

88
namespace LabExtended.API.Settings.Entries.Buttons
99
{
10+
/// <summary>
11+
/// A pressable button.
12+
/// </summary>
1013
public class SettingsButton : SettingsEntry, IWrapper<SSButton>
1114
{
15+
/// <summary>
16+
/// Initializes a new instance of the SettingsButton class with the specified button configuration and behavior.
17+
/// </summary>
18+
/// <param name="customId">A unique identifier for the button, used to associate the button with a specific setting.</param>
19+
/// <param name="buttonLabel">The text displayed as the button's label in the user interface.</param>
20+
/// <param name="buttonText">The text shown on the button itself.</param>
21+
/// <param name="buttonHint">An optional hint or tooltip text displayed to provide additional information about the button. Can be null
22+
/// if no hint is required.</param>
23+
/// <param name="requiredHeldTimeSeconds">The minimum time, in seconds, that the button must be held down to activate. If null, the button does not
24+
/// require a hold duration.</param>
1225
public SettingsButton(
1326
string customId,
1427
string buttonLabel,
1528
string buttonText,
16-
string buttonHint = null,
29+
string? buttonHint = null,
1730
float? requiredHeldTimeSeconds = null)
1831

1932
: base(new SSButton(
@@ -32,12 +45,24 @@ public SettingsButton(
3245
private SettingsButton(SSButton baseValue, string customId) : base(baseValue, customId)
3346
=> Base = baseValue;
3447

48+
/// <summary>
49+
/// Gets or sets the callback to invoke when the settings button is triggered.
50+
/// </summary>
3551
public Action<SettingsButton> OnTriggered { get; set; }
3652

53+
/// <summary>
54+
/// Gets the base entry.
55+
/// </summary>
3756
public new SSButton Base { get; }
3857

58+
/// <summary>
59+
/// Gets the time, in seconds (using <see cref="Time.realtimeSinceStartup"/>), at which the last trigger event occurred, or null if no trigger has occurred.
60+
/// </summary>
3961
public float? LastTriggerTime { get; private set; }
4062

63+
/// <summary>
64+
/// Gets the time (using <see cref="Time.realtimeSinceStartup"/>), at which the last trigger event occurred, or null if no trigger has occurred.
65+
/// </summary>
4166
public TimeSpan? TimeSinceLastTrigger
4267
{
4368
get
@@ -49,16 +74,19 @@ public TimeSpan? TimeSinceLastTrigger
4974
}
5075
}
5176

77+
/// <summary>
78+
/// Gets or sets the required duration, in seconds, that the button must be held to trigger successfully.
79+
/// </summary>
5280
public float RequiredHeldTimeSeconds
5381
{
5482
get => Base.HoldTimeSeconds;
5583
set => Base.HoldTimeSeconds = value;
5684
}
5785

5886
/// <inheritdoc />
59-
internal override void InternalOnUpdated()
87+
internal override void Internal_Updated()
6088
{
61-
base.InternalOnUpdated();
89+
base.Internal_Updated();
6290

6391
LastTriggerTime = Time.realtimeSinceStartup;
6492

@@ -67,12 +95,33 @@ internal override void InternalOnUpdated()
6795
OnTriggered.InvokeSafe(this);
6896
}
6997

98+
/// <summary>
99+
/// An overridable method called when the button is pressed.
100+
/// </summary>
70101
public virtual void HandleTrigger() { }
71102

103+
/// <summary>
104+
/// Returns a string that represents the current settings button, including its custom ID, assigned ID, and
105+
/// associated player user ID.
106+
/// </summary>
107+
/// <returns>A string containing the custom ID, assigned ID, and player user ID of the settings button. If no player is
108+
/// assigned, the player user ID is represented as "null".</returns>
72109
public override string ToString()
73110
=> $"SettingsButton (CustomId={CustomId}; AssignedId={AssignedId}; Ply={Player?.UserId ?? "null"})";
74111

75-
public static SettingsButton Create(string customId, string buttonLabel, string buttonText, string buttonHint = null, float? requiredHeldTimeSeconds = null)
112+
/// <summary>
113+
/// Creates a new instance of the SettingsButton class with the specified identifier, label, text, and optional
114+
/// settings.
115+
/// </summary>
116+
/// <param name="customId">A unique string identifier for the button. Cannot be null, empty, or consist only of white-space characters.</param>
117+
/// <param name="buttonLabel">The label displayed on the button. Cannot be null, empty, or consist only of white-space characters.</param>
118+
/// <param name="buttonText">The text shown on the button. Cannot be null, empty, or consist only of white-space characters.</param>
119+
/// <param name="buttonHint">An optional hint or tooltip text for the button. May be null if no hint is required.</param>
120+
/// <param name="requiredHeldTimeSeconds">The optional duration, in seconds, that the button must be held before activation. If null, the button does
121+
/// not require a hold.</param>
122+
/// <returns>A new SettingsButton instance configured with the specified parameters.</returns>
123+
/// <exception cref="ArgumentNullException">Thrown if customId, buttonLabel, or buttonText is null, empty, or consists only of white-space characters.</exception>
124+
public static SettingsButton Create(string customId, string buttonLabel, string buttonText, string? buttonHint = null, float? requiredHeldTimeSeconds = null)
76125
{
77126
if (string.IsNullOrWhiteSpace(customId))
78127
throw new ArgumentNullException(nameof(customId));

LabExtended/API/Settings/Entries/Buttons/SettingsTwoButtons.cs

Lines changed: 94 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
11
using LabExtended.API.Interfaces;
22
using LabExtended.Extensions;
3+
34
using UnityEngine;
45
using UserSettings.ServerSpecific;
56

67
namespace LabExtended.API.Settings.Entries.Buttons
78
{
9+
/// <summary>
10+
/// An entry with two buttons (A and B) used as a selection.
11+
/// </summary>
812
public class SettingsTwoButtons : SettingsEntry, IWrapper<SSTwoButtonsSetting>
913
{
1014
private bool _previousIsSyncB;
1115

16+
/// <summary>
17+
/// Initializes a new instance of the SettingsTwoButtons class with the specified button labels, texts, and
18+
/// configuration options.
19+
/// </summary>
20+
/// <param name="customId">A unique identifier for the setting. This value is used to distinguish this setting from others and must not
21+
/// be null or empty.</param>
22+
/// <param name="buttonLabel">The label displayed above the two buttons. This provides context or a description for the button group.</param>
23+
/// <param name="buttonAText">The text displayed on the first button (Button A).</param>
24+
/// <param name="buttonBText">The text displayed on the second button (Button B).</param>
25+
/// <param name="isDefaultButtonB">true to make Button B the default selection; otherwise, false to make Button A the default.</param>
26+
/// <param name="buttonsHint">An optional hint or description displayed alongside the buttons to assist the user. Can be null if no hint
27+
/// is needed.</param>
1228
public SettingsTwoButtons(
1329
string customId,
1430
string buttonLabel,
@@ -33,6 +49,7 @@ public SettingsTwoButtons(
3349
customId)
3450
{
3551
Base = (SSTwoButtonsSetting)base.Base;
52+
3653
_previousIsSyncB = Base.DefaultIsB;
3754
}
3855

@@ -42,13 +59,29 @@ private SettingsTwoButtons(SSTwoButtonsSetting baseValue, string customId) : bas
4259
_previousIsSyncB = Base.DefaultIsB;
4360
}
4461

62+
/// <summary>
63+
/// Gets or sets the callback that is invoked when the trigger event occurs.
64+
/// </summary>
4565
public Action<SettingsTwoButtons> OnTriggered { get; set; }
4666

67+
/// <summary>
68+
/// Gets the base entry.
69+
/// </summary>
4770
public new SSTwoButtonsSetting Base { get; }
4871

72+
/// <summary>
73+
/// Gets the last trigger time (in seconds) of button A (using <see cref="Time.realtimeSinceStartup"/>).
74+
/// </summary>
4975
public float? LastATriggerTime { get; private set; }
76+
77+
/// <summary>
78+
/// Gets the last trigger time (in seconds) of button B (using <see cref="Time.realtimeSinceStartup"/>).
79+
/// </summary>
5080
public float? LastBTriggerTime { get; private set; }
5181

82+
/// <summary>
83+
/// Gets the last trigger time of button A (using <see cref="Time.realtimeSinceStartup"/>).
84+
/// </summary>
5285
public TimeSpan? TimeSinceLastATrigger
5386
{
5487
get
@@ -59,7 +92,10 @@ public TimeSpan? TimeSinceLastATrigger
5992
return TimeSpan.FromMilliseconds(Time.realtimeSinceStartup - LastATriggerTime.Value);
6093
}
6194
}
62-
95+
96+
/// <summary>
97+
/// Gets the last trigger time of button B (using <see cref="Time.realtimeSinceStartup"/>).
98+
/// </summary>
6399
public TimeSpan? TimeSinceLastBTrigger
64100
{
65101
get
@@ -71,37 +107,68 @@ public TimeSpan? TimeSinceLastBTrigger
71107
}
72108
}
73109

110+
/// <summary>
111+
/// Gets or sets the label of the A button.
112+
/// </summary>
74113
public string ButtonAText
75114
{
76115
get => Base.OptionA;
77116
set => Base.OptionA = value;
78117
}
79118

119+
/// <summary>
120+
/// Gets or sets the label of the B button.
121+
/// </summary>
80122
public string ButtonBText
81123
{
82124
get => Base.OptionB;
83125
set => Base.OptionB = value;
84126
}
85127

128+
/// <summary>
129+
/// Whether or not the B button shouldd be selected as default.
130+
/// </summary>
86131
public bool IsDefaultButtonB
87132
{
88133
get => Base.DefaultIsB;
89134
set => Base.DefaultIsB = value;
90135
}
91136

137+
/// <summary>
138+
/// Whether or not the A button is selected.
139+
/// </summary>
92140
public bool IsAButtonActive => Base.SyncIsA;
141+
142+
/// <summary>
143+
/// Whether or not the B button is selected.
144+
/// </summary>
93145
public bool IsBButtonActive => Base.SyncIsB;
94146

147+
/// <summary>
148+
/// Whether or not the A button was selected before the last update.
149+
/// </summary>
95150
public bool WasAButtonActive => !_previousIsSyncB;
151+
152+
/// <summary>
153+
/// Whether or not the B button was selected before the last update.
154+
/// </summary>
96155
public bool WasBButtonActive => _previousIsSyncB;
97156

157+
/// <summary>
158+
/// Returns one of two provided options based on whether a button is currently active.
159+
/// </summary>
160+
/// <typeparam name="T">The type of the options to select from.</typeparam>
161+
/// <param name="primaryOption">The option to return if button A is active.</param>
162+
/// <param name="secondaryOption">The option to return if button B is active.</param>
163+
/// <returns>The value of <paramref name="primaryOption"/> if a button is active; otherwise, the value of <paramref
164+
/// name="secondaryOption"/>.</returns>
98165
public T GetOption<T>(T primaryOption, T secondaryOption)
99166
=> IsAButtonActive ? primaryOption : secondaryOption;
100167

101168
/// <inheritdoc />
102-
internal override void InternalOnUpdated()
169+
internal override void Internal_Updated()
103170
{
104-
base.InternalOnUpdated();
171+
base.Internal_Updated();
105172

106173
if (_previousIsSyncB != IsBButtonActive)
107174
{
@@ -118,12 +185,35 @@ internal override void InternalOnUpdated()
118185
}
119186
}
120187

188+
/// <summary>
189+
/// An overridable method called when the selected button is changed.
190+
/// </summary>
191+
/// <param name="isB">Whether or not the selected button is the B button.</param>
121192
public virtual void HandleTrigger(bool isB) { }
122193

194+
/// <summary>
195+
/// Returns a string that represents the current state of the SettingsTwoButtons instance.
196+
/// </summary>
197+
/// <returns>A string containing the values of CustomId, AssignedId, Player.UserId (or "null" if Player is null), and
198+
/// IsAButtonActive.</returns>
123199
public override string ToString()
124200
=> $"SettingsTwoButtons (CustomId={CustomId}; AssignedId={AssignedId}; Ply={Player?.UserId ?? "null"}; IsA={IsAButtonActive})";
125201

126-
public static SettingsTwoButtons Create(string customId, string buttonLabel, string buttonAText, string buttonBText, bool isDefaultButtonB = true, string buttonsHint = null)
202+
/// <summary>
203+
/// Creates a new instance of the SettingsTwoButtons class with the specified button configuration.
204+
/// </summary>
205+
/// <param name="customId">A unique identifier for the button setting. Cannot be null, empty, or consist only of white-space
206+
/// characters.</param>
207+
/// <param name="buttonLabel">The label displayed above the two buttons. Cannot be null, empty, or consist only of white-space characters.</param>
208+
/// <param name="buttonAText">The text displayed on the first button. Cannot be null, empty, or consist only of white-space characters.</param>
209+
/// <param name="buttonBText">The text displayed on the second button. Cannot be null, empty, or consist only of white-space characters.</param>
210+
/// <param name="isDefaultButtonB">true to make the second button the default selection; otherwise, false.</param>
211+
/// <param name="buttonsHint">An optional hint or description displayed below the buttons. Can be null.</param>
212+
/// <returns>A new SettingsTwoButtons instance configured with the specified labels, button texts, and options.</returns>
213+
/// <exception cref="ArgumentNullException">Thrown if customId, buttonLabel, buttonAText, or buttonBText is null, empty, or consists only of white-space
214+
/// characters.</exception>
215+
public static SettingsTwoButtons Create(string customId, string buttonLabel, string buttonAText, string buttonBText,
216+
bool isDefaultButtonB = true, string? buttonsHint = null)
127217
{
128218
if (string.IsNullOrWhiteSpace(customId))
129219
throw new ArgumentNullException(nameof(customId));

0 commit comments

Comments
 (0)