-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
014d997
commit a299fe4
Showing
6 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
ContentLibrary/Source/ConfigurableWarning/API/Options/KeyCodeOption.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
#pragma warning disable CS0612 // The obsolete message is only for other mods, we still need to use these methods. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using ConfigurableWarning.API.State; | ||
using ContentSettings.API; | ||
using Unity.Mathematics; | ||
using UnityEngine; | ||
using Zorro.Settings; | ||
|
||
namespace ConfigurableWarning.API.Options; | ||
|
||
/// <summary> | ||
/// A float option. This *must* be inherited from to use. | ||
/// Its state is stored in the <see cref="OptionsState" /> class. | ||
/// </summary> | ||
public class KeyCodeOption : KeyCodeSetting, IOption<KeyCode> { | ||
private readonly List<Action<KeyCodeOption>> _applyActions; | ||
private readonly KeyCode _defaultValue; | ||
private readonly string _displayName; | ||
private readonly string _name; | ||
|
||
/// <summary> | ||
/// Initialize a <see cref="IOption{T}" /> with the <see cref="KeyCode" /> type. | ||
/// </summary> | ||
/// <param name="name">The option's name.</param> | ||
/// <param name="defaultValue">The default value.</param> | ||
/// <param name="displayName">The option's displayed name.</param> | ||
/// <param name="min">The minimum value.</param> | ||
/// <param name="max">The maximum value.</param> | ||
/// <param name="doClamp">Whether to clamp the value when changed.</param> | ||
protected KeyCodeOption(string name, KeyCode defaultValue, string displayName) : this(name, defaultValue, displayName, []) { | ||
} | ||
|
||
/// <summary> | ||
/// Initialize a <see cref="IOption{T}" /> with the <see cref="KeyCode" /> type. | ||
/// </summary> | ||
/// <param name="name">The option's name.</param> | ||
/// <param name="defaultValue">The default value.</param> | ||
/// <param name="displayName">The option's displayed name.</param> | ||
/// <param name="actions">Functions to run when the value is applied.</param> | ||
protected KeyCodeOption(string name, KeyCode defaultValue, string displayName, Action<KeyCodeOption>[] actions) { | ||
_name = name; | ||
_displayName = displayName; | ||
_defaultValue = defaultValue; | ||
_applyActions = [.. actions]; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public KeyCode State { | ||
get => AsOption().State; | ||
set => AsOption().State = value; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void RegisterSetting(string tab, string category) { | ||
SettingsLoader.RegisterSetting(tab, category, this); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void SetValue(KeyCode value) { | ||
Value = (int) value; | ||
GameHandler.Instance.SettingsHandler.SaveSetting(this); | ||
} | ||
|
||
/// <summary> | ||
/// Gets this option's name. This is its name in the registry | ||
/// and in the state holder. | ||
/// </summary> | ||
/// <returns>The option's name.</returns> | ||
public string GetName() { | ||
return _name; | ||
} | ||
|
||
/// <summary> | ||
/// Get this option's default value. | ||
/// </summary> | ||
/// <returns>The option's default value.</returns> | ||
public new KeyCode GetDefaultValue() { | ||
return _defaultValue; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public KeyCode GetValue() { | ||
return (KeyCode) Value; | ||
} | ||
|
||
/// <summary> | ||
/// Get the display name of this option. | ||
/// </summary> | ||
/// <returns>The option's display name.</returns> | ||
public string GetDisplayName() { | ||
return _displayName; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public IUntypedOption AsUntyped() { | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Get this option's default value. | ||
/// </summary> | ||
/// <returns>The option's default value.</returns> | ||
public override KeyCode GetDefaultKey() { | ||
return _defaultValue; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public IOption<KeyCode> AsOption() { | ||
return this; | ||
} | ||
|
||
object IUntypedOption.GetValue() { | ||
return GetValue(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void SetValue(object value) { | ||
SetValue((KeyCode) value); | ||
} | ||
|
||
/// <summary> | ||
/// Get an instance of an option. | ||
/// </summary> | ||
/// <param name="name">The option's name.</param> | ||
/// <returns>The option.</returns> | ||
public static KeyCodeOption? Instance(string name) { | ||
return (KeyCodeOption?) IOption<KeyCode>.Instance(name); | ||
} | ||
|
||
/// <summary> | ||
/// Applies the value. This is run when the user changes the value. | ||
/// This will sync it, update the state, and run any apply actions. | ||
/// </summary> | ||
public override void ApplyValue() { | ||
OptionsState.Instance.Update(this); | ||
ConfigurableWarningAPI.Sync.SyncSettings(); | ||
|
||
foreach (var action in _applyActions) action(this); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public StateHolder<KeyCode> StateHolder => States.Keys; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
ContentLibrary/Source/ConfigurableWarning/Settings/Compat/HellDivers.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System.Linq; | ||
using ConfigurableWarning.API.Attributes; | ||
using ConfigurableWarning.API.Compat; | ||
using ConfigurableWarning.API.Options; | ||
using ConfigurableWarning.API.State; | ||
using UnityEngine; | ||
|
||
namespace ConfigurableWarning.Settings.Compat; | ||
|
||
/// <summary> | ||
/// Settings keys for HellDivers compat | ||
/// </summary> | ||
public static class HellDiversSettingKeys { | ||
#pragma warning disable CS1591 | ||
public const string DiveKey = "DiveKey"; | ||
#pragma warning restore CS1591 | ||
} | ||
|
||
/// <summary> | ||
/// HellDivers compat settings | ||
/// </summary> | ||
[CompatModule(["HellDivers"])] | ||
public class HellDiversCompat : ICompatModule { | ||
/// <inheritdoc /> | ||
public void Init() { | ||
States.Keys[HellDiversSettingKeys.DiveKey] = HellDivers.Main.instance.diveKeybind.Value; | ||
} | ||
|
||
internal static void ApplySettings(IUntypedOption _opt) { | ||
string[] all = [HellDiversSettingKeys.DiveKey]; | ||
|
||
if (!all.All(v => OptionsState.Instance.Has(v))) return; | ||
|
||
HellDivers.Main.instance.diveKeybind.Value = States.Keys[HellDiversSettingKeys.DiveKey]; | ||
} | ||
|
||
[CompatGroup("HELLDIVERS", "GENERAL")] | ||
private static class Settings { | ||
[Register] | ||
private class DiveKey() | ||
: KeyCodeOption(HellDiversSettingKeys.DiveKey, KeyCode.F, "Max Players", [ApplySettings]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters