Input State

lxXTaCoXxl 0 Tallied Votes 116 Views Share

I realize I haven't released anything in a while so I figure I'll just type a basic set of things for you all to use or learn from. All of the following requires XNA Framework, but can be easily ported over to .NET Framework for use with forms applications. The Drag and Drop class is NOT finished nor will it be by my hand for about a week considering I have been busy with getting enlisted into the Army to pay for college and go off to see the world. The Input State class that is included is mainly to show you how to check for input on the XBOX 360 console, and can also be used for quicker navigation through forms apps using the keyboard instead of just the mouse, like if you were making a game for example?

Hope you all enjoy the snippet and maybe some of you Indie Game Developers can get some use from it?

Jamie

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace Dev9.Input {
    public class InputState {
        private static KeyboardState previousKey;
        private static KeyboardState currentKey;

        private static GamePadState previousPad;
        private static GamePadState currentPad;

        public static Vector2 LeftThumbstick = new Vector2(0, 0);
        public static Vector2 RightThumbstick = new Vector2(0, 0);

        public static void Update() {
            previousKey = currentKey;
            previousPad = currentPad;

            currentKey = Keyboard.GetState();
            currentPad = GamePad.GetState(PlayerIndex.One);

            LeftThumbstick = Vector2.Normalize(currentPad.ThumbSticks.Left);
            RightThumbstick = Vector2.Normalize(currentPad.ThumbSticks.Right);
        }

        public static bool IsNewKeyPress(Keys Key) {
            return previousKey.IsKeyUp(Key) && currentKey.IsKeyDown(Key);
        }

        public static bool IsNewButtonPress(Buttons Button) {
            return previousPad.IsButtonUp(Button) && currentPad.IsButtonDown(Button);
        }

        public static bool MenuUp {
            get {
                return IsNewKeyPress(Keys.Up) || IsNewButtonPress(Buttons.DPadUp) || IsNewButtonPress(Buttons.LeftThumbstickUp);
            }
        }

        public static bool MenuDown {
            get {
                return IsNewKeyPress(Keys.Down) || IsNewButtonPress(Buttons.DPadDown) || IsNewButtonPress(Buttons.LeftThumbstickDown);
            }
        }

        public static bool MenuLeft {
            get {
                return IsNewKeyPress(Keys.Left) || IsNewButtonPress(Buttons.DPadLeft) || IsNewButtonPress(Buttons.LeftThumbstickLeft);
            }
        }

        public static bool MenuRight {
            get {
                return IsNewKeyPress(Keys.Right) || IsNewButtonPress(Buttons.DPadRight) || IsNewButtonPress(Buttons.LeftThumbstickRight);
            }
        }

        public static bool Accept {
            get {
                return IsNewKeyPress(Keys.A) || IsNewKeyPress(Keys.Space) || IsNewKeyPress(Keys.Enter) || IsNewButtonPress(Buttons.A);
            }
        }

        public static bool Cancel {
            get {
                return IsNewKeyPress(Keys.B) || IsNewKeyPress(Keys.Delete) || IsNewButtonPress(Buttons.B);
            }
        }

        public static bool Y {
            get {
                return IsNewKeyPress(Keys.Y) || IsNewButtonPress(Buttons.Y);
            }
        }

        public static bool X {
            get {
                return IsNewKeyPress(Keys.X) || IsNewButtonPress(Buttons.X);
            }
        }

        public static bool LeftBumper {
            get {
                return (IsNewKeyPress(Keys.LeftShift) && IsNewKeyPress(Keys.B)) || IsNewButtonPress(Buttons.LeftShoulder);
            }
        }

        public static bool RightBumper {
            get {
                return (IsNewKeyPress(Keys.RightShift) && IsNewKeyPress(Keys.B)) || IsNewButtonPress(Buttons.RightShoulder);
            }
        }

        public static bool Start {
            get {
                return (IsNewKeyPress(Keys.RightShift) && IsNewKeyPress(Keys.S)) || IsNewButtonPress(Buttons.Start);
            }
        }

        public static bool Select {
            get {
                return (IsNewKeyPress(Keys.LeftShift) && IsNewKeyPress(Keys.S)) || IsNewButtonPress(Buttons.Back);
            }
        }

        public static bool EmergencyExit {
            get {
                return (IsNewKeyPress(Keys.Escape) && IsNewKeyPress(Keys.LeftShift)) ||
                    (IsNewButtonPress(Buttons.LeftTrigger) && IsNewButtonPress(Buttons.Back));
            }
        }
    }
	
	    public class DevMouse {
        private static MouseState previousState;
        private static MouseState currentState;

        private static Vector2 location;
        public static Vector2 Location { get { return location; } }

        private static Vector2 click;
        public static Vector2 ClickLocation { get { return click; } }

        private static bool IsClick = false;

        private static Rectangle bounds;
        public static Rectangle Mouse50x50Box { get { return bounds; } }

        public static void Update() {
            previousState = currentState;
            currentState = Mouse.GetState();

            location = new Vector2(currentState.X, currentState.Y);

            if (IsLeftClick || IsRightClick) {
                click = new Vector2(currentState.X, currentState.Y);
                IsLeftClick = false;
                IsRightClick = false;
            }

            bounds = new Rectangle(currentState.X, currentState.Y, 50, 50);
        }

        public static bool IsLeftClick {
            get {
                return previousState.LeftButton == ButtonState.Pressed && currentState.LeftButton == ButtonState.Released;
            }
            private set;
        }

        public static bool IsRightClick {
            get {
                return previousState.RightButton == ButtonState.Pressed && currentState.RightButton == ButtonState.Released;
            }
            private set;
        }

        public static bool IsLeftDown {
            get {
                return currentState.LeftButton == ButtonState.Pressed;
            }
        }

        public static bool IsRightDown {
            get {
                return currentState.RightButton == ButtonState.Pressed;
            }
        }
    }
	
	    public class DragAndDrop {
        private static int activeID = 0;
        public static int ActiveID { get { return ActiveID; } }

        private static int lastID = 0;
        public static int LastID { get { return LastID; } }

        public static Rectangle GripAndMove(int ObjectID, Rectangle ObjectBounds) {
            lastID = activeID;
            activeID = ObjectID;

            Rectangle x = ObjectBounds;

            if (DevMouse.IsLeftDown && DevMouse.Mouse50x50Box.Intersects(ObjectBounds)) {
                x = new Rectangle((int)DevMouse.Location.X, (int)DevMouse.Location.Y, ObjectBounds.Width, ObjectBounds.Height);
            }

            return x;
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.