Example #1
0
 /// <summary>
 /// 自己的帧逻辑更新函数
 /// </summary>
 private void MyFKGameUpdate(GameTime gameTime)
 {
     // 每帧将按键事件注入给自定义KeyboardState中
     FKKeyboardState.SetKeys(m_HookFormKeysList);
     m_HookFormKeysList.Clear();
     // 检查本帧的按键操作情况
     HandleInputInThisFrame();
     // 游戏状态更新
     m_GameState.Update(gameTime);
 }
Example #2
0
        /// <summary>
        /// 每帧更新键盘按键状态
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Update(GameTime gameTime)
        {
            _lastKeyboardState = _keyboardState;
            if (_bIsUseCustomKeyboardState)
            {
                _keyboardState = FKKeyboardState.GetState();
            }
            else
            {
                _keyboardState = Keyboard.GetState();
            }

            // 输出Debug信息
            DebugOutput();

            base.Update(gameTime);
        }
        /// <summary>
        /// 按键消息处理
        /// </summary>
        private void HandleInput()
        {
            var ms = Mouse.GetState();
            var ks = FKKeyboardState.GetState();

            // 若鼠标不在Windows管理区内,则不处理
            if (!Program.s_GameInstance.Window.ClientBounds.Contains(ms.Position))
            {
                System.Diagnostics.Debug.WriteLine("ClientBounds:  " + Program.s_GameInstance.Window.ClientBounds.ToString());
                System.Diagnostics.Debug.WriteLine("MousePosition:  " + ms.Position.ToString());
                return;
            }

            // 左键按下
            if (ms.LeftButton == ButtonState.Pressed && m_prevMouseState.LeftButton == ButtonState.Released)
            {
                System.Diagnostics.Debug.WriteLine("LefeButtonDown");
                m_nStartDragX = ms.X;
                m_nStartDragY = ms.Y;

                if (ks.IsKeyDown(Keys.LeftControl))
                {
                    m_bIsZoomingCamera = true;  //  按下Ctrl则缩放
                }
                else
                {
                    m_bIsDraggingCamera = true; // 仅仅是拖拽
                }
            }

            // 左键释放
            if (ms.LeftButton == ButtonState.Released && m_prevMouseState.LeftButton == ButtonState.Pressed)
            {
                System.Diagnostics.Debug.WriteLine("LefeButtonUp");
                m_bIsDraggingCamera = false;
                m_bIsZoomingCamera  = false;
            }

            // 处理拖拽
            if (m_bIsDraggingCamera)
            {
                float fTurnSpeed = 8f;

                float offsetX = ((ms.X - m_nStartDragX) * fTurnSpeed * 0.001f); // pitch degree
                float offsetY = ((ms.Y - m_nStartDragY) * fTurnSpeed * 0.001f); // yaw degree

                var angleRadian = m_Camera.AngleRadian;
                angleRadian.X -= offsetY;
                angleRadian.Y -= offsetX;

                angleRadian.Y = MathHelper.Clamp(angleRadian.Y, 0, MathHelper.Pi * 2);
                angleRadian.X = MathHelper.Clamp(angleRadian.X, -(MathHelper.PiOver2 - 0.1f), (MathHelper.PiOver2 - 0.1f));

                m_Camera.AngleRadian = angleRadian;

                m_nStartDragX = ms.X;
                m_nStartDragY = ms.Y;
            }
            // 处理缩放
            if (m_bIsZoomingCamera == true)
            {
                float speed   = 8f;
                float offsetY = ((ms.Y - m_nStartDragY) * speed * 0.001f);

                m_Camera.TargetDistance += offsetY;
                m_Camera.TargetDistance  = MathHelper.Clamp(m_Camera.TargetDistance, 1.0f, 10);

                m_nStartDragY = ms.Y;
            }
            // 鼠标滚轮
            var wheelDelta = m_prevMouseState.ScrollWheelValue - ms.ScrollWheelValue;

            if (wheelDelta != 0)
            {
                m_Camera.TargetDistance += (wheelDelta / 100.0f);
                m_Camera.TargetDistance  = MathHelper.Clamp(m_Camera.TargetDistance, 1.0f, 10);
            }

            // 记录上一次鼠标数据信息
            m_prevMouseState = ms;
        }