Ejemplo n.º 1
0
 public void EventInput_KeyUp(object sender, EventInput.KeyEventArgs e)
 {
     if (currentState != null)
     {
         currentState.OnKeyUp(e.KeyCode);
     }
 }
Ejemplo n.º 2
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);
        }