コード例 #1
0
        protected override void Update(GameTime gameTime)
        {
            if (frameCount > 0)
            {
                frameRate = frameCount / gameTime.TotalGameTime.TotalSeconds;
            }

            if (currentState != null && propertyBag != null)
            {
                // Call OnUpdate.
                string newState = currentState.OnUpdate(gameTime, Keyboard.GetState(), Mouse.GetState());
                if (newState != null)
                {
                    ChangeState(newState);
                }

                // Check for mouse events.
                MouseState msNew = Mouse.GetState();
                if (WindowHasFocus())
                {
                    if (msOld.LeftButton == ButtonState.Released && msNew.LeftButton == ButtonState.Pressed)
                    {
                        currentState.OnMouseDown(MouseButton.LeftButton, msNew.X, msNew.Y);
                    }
                    if (msOld.MiddleButton == ButtonState.Released && msNew.MiddleButton == ButtonState.Pressed)
                    {
                        currentState.OnMouseDown(MouseButton.MiddleButton, msNew.X, msNew.Y);
                    }
                    if (msOld.RightButton == ButtonState.Released && msNew.RightButton == ButtonState.Pressed)
                    {
                        currentState.OnMouseDown(MouseButton.RightButton, msNew.X, msNew.Y);
                    }
                    if (msOld.LeftButton == ButtonState.Pressed && msNew.LeftButton == ButtonState.Released)
                    {
                        currentState.OnMouseUp(MouseButton.LeftButton, msNew.X, msNew.Y);
                    }
                    if (msOld.MiddleButton == ButtonState.Pressed && msNew.MiddleButton == ButtonState.Released)
                    {
                        currentState.OnMouseUp(MouseButton.MiddleButton, msNew.X, msNew.Y);
                    }
                    if (msOld.RightButton == ButtonState.Pressed && msNew.RightButton == ButtonState.Released)
                    {
                        currentState.OnMouseUp(MouseButton.RightButton, msNew.X, msNew.Y);
                    }
                    if (msOld.ScrollWheelValue != msNew.ScrollWheelValue)
                    {
                        currentState.OnMouseScroll(msNew.ScrollWheelValue - msOld.ScrollWheelValue);
                    }
                }
                msOld = msNew;
            }

            base.Update(gameTime);
        }
コード例 #2
0
ファイル: StateMachine.cs プロジェクト: Kleadron/infiniminer
        protected override void Update(GameTime gameTime)
        {
            if (frameCount > 0)
            {
                frameRate = frameCount / gameTime.TotalGameTime.TotalSeconds;
            }

#if SDL2
            KeyboardState kbState = Keyboard.GetState();
            for (int i = 0; i <= 255; i++)
            {
                Keys key = (Keys)i;
                if (kbState.IsKeyDown(key) && kbStateOld.IsKeyUp(key))
                {
                    EventInput_KeyDown(null, new EventInput.KeyEventArgs(key));

                    // this is not how it should be done but I don't care right now
                    //EventInput_CharEntered(null, new EventInput.CharacterEventArgs((char)i, 0));
                    // screw it, fixing it
                }
                else if (kbState.IsKeyUp(key) && kbStateOld.IsKeyDown(key))
                {
                    EventInput_KeyUp(null, new EventInput.KeyEventArgs(key));
                }
            }
            kbStateOld = kbState;
#endif

            if (currentState != null && propertyBag != null)
            {
                // Call OnUpdate.
                string newState = currentState.OnUpdate(gameTime, Keyboard.GetState(), Mouse.GetState());
                if (newState != null)
                {
                    ChangeState(newState);
                }

                // Check for mouse events.
                MouseState msNew = Mouse.GetState();
                if (WindowHasFocus())
                {
                    if (msOld.LeftButton == ButtonState.Released && msNew.LeftButton == ButtonState.Pressed)
                    {
                        currentState.OnMouseDown(MouseButton.LeftButton, msNew.X, msNew.Y);
                    }
                    if (msOld.MiddleButton == ButtonState.Released && msNew.MiddleButton == ButtonState.Pressed)
                    {
                        currentState.OnMouseDown(MouseButton.MiddleButton, msNew.X, msNew.Y);
                    }
                    if (msOld.RightButton == ButtonState.Released && msNew.RightButton == ButtonState.Pressed)
                    {
                        currentState.OnMouseDown(MouseButton.RightButton, msNew.X, msNew.Y);
                    }
                    if (msOld.LeftButton == ButtonState.Pressed && msNew.LeftButton == ButtonState.Released)
                    {
                        currentState.OnMouseUp(MouseButton.LeftButton, msNew.X, msNew.Y);
                    }
                    if (msOld.MiddleButton == ButtonState.Pressed && msNew.MiddleButton == ButtonState.Released)
                    {
                        currentState.OnMouseUp(MouseButton.MiddleButton, msNew.X, msNew.Y);
                    }
                    if (msOld.RightButton == ButtonState.Pressed && msNew.RightButton == ButtonState.Released)
                    {
                        currentState.OnMouseUp(MouseButton.RightButton, msNew.X, msNew.Y);
                    }
                    if (msOld.ScrollWheelValue != msNew.ScrollWheelValue)
                    {
                        currentState.OnMouseScroll(msNew.ScrollWheelValue - msOld.ScrollWheelValue);
                    }
                }
                msOld = msNew;
            }

            base.Update(gameTime);
        }
コード例 #3
0
        protected override void Update(GameTime gameTime)
        {
            if (frameCount > 0)
            {
                frameRate = frameCount / gameTime.TotalRealTime.TotalSeconds;
            }

            if (currentState != null && propertyBag != null)
            {
                // Call OnUpdate.
                string newState = currentState.OnUpdate(gameTime, Keyboard.GetState(), Mouse.GetState());
                if (newState != null)
                {
                    ChangeState(newState);
                }

                // Check for keyboard events.
                KeyboardState           keyState    = Keyboard.GetState();
                Dictionary <Keys, bool> keysDownNow = new Dictionary <Keys, bool>();
                foreach (Keys k in keyState.GetPressedKeys())
                {
                    keysDownNow.Add(k, true);
                }
                if (WindowHasFocus())
                {
                    foreach (Keys k in keysDownNow.Keys)
                    {
                        if (!keysDown.ContainsKey(k))
                        {
                            currentState.OnKeyDown(k);
                        }
                    }
                    foreach (Keys k in keysDown.Keys)
                    {
                        if (!keysDownNow.ContainsKey(k))
                        {
                            currentState.OnKeyUp(k);
                        }
                    }
                }
                keysDown = keysDownNow;

                // Check for mouse events.
                MouseState msNew = Mouse.GetState();
                if (WindowHasFocus())
                {
                    if (msOld.LeftButton == ButtonState.Released && msNew.LeftButton == ButtonState.Pressed)
                    {
                        currentState.OnMouseDown(MouseButton.LeftButton, msNew.X, msNew.Y);
                    }
                    if (msOld.MiddleButton == ButtonState.Released && msNew.MiddleButton == ButtonState.Pressed)
                    {
                        currentState.OnMouseDown(MouseButton.MiddleButton, msNew.X, msNew.Y);
                    }
                    if (msOld.RightButton == ButtonState.Released && msNew.RightButton == ButtonState.Pressed)
                    {
                        currentState.OnMouseDown(MouseButton.RightButton, msNew.X, msNew.Y);
                    }
                    if (msOld.LeftButton == ButtonState.Pressed && msNew.LeftButton == ButtonState.Released)
                    {
                        currentState.OnMouseUp(MouseButton.LeftButton, msNew.X, msNew.Y);
                    }
                    if (msOld.MiddleButton == ButtonState.Pressed && msNew.MiddleButton == ButtonState.Released)
                    {
                        currentState.OnMouseUp(MouseButton.MiddleButton, msNew.X, msNew.Y);
                    }
                    if (msOld.RightButton == ButtonState.Pressed && msNew.RightButton == ButtonState.Released)
                    {
                        currentState.OnMouseUp(MouseButton.RightButton, msNew.X, msNew.Y);
                    }
                    if (msOld.ScrollWheelValue != msNew.ScrollWheelValue)
                    {
                        currentState.OnMouseScroll(msNew.ScrollWheelValue - msOld.ScrollWheelValue);
                    }
                }
                msOld = msNew;
            }

            base.Update(gameTime);
        }