Exemple #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);
        }
Exemple #2
0
        private static FrameworkElement GetInputMappingControl(IWrappedButton button, ButtonInfoAttribute buttonInfo)
        {
            var control = new ButtonMappingControl();

            control.Setup(button, buttonInfo);
            return(control);
        }
Exemple #3
0
 private void ButtonKeyMappingUpdatedHandler(IWrappedButton obj)
 {
     if (this.Button.Equals(obj))
     {
         this.UpdateMapping();
     }
 }
Exemple #4
0
        public static InputKey GetKeyForAbstractButton(IWrappedButton button)
        {
            var mapping = GetMappingForAbstractButton(button);

            return(mapping.PrimaryKey != InputKey.None
                       ? mapping.PrimaryKey
                       : mapping.SecondaryKey);
        }
Exemple #5
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);
        }
Exemple #6
0
        public ViewModelButtonMappingControl(IWrappedButton button, ButtonInfoAttribute buttonInfo)
        {
            // ReSharper disable once CanExtractXamlLocalizableStringCSharp
            this.CommandBindKey = new ActionCommandWithParameter(
                param => this.ExecuteCommandBindKey("Secondary".Equals(param)));
            this.Button     = button;
            this.ButtonInfo = buttonInfo;
            this.UpdateMapping();

            ClientInputManager.ButtonKeyMappingUpdated += this.ButtonKeyMappingUpdatedHandler;
        }
Exemple #7
0
        public static void ConsumeButton(IWrappedButton button)
        {
            if (!MappingButtonToKeys.TryGetValue(button, out var mapping))
            {
                return;

                //throw new Exception("The button is not mapped, it cannot be consumed");
            }

            RawInput.ConsumeKey(mapping.PrimaryKey);
            RawInput.ConsumeKey(mapping.SecondaryKey);
        }
Exemple #8
0
        public ViewModelRebindKeyWindow(
            ViewModelButtonMappingControl viewModelMapping,
            bool isSecondaryKey,
            Action onClose)
        {
            this.viewModelMapping = viewModelMapping;
            this.buttonToRebind   = viewModelMapping.Button;

            this.onClose        = onClose;
            this.isSecondaryKey = isSecondaryKey;

            // ReSharper disable once CanExtractXamlLocalizableStringCSharp
            this.clientInputContext = ClientInputContext.Start("Key binding")
                                      .HandleAll(this.InputCallback);

            Instance = this;
        }
Exemple #9
0
        private static bool Check(
            IWrappedButton wrappedButton,
            Func <InputKey, bool, bool> checkFunc,
            bool evenIfHandled)
        {
            if (!isFrozen)
            {
                LoadButtonsMapping();
            }

            if (!MappingButtonToKeys.TryGetValue(
                    wrappedButton,
                    out var mapping))
            {
                // the key is not mapped
                var buttonType = wrappedButton.WrappedButtonType;
                ValidateButtonType(buttonType);

                if (!RegisteredButtonEnums.Contains(buttonType))
                {
                    throw new Exception(
                              $"The button enum type is not registered: {buttonType.FullName}. Please register it in your mod initialization code by calling {nameof(ClientInputManager)}.{nameof(RegisterButtonsEnum)}<{buttonType.Name}>()");
                }

                return(false);
            }

            if (mapping.PrimaryKey != InputKey.None &&
                checkFunc(mapping.PrimaryKey, evenIfHandled))
            {
                // check successful
                return(true);
            }

            if (mapping.SecondaryKey != InputKey.None &&
                checkFunc(mapping.SecondaryKey, evenIfHandled))
            {
                // check successful
                return(true);
            }

            return(false);
        }
Exemple #10
0
 public void Setup(IWrappedButton button, ButtonInfoAttribute buttonInfo)
 {
     this.button     = button;
     this.buttonInfo = buttonInfo;
 }
 public ButtonCallback(IWrappedButton wrappedButton, Action callback, bool evenIfHandled)
 {
     this.WrappedButton = wrappedButton;
     this.Callback      = callback;
     this.EvenIfHandled = evenIfHandled;
 }
Exemple #12
0
 public static ButtonInfoAttribute GetButtonInfo(IWrappedButton button)
 {
     return(RegisteredButtons.Find(button));
 }
Exemple #13
0
 public static ButtonMapping GetMappingForAbstractButton(IWrappedButton button)
 {
     return(MappingButtonToKeys.Find(button));
 }
Exemple #14
0
 private static string GetButtonId(IWrappedButton wrappedButton)
 {
     return(wrappedButton.WrappedButtonType.FullName + "." + wrappedButton.WrappedButtonName);
 }
Exemple #15
0
 internal static bool IsButtonUp(IWrappedButton wrappedButton, bool evenIfHandled = false)
 {
     return(Check(wrappedButton, DelegateRawInputIsKeyUp, evenIfHandled));
 }
Exemple #16
0
 private static void ApplyDefaultMapping(IWrappedButton button, ButtonInfoAttribute info)
 {
     MappingButtonToKeys.Add(button, info.DefaultButtonMapping);
 }
Exemple #17
0
 private static bool IsSameCategory(
     IWrappedButton foundButton,
     string buttonCategory)
 {
     return(RegisteredButtons[foundButton].Category == buttonCategory);
 }