forked from HasteModding/HasteEffects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHastySetting.cs
More file actions
135 lines (104 loc) · 5.09 KB
/
HastySetting.cs
File metadata and controls
135 lines (104 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using Zorro.Settings;
// Modified from https://github.com/NaokoAF/HastyControls, just making it fit my style better ig
namespace HasteEffects;
public class HastyFloat : FloatSetting, IExposedSetting
{
private HastySetting config;
private float defaultValue;
private UnityEngine.Localization.LocalizedString displayName;
private Unity.Mathematics.float2 minMax;
/// <summary>
/// Creates a new float setting in a <see cref="HastySetting"/> menu.
/// </summary>
/// <param name="cfg">The config this setting belongs to.</param>
/// <param name="name">Name of the setting (used as a key).</param>
/// <param name="description">Displayed description in the UI.</param>
/// <param name="min">Minimum allowed value.</param>
/// <param name="max">Maximum allowed value.</param>
/// <param name="defaultValue">Default value when nothing is set.</param>
public HastyFloat(HastySetting cfg, string name, string description, float min, float max, float defaultValue)
{
config = cfg;
this.defaultValue = defaultValue;
minMax = new Unity.Mathematics.float2(min, max);
displayName = cfg.CreateDisplayName(name, description);
cfg.Add(this);
}
public event Action<float>? Applied;
public override void ApplyValue() => Applied?.Invoke(Value);
public string GetCategory() => config.ModName;
public UnityEngine.Localization.LocalizedString GetDisplayName() => displayName;
protected override float GetDefaultValue() => defaultValue;
protected override Unity.Mathematics.float2 GetMinMaxValue() => minMax;
}
public class HastySetting
{
// Access references to the settings list and the save/load system.
private static HarmonyLib.AccessTools.FieldRef<HasteSettingsHandler, List<Setting>> settingsRef = HarmonyLib.AccessTools.FieldRefAccess<HasteSettingsHandler, List<Setting>>("settings");
private static HarmonyLib.AccessTools.FieldRef<HasteSettingsHandler, ISettingsSaveLoad> settingsSaveLoadRef = HarmonyLib.AccessTools.FieldRefAccess<HasteSettingsHandler, ISettingsSaveLoad>("_settingsSaveLoad");
/// <summary>
/// Sets up a new setting handler for a mod, linking the mod's name and GUID.
/// </summary>
/// <param name="modName">The name of the mod.</param>
/// <param name="modGUID">The unique GUID of the mod.</param>
public HastySetting(string modName, string modGUID)
{
ModName = modName;
SettingsUIPage.LocalizedTitles.Add(ModName, new(Main.GUID, ModName));
}
public string ModName { get; private set; }
/// <summary>
/// Adds a new setting to the mod's settings list and applies its initial value.
/// </summary>
/// <typeparam name="T">The type of setting being added (e.g., HastyFloat, HastyBool).</typeparam>
/// <param name="setting">The setting to add.</param>
public void Add<T>(T setting) where T : Setting
{
var handler = GameHandler.Instance.SettingsHandler;
settingsRef(handler).Add(setting);
setting.Load(settingsSaveLoadRef(handler));
setting.ApplyValue();
}
/// <summary>
/// Creates a localized display name for a setting with an optional description.
/// </summary>
/// <param name="name">The name of the setting.</param>
/// <param name="description">A brief description for the setting.</param>
/// <returns>A <see cref="LocalizedString"/> for the setting's name and description.</returns>
internal UnityEngine.Localization.LocalizedString CreateDisplayName(string name, string description = "") => new(Main.GUID, $"{name}\n<size=60%><alpha=#50>{description}");
}
public class HastyBool : BoolSetting, IExposedSetting, IEnumSetting
{
private List<string> choices;
private HastySetting config;
private bool defaultValue;
private UnityEngine.Localization.LocalizedString displayName;
/// <summary>
/// Creates a new <see cref="bool"/> setting with custom choices for "On" and "Off" values.
/// </summary>
/// <param name="cfg">The config object the setting belongs to.</param>
/// <param name="name">Name of the setting (used as a key).</param>
/// <param name="description">Displayed description in the UI.</param>
/// <param name="defaultValue">Default boolean value (true or false).</param>
/// <param name="offChoice">Text for the "Off" option (defaults to "Off").</param>
/// <param name="onChoice">Text for the "On" option (defaults to "On").</param>
public HastyBool(HastySetting cfg, string name, string description, bool defaultValue, string offChoice = "Off", string onChoice = "On")
{
config = cfg;
this.defaultValue = defaultValue;
displayName = cfg.CreateDisplayName(name, description);
choices = new List<string> { offChoice, onChoice };
cfg.Add(this);
}
public event Action<bool>? Applied;
public override UnityEngine.Localization.LocalizedString OffString => null!;
public override UnityEngine.Localization.LocalizedString OnString => null!;
public override void ApplyValue() => Applied?.Invoke(Value);
public string GetCategory() => config.ModName;
public UnityEngine.Localization.LocalizedString GetDisplayName() => displayName;
/// <summary>
/// Gets the unlocalized choices for this setting ("On" and "Off").
/// </summary>
List<string> IEnumSetting.GetUnlocalizedChoices() => choices;
protected override bool GetDefaultValue() => defaultValue;
}