Beispiel #1
0
        public bool PropagateMouse(Point mousePosition)
        {
            if (Visibility == Visibility.Hidden)
            {
                return(false);
            }
            if (mousePosition.X >= AbsolutePosition.X && mousePosition.X <= AbsolutePosition.X + Width &&
                mousePosition.Y >= AbsolutePosition.Y && mousePosition.Y <= AbsolutePosition.Y + Height)
            {
                if (Children.Count > 0)
                {
                    bool ret = false;
                    for (int i = Children.Count - 1; i >= 0; i--)
                    {
                        LogicalNode child = Children[i];
                        if (child.PropagateMouse(mousePosition))
                        {
                            ret = true;
                        }
                        if (ret)
                        {
                            break;
                        }
                    }
                    if (ret == false)
                    {
                        bool mEnter = false, mPressed = false, mLPressed = false, mRPressed = false, mReleased = false,
                             mLReleased = false, mRReleased = false;
                        MouseOver?.Invoke(this, null);
                        if (!mouseOver)
                        {
                            mouseOver = true;
                            MouseEnter?.Invoke(this, null);
                            mEnter = true;
                        }
                        if ((Mouse.GetState().LeftButton == ButtonState.Pressed &&
                             !leftMousePressed))
                        {
                            LeftMouseDown?.Invoke(this, null);
                            leftMousePressed = true;
                            mLPressed        = true;
                        }
                        else if ((Mouse.GetState().LeftButton == ButtonState.Released &&
                                  leftMousePressed))
                        {
                            LeftMouseUp?.Invoke(this, null);
                            leftMousePressed = false;
                            mLReleased       = true;
                        }
                        if ((Mouse.GetState().RightButton == ButtonState.Pressed &&
                             !rightMousePressed))
                        {
                            RightMouseDown?.Invoke(this, null);
                            rightMousePressed = true;
                            mRPressed         = true;
                        }
                        else if ((Mouse.GetState().RightButton == ButtonState.Released &&
                                  rightMousePressed))
                        {
                            RightMouseUp?.Invoke(this, null);
                            rightMousePressed = false;
                            mRReleased        = true;
                        }

                        foreach (MenuEvent ev in events.Values)
                        {
                            if (ev.EventType == MenuEvent.Type.MouseEnter && mEnter)
                            {
                                ev.Trigger(this);
                            }
                            if (ev.EventType == MenuEvent.Type.MouseDown && mPressed)
                            {
                                ev.Trigger(this);
                            }
                            if (ev.EventType == MenuEvent.Type.MouseUp && mReleased)
                            {
                                ev.Trigger(this);
                            }
                            if (ev.EventType == MenuEvent.Type.LeftMouseDown && mLPressed)
                            {
                                ev.Trigger(this);
                            }
                            if (ev.EventType == MenuEvent.Type.LeftMouseUp && mLReleased)
                            {
                                ev.Trigger(this);
                            }
                            if (ev.EventType == MenuEvent.Type.RightMouseDown && mRPressed)
                            {
                                ev.Trigger(this);
                            }
                            if (ev.EventType == MenuEvent.Type.RightMouseUp && mRReleased)
                            {
                                ev.Trigger(this);
                            }
                        }
                        return(true);
                    }
                    else
                    {
                        if (mouseOver)
                        {
                            mouseOver = false;
                            MouseLeave?.Invoke(this, null);
                            foreach (MenuEvent ev in events.Values)
                            {
                                if (ev.EventType == MenuEvent.Type.MouseLeave)
                                {
                                    ev.Trigger(this);
                                }
                            }
                        }
                        return(true);
                    }
                }
                else
                {
                    bool mEnter = false, mPressed = false, mLPressed = false, mRPressed = false, mReleased = false,
                         mLReleased = false, mRReleased = false;
                    MouseOver?.Invoke(this, null);
                    if (!mouseOver)
                    {
                        mouseOver = true;
                        MouseEnter?.Invoke(this, null);
                        mEnter = true;
                    }
                    if ((Mouse.GetState().LeftButton == ButtonState.Pressed &&
                         !leftMousePressed))
                    {
                        LeftMouseDown?.Invoke(this, null);
                        leftMousePressed = true;
                        mLPressed        = true;
                    }
                    else if ((Mouse.GetState().LeftButton == ButtonState.Released &&
                              leftMousePressed))
                    {
                        LeftMouseUp?.Invoke(this, null);
                        leftMousePressed = false;
                        mLReleased       = true;
                    }
                    if ((Mouse.GetState().RightButton == ButtonState.Pressed &&
                         !rightMousePressed))
                    {
                        RightMouseDown?.Invoke(this, null);
                        rightMousePressed = true;
                        mRPressed         = true;
                    }
                    else if ((Mouse.GetState().RightButton == ButtonState.Released &&
                              rightMousePressed))
                    {
                        RightMouseUp?.Invoke(this, null);
                        rightMousePressed = false;
                        mRReleased        = true;
                    }

                    foreach (MenuEvent ev in events.Values)
                    {
                        if (ev.EventType == MenuEvent.Type.MouseEnter && mEnter)
                        {
                            ev.Trigger(this);
                        }
                        if (ev.EventType == MenuEvent.Type.MouseDown && mPressed)
                        {
                            ev.Trigger(this);
                        }
                        if (ev.EventType == MenuEvent.Type.MouseUp && mReleased)
                        {
                            ev.Trigger(this);
                        }
                        if (ev.EventType == MenuEvent.Type.LeftMouseDown && mLPressed)
                        {
                            ev.Trigger(this);
                        }
                        if (ev.EventType == MenuEvent.Type.LeftMouseUp && mLReleased)
                        {
                            ev.Trigger(this);
                        }
                        if (ev.EventType == MenuEvent.Type.RightMouseDown && mRPressed)
                        {
                            ev.Trigger(this);
                        }
                        if (ev.EventType == MenuEvent.Type.RightMouseUp && mRReleased)
                        {
                            ev.Trigger(this);
                        }
                    }
                    return(true);
                }
            }
            else
            {
                if (mouseOver)
                {
                    mouseOver = false;
                    MouseLeave?.Invoke(this, null);
                    foreach (MenuEvent ev in events.Values)
                    {
                        if (ev.EventType == MenuEvent.Type.MouseLeave)
                        {
                            ev.Trigger(this);
                        }
                    }
                }
                foreach (LogicalNode child in Children)
                {
                    child.PropagateMouse(mousePosition);
                }
                return(false);
            }
        }
        /// <summary>
        /// Handling aller Eingaben, Mausbewegungen und Updaten aller Screens und Controls.
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Update(GameTime gameTime)
        {
            if (Game.IsActive)
            {
                #region Mouse Interaction

                if (MouseEnabled)
                {
                    MouseState mouse;
                    Point      mousePosition;
                    if (MouseMode == MouseMode.Captured)
                    {
                        mouse         = Mouse.GetState();
                        mousePosition = new Point(
                            mouse.X - (lastMousePosition.X),
                            mouse.Y - (lastMousePosition.Y));
                    }
                    else
                    {
                        mouse         = Mouse.GetCursorState();
                        mousePosition = Game.Window.PointToClient(mouse.Location);
                    }

                    // Mausposition anhand des Mouse Modes ermitteln



                    MouseEventArgs mouseEventArgs = MouseEventArgsPool.Instance.Take();

                    mouseEventArgs.MouseMode      = MouseMode;
                    mouseEventArgs.GlobalPosition = mousePosition;
                    mouseEventArgs.LocalPosition  = mousePosition;

                    // Mouse Move
                    if (mousePosition != lastMousePosition)
                    {
                        mouseEventArgs.Handled = false;

                        root.InternalMouseMove(mouseEventArgs);
                        if (!mouseEventArgs.Handled)
                        {
                            MouseMove?.Invoke(mouseEventArgs);
                        }

                        // Start Drag Handling
                        if (mouse.LeftButton == ButtonState.Pressed &&
                            DraggingArgs == null)
                        {
                            DraggingArgs = DragEventArgsPool.Instance.Take();
                            DraggingArgs.GlobalPosition = mousePosition;
                            DraggingArgs.LocalPosition  = mousePosition;

                            draggingId = null;

                            root.InternalStartDrag(DraggingArgs);
                            if (!DraggingArgs.Handled)
                            {
                                StartDrag?.Invoke(DraggingArgs);
                            }
                        }

                        // Drop move
                        if (mouse.LeftButton == ButtonState.Pressed &&
                            DraggingArgs != null &&
                            draggingId == null &&
                            DraggingArgs.Handled)
                        {
                            //TODO: perhaps pool single object?
                            DragEventArgs args = DragEventArgsPool.Instance.Take();

                            args.GlobalPosition = mousePosition;
                            args.LocalPosition  = mousePosition;
                            args.Content        = DraggingArgs.Content;
                            args.Icon           = DraggingArgs.Icon;
                            args.Sender         = DraggingArgs.Sender;

                            root.InternalDropMove(args);
                            if (!args.Handled)
                            {
                                DropMove?.Invoke(args);
                            }
                        }
                    }

                    // Linke Maustaste
                    if (mouse.LeftButton == ButtonState.Pressed)
                    {
                        if (!lastLeftMouseButtonPressed)
                        {
                            mouseEventArgs.Handled = false;

                            // Linke Maustaste wurde neu gedrückt
                            root.InternalLeftMouseDown(mouseEventArgs);
                            if (!mouseEventArgs.Handled)
                            {
                                LeftMouseDown?.Invoke(mouseEventArgs);
                            }
                        }
                        lastLeftMouseButtonPressed = true;
                    }
                    else
                    {
                        if (lastLeftMouseButtonPressed)
                        {
                            // Handle Drop
                            if (DraggingArgs != null && DraggingArgs.Handled)
                            {
                                DragEventArgs args = DragEventArgsPool.Instance.Take();
                                args.GlobalPosition = mousePosition;
                                args.LocalPosition  = mousePosition;
                                args.Content        = DraggingArgs.Content;
                                args.Icon           = DraggingArgs.Icon;
                                args.Sender         = DraggingArgs.Sender;

                                root.InternalEndDrop(args);
                                if (!args.Handled)
                                {
                                    EndDrop?.Invoke(args);
                                }
                            }

                            // Discard Dragging Infos
                            DragEventArgsPool.Instance.Release(DraggingArgs);
                            DraggingArgs = null;
                            draggingId   = null;

                            // Linke Maustaste wurde losgelassen
                            mouseEventArgs.Handled = false;

                            root.InternalLeftMouseClick(mouseEventArgs);
                            if (!mouseEventArgs.Handled)
                            {
                                LeftMouseClick?.Invoke(mouseEventArgs);
                            }

                            if (lastLeftClick.HasValue &&
                                gameTime.TotalGameTime - lastLeftClick.Value < TimeSpan.FromMilliseconds(DoubleClickDelay))
                            {
                                // Double Left Click
                                mouseEventArgs.Handled = false;

                                root.InternalLeftMouseDoubleClick(mouseEventArgs);
                                if (!mouseEventArgs.Handled)
                                {
                                    LeftMouseDoubleClick?.Invoke(mouseEventArgs);
                                }

                                lastLeftClick = null;
                            }
                            else
                            {
                                lastLeftClick = gameTime.TotalGameTime;
                            }

                            // Mouse Up
                            mouseEventArgs.Handled = false;

                            root.InternalLeftMouseUp(mouseEventArgs);
                            if (!mouseEventArgs.Handled)
                            {
                                LeftMouseUp?.Invoke(mouseEventArgs);
                            }
                        }
                        lastLeftMouseButtonPressed = false;
                    }

                    // Rechte Maustaste
                    if (mouse.RightButton == ButtonState.Pressed)
                    {
                        if (!lastRightMouseButtonPressed)
                        {
                            // Rechte Maustaste neu gedrückt
                            mouseEventArgs.Handled = false;

                            root.InternalRightMouseDown(mouseEventArgs);
                            if (!mouseEventArgs.Handled)
                            {
                                RightMouseDown?.Invoke(mouseEventArgs);
                            }
                        }
                        lastRightMouseButtonPressed = true;
                    }
                    else
                    {
                        if (lastRightMouseButtonPressed)
                        {
                            // Rechte Maustaste losgelassen
                            mouseEventArgs.Handled = false;
                            root.InternalRightMouseClick(mouseEventArgs);
                            if (!mouseEventArgs.Handled)
                            {
                                RightMouseClick?.Invoke(mouseEventArgs);
                            }

                            if (lastRightClick.HasValue &&
                                gameTime.TotalGameTime - lastRightClick.Value < TimeSpan.FromMilliseconds(DoubleClickDelay))
                            {
                                // Double Left Click
                                mouseEventArgs.Handled = false;

                                root.InternalRightMouseDoubleClick(mouseEventArgs);
                                if (!mouseEventArgs.Handled)
                                {
                                    RightMouseDoubleClick?.Invoke(mouseEventArgs);
                                }

                                lastRightClick = null;
                            }
                            else
                            {
                                lastRightClick = gameTime.TotalGameTime;
                            }

                            mouseEventArgs.Handled = false;

                            root.InternalRightMouseUp(mouseEventArgs);
                            if (!mouseEventArgs.Handled)
                            {
                                RightMouseUp?.Invoke(mouseEventArgs);
                            }
                        }
                        lastRightMouseButtonPressed = false;
                    }

                    // Mousewheel
                    if (lastMouseWheelValue != mouse.ScrollWheelValue)
                    {
                        int diff = (mouse.ScrollWheelValue - lastMouseWheelValue);

                        MouseScrollEventArgs scrollArgs = new MouseScrollEventArgs
                        {
                            MouseMode      = MouseMode,
                            GlobalPosition = mousePosition,
                            LocalPosition  = mousePosition,
                            Steps          = diff
                        };
                        root.InternalMouseScroll(scrollArgs);
                        if (!scrollArgs.Handled)
                        {
                            MouseScroll?.Invoke(scrollArgs);
                        }

                        lastMouseWheelValue = mouse.ScrollWheelValue;
                    }

                    // Potentieller Positionsreset
                    if (MouseMode == MouseMode.Free)
                    {
                        lastMousePosition = mousePosition;
                    }
                    else if (mousePosition.X != 0 || mousePosition.Y != 0)
                    {
                        lastMousePosition = mouse.Location;
                    }

                    MouseEventArgsPool.Instance.Release(mouseEventArgs);
                }

                #endregion

                #region Keyboard Interaction

                if (KeyboardEnabled)
                {
                    KeyboardState keyboard = Keyboard.GetState();

                    bool shift = keyboard.IsKeyDown(Keys.LeftShift) | keyboard.IsKeyDown(Keys.RightShift);
                    bool ctrl  = keyboard.IsKeyDown(Keys.LeftControl) | keyboard.IsKeyDown(Keys.RightControl);
                    bool alt   = keyboard.IsKeyDown(Keys.LeftAlt) | keyboard.IsKeyDown(Keys.RightAlt);

                    KeyEventArgs args;

                    for (int i = 0; i < _pressedKeys.Length; i++)
                    {
                        var key = (Keys)i;
                        if (keyboard.IsKeyDown(key))
                        {
                            // ReSharper disable once CompareOfFloatsByEqualityOperator
                            if (_pressedKeys[i] == UnpressedKeyTimestamp)
                            {
                                // Taste ist neu

                                args = KeyEventArgsPool.Take();

                                args.Key   = key;
                                args.Shift = shift;
                                args.Ctrl  = ctrl;
                                args.Alt   = alt;

                                root.InternalKeyDown(args);

                                if (!args.Handled)
                                {
                                    KeyDown?.Invoke(args);
                                }

                                KeyEventArgsPool.Release(args);

                                args = KeyEventArgsPool.Take();

                                args.Key   = key;
                                args.Shift = shift;
                                args.Ctrl  = ctrl;
                                args.Alt   = alt;

                                root.InternalKeyPress(args);
                                _pressedKeys[i] = gameTime.TotalGameTime.TotalMilliseconds + 500;

                                KeyEventArgsPool.Release(args);

                                // Spezialfall Tab-Taste (falls nicht verarbeitet wurde)
                                if (key == Keys.Tab && !args.Handled)
                                {
                                    if (shift)
                                    {
                                        root.InternalTabbedBackward();
                                    }
                                    else
                                    {
                                        root.InternalTabbedForward();
                                    }
                                }
                            }
                            else
                            {
                                // Taste ist immernoch gedrückt
                                if (_pressedKeys[i] <= gameTime.TotalGameTime.TotalMilliseconds)
                                {
                                    args = KeyEventArgsPool.Take();

                                    args.Key   = key;
                                    args.Shift = shift;
                                    args.Ctrl  = ctrl;
                                    args.Alt   = alt;


                                    root.InternalKeyPress(args);
                                    if (!args.Handled)
                                    {
                                        KeyPress?.Invoke(args);
                                    }

                                    KeyEventArgsPool.Release(args);

                                    _pressedKeys[i] = gameTime.TotalGameTime.TotalMilliseconds + 50;
                                }
                            }
                        }
                        else
                        {
                            // ReSharper disable once CompareOfFloatsByEqualityOperator
                            if (_pressedKeys[i] != UnpressedKeyTimestamp)
                            {
                                // Taste losgelassen
                                args = KeyEventArgsPool.Take();

                                args.Key   = key;
                                args.Shift = shift;
                                args.Ctrl  = ctrl;
                                args.Alt   = alt;

                                root.InternalKeyUp(args);
                                _pressedKeys[i] = UnpressedKeyTimestamp;

                                if (!args.Handled)
                                {
                                    KeyUp?.Invoke(args);
                                }

                                KeyEventArgsPool.Release(args);
                            }
                        }
                    }
                }

                #endregion

                #region Touchpanel Interaction

                //if (TouchEnabled)
                //{
                //    TouchCollection touchPoints = TouchPanel.GetState();
                //    foreach (var touchPoint in touchPoints)
                //    {
                //        Point point = touchPoint.Position.ToPoint();
                //        TouchEventArgs args = new TouchEventArgs()
                //        {
                //            TouchId = touchPoint.Id,
                //            GlobalPosition = point,
                //            LocalPosition = point
                //        };

                //        switch (touchPoint.State)
                //        {
                //            case TouchLocationState.Pressed:
                //                root.InternalTouchDown(args);
                //                if (!args.Handled && TouchDown != null)
                //                    TouchDown(args);
                //                break;
                //            case TouchLocationState.Moved:

                //                // Touch Move
                //                root.InternalTouchMove(args);
                //                if (!args.Handled && TouchMove != null)
                //                    TouchMove(args);

                //                // Start Dragging
                //                if (DraggingArgs == null)
                //                {
                //                    DraggingArgs = new DragEventArgs()
                //                    {
                //                        GlobalPosition = point,
                //                        LocalPosition = point,
                //                    };

                //                    draggingId = touchPoint.Id;

                //                    root.InternalStartDrag(DraggingArgs);
                //                    if (!DraggingArgs.Handled && StartDrag != null)
                //                        StartDrag(DraggingArgs);
                //                }

                //                // Drop move
                //                if (DraggingArgs != null &&
                //                    draggingId == touchPoint.Id &&
                //                    DraggingArgs.Handled)
                //                {
                //                    DragEventArgs moveArgs = new DragEventArgs()
                //                    {
                //                        GlobalPosition = point,
                //                        LocalPosition = point,
                //                        Content = DraggingArgs.Content,
                //                        Icon = DraggingArgs.Icon,
                //                        Sender = DraggingArgs.Sender
                //                    };

                //                    root.InternalDropMove(moveArgs);
                //                    if (!args.Handled && DropMove != null)
                //                        DropMove(moveArgs);
                //                }

                //                break;
                //            case TouchLocationState.Released:

                //                // Handle Drop
                //                if (DraggingArgs != null &&
                //                    draggingId == touchPoint.Id &&
                //                    DraggingArgs.Handled)
                //                {
                //                    DragEventArgs dropArgs = new DragEventArgs()
                //                    {
                //                        GlobalPosition = point,
                //                        LocalPosition = point,
                //                        Content = DraggingArgs.Content,
                //                        Icon = DraggingArgs.Icon,
                //                        Sender = DraggingArgs.Sender
                //                    };

                //                    root.InternalEndDrop(dropArgs);
                //                    if (!args.Handled && EndDrop != null)
                //                        EndDrop(dropArgs);
                //                }

                //                // Discard Dragging Infos
                //                DraggingArgs = null;
                //                draggingId = null;

                //                // Linke Maustaste wurde losgelassen
                //                TouchEventArgs tapArgs = new TouchEventArgs
                //                {
                //                    TouchId = touchPoint.Id,
                //                    GlobalPosition = point,
                //                    LocalPosition = point
                //                };

                //                root.InternalTouchTap(tapArgs);
                //                if (!tapArgs.Handled && TouchTap != null)
                //                    TouchTap(tapArgs);

                //                if (lastTouchTap.HasValue &&
                //                gameTime.TotalGameTime - lastLeftClick.Value < TimeSpan.FromMilliseconds(DoubleClickDelay))
                //                {
                //                    // Double Tap
                //                    TouchEventArgs doubleTapArgs = new TouchEventArgs
                //                    {
                //                        TouchId = touchPoint.Id,
                //                        GlobalPosition = point,
                //                        LocalPosition = point
                //                    };

                //                    root.InternalTouchDoubleTap(doubleTapArgs);
                //                    if (!doubleTapArgs.Handled && TouchDoubleTap != null)
                //                        TouchDoubleTap(doubleTapArgs);

                //                    lastTouchTap = null;
                //                }
                //                else
                //                {
                //                    lastTouchTap = gameTime.TotalGameTime;
                //                }

                //                root.InternalTouchUp(args);
                //                if (!args.Handled && TouchUp != null)
                //                    TouchUp(args);
                //                break;
                //        }
                //    }
                //}

                #endregion
            }

            #region Recalculate Sizes

            if (root.HasInvalidDimensions())
            {
                Point available = new Point(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
                Point required  = root.GetExpectedSize(available);
                root.SetActualSize(available);
            }

            root.Update(gameTime);

            #endregion

            #region Form anpassen


            if (_titleDirty || (ActiveScreen?.Title != _lastActiveScreenTitle))
            {
                string screentitle = ActiveScreen?.Title ?? string.Empty;
                string windowtitle = TitlePrefix + (string.IsNullOrEmpty(screentitle) ? string.Empty : " - " + screentitle);

                if (Game.Window != null && Game.Window.Title != windowtitle)
                {
                    Game.Window.Title = windowtitle;
                }

                _titleDirty            = false;
                _lastActiveScreenTitle = ActiveScreen?.Title;
            }

            #endregion
        }
Beispiel #3
0
        public override void Update(GameTime gameTime)
        {
            if (!Enabled)
            {
                return;
            }
            var keystate   = Keyboard.GetState();
            var mousestate = Mouse.GetState();

            MousePosition = new Vector2(mousestate.X, mousestate.Y);
            if (IsLeftMouseDown && mousestate.LeftButton == ButtonState.Released)
            {
                IsLeftMouseDown = false;
                LeftMouseUp?.Invoke(mousestate);
            }
            else if (!IsLeftMouseDown && mousestate.LeftButton == ButtonState.Pressed)
            {
                IsLeftMouseDown = true;
                LeftMouseDown?.Invoke(mousestate);
            }

            if (IsRightMouseDown && mousestate.RightButton == ButtonState.Released)
            {
                IsRightMouseDown = false;
                RightMouseUp?.Invoke(mousestate);
            }
            else if (!IsRightMouseDown && mousestate.RightButton == ButtonState.Pressed)
            {
                IsRightMouseDown = true;
                RightMouseDown?.Invoke(mousestate);
            }

            if (IsMiddleMouseDown && mousestate.MiddleButton == ButtonState.Released)
            {
                IsMiddleMouseDown = false;
                MiddleMouseUp?.Invoke(mousestate);
            }
            else if (!IsMiddleMouseDown && mousestate.MiddleButton == ButtonState.Pressed)
            {
                IsMiddleMouseDown = true;
                MiddleMouseDown?.Invoke(mousestate);
            }

            ScrollWheel = mousestate.ScrollWheelValue;

            var releasedKeys = _keyStates.Where(k => k.Value && keystate.IsKeyUp(k.Key)).Select(k => k.Key).ToArray();

            foreach (var releasedKey in releasedKeys)
            {
                _keyStates[releasedKey] = false;
                KeyReleased?.Invoke(releasedKey);
            }

            foreach (var pressedKey in keystate.GetPressedKeys())
            {
                if (_keyStates.ContainsKey(pressedKey))
                {
                    if (_keyStates[pressedKey])
                    {
                        continue;
                    }
                    _keyStates[pressedKey] = true;
                    KeyPressed?.Invoke(pressedKey);
                }
                else
                {
                    _keyStates.Add(pressedKey, true);
                }
            }
        }