Example #1
0
        protected override void Update(GameTime time)
        {
            if (_first)
            {
                // WPF window is not yet loaded when initialize is called, thus SetCursor does not work there, instead call it in the first update
                _mouse.SetCursor(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height / 2);
                _first = false;
            }
            // every update we can now query the keyboard & mouse for our WpfGame
            var mouseState = _mouse.GetState();

            if (mouseState.X == 0 && mouseState.Y == 0)
            {
                // WPF returns empty mouseposition at startup, until window is at least once rendered. only then will it return proper mouse positions
                // so we deferr updating until we get a valid mousestate
                return;
            }
            var keyboardState = _keyboard.GetState();

            if (!IsActive())
            {
                return;
            }
            if (keyboardState.IsKeyDown(Keys.Escape))
            {
                Environment.Exit(0);
            }
            _terrainScene.Update(time, mouseState, keyboardState, (x, y) => _mouse.SetCursor(x, y));
        }
Example #2
0
        public void Update(GameTime gameTime)
        {
            if (Mouse == null)
            {
                return;
            }

            var mouseState    = Mouse.GetState();
            var keyboardState = Keyboard.GetState();

            if (!Game.IsActive)
            {
                PrevMouseState = mouseState;
                return;
            }
            if (keyboardState.IsKeyDown(Keys.W))
            {
                Position += Dir * Speed;
            }
            if (keyboardState.IsKeyDown(Keys.S))
            {
                Position -= Dir * Speed;
            }
            if (keyboardState.IsKeyDown(Keys.A))
            {
                Position += Vector3.Cross(Up, Dir) * Speed;
            }
            if (keyboardState.IsKeyDown(Keys.D))
            {
                Position -= Vector3.Cross(Up, Dir) * Speed;
            }
            if (keyboardState.IsKeyDown(Keys.Space))
            {
                Position += Up * Speed;
            }

            // camera speed control
            if (mouseState.ScrollWheelValue != PrevScrollWheelValue)
            {
                var diff = mouseState.ScrollWheelValue - PrevScrollWheelValue;
                if (diff >= 0)
                {
                    Speed *= SpeedMod;
                }
                else
                {
                    Speed /= SpeedMod;
                }

                PrevScrollWheelValue = mouseState.ScrollWheelValue;
            }

            if (mouseState.RightButton == ButtonState.Pressed)
            {
                if (PrevMouseState.RightButton == ButtonState.Pressed)
                {
                    // yaw / x-rotation
                    Dir = Vector3.Transform(Dir, Matrix.CreateFromAxisAngle(Up,
                                                                            -MathHelper.PiOver4 / 160 * (mouseState.X - centerX)));

                    // pitch / y-rotation
                    Dir = Vector3.Transform(Dir, Matrix.CreateFromAxisAngle(Vector3.Cross(Up, Dir),
                                                                            MathHelper.PiOver4 / 160 * (mouseState.Y - centerY)));
                }
                else
                {
                    System.Windows.Input.Mouse.OverrideCursor = Cursors.None;
                }
                // there is a delay here, so Mouse.GetState() won't be immediately affected
                Mouse.SetCursor(centerX, centerY);
            }
            else if (PrevMouseState.RightButton == ButtonState.Pressed)
            {
                System.Windows.Input.Mouse.OverrideCursor = Cursors.Arrow;
            }

            PrevMouseState = mouseState;

            Dir.Normalize();

            CreateLookAt();

            //Console.WriteLine("Camera pos: " + GameView.Instance.Render.Camera.Position);
            //Console.WriteLine("Camera dir: " + GameView.Instance.Render.Camera.Dir);
        }