Esempio n. 1
0
        /// <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 = Mouse.GetState();

                    // Mausposition anhand des Mouse Modes ermitteln
                    Point mousePosition = mouse.Position;
                    if (MouseMode == MouseMode.Captured)
                    {
                        mousePosition = new Point(
                            mousePosition.X - (GraphicsDevice.Viewport.Width / 2),
                            mousePosition.Y - (GraphicsDevice.Viewport.Height / 2));
                    }

                    // Mouse Move
                    if (mousePosition != lastMousePosition)
                    {
                        MouseEventArgs moveArgs = new MouseEventArgs()
                        {
                            MouseMode      = MouseMode,
                            GlobalPosition = mousePosition,
                            LocalPosition  = mousePosition,
                        };

                        root.InternalMouseMove(moveArgs);
                        if (!moveArgs.Handled && MouseMove != null)
                        {
                            MouseMove(moveArgs);
                        }

                        // Start Drag Handling
                        if (mouse.LeftButton == ButtonState.Pressed &&
                            DraggingArgs == null)
                        {
                            DraggingArgs = new DragEventArgs()
                            {
                                GlobalPosition = mousePosition,
                                LocalPosition  = mousePosition,
                            };

                            draggingId = null;

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

                        // Drop move
                        if (mouse.LeftButton == ButtonState.Pressed &&
                            DraggingArgs != null &&
                            draggingId == null &&
                            DraggingArgs.Handled)
                        {
                            DragEventArgs args = new DragEventArgs()
                            {
                                GlobalPosition = mousePosition,
                                LocalPosition  = mousePosition,
                                Content        = DraggingArgs.Content,
                                Icon           = DraggingArgs.Icon,
                                Sender         = DraggingArgs.Sender
                            };

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

                    // Linke Maustaste
                    if (mouse.LeftButton == ButtonState.Pressed)
                    {
                        if (!lastLeftMouseButtonPressed)
                        {
                            MouseEventArgs leftDownArgs = new MouseEventArgs
                            {
                                MouseMode      = MouseMode,
                                GlobalPosition = mousePosition,
                                LocalPosition  = mousePosition
                            };

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

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

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

                            // Linke Maustaste wurde losgelassen
                            MouseEventArgs leftClickArgs = new MouseEventArgs
                            {
                                MouseMode      = MouseMode,
                                GlobalPosition = mousePosition,
                                LocalPosition  = mousePosition
                            };

                            root.InternalLeftMouseClick(leftClickArgs);
                            if (!leftClickArgs.Handled && LeftMouseClick != null)
                            {
                                LeftMouseClick(leftClickArgs);
                            }

                            if (lastLeftClick.HasValue &&
                                gameTime.TotalGameTime - lastLeftClick.Value < TimeSpan.FromMilliseconds(DoubleClickDelay))
                            {
                                // Double Left Click
                                MouseEventArgs leftDoubleClickArgs = new MouseEventArgs
                                {
                                    MouseMode      = MouseMode,
                                    GlobalPosition = mousePosition,
                                    LocalPosition  = mousePosition
                                };

                                root.InternalLeftMouseDoubleClick(leftDoubleClickArgs);
                                if (!leftDoubleClickArgs.Handled && LeftMouseDoubleClick != null)
                                {
                                    LeftMouseDoubleClick(leftDoubleClickArgs);
                                }

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

                            // Mouse Up
                            MouseEventArgs leftUpArgs = new MouseEventArgs
                            {
                                MouseMode      = MouseMode,
                                GlobalPosition = mousePosition,
                                LocalPosition  = mousePosition
                            };

                            root.InternalLeftMouseUp(leftUpArgs);
                            if (!leftUpArgs.Handled && LeftMouseUp != null)
                            {
                                LeftMouseUp(leftUpArgs);
                            }
                        }
                        lastLeftMouseButtonPressed = false;
                    }

                    // Rechte Maustaste
                    if (mouse.RightButton == ButtonState.Pressed)
                    {
                        if (!lastRightMouseButtonPressed)
                        {
                            // Rechte Maustaste neu gedrückt
                            MouseEventArgs rightDownArgs = new MouseEventArgs
                            {
                                MouseMode      = MouseMode,
                                GlobalPosition = mousePosition,
                                LocalPosition  = mousePosition
                            };

                            root.InternalRightMouseDown(rightDownArgs);
                            if (!rightDownArgs.Handled && RightMouseDown != null)
                            {
                                RightMouseDown(rightDownArgs);
                            }
                        }
                        lastRightMouseButtonPressed = true;
                    }
                    else
                    {
                        if (lastRightMouseButtonPressed)
                        {
                            // Rechte Maustaste losgelassen
                            MouseEventArgs rightClickArgs = new MouseEventArgs
                            {
                                MouseMode      = MouseMode,
                                GlobalPosition = mousePosition,
                                LocalPosition  = mousePosition
                            };
                            root.InternalRightMouseClick(rightClickArgs);
                            if (!rightClickArgs.Handled && RightMouseClick != null)
                            {
                                RightMouseClick(rightClickArgs);
                            }

                            if (lastRightClick.HasValue &&
                                gameTime.TotalGameTime - lastRightClick.Value < TimeSpan.FromMilliseconds(DoubleClickDelay))
                            {
                                // Double Left Click
                                MouseEventArgs rightDoubleClickArgs = new MouseEventArgs
                                {
                                    MouseMode      = MouseMode,
                                    GlobalPosition = mousePosition,
                                    LocalPosition  = mousePosition
                                };

                                root.InternalRightMouseDoubleClick(rightDoubleClickArgs);
                                if (!rightDoubleClickArgs.Handled && RightMouseDoubleClick != null)
                                {
                                    RightMouseDoubleClick(rightDoubleClickArgs);
                                }

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

                            MouseEventArgs rightUpArgs = new MouseEventArgs
                            {
                                MouseMode      = MouseMode,
                                GlobalPosition = mousePosition,
                                LocalPosition  = mousePosition
                            };
                            root.InternalRightMouseUp(rightUpArgs);
                            if (!rightUpArgs.Handled && RightMouseUp != null)
                            {
                                RightMouseUp(rightUpArgs);
                            }
                        }
                        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 != null)
                        {
                            MouseScroll(scrollArgs);
                        }

                        lastMouseWheelValue = mouse.ScrollWheelValue;
                    }

                    // Potentieller Positionsreset
                    if (MouseMode == MouseMode.Free)
                    {
                        lastMousePosition = mouse.Position;
                    }
                    else if (mousePosition.X != 0 || mousePosition.Y != 0)
                    {
                        Mouse.SetPosition(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height / 2);
                    }
                }

                #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;
                    foreach (Keys key in Enum.GetValues(typeof(Keys)))
                    {
                        if (keyboard.IsKeyDown(key))
                        {
                            if (!pressedKeys.ContainsKey(key))
                            {
                                // Taste ist neu

                                args = new KeyEventArgs()
                                {
                                    Key   = key,
                                    Shift = shift,
                                    Ctrl  = ctrl,
                                    Alt   = alt
                                };
                                root.InternalKeyDown(args);

                                if (!args.Handled)
                                {
                                    if (KeyDown != null)
                                    {
                                        KeyDown(args);
                                    }
                                }

                                args = new KeyEventArgs()
                                {
                                    Key   = key,
                                    Shift = shift,
                                    Ctrl  = ctrl,
                                    Alt   = alt
                                };
                                root.InternalKeyPress(args);
                                pressedKeys.Add(key, gameTime.TotalGameTime.TotalMilliseconds + 500);



                                // 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[key] <= gameTime.TotalGameTime.TotalMilliseconds)
                                {
                                    args = new KeyEventArgs()
                                    {
                                        Key   = key,
                                        Shift = shift,
                                        Ctrl  = ctrl,
                                        Alt   = alt
                                    };
                                    root.InternalKeyPress(args);
                                    if (!args.Handled)
                                    {
                                        if (KeyPress != null)
                                        {
                                            KeyPress(args);
                                        }
                                    }
                                    pressedKeys[key] = gameTime.TotalGameTime.TotalMilliseconds + 50;
                                }
                            }
                        }
                        else
                        {
                            if (pressedKeys.ContainsKey(key))
                            {
                                // Taste losgelassen
                                args = new KeyEventArgs()
                                {
                                    Key   = key,
                                    Shift = shift,
                                    Ctrl  = ctrl,
                                    Alt   = alt
                                };
                                root.InternalKeyUp(args);
                                pressedKeys.Remove(key);

                                if (!args.Handled)
                                {
                                    if (KeyUp != null)
                                    {
                                        KeyUp(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

            string screentitle = ActiveScreen != null ? ActiveScreen.Title : string.Empty;
            string windowtitle = TitlePrefix + (string.IsNullOrEmpty(screentitle) ? "" : " - " + screentitle);

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

            #endregion
        }