Ejemplo n.º 1
0
        private void InitBindings()
        {
            _bindings = new Dictionary <string, KeyBinding>();
            var settings = Plugin.LoadBindingSettings();

            // build all of the commands/actions
            _commandsByName = new Dictionary <string, Command>();
            var commands = _actionController.BuildCommands();

            foreach (var command in commands)
            {
                var commandName = command.Name;

                _commandsByName[commandName] = command;
                // Consider exporting this action to the plugin so it shows up in GetActions elsewhere
                //_plugin.RegisterAction(new JSONStorableAction(commandName, () => command.Execute()));

                // fill in bindings from the saved settings
                if (settings != null)
                {
                    if (settings[commandName] != null)
                    {
                        try
                        {
                            var chord   = settings[commandName]["chord"].Value;
                            var enabled = settings[commandName]["enabled"].AsBool;

                            var binding = KeyBinding.Build(Plugin, commandName, new KeyChord(chord), command);
                            binding.Enabled        = enabled;
                            _bindings[commandName] = binding;
                        }
                        catch
                        {
                            SuperController.LogError($"Failed loading action {commandName} from save file");
                            _bindings[commandName] = null;
                        }
                    }
                    else
                    {
                        _bindings[commandName] = null;
                    }
                }
                // ... or use defaults if there isn't anything there yet
                else
                {
                    var defaultKeyChord = _actionController.GetDefaultKeyChordByActionName(commandName);
                    if (defaultKeyChord != null)
                    {
                        _bindings[commandName] = KeyBinding.Build(Plugin, commandName, defaultKeyChord, command);
                    }
                    else
                    {
                        _bindings[commandName] = null;
                    }
                }
            }
        }