public KeyBindListGui(KeyBindListInfo keyBindListInfo) : base(keyBindListInfo.Name) { foreach (var keyValuePair in keyBindListInfo.KeyBindList) { AddElement(new Button(keyValuePair.Key, ModEntry.GetInstance().Helper.Translation.Get("KeyBindUI.Framework.Screen.ModsGui.KeySetting")) { OnLeftClicked = () => { OpenScreenGui( new ModKeyBindListGui(keyBindListInfo, new Tuple <string, string>(keyValuePair.Key, keyValuePair.Value))); } }); } }
private static void SaveConfig(KeyBindListInfo keyBindListInfo, Tuple <string, string> keyBind, IEnumerable <string> keyBinds) { keyBindListInfo.KeyBindList[keyBind.Item1] = string.Join(", ", keyBinds); var read = new JsonTextReader(keyBindListInfo.ConfigFileInfo.OpenText()); var readFrom = (JObject)JToken.ReadFrom(read); read.Close(); foreach (var keyValuePair in keyBindListInfo.KeyBindList) { readFrom[keyValuePair.Key] = keyValuePair.Value; } System.IO.File.WriteAllText(keyBindListInfo.ConfigFileInfo.FullName, readFrom.ToString(), Encoding.UTF8); }
public KeyBindGui() : base("Key Bind UI") { var mods = Environment.CurrentDirectory + "/" + "Mods"; foreach (var info in new DirectoryInfo(mods).GetDirectories()) { var keyBindListInfo = new KeyBindListInfo(); var config = new FileInfo(info.FullName + "/config.json"); keyBindListInfo.ConfigFileInfo = config; if (!config.Exists) { continue; } keyBindListInfo.Name = info.Name; var read = new JsonTextReader(config.OpenText()); var readFrom = (JObject)JToken.ReadFrom(read); read.Close(); foreach (var keyValuePair in readFrom) { if (keyValuePair.Value == null || keyValuePair.Value.Type != JTokenType.String) { continue; } try { keyBindListInfo.KeyBindList[keyValuePair.Key] = KeybindList.Parse(keyValuePair.Value.ToString()).ToString(); } catch (Exception e) { // ignored } } AddElement(new Button(info.Name, ModEntry.GetInstance().Helper.Translation.Get("KeyBindUI.Framework.Screen.ModsGui.KeySetting")) { OnLeftClicked = () => { OpenScreenGui(new KeyBindListGui(keyBindListInfo)); } }); } }
public ModKeyBindListGui(KeyBindListInfo keyBindListInfo, Tuple <string, string> keyBind) { var keyBinds = Regex.Split(keyBind.Item2, ", ").ToList(); keyBinds.Sort((s1, s2) => FontUtils.GetWidth(s2) - FontUtils.GetWidth(s1)); var w = FontUtils.GetWidth(keyBinds[0]) + 200; _slot = new Slot <KeyBindSlot>("", "", Game1.viewport.Width / 2 - w / 2, 50, w, (Game1.viewport.Height - 400) / 80 * 80, 80); foreach (var key in keyBinds) { _slot.AddEntry(new KeyBindSlot(key)); } _add = new Button( ModEntry.GetInstance().Helper.Translation.Get("KeyBindUI.Framework.Screen.ModKeyBindListGui.add"), "", _slot.X, _slot.Height + 100, _slot.Width, 80) { OnLeftClicked = () => { _record = true; } }; _remove = new Button( ModEntry.GetInstance().Helper.Translation.Get("KeyBindUI.Framework.Screen.ModKeyBindListGui.remove"), "", _slot.X, _slot.Height + 200, _slot.Width, 80) { OnLeftClicked = () => { keyBinds.Remove(_slot.SelectedEntry.KeyBind); _slot.RemoveEntry(_slot.SelectedEntry); _slot.SelectedEntry = null; SaveConfig(keyBindListInfo, keyBind, keyBinds); } }; AddComponent(_slot); AddComponentRange(_add, _remove); _done = new Button( ModEntry.GetInstance().Helper.Translation.Get("KeyBindUI.Framework.Screen.ModKeyBindListGui.done"), "", X, Y + 100, 200, 80) { OnLeftClicked = () => { var k = string.Join(" + ", KeyList); keyBinds.Add(k); _slot.AddEntry(new KeyBindSlot(k)); SaveConfig(keyBindListInfo, keyBind, keyBinds); _record = false; KeyList.Clear(); Game1.playSound("coin"); } }; _cancel = new Button( ModEntry.GetInstance().Helper.Translation.Get("KeyBindUI.Framework.Screen.ModKeyBindListGui.cancel"), "", X, Y + 200, 200, 80) { OnLeftClicked = () => { _record = false; KeyList.Clear(); Game1.playSound("bigDeSelect"); } }; AddComponent(_done); AddComponent(_cancel); }