|
| 1 | +using ModLoader.Mod; |
| 2 | +using SFS.Input; |
| 3 | +using SFS.IO; |
| 4 | +using SFS.Parsers.Json; |
| 5 | +using SFS.UI; |
| 6 | +using System; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Text; |
| 9 | +using TMPro; |
| 10 | +using UnityEngine; |
| 11 | + |
| 12 | +namespace ModLoader.IO |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// This class handles the Keybinding buttons in the settings menu. You can also load your configuration and store it if it changes. |
| 16 | + /// </summary> |
| 17 | + public class ModSettings : MonoBehaviour |
| 18 | + { |
| 19 | + /// <summary> |
| 20 | + /// Main instance of ModSettings. Use this to load settings or add the Keybidings buttons. |
| 21 | + /// </summary> |
| 22 | + public static ModSettings main; |
| 23 | + |
| 24 | + private Transform _keybindingsHolder; |
| 25 | + private GameObject _textPrefab; |
| 26 | + private GameObject _spacePrefab; |
| 27 | + private GameObject _keybindingPrefab; |
| 28 | + private Dictionary<string, List<KeyBinder>> _elements = new Dictionary<string, List<KeyBinder>>(); |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// This prefab is used to add text in the settings menu |
| 32 | + /// </summary> |
| 33 | + public GameObject TextPrefab |
| 34 | + { |
| 35 | + set { this._textPrefab = value; } |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// This prefab is used to add space in the settings menu |
| 40 | + /// </summary> |
| 41 | + public GameObject SpacePrefab |
| 42 | + { |
| 43 | + set { this._spacePrefab = value; } |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// This prefab is used to add keybinding buttons in the settings menu |
| 48 | + /// </summary> |
| 49 | + public GameObject KeybindingPrefab |
| 50 | + { |
| 51 | + set { this._keybindingPrefab = value; } |
| 52 | + } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// This transforms keep all keybiding buttons. |
| 56 | + /// </summary> |
| 57 | + public Transform KeybindingsHolder |
| 58 | + { |
| 59 | + set { this._keybindingsHolder = value; } |
| 60 | + } |
| 61 | + |
| 62 | + private void Awake() |
| 63 | + { |
| 64 | + ModSettings.main = this; |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// This method adds simple hotkey buttons in the settings menu |
| 70 | + /// </summary> |
| 71 | + /// <param name="key"> Your current key value </param> |
| 72 | + /// <param name="defaultKey"> Your default key value </param> |
| 73 | + /// <param name="name"> Your key name.</param> |
| 74 | + /// <param name="modSettings"> Your mod settings. This used to store your settings</param> |
| 75 | + /// <example> |
| 76 | + /// ModSettings.main.addKeybinding( MyLoadedSettings.My_Key, MyDefaultSettings.My_Key, "My_Key", MyLoadedSettings); |
| 77 | + /// </example> |
| 78 | + /// <exception cref="NullReferenceException">If you call this method on early_load</exception> |
| 79 | + public void addKeybinding(KeybindingsPC.Key key, KeybindingsPC.Key defaultKey, string name, SFSSettings modSettings) |
| 80 | + { |
| 81 | + this.addKeybinding(new KeybindingsPC.Key[] |
| 82 | + { |
| 83 | + key |
| 84 | + }, new KeybindingsPC.Key[] |
| 85 | + { |
| 86 | + defaultKey |
| 87 | + }, name, modSettings); |
| 88 | + } |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// This method adds multiple hotkey buttons in the settings menu |
| 92 | + /// </summary> |
| 93 | + /// <param name="keys"> Your current key values </param> |
| 94 | + /// <param name="defaultKeys"> Your default key values </param> |
| 95 | + /// <param name="name"> Your key name.</param> |
| 96 | + /// <param name="modSettings"> Your mod settings. This used to store your settings</param> |
| 97 | + /// <example> |
| 98 | + /// ModSettings.main.addKeybinding( MyLoadedSettings.My_Key, MyDefaultSettings.My_Key, "My_Key", MyLoadedSettings); |
| 99 | + /// </example> |
| 100 | + /// <exception cref="NullReferenceException">If you call this method on early_load</exception> |
| 101 | + public void addKeybinding(KeybindingsPC.Key[] keys, KeybindingsPC.Key[] defaultKeys, string name, SFSSettings modSettings) |
| 102 | + { |
| 103 | + // Code taken from SFS.Input.KeybindingsPC |
| 104 | + GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(this._keybindingPrefab, this._keybindingsHolder); |
| 105 | + |
| 106 | + // change name format |
| 107 | + if (name.Contains("_")) |
| 108 | + { |
| 109 | + StringBuilder stringBuilder = new StringBuilder(name[0].ToString()); |
| 110 | + for (int i = 1; i < name.Length; i++) |
| 111 | + { |
| 112 | + stringBuilder.Append(name[i].ToString().ToLower()); |
| 113 | + } |
| 114 | + gameObject.GetComponentInChildren<TMP_Text>().text = stringBuilder.Replace("_", " ").ToString(); |
| 115 | + } |
| 116 | + else |
| 117 | + { |
| 118 | + gameObject.GetComponentInChildren<TMP_Text>().text = name; |
| 119 | + } |
| 120 | + |
| 121 | + List<KeyBinder> list = new List<KeyBinder> |
| 122 | + { |
| 123 | + gameObject.GetComponentInChildren<KeyBinder>() |
| 124 | + }; |
| 125 | + int num = 0; |
| 126 | + |
| 127 | + while (list.Count < keys.Length && num++ < 20) |
| 128 | + { |
| 129 | + list.Add(UnityEngine.Object.Instantiate<KeyBinder>(list[0], gameObject.transform)); |
| 130 | + } |
| 131 | + |
| 132 | + for (int j = 0; j < keys.Length; j++) |
| 133 | + { |
| 134 | + KeybindingsPC.Key key = keys[j]; |
| 135 | + KeybindingsPC.Key defaultKey = defaultKeys[j]; |
| 136 | + |
| 137 | + // this action saves the key value when it changes |
| 138 | + Action save = () => |
| 139 | + { |
| 140 | + this.save(modSettings); |
| 141 | + }; |
| 142 | + |
| 143 | + list[j].Initialize(Menu.settings.menuHolder, key, defaultKey, save ); |
| 144 | + } |
| 145 | + |
| 146 | + List<KeyBinder> modList; |
| 147 | + if(this._elements.TryGetValue(modSettings.getModId(), out modList)){ |
| 148 | + modList.AddRange(list); |
| 149 | + } |
| 150 | + else |
| 151 | + { |
| 152 | + this._elements.Add(modSettings.getModId(), list); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + /// <summary> |
| 157 | + /// This method add text in the settings menu |
| 158 | + /// </summary> |
| 159 | + /// <param name="text">Text you want to add</param> |
| 160 | + public void addText(string text) |
| 161 | + { |
| 162 | + UnityEngine.Object.Instantiate<GameObject>(this._textPrefab, this._keybindingsHolder).GetComponentInChildren<TMP_Text>().text = text; |
| 163 | + } |
| 164 | + |
| 165 | + /// <summary> |
| 166 | + /// This function resets all mod keybindings |
| 167 | + /// </summary> |
| 168 | + public void resetKeybindings() |
| 169 | + { |
| 170 | + foreach (List<KeyBinder> keyBinders in this._elements.Values) |
| 171 | + { |
| 172 | + foreach(KeyBinder keyBinder in keyBinders) |
| 173 | + { |
| 174 | + keyBinder.ResetToDefault(); |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + private void save(SFSSettings modSettings) |
| 180 | + { |
| 181 | + FolderPath folder = new FolderPath(modSettings.getModFolder()).Extend("Settings").CreateFolder(); |
| 182 | + FilePath settingsFile = folder.ExtendToFile("keybindings.json"); |
| 183 | + string text = JsonWrapper.ToJson(modSettings, true); |
| 184 | + settingsFile.WriteText(text); |
| 185 | + } |
| 186 | + |
| 187 | + /// <summary> |
| 188 | + /// This method load the current keybindings. |
| 189 | + /// </summary> |
| 190 | + /// <typeparam name="T">Your keybinding class. This class has all keybindings your mod needs</typeparam> |
| 191 | + /// <param name="mod">Your mod instance. This is used to get your modid and modfolder</param> |
| 192 | + /// <returns> Return your currents keybinding</returns> |
| 193 | + /// <example> |
| 194 | + /// MySettings currentSettings = ModSettings.main.loadSettings<MySettings>(MyMod); |
| 195 | + /// </example> |
| 196 | + public T loadSettings<T>(SFSMod mod) |
| 197 | + { |
| 198 | + FolderPath folder = new FolderPath(mod.ModFolder).Extend("Settings").CreateFolder(); |
| 199 | + FilePath settingsFile = folder.ExtendToFile("keybindings.json"); |
| 200 | + this._elements.Add(mod.ModId, new List<KeyBinder>()); |
| 201 | + if (settingsFile.FileExists()) |
| 202 | + { |
| 203 | + return JsonWrapper.FromJson<T>(settingsFile.ReadText()); |
| 204 | + } |
| 205 | + |
| 206 | + return Activator.CreateInstance<T>(); |
| 207 | + } |
| 208 | + |
| 209 | + } |
| 210 | +} |
0 commit comments