Пример #1
0
    public void Handle(AppSettingsMessage message)
    {
        if (message.Action == SettingsAction.Saving)
        {
            message.Settings["Shortcuts"] = JObject.FromObject(this);

            if (!message.Settings.TryGetObject(out var settings, "Shortcuts"))
            {
                return;
            }

            settings[nameof(Bindings)] = JArray.FromObject(Bindings.Select(x => BindingSettingsModel.FromBinding(x)));
        }
        else if (message.Action == SettingsAction.Loading)
        {
            if (!message.Settings.TryGetObject(out var settings, "Shortcuts"))
            {
                return;
            }

            if (settings.TryGetValue <bool>(nameof(IsKeyboardKeysGestureEnabled), out var isKeyboardKeysGestureEnabled))
            {
                IsKeyboardKeysGestureEnabled = isKeyboardKeysGestureEnabled;
            }
            if (settings.TryGetValue <bool>(nameof(IsMouseAxisGestureEnabled), out var isMouseAxisGestureEnabled))
            {
                IsMouseAxisGestureEnabled = isMouseAxisGestureEnabled;
            }
            if (settings.TryGetValue <bool>(nameof(IsMouseButtonGestureEnabled), out var isMouseButtonGestureEnabled))
            {
                IsMouseButtonGestureEnabled = isMouseButtonGestureEnabled;
            }
            if (settings.TryGetValue <bool>(nameof(IsGamepadAxisGestureEnabled), out var isHidAxisGestureEnabled))
            {
                IsGamepadAxisGestureEnabled = isHidAxisGestureEnabled;
            }
            if (settings.TryGetValue <bool>(nameof(IsGamepadButtonGestureEnabled), out var isHidButtonGestureEnabled))
            {
                IsGamepadButtonGestureEnabled = isHidButtonGestureEnabled;
            }

            if (settings.TryGetValue <List <BindingSettingsModel> >(nameof(Bindings), out var loadedBindings))
            {
                foreach (var gestureDescriptor in Bindings.Keys.ToList())
                {
                    _manager.UnregisterGesture(gestureDescriptor);
                }

                foreach (var binding in loadedBindings)
                {
                    var gestureDescriptor = binding.Gesture;
                    _manager.RegisterGesture(gestureDescriptor);

                    if (binding.Actions == null)
                    {
                        continue;
                    }

                    foreach (var action in binding.Actions)
                    {
                        var actionDescriptor = AvailableActions.FirstOrDefault(d => string.Equals(d.Name, action.Descriptor, StringComparison.OrdinalIgnoreCase));
                        if (actionDescriptor == null)
                        {
                            Logger.Warn($"Action \"{action.Descriptor}\" not found!");
                        }
                        else
                        {
                            _manager.BindActionWithSettings(gestureDescriptor, actionDescriptor, action.Values);
                        }
                    }
                }
            }
        }
    }