Esempio n. 1
0
        private static void ReleaseAbstractButtonMapping(
            IWrappedButton buttonToReset,
            InputKey keyToRemove)
        {
            if (!RegisteredButtons.TryGetValue(buttonToReset, out _))
            {
                throw new Exception("Unknown button: " + buttonToReset);
            }

            var mapping = MappingButtonToKeys[buttonToReset];

            if (mapping.PrimaryKey == keyToRemove)
            {
                mapping = new ButtonMapping(InputKey.None, mapping.SecondaryKey);
            }

            if (mapping.SecondaryKey == keyToRemove)
            {
                mapping = new ButtonMapping(mapping.PrimaryKey, InputKey.None);
            }

            Api.Logger.Important(
                $"Releasing button mapping: {buttonToReset}; key to remove: {keyToRemove}; mapping: {mapping}");
            MappingButtonToKeys[buttonToReset] = mapping;

            ButtonKeyMappingUpdated?.Invoke(buttonToReset);
        }
Esempio n. 2
0
        public static void SetAbstractButtonMapping(
            IWrappedButton buttonToRebind,
            ButtonMapping mapping,
            bool writeToLog = true)
        {
            if (!RegisteredButtons.TryGetValue(buttonToRebind, out var buttonToRebindInfo))
            {
                throw new Exception("Unknown button: " + buttonToRebind);
            }

            var buttonCategory = buttonToRebindInfo.Category;

            foreach (var pair in MappingButtonToKeys.ToList())
            {
                var existingButton = pair.Key;
                if (existingButton.Equals(buttonToRebind))
                {
                    continue;
                }

                var existingMapping = pair.Value;

                if (existingMapping.PrimaryKey != InputKey.None &&
                    (existingMapping.PrimaryKey == mapping.PrimaryKey ||
                     existingMapping.PrimaryKey == mapping.SecondaryKey))
                {
                    if (IsSameCategory(existingButton, buttonCategory))
                    {
                        // the key is already used - release this mapping
                        ReleaseAbstractButtonMapping(existingButton, existingMapping.PrimaryKey);
                    }
                }

                if (existingMapping.SecondaryKey != InputKey.None &&
                    (existingMapping.SecondaryKey == mapping.PrimaryKey ||
                     existingMapping.SecondaryKey == mapping.SecondaryKey))
                {
                    if (IsSameCategory(existingButton, buttonCategory))
                    {
                        // the key is already used - release this mapping
                        ReleaseAbstractButtonMapping(existingButton, existingMapping.SecondaryKey);
                    }
                }
            }

            if (writeToLog)
            {
                Api.Logger.Important($"Setting button mapping: {buttonToRebind}; mapping: {mapping}");
            }

            MappingButtonToKeys[buttonToRebind] = mapping;

            ButtonKeyMappingUpdated?.Invoke(buttonToRebind);
        }
Esempio n. 3
0
        public static void ResetMappingToDefault()
        {
            Api.Logger.Important("Input manager: reset mapping to default");

            MappingButtonToKeys.Clear();

            foreach (var pair in RegisteredButtons)
            {
                ApplyDefaultMapping(pair.Key, pair.Value);
                ButtonKeyMappingUpdated?.Invoke(pair.Key);
            }

            Freeze();
        }