Esempio n. 1
0
        private bool SetBindState(KeyBinding binding, BoundKeyState state)
        {
            binding.State = state;

            var eventArgs = new BoundKeyEventArgs(binding.Function, binding.State,
                                                  new ScreenCoordinates(MouseScreenPosition), binding.CanFocus);

            UIKeyBindStateChanged?.Invoke(eventArgs);
            if (state == BoundKeyState.Up || !eventArgs.Handled)
            {
                KeyBindStateChanged?.Invoke(eventArgs);
            }

            var cmd = GetInputCommand(binding.Function);

            if (state == BoundKeyState.Up)
            {
                cmd?.Disabled(null);
            }
            else
            {
                cmd?.Enabled(null);
            }

            return(eventArgs.Handled);
        }
 public virtual void KeyUp(object sender, BoundKeyEventArgs e)
 {
     if (!_enabled)
     {
         return;
     }
     Owner.SendComponentNetworkMessage(this, NetDeliveryMethod.ReliableUnordered, e.Function, e.FunctionState);
     SetKeyState(e.Function, false);
     Owner.SendMessage(this, ComponentMessageType.BoundKeyChange, e.Function, e.FunctionState);
 }
 public virtual void KeyDown(object sender, BoundKeyEventArgs e)
 {
     if (!_enabled || GetKeyState(e.Function))
     {
         return; //Don't repeat keys that are already down.
     }
     Owner.SendComponentNetworkMessage(this, NetDeliveryMethod.ReliableUnordered, e.Function, e.FunctionState);
     SetKeyState(e.Function, true);
     Owner.SendMessage(this, ComponentMessageType.BoundKeyChange, e.Function, e.FunctionState);
 }
        public void KeyBindDown(BoundKeyEventArgs args)
        {
            var control = MouseGetControl(args.PointerLocation.Position);

            if (args.CanFocus)
            {
                // If we have a modal open and the mouse down was outside it, close said modal.
                if (_modalStack.Count != 0)
                {
                    var top    = _modalStack[_modalStack.Count - 1];
                    var offset = args.PointerLocation.Position - top.GlobalPixelPosition;
                    if (!top.HasPoint(offset / UIScale))
                    {
                        RemoveModal(top);
                        return;
                    }
                }

                ReleaseKeyboardFocus();

                if (control == null)
                {
                    return;
                }

                _controlFocused = control;

                if (_controlFocused.CanKeyboardFocus && _controlFocused.KeyboardFocusOnClick)
                {
                    _controlFocused.GrabKeyboardFocus();
                }
            }
            else if (KeyboardFocused != null)
            {
                control = KeyboardFocused;
            }

            if (control == null)
            {
                return;
            }

            var guiArgs = new GUIBoundKeyEventArgs(args.Function, args.State, args.PointerLocation, args.CanFocus,
                                                   args.PointerLocation.Position / UIScale - control.GlobalPosition,
                                                   args.PointerLocation.Position - control.GlobalPixelPosition);

            _doGuiInput(control, guiArgs, (c, ev) => c.KeyBindDown(ev));

            if (args.CanFocus)
            {
                args.Handle();
            }
        }
Esempio n. 5
0
        public virtual void KeyDown(object sender, BoundKeyEventArgs e)
        {
            if (!_enabled || GetKeyState(e.Function))
            {
                return; //Don't repeat keys that are already down.
            }
            SetKeyState(e.Function, true);
            var message = new BoundKeyChangedMessage(e.Function, e.FunctionState);

            RaiseEvent(message);
            RaiseNetworkEvent(message);
        }
Esempio n. 6
0
        public virtual void KeyUp(object sender, BoundKeyEventArgs e)
        {
            if (!_enabled)
            {
                return;
            }

            SetKeyState(e.Function, false);

            var message = new BoundKeyChangedMessage(e.Function, e.FunctionState);

            RaiseEvent(message);
            RaiseNetworkEvent(message);
        }
 /// <summary>
 ///     Converts
 /// </summary>
 /// <param name="args">Event data values for a bound key state change.</param>
 private void OnUIKeyBindStateChanged(BoundKeyEventArgs args)
 {
     if (!args.CanFocus && KeyboardFocused != null)
     {
         args.Handle();
     }
     if (args.State == BoundKeyState.Down)
     {
         KeyBindDown(args);
     }
     else
     {
         KeyBindUp(args);
     }
 }
        public void KeyBindUp(BoundKeyEventArgs args)
        {
            var control = _controlFocused ?? KeyboardFocused ?? MouseGetControl(args.PointerLocation.Position);

            if (control == null)
            {
                return;
            }

            var guiArgs = new GUIBoundKeyEventArgs(args.Function, args.State, args.PointerLocation, args.CanFocus,
                                                   args.PointerLocation.Position / UIScale - control.GlobalPosition,
                                                   args.PointerLocation.Position - control.GlobalPixelPosition);

            _doGuiInput(control, guiArgs, (c, ev) => c.KeyBindUp(ev));
            _controlFocused = null;

            // Always mark this as handled.
            // The only case it should not be is if we do not have a control to click on,
            // in which case we never reach this.
            args.Handle();
        }
Esempio n. 9
0
        public void TestMouseDown()
        {
            // We create 4 controls.
            // Control 2 is set to stop mouse events,
            // Control 3 pass,
            // Control 4 ignore.
            // We check that 4 and 1 do not receive events, that 3 receives before 2, and that positions are correct.
            var control1 = new LayoutContainer
            {
                MinSize = new Vector2(50, 50)
            };
            var control2 = new LayoutContainer
            {
                MinSize     = new Vector2(50, 50),
                MouseFilter = Control.MouseFilterMode.Stop
            };
            var control3 = new LayoutContainer
            {
                MinSize     = new Vector2(50, 50),
                MouseFilter = Control.MouseFilterMode.Pass
            };
            var control4 = new LayoutContainer
            {
                MinSize     = new Vector2(50, 50),
                MouseFilter = Control.MouseFilterMode.Ignore
            };

            _userInterfaceManager.RootControl.AddChild(control1);
            control1.AddChild(control2);
            // Offsets to test relative positioning on the events.
            LayoutContainer.SetPosition(control2, (5, 5));
            control2.AddChild(control3);
            LayoutContainer.SetPosition(control3, (5, 5));
            control3.AddChild(control4);
            LayoutContainer.SetPosition(control4, (5, 5));

            control1.Arrange(new UIBox2(0, 0, 50, 50));

            var mouseEvent = new BoundKeyEventArgs(EngineKeyFunctions.Use, BoundKeyState.Down,
                                                   new Robust.Shared.Map.ScreenCoordinates(30, 30), true);

            var control2Fired = false;
            var control3Fired = false;

            control1.OnKeyBindDown += _ => Assert.Fail("Control 1 should not get a mouse event.");

            void Control2MouseDown(GUIBoundKeyEventArgs ev)
            {
                Assert.That(control2Fired, Is.False);
                Assert.That(control3Fired, Is.True);

                Assert.That(ev.RelativePosition, Is.EqualTo(new Vector2(25, 25)));

                control2Fired = true;
            }

            control2.OnKeyBindDown += Control2MouseDown;

            control3.OnKeyBindDown += ev =>
            {
                Assert.That(control2Fired, Is.False);
                Assert.That(control3Fired, Is.False);

                Assert.That(ev.RelativePosition, Is.EqualTo(new Vector2(20, 20)));

                control3Fired = true;
            };

            control4.OnKeyBindDown += _ => Assert.Fail("Control 4 should not get a mouse event.");

            _userInterfaceManager.KeyBindDown(mouseEvent);

            Assert.Multiple(() =>
            {
                Assert.That(control2Fired, Is.True);
                Assert.That(control3Fired, Is.True);
            });

            // Step two: instead of relying on stop for control2 to prevent the event reaching control1,
            // handle the event in control2.

            control2Fired = false;
            control3Fired = false;

            control2.OnKeyBindDown -= Control2MouseDown;
            control2.OnKeyBindDown += ev =>
            {
                Assert.That(control2Fired, Is.False);
                Assert.That(control3Fired, Is.True);

                Assert.That(ev.RelativePosition, Is.EqualTo(new Vector2(25, 25)));

                control2Fired = true;
                ev.Handle();
            };
            control2.MouseFilter = Control.MouseFilterMode.Pass;

            _userInterfaceManager.KeyBindDown(mouseEvent);

            Assert.Multiple(() =>
            {
                Assert.That(control2Fired, Is.True);
                Assert.That(control3Fired, Is.True);
            });

            control1.Dispose();
            control2.Dispose();
            control3.Dispose();
            control4.Dispose();
        }
Esempio n. 10
0
 public ViewportBoundKeyEventArgs(BoundKeyEventArgs keyEventArgs, Control?viewport)
 {
     KeyEventArgs = keyEventArgs;
     Viewport     = viewport;
 }