예제 #1
0
 //check if a key is pressed, and repeat it at the default repeat rate
 private bool KeyPressWithRepeat(Keys key, double elapsedTime)
 {
     if (CurrentKeyState.IsKeyDown(key))
     {
         if (IsKeyPressed(key))
         {
             return(true);                   //if the key has just been pressed, it automatically counts
         }
         keyTimes[key] -= elapsedTime;       //count down to next repeat
         double keyTime = keyTimes[key];     //get the time left
         if (keyTimes[key] <= 0)             //if the time has run out, repeat the letter
         {
             keyTimes[key] = repeatInterval; //reset the timer to the repeat interval
             return(true);
         }
         else
         {
             return(false);
         }
     }
     //if the key is not pressed, reset it's time to the first interval, which is usually longer
     else
     {
         keyTimes[key] = firstInterval;
         return(false);
     }
 }
예제 #2
0
 public virtual void OnTextInput(TextInputEventArgs e, GameTime gameTime, CurrentKeyState currentKeyState)
 {
     for (int i = 0; i < _widgets.Count; i++)
     {
         _widgets[i].OnTextInput(e, gameTime, currentKeyState);
     }
 }
예제 #3
0
        public void HandleInput()
        {
            CurrentKeyState = Keyboard.GetState();

            if (!(CurrentKeyState.IsKeyDown(Keys.Right)) && !(CurrentKeyState.IsKeyDown(Keys.Left)))
            {
                //dir = 0;
                IsLeftPressed  = false;
                IsRightPressed = false;
                if (PlayerState != PLAYER_STATE.IDLE && PlayerState != PLAYER_STATE.JUMP && PlayerState != PLAYER_STATE.FALLING)
                {
                    PlayerState = PLAYER_STATE.IDLE;
                }
            }

            if (CurrentKeyState.IsKeyDown(Keys.Right))
            {
                dir            = 1;
                IsLeftPressed  = false;
                IsRightPressed = true;
                if (Skeleton.FlipX == true)
                {
                    Skeleton.FlipX = false;
                }
                if (PlayerState != PLAYER_STATE.R_RUN && PlayerState != PLAYER_STATE.JUMP && PlayerState != PLAYER_STATE.FALLING)
                {
                    PlayerState = PLAYER_STATE.R_RUN;
                }
            }
            else if (CurrentKeyState.IsKeyDown(Keys.Left))
            {
                dir            = -1;
                IsLeftPressed  = true;
                IsRightPressed = false;
                if (Skeleton.FlipX == false)
                {
                    Skeleton.FlipX = true;
                }
                if (PlayerState != PLAYER_STATE.L_RUN && PlayerState != PLAYER_STATE.JUMP && PlayerState != PLAYER_STATE.FALLING)
                {
                    PlayerState = PLAYER_STATE.L_RUN;
                }
            }

            if (PlayerState != PLAYER_STATE.JUMP && PlayerState != PLAYER_STATE.FALLING &&
                CurrentKeyState.IsKeyDown(Keys.Z) && PrevKeyState.IsKeyUp(Keys.Z))
            {
                IsGrounded    = false;
                PlayerState   = PLAYER_STATE.JUMP;
                IsJumpPressed = true;
            }

            if (/*PlayerState == PLAYER_STATE.JUMP && PlayerState != PLAYER_STATE.FALLING
                 * && */CurrentKeyState.IsKeyUp(Keys.Z) && PrevKeyState.IsKeyDown(Keys.Z))
            {
                IsJumpPressed = false;
            }

            PrevKeyState = CurrentKeyState;
        }
예제 #4
0
        /// <summary>
        /// Takes keyboard input and returns certain characters as a string
        /// </summary>
        /// <param name="elapsedTime"></param>
        /// <returns></returns>
        private string GetStringFromKeyState(double elapsedTime)
        {
            bool shiftPressed = CurrentKeyState.IsKeyDown(Keys.LeftShift) || CurrentKeyState.IsKeyDown(Keys.RightShift);
            bool altPressed   = CurrentKeyState.IsKeyDown(Keys.LeftAlt) || CurrentKeyState.IsKeyDown(Keys.RightAlt);

            foreach (KeyBinding binding in KeyboardHelper.AmericanBindings)
            {
                if (KeyPressWithRepeat(binding.Key, elapsedTime))
                {
                    if (!shiftPressed && !altPressed)
                    {
                        return(binding.UnmodifiedString);
                    }
                    else if (shiftPressed && !altPressed)
                    {
                        return(binding.ShiftString);
                    }
                    else if (!shiftPressed && altPressed)
                    {
                        return(binding.AltString);
                    }
                    else if (shiftPressed && altPressed)
                    {
                        return(binding.ShiftAltString);
                    }
                }
            }

            return("");
        }
예제 #5
0
        public override void OnKeyDown(Keys key, GameTime gameTime, CurrentKeyState currentKeyState)
        {
            if (_menu.Focused)
            {
                _menu.OnKeyDown(key, gameTime, currentKeyState);
                return;
            }

            switch (key)
            {
            case Keys.W:
            case Keys.Up:
                Camera.OffsetPosition(new Vector2(0, -ScrollSpeed / Camera.Zoom) * gameTime.DeltaTime());
                break;

            case Keys.S:
            case Keys.Down:
                Camera.OffsetPosition(new Vector2(0, ScrollSpeed / Camera.Zoom) * gameTime.DeltaTime());
                break;

            case Keys.A:
            case Keys.Left:
                Camera.OffsetPosition(new Vector2(-ScrollSpeed / Camera.Zoom, 0) * gameTime.DeltaTime());
                break;

            case Keys.D:
            case Keys.Right:
                Camera.OffsetPosition(new Vector2(ScrollSpeed / Camera.Zoom, 0) * gameTime.DeltaTime());
                break;
            }
        }
예제 #6
0
        public override void OnKeyReleased(Keys key, GameTime gameTime, CurrentKeyState currentKeyState)
        {
            if (_menu.Focused)
            {
                _menu.OnKeyReleased(key, gameTime, currentKeyState);
                return;
            }

            if (key == Keys.OemTilde)
            {
                _showDebug = !_showDebug;
                _menu.GetFrame("debugFrame").Visible = _showDebug;
                _menu.GetFrame("debugFrame").Active  = _showDebug;
            }
            else if (key == Keys.Space)
            {
                //_lockCamera = !_lockCamera;
                Camera.CenterPosition(WorldManager.PlayerEntity.Position);
            }
            else if (key == Keys.P)
            {
                WorldManager.PlayerEntity.Inventory.AddResource(ResourceType.Metal, 100);
                WorldManager.PlayerEntity.Inventory.AddResource(ResourceType.Gas, 50);
            }
            else if (key == Keys.O)
            {
                WorldManager.PlayerEntity.BuildShip(ShipType.Miner);
            }
        }
예제 #7
0
 public void OnKeyDown(Keys key, GameTime gameTime, CurrentKeyState currentKeyState)
 {
     for (int i = 0; i < _widgets.Count; i++)
     {
         _widgets[i].OnKeyDown(key, gameTime, currentKeyState);
     }
 }
예제 #8
0
        public virtual void OnTextInput(TextInputEventArgs e, GameTime gameTime, CurrentKeyState currentKeyState)
        {
            if (!Active)
            {
                return;
            }

            SceneFrames.OnTextInput(e, gameTime, currentKeyState);
        }
예제 #9
0
        public virtual void OnKeyDown(Keys key, GameTime gameTime, CurrentKeyState currentKeyState)
        {
            if (!Active)
            {
                return;
            }

            SceneFrames.OnKeyDown(key, gameTime, currentKeyState);
        }
예제 #10
0
        public static bool IsButtonDown(Keys key)
        {
            bool down = false;

            if (CurrentKeyState.IsKeyDown(key))
            {
                down = true;
            }
            return(down);
        }
예제 #11
0
        public static bool WasButtonPressed(Keys key)
        {
            bool pressed = false;

            if (CurrentKeyState.IsKeyUp(key) && PreviousKeyState.IsKeyDown(key))
            {
                pressed = true;
            }
            return(pressed);
        }
예제 #12
0
        public override void OnKeyReleased(Keys key, GameTime gameTime, CurrentKeyState currentKeyState)
        {
            if (Menu.Focused)
            {
                Menu.OnKeyReleased(key, gameTime, currentKeyState);
                return;
            }

            if (key == Keys.OemTilde)
            {
#if DEBUG
                ShowDebug = !ShowDebug;
                Menu.GetFrame("debugFrame").Visible = ShowDebug;
                Menu.GetFrame("debugFrame").Active  = ShowDebug;
#endif
            }
            else if (key == Keys.Space)
            {
                //_lockCamera = !_lockCamera;
                Camera.CenterPosition(WorldManager.PlayerEntity.Position);
            }
            else if (key == Keys.P)
            {
#if DEBUG
                WorldManager.PlayerEntity.Inventory.AddAll(1000);
#endif
            }
            else if (key == Keys.B)
            {
                var productionFrame = Menu.GetFrame("frmProduction");

                productionFrame.Visible = !productionFrame.Visible;
                productionFrame.Active  = productionFrame.Visible;
            }
            else if (key == Keys.Tab)
            {
                var helpFrame = Menu.GetFrame("frmHelp");

                helpFrame.Visible = !helpFrame.Visible;
                helpFrame.Active  = helpFrame.Visible;
            }
            else if (key == Keys.U)
            {
                var upgradesFrame = Menu.GetFrame("frmUpgrades");

                upgradesFrame.Visible = !upgradesFrame.Visible;
                upgradesFrame.Active  = upgradesFrame.Visible;
            }
        } // OnKeyReleased
예제 #13
0
        public override void OnKeyPressed(Keys key, GameTime gameTime, CurrentKeyState currentKeyState)
        {
            _menu.OnKeyPressed(key, gameTime, currentKeyState);

            switch (key)
            {
            case Keys.Escape:
            case Keys.Space:
            case Keys.Enter:
            case Keys.Back:
            {
                _nextState = GameStateType.Menu;
            }
            break;
            }
        }
예제 #14
0
        public static bool GetKeyUp(Keys Key, KeyboardFlags keyboardFlags = KeyboardFlags.ANY)
        {
            bool BaseCond = CurrentKeyState.IsKeyUp(Key) && LastKeyState.IsKeyDown(Key);

            switch (keyboardFlags)
            {
            case KeyboardFlags.ANY:
                return(BaseCond);

            case KeyboardFlags.SOLO:
                return(BaseCond && Keyboard.GetState().IsKeyUp(Keys.LeftAlt) && Keyboard.GetState().IsKeyUp(Keys.LeftShift) && Keyboard.GetState().IsKeyUp(Keys.LeftControl));

            case KeyboardFlags.SHIFT:
                return(BaseCond && Keyboard.GetState().IsKeyDown(Keys.LeftShift));

            case KeyboardFlags.ALT:
                return(BaseCond && Keyboard.GetState().IsKeyDown(Keys.LeftAlt));

            default:
                return(BaseCond && Keyboard.GetState().IsKeyDown(Keys.LeftControl));
            }
        }
예제 #15
0
 public void OnTextInput(TextInputEventArgs e, GameTime gameTime, CurrentKeyState currentKeyState)
 {
 }
예제 #16
0
 public void OnKeyDown(Keys key, GameTime gameTime, CurrentKeyState currentKeyState)
 {
 }
예제 #17
0
 public void OnKeyReleased(Keys key, GameTime gameTime, CurrentKeyState currentKeyState)
 {
 }
예제 #18
0
 public override void OnKeyPressed(Keys key, GameTime gameTime, CurrentKeyState currentKeyState)
 {
 }
예제 #19
0
 public override void OnKeyReleased(Keys key, GameTime gameTime, CurrentKeyState currentKeyState)
 {
     _nextGameState = (int)GameStateType.Menu;
 }
예제 #20
0
 //check if the key has just been pressed
 private bool IsKeyPressed(Keys key)
 {
     return(CurrentKeyState.IsKeyDown(key) && !LastKeyState.IsKeyDown(key));
 }
예제 #21
0
 static bool IsKeyReleased(QKeyStates keyState)
 {
     return(CurrentKeyState.IsKeyUp((Keys)keyState) && PreviousKeyState.IsKeyDown((Keys)keyState));
 }
예제 #22
0
 public override void OnTextInput(TextInputEventArgs e, GameTime gameTime, CurrentKeyState currentKeyState)
 {
     _menu.OnTextInput(e, gameTime, currentKeyState);
 }
예제 #23
0
 public virtual void OnKeyDown(Keys key, GameTime gameTime, CurrentKeyState currentKeyState)
 {
     Widgets.OnKeyDown(key, gameTime, currentKeyState);
 }
예제 #24
0
 public virtual void OnTextInput(TextInputEventArgs e, GameTime gameTime, CurrentKeyState currentKeyState)
 {
     Widgets.OnTextInput(e, gameTime, currentKeyState);
 }
예제 #25
0
 public static bool KeyPressed(Keys key)
 {
     return(CurrentKeyState.IsKeyDown(key));
 }
예제 #26
0
 public static bool KeyDown(Keys key)
 {
     return(LastKeyState.IsKeyUp(key) && CurrentKeyState.IsKeyDown(key));
 }
예제 #27
0
 static bool IsAnyKeyHeld()
 {
     return(CurrentKeyState.GetPressedKeys().Length > 0 && PreviousKeyState.GetPressedKeys().Length > 0);
 }
예제 #28
0
 public override void OnKeyDown(Keys key, GameTime gameTime, CurrentKeyState currentKeyState)
 {
     _menu.OnKeyDown(key, gameTime, currentKeyState);
 }
예제 #29
0
 static bool IsNoKey()
 {
     return(CurrentKeyState.GetPressedKeys().Length == 0 && PreviousKeyState.GetPressedKeys().Length == 0);
 }
예제 #30
0
 static bool IsKeyHeld(QKeyStates keyState)
 {
     return(CurrentKeyState.IsKeyDown((Keys)keyState));
 }