コード例 #1
0
ファイル: KeyGroup.cs プロジェクト: spaar/key-manager-mod
 public void RemoveKeybinding(KeyCode key, Keybinding keybinding)
 {
     if (Keybindings.ContainsKey(key))
     {
         Keybindings[key].Remove(keybinding);
     }
 }
コード例 #2
0
ファイル: KeyGroup.cs プロジェクト: spaar/key-manager-mod
        /*private bool NoBindingsAssigned()
         * {
         * return Keybindings.Values.Aggregate(0, (count, list) => count + list.Count) == 0;
         * }
         *
         * private bool NoKeysSpecified()
         * {
         * return Keybindings.Keys.All(k => k.KeyCode == KeyCode.None);
         * }*/

        public void AddKeybinding(KeyCode key, Keybinding binding)
        {
            if (Keybindings.ContainsKey(key))
            {
                Keybindings[key].Add(binding);
            }
            else
            {
                Keybindings.Add(key, new List <Keybinding> {
                    binding
                });
            }
        }
コード例 #3
0
ファイル: KeyGroup.cs プロジェクト: spaar/key-manager-mod
 public void AddKeybinding(Keybinding binding)
 {
     AddKeybinding(binding.Block.Keys[binding.MKeyIndex].KeyCode[binding.KeyCodeIndex], binding);
 }
コード例 #4
0
        public KeyManager()
        {
            groups = new List <KeyGroup>();

            XmlSaver.OnSave += info =>
            {
                var machineData = info.MachineData;

                const string keyManagerTag = "keymanager-v2";
                machineData.Write($"{keyManagerTag}-groups-count", groups.Count);
                for (int i = 0; i < groups.Count; i++)
                {
                    var groupTag = $"{keyManagerTag}-groups-{i}";
                    var bindings = groups[i].Keybindings.ToList();

                    machineData.Write($"{groupTag}-name", groups[i].Name);

                    machineData.Write($"{groupTag}-keys-count", bindings.Count);
                    for (int j = 0; j < bindings.Count; j++)
                    {
                        var keyTag = $"{groupTag}-keys-{j}";
                        machineData.Write($"{keyTag}-key", bindings[j].Key.ToString());

                        machineData.Write($"{keyTag}-bindings-count", bindings[j].Value.Count);
                        for (int k = 0; k < bindings[j].Value.Count; k++)
                        {
                            var bindingTag = $"{keyTag}-bindings-{k}";
                            machineData.Write($"{bindingTag}-guid", bindings[j].Value[k].Guid.ToString());
                            machineData.Write($"{bindingTag}-keyIndex", bindings[j].Value[k].MKeyIndex);
                            machineData.Write($"{bindingTag}-keyCodeIndex", bindings[j].Value[k].KeyCodeIndex);
                        }
                    }
                }
            };

            XmlLoader.OnLoad += info =>
            {
                groups.Clear();
                var machineData = info.MachineData;

                const string keyManagerTag = "keymanager-v2";

                if (!machineData.HasKey($"{keyManagerTag}-groups-count"))
                {
                    return;
                }

                var count = machineData.ReadInt($"{keyManagerTag}-groups-count");
                groups.Capacity = count;

                for (int i = 0; i < count; i++)
                {
                    var groupTag = $"{keyManagerTag}-groups-{i}";
                    var name     = machineData.ReadString($"{groupTag}-name");

                    var group = new KeyGroup(name);

                    var keyCount = machineData.ReadInt($"{groupTag}-keys-count");
                    for (int j = 0; j < keyCount; j++)
                    {
                        var keyTag = $"{groupTag}-keys-{j}";
                        var key    = Util.ParseEnum <KeyCode>(machineData.ReadString($"{keyTag}-key"));

                        var bindingsCount = machineData.ReadInt($"{keyTag}-bindings-count");
                        for (int k = 0; k < bindingsCount; k++)
                        {
                            var bindingTag = $"{keyTag}-bindings-{k}";

                            var guid         = new Guid(machineData.ReadString($"{bindingTag}-guid"));
                            var keyIndex     = machineData.ReadInt($"{bindingTag}-keyIndex");
                            var keyCodeIndex = machineData.ReadInt($"{bindingTag}-keyCodeIndex");

                            var binding = new Keybinding(guid, keyIndex, keyCodeIndex);
                            group.AddKeybinding(key, binding);
                        }
                    }

                    groups.Add(group);
                }

                // Show interface when loading a machine with key manager data.
                KeyManagerInterface.Instance.SetActive();
            };

            // When a block is deleted, delete any corresponding keybindings
            Game.OnBlockRemoved += () =>
            {
                foreach (var group in groups)
                {
                    group.RemoveAllBindingsWithoutBlock();
                }
                // If any groups were completely emtpied by the above checks, delete them entirely
                groups.RemoveAll(g => g.HasNoBindings());
            };
        }