Exemple #1
0
 private void OnMouseClick(object sender, MouseEventArgs e)
 {
     MouseClick?.Invoke(this, new EventArgs());
     if (e.Button == MouseButton.Left)
     {
         LeftMouseClick?.Invoke(this, new EventArgs());
     }
     else if (e.Button == MouseButton.Right)
     {
         RightMouseClick?.Invoke(this, new EventArgs());
     }
 }
Exemple #2
0
        private void PictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            Point ctrlPoint = (sender as Control).Location;

            if (e.Button == MouseButtons.Left)
            {
                LeftMouseClick?.Invoke(sender, new MouseClickEventArgs(ctrlPoint.X, ctrlPoint.Y));
            }
            else if (e.Button == MouseButtons.Right)
            {
                RightMouseClick?.Invoke(sender, new MouseClickEventArgs(ctrlPoint.X, ctrlPoint.Y));
            }
        }
        /// <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
        }