Пример #1
0
                public KeyControl(KeyRebindControl parent, BoundKeyFunction function)
                {
                    Function = function;
                    var name = new Label
                    {
                        Text = Loc.GetString(
                            $"ui-options-function-{CaseConversion.PascalToKebab(function.FunctionName)}"),
                        HorizontalExpand    = true,
                        HorizontalAlignment = HAlignment.Left
                    };

                    BindButton1 = new BindButton(parent, this, StyleBase.ButtonOpenRight);
                    BindButton2 = new BindButton(parent, this, StyleBase.ButtonOpenLeft);
                    ResetButton = new Button {
                        Text = Loc.GetString("ui-options-bind-reset"), StyleClasses = { StyleBase.ButtonCaution }
                    };

                    var hBox = new BoxContainer
                    {
                        Orientation = LayoutOrientation.Horizontal,
                        Children    =
                        {
                            new Control {
                                MinSize = (5, 0)
                            },
Пример #2
0
        /// <summary>
        ///     Inserts an Input Command into the simulation.
        /// </summary>
        /// <param name="session">Player session that raised the command. On client, this is always the LocalPlayer session.</param>
        /// <param name="function">Function that is being changed.</param>
        /// <param name="message">Arguments for this event.</param>
        /// <param name="replay">if true, current cmd state will not be checked or updated - use this for "replaying" an
        /// old input that was saved or buffered until further processing could be done</param>
        public bool HandleInputCommand(ICommonSession session, BoundKeyFunction function, FullInputCmdMessage message, bool replay = false)
        {
            #if DEBUG
            var funcId = _inputManager.NetworkBindMap.KeyFunctionID(function);
            DebugTools.Assert(funcId == message.InputFunctionId, "Function ID in message does not match function.");
            #endif

            if (!replay)
            {
                // set state, state change is updated regardless if it is locally bound
                if (_cmdStates.GetState(function) == message.State)
                {
                    return(false);
                }
                _cmdStates.SetState(function, message.State);
            }

            // handle local binds before sending off
            foreach (var handler in BindRegistry.GetHandlers(function))
            {
                // local handlers can block sending over the network.
                if (handler.HandleCmdMessage(session, message))
                {
                    return(true);
                }
            }

            // send it off to the server
            DispatchInputCommand(message);
            return(false);
        }
Пример #3
0
 public GUIBoundKeyEventArgs(BoundKeyFunction function, BoundKeyState state, ScreenCoordinates pointerLocation,
                             bool canFocus, Vector2 relativePosition, Vector2 relativePixelPosition)
     : base(function, state, pointerLocation, canFocus)
 {
     RelativePosition      = relativePosition;
     RelativePixelPosition = relativePixelPosition;
 }
Пример #4
0
        public void ThrowsError_WhenCircularDependency()
        {
            var registry = new CommandBindRegistry();
            var bkf      = new BoundKeyFunction("test");

            var aHandler1 = new TestInputCmdHandler();
            var aHandler2 = new TestInputCmdHandler();
            var bHandler1 = new TestInputCmdHandler();
            var bHandler2 = new TestInputCmdHandler();
            var cHandler1 = new TestInputCmdHandler();
            var cHandler2 = new TestInputCmdHandler();

            CommandBinds.Builder
            .Bind(bkf, aHandler1)
            .BindAfter(bkf, aHandler2, typeof(TypeB), typeof(TypeC))
            .Register <TypeA>(registry);
            CommandBinds.Builder
            .Bind(bkf, bHandler1)
            .Bind(bkf, bHandler2)
            .Register <TypeB>(registry);
            CommandBinds.Builder
            .Bind(bkf, cHandler1)
            .BindAfter(bkf, cHandler2, typeof(TypeA))
            .Register <TypeC>(registry);

            Assert.Throws <InvalidOperationException>(registry.RebuildGraph);
        }
Пример #5
0
 /// <inheritdoc />
 public IKeyBinding GetKeyBinding(BoundKeyFunction function)
 {
     if (TryGetKeyBinding(function, out var binding))
     {
         return(binding);
     }
     throw new KeyNotFoundException($"No keys are bound for function '{function}'");
 }
 /// <inheritdoc />
 public IEnumerable <InputCmdHandler> GetHandlers(BoundKeyFunction function)
 {
     if (_bindingsForKey.TryGetValue(function, out var handlers))
     {
         return(handlers);
     }
     return(Enumerable.Empty <InputCmdHandler>());
 }
Пример #7
0
 /// <summary>
 /// A binding of a handler to the indicated key function, with the indicated dependencies.
 /// </summary>
 /// <param name="boundKeyFunction">key function this handler should handle</param>
 /// <param name="handler">handler to handle the input</param>
 /// <param name="before">If other types register bindings for this key function, this handler will always fire
 /// before them if they appear in this list.</param>
 /// <param name="after">If other types register bindings for this key function, this handler will always fire
 /// after them if they appear in this list.</param>
 public CommandBind(BoundKeyFunction boundKeyFunction, InputCmdHandler handler, IEnumerable <Type>?before = null,
                    IEnumerable <Type>?after = null)
 {
     _boundKeyFunction = boundKeyFunction;
     _after            = after ?? Enumerable.Empty <Type>();
     _before           = before ?? Enumerable.Empty <Type>();
     _handler          = handler;
 }
        public void ResolvesHandlers_WhenNoDependencies(int handlersPerType, int numFunctions)
        {
            var registry    = new CommandBindRegistry();
            var allHandlers = new Dictionary <BoundKeyFunction, List <InputCmdHandler> >();

            for (int i = 0; i < numFunctions; i++)
            {
                var bkf           = new BoundKeyFunction(i.ToString());
                var theseHandlers = new List <InputCmdHandler>();
                allHandlers[bkf] = theseHandlers;

                var aHandlers = new List <InputCmdHandler>();
                var bHandlers = new List <InputCmdHandler>();
                var cHandlers = new List <InputCmdHandler>();
                for (int j = 0; j < handlersPerType; j++)
                {
                    aHandlers.Add(new TestInputCmdHandler(typeof(TypeA)));
                    bHandlers.Add(new TestInputCmdHandler(typeof(TypeB)));
                    cHandlers.Add(new TestInputCmdHandler(typeof(TypeC)));
                }
                theseHandlers.AddRange(aHandlers);
                theseHandlers.AddRange(bHandlers);
                theseHandlers.AddRange(cHandlers);

                CommandBinds.Builder
                .Bind(bkf, aHandlers)
                .Register <TypeA>(registry);
                CommandBinds.Builder
                .Bind(bkf, bHandlers)
                .Register <TypeB>(registry);
                CommandBinds.Builder
                .Bind(bkf, cHandlers)
                .Register <TypeC>(registry);
            }


            //order doesn't matter, just verify that all handlers are returned
            foreach (var bkfToExpectedHandlers in allHandlers)
            {
                var bkf = bkfToExpectedHandlers.Key;
                var expectedHandlers = bkfToExpectedHandlers.Value;
                HashSet <InputCmdHandler> returnedHandlers = registry.GetHandlers(bkf).ToHashSet();

                CollectionAssert.AreEqual(returnedHandlers, expectedHandlers);
            }

            // type b stuff should no longer fire
            CommandBinds.Unregister <TypeB>(registry);

            foreach (var bkfToExpectedHandlers in allHandlers)
            {
                var bkf = bkfToExpectedHandlers.Key;
                var expectedHandlers = bkfToExpectedHandlers.Value;
                expectedHandlers.RemoveAll(handler => ((TestInputCmdHandler)handler).ForType == typeof(TypeB));
                HashSet <InputCmdHandler> returnedHandlers = registry.GetHandlers(bkf).ToHashSet();
                CollectionAssert.AreEqual(returnedHandlers, expectedHandlers);
            }
        }
Пример #9
0
        /// <inheritdoc />
        public InputCmdHandler GetInputCommand(BoundKeyFunction function)
        {
            if (_commands.TryGetValue(function, out var val))
            {
                return(val);
            }

            return(null);
        }
Пример #10
0
            /// <summary>
            /// Bind the indicated handlers to the indicated function, with no
            /// particular dependency on bindings from other owner types.
            ///
            /// If multiple
            /// handlers in this builder are registered to the same key function,
            /// the handlers will fire in the order in which they were added to this builder.
            /// </summary>
            public BindingsBuilder Bind(BoundKeyFunction function, IEnumerable <InputCmdHandler> commands)
            {
                foreach (var command in commands)
                {
                    Bind(new CommandBind(function, command));
                }

                return(this);
            }
Пример #11
0
        public string GetKeyFunctionButtonString(BoundKeyFunction function)
        {
            if (!TryGetKeyBinding(function, out var bind))
            {
                return(Loc.GetString("<not bound>"));
            }

            return(bind.GetKeyString());
        }
Пример #12
0
            public KeyBinding(BoundKeyFunction function,
                              KeyBindingType bindingType,
                              Keyboard.Key baseKey,
                              Keyboard.Key mod1 = Keyboard.Key.Unknown,
                              Keyboard.Key mod2 = Keyboard.Key.Unknown,
                              Keyboard.Key mod3 = Keyboard.Key.Unknown)
            {
                Function    = function;
                BindingType = bindingType;

                PackedKeyCombo = PackKeyCombo(baseKey, mod1, mod2, mod3);
            }
Пример #13
0
        public string GetKeyFunctionButtonString(BoundKeyFunction function)
        {
            IKeyBinding bind;

            try
            {
                bind = GetKeyBinding(function);
            }
            catch (KeyNotFoundException)
            {
                return(Loc.GetString("<not bound>"));
            }

            return(bind.GetKeyString());
        }
Пример #14
0
            public KeyBinding(BoundKeyFunction function,
                              KeyBindingType bindingType,
                              Keyboard.Key baseKey,
                              bool canFocus, bool canRepeat,
                              Keyboard.Key mod1 = Keyboard.Key.Unknown,
                              Keyboard.Key mod2 = Keyboard.Key.Unknown,
                              Keyboard.Key mod3 = Keyboard.Key.Unknown)
            {
                Function    = function;
                BindingType = bindingType;
                CanFocus    = canFocus;
                CanRepeat   = canRepeat;

                PackedKeyCombo = PackKeyCombo(baseKey, mod1, mod2, mod3);
            }
Пример #15
0
            public KeyBinding(InputManager inputManager, BoundKeyFunction function,
                              KeyBindingType bindingType,
                              Key baseKey,
                              bool canFocus, bool canRepeat, int priority, Key mod1 = Key.Unknown,
                              Key mod2 = Key.Unknown,
                              Key mod3 = Key.Unknown)
            {
                Function      = function;
                BindingType   = bindingType;
                CanFocus      = canFocus;
                CanRepeat     = canRepeat;
                Priority      = priority;
                _inputManager = inputManager;

                PackedKeyCombo = new PackedKeyCombo(baseKey, mod1, mod2, mod3);
            }
Пример #16
0
        /// <summary>
        ///     Inserts an Input Command into the simulation.
        /// </summary>
        /// <param name="session">Player session that raised the command. On client, this is always the LocalPlayer session.</param>
        /// <param name="function">Function that is being changed.</param>
        /// <param name="message">Arguments for this event.</param>
        public void HandleInputCommand(ICommonSession session, BoundKeyFunction function, FullInputCmdMessage message)
        {
            // set state, state change is updated regardless if it is locally bound
            _cmdStates.SetState(function, message.State);

            // handle local binds before sending off
            if (_bindMap.TryGetHandler(function, out var handler))
            {
                // local handlers can block sending over the network.
                if (handler.HandleCmdMessage(session, message))
                {
                    return;
                }
            }

            RaiseNetworkEvent(message);
        }
Пример #17
0
        private KeyBindingRegistration CreateMacroBinding(BoundKeyFunction function, string macroName)
        {
            macroName = macroName.Replace("SHIFT+", String.Empty);
            macroName = macroName.Replace("CTRL+", String.Empty);
            macroName = macroName.Replace("ALT+", String.Empty);
            macroName = macroName.Replace("+UP", String.Empty);
            macroName = macroName.Replace("+REP", String.Empty);

            //TODO: modifiers
            var key = KeyNameToKey(macroName);

            if (key == Key.Unknown)
            {
                Logger.Warning($"Unknown key: {macroName}");
                return(null);
            }
            return(new KeyBindingRegistration()
            {
                BaseKey = key,
                Function = function
            });
        }
Пример #18
0
                public KeyControl(KeyRebindControl parent, string niceName, BoundKeyFunction function)
                {
                    Function = function;
                    var name = new Label
                    {
                        Text = Loc.GetString(niceName),
                        SizeFlagsHorizontal = SizeFlags.Expand
                    };

                    BindButton1 = new BindButton(parent, this, StyleBase.ButtonOpenRight);
                    BindButton2 = new BindButton(parent, this, StyleBase.ButtonOpenLeft);
                    ResetButton = new Button {
                        Text = "Reset", StyleClasses = { StyleBase.ButtonCaution }
                    };

                    var hBox = new HBoxContainer
                    {
                        Children =
                        {
                            new Control {
                                CustomMinimumSize = (5, 0)
                            },
Пример #19
0
        /// <summary>
        ///     Inserts an Input Command into the simulation.
        /// </summary>
        /// <param name="session">Player session that raised the command. On client, this is always the LocalPlayer session.</param>
        /// <param name="function">Function that is being changed.</param>
        /// <param name="message">Arguments for this event.</param>
        public void HandleInputCommand(ICommonSession session, BoundKeyFunction function, FullInputCmdMessage message)
        {
            #if DEBUG
            var funcId = _inputManager.NetworkBindMap.KeyFunctionID(function);
            DebugTools.Assert(funcId == message.InputFunctionId, "Function ID in message does not match function.");
            #endif

            // set state, state change is updated regardless if it is locally bound
            _cmdStates.SetState(function, message.State);

            // handle local binds before sending off
            if (_bindMap.TryGetHandler(function, out var handler))
            {
                // local handlers can block sending over the network.
                if (handler.HandleCmdMessage(session, message))
                {
                    return;
                }
            }

            RaiseNetworkEvent(message);
        }
Пример #20
0
    public TopButton(Texture texture, BoundKeyFunction function, IInputManager inputManager)
    {
        _function     = function;
        _inputManager = inputManager;
        TooltipDelay  = CustomTooltipDelay;

        AddChild(
            new BoxContainer
        {
            Orientation = BoxContainer.LayoutOrientation.Vertical,
            Children    =
            {
                (_textureRect            = new TextureRect
                {
                    TextureScale         = (0.5f,                      0.5f),
                    Texture              = texture,
                    HorizontalAlignment  = HAlignment.Center,
                    VerticalAlignment    = VAlignment.Center,
                    VerticalExpand       = true,
                    Margin               = new Thickness(0,         VertPad),
                    ModulateSelfOverride = NormalColor,
                    Stretch              = TextureRect.StretchMode.KeepCentered
                }),
Пример #21
0
                public KeyControl(KeyRebindControl parent, string niceName, BoundKeyFunction function)
                {
                    Function = function;
                    var name = new Label
                    {
                        Text                = Loc.GetString(niceName),
                        HorizontalExpand    = true,
                        HorizontalAlignment = HAlignment.Left
                    };

                    BindButton1 = new BindButton(parent, this, StyleBase.ButtonOpenRight);
                    BindButton2 = new BindButton(parent, this, StyleBase.ButtonOpenLeft);
                    ResetButton = new Button {
                        Text = Loc.GetString("Reset"), StyleClasses = { StyleBase.ButtonCaution }
                    };

                    var hBox = new HBoxContainer
                    {
                        Children =
                        {
                            new Control {
                                MinSize = (5, 0)
                            },
Пример #22
0
        public void LoadMacroSets(List <MacroSetDescriptor> macroSets)
        {
            IInputContextContainer contexts = _inputManager.Contexts;

            foreach (MacroSetDescriptor macroSet in macroSets)
            {
                IInputCmdContext context = contexts.New(InputContextPrefix + macroSet.Name, "common");

                foreach (MacroDescriptor macro in macroSet.Macros)
                {
                    BoundKeyFunction       function = new BoundKeyFunction(macro.Id);
                    KeyBindingRegistration binding  = CreateMacroBinding(function, macro.Name);

                    if (binding == null)
                    {
                        continue;
                    }

                    context.AddFunction(function);
                    _inputManager.RegisterBinding(in binding);
                    _inputManager.SetInputCommand(function, InputCmdHandler.FromDelegate(_ => OnMacroPress(macro), _ => OnMacroRelease(macro)));
                }
            }
        }
        public void ResolvesHandlers_WithDependency(bool before, bool after)
        {
            var registry = new CommandBindRegistry();
            var bkf      = new BoundKeyFunction("test");

            var aHandler1 = new TestInputCmdHandler( );
            var aHandler2 = new TestInputCmdHandler();
            var bHandler1 = new TestInputCmdHandler();
            var bHandler2 = new TestInputCmdHandler();
            var cHandler1 = new TestInputCmdHandler();
            var cHandler2 = new TestInputCmdHandler();

            // a handler 2 should run after both b and c handlers for all the below cases
            if (before && after)
            {
                CommandBinds.Builder
                .Bind(bkf, aHandler1)
                .BindAfter(bkf, aHandler2, typeof(TypeB), typeof(TypeC))
                .Register <TypeA>(registry);
                CommandBinds.Builder
                .BindBefore(bkf, bHandler1, typeof(TypeA))
                .BindBefore(bkf, bHandler2, typeof(TypeA))
                .Register <TypeB>(registry);
                CommandBinds.Builder
                .BindBefore(bkf, cHandler1, typeof(TypeA))
                .BindBefore(bkf, cHandler2, typeof(TypeA))
                .Register <TypeC>(registry);
            }
            else if (before)
            {
                CommandBinds.Builder
                .Bind(bkf, aHandler1)
                .Bind(bkf, aHandler2)
                .Register <TypeA>(registry);
                CommandBinds.Builder
                .BindBefore(bkf, bHandler1, typeof(TypeA))
                .BindBefore(bkf, bHandler2, typeof(TypeA))
                .Register <TypeB>(registry);
                CommandBinds.Builder
                .BindBefore(bkf, cHandler1, typeof(TypeA))
                .BindBefore(bkf, cHandler2, typeof(TypeA))
                .Register <TypeC>(registry);
            }
            else if (after)
            {
                CommandBinds.Builder
                .Bind(bkf, aHandler1)
                .BindAfter(bkf, aHandler2, typeof(TypeB), typeof(TypeC))
                .Register <TypeA>(registry);
                CommandBinds.Builder
                .Bind(bkf, bHandler1)
                .Bind(bkf, bHandler2)
                .Register <TypeB>(registry);
                CommandBinds.Builder
                .Bind(bkf, cHandler1)
                .Bind(bkf, cHandler2)
                .Register <TypeC>(registry);
            }


            var returnedHandlers = registry.GetHandlers(bkf);

            // b1 , b2, c1, c2 should be fired before a2
            bool foundB1 = false, foundB2 = false, foundC1 = false, foundC2 = false;

            foreach (var returnedHandler in returnedHandlers)
            {
                if (returnedHandler == bHandler1)
                {
                    foundB1 = true;
                }
                else if (returnedHandler == bHandler2)
                {
                    foundB2 = true;
                }
                else if (returnedHandler == cHandler1)
                {
                    foundC1 = true;
                }
                else if (returnedHandler == cHandler2)
                {
                    foundC2 = true;
                }
                else if (returnedHandler == aHandler2)
                {
                    Assert.True(foundB1 && foundB2 && foundC1 && foundC2, "bind registry didn't respect" +
                                " handler dependency order");
                }
            }

            var expectedHandlers =
                new [] { aHandler1, aHandler2, bHandler1, bHandler2, cHandler1, cHandler2 };
            var returnedHandlerSet = new HashSet <InputCmdHandler>(returnedHandlers);

            foreach (var expectedHandler in expectedHandlers)
            {
                Assert.True(returnedHandlerSet.Contains(expectedHandler));
            }
        }
Пример #24
0
 public BoundKeyChangedMsg(BoundKeyFunction function, BoundKeyState state)
 {
     Directed = true;
     Function = function;
     State    = state;
 }
Пример #25
0
 /// <inheritdoc />
 public void SetInputCommand(BoundKeyFunction function, InputCommand command)
 {
     _commands[function] = command;
 }
Пример #26
0
 /// <inheritdoc />
 public bool TryGetKeyBinding(BoundKeyFunction function, out IKeyBinding binding)
 {
     binding = _bindings.FirstOrDefault(k => k.Function == function);
     return(binding != null);
 }
Пример #27
0
 /// <inheritdoc />
 public void SetInputCommand(BoundKeyFunction function, InputCmdHandler cmdHandler)
 {
     _commands[function] = cmdHandler;
 }
Пример #28
0
 /// <summary>
 /// Bind the indicated handler to the indicated function, with no
 /// particular dependency on bindings from other owner types. If multiple
 /// handlers in this builder are registered to the same key function,
 /// the handlers will fire in the order in which they were added to this builder.
 /// </summary>
 public BindingsBuilder Bind(BoundKeyFunction function, InputCmdHandler command)
 {
     return(Bind(new CommandBind(function, command)));
 }
Пример #29
0
 /// <summary>
 /// Bind the indicated handler to the indicated function. If other owner types register bindings for this key
 /// function, this handler will always fire before them if they appear in the "before" list.
 ///
 /// If multiple handlers in this builder are registered to the same key function,
 /// the handlers will fire in the order in which they were added to this builder.
 /// </summary>
 /// <param name="before">If other owner types register bindings for this key
 /// function, this handler will always fire before them if they appear in this list</param>
 public BindingsBuilder BindBefore(BoundKeyFunction function, InputCmdHandler command, params Type[] before)
 {
     return(Bind(new CommandBind(function, command, before)));
 }
Пример #30
0
 /// <summary>
 /// Bind the indicated handler to the indicated function. If other owner types register bindings for this key
 /// function, this handler will always fire after them if they appear in the "after" list.
 ///
 /// If multiple handlers in this builder are registered to the same key function,
 /// the handlers will fire in the order in which they were added to this builder.
 /// </summary>
 /// <param name="after">If other owner types register bindings for this key
 /// function, this handler will always fire after them if they appear in this list</param>
 public BindingsBuilder BindAfter(BoundKeyFunction function, InputCmdHandler command, params Type[] after)
 {
     return(Bind(new CommandBind(function, command, after: after)));
 }