All pastes #503819 Raw Edit

Stuff

public text v1 · immutable
·published 2007-05-23 01:36 UTC
rendered paste body
#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion

namespace DoraMataEngine.Input
{
    /// <summary>
    /// Input manager class.
    /// </summary>
    public class InputManagerDM
    {
        #region Members

        /// <summary>
        /// Keeps track of the current game pads states.
        /// </summary>
        private GamePadState[] currentGamePadsState;

        /// <summary>
        /// Keeps track of the last game pads states.
        /// </summary>
        private GamePadState[] lastGamePadsState;

        /// <summary>
        /// Keeps track of the current keyboard state.
        /// </summary>
        private KeyboardState currentKeyboardState;

        /// <summary>
        /// Keeps track of the last keyboard state.
        /// </summary>
        private KeyboardState lastKeyboardState;

        /// <summary>
        /// Keeps track of the current mouse state.
        /// </summary>
        private MouseState currentMouseState;

        /// <summary>
        /// Keeps track of the last mouse state.
        /// </summary>
        private MouseState lastMouseState;

        #endregion

        #region Properties

        /// <summary>
        /// Current keyboard state.
        /// </summary>
        public KeyboardState CurrentKeyboardState
        {
            get
            {
                return currentKeyboardState;
            }
        }

        /// <summary>
        /// Last keyboard state.
        /// </summary>
        public KeyboardState LastKeyboardState
        {
            get
            {
                return lastKeyboardState;
            }
        }

        /// <summary>
        /// Current mouse state.
        /// </summary>
        public MouseState CurrentMouseState
        {
            get
            {
                return currentMouseState;
            }
        }

        /// <summary>
        /// Last mouse state.
        /// </summary>
        public MouseState LastMouseState
        {
            get
            {
                return lastMouseState;
            }
        }

        /// <summary>
        /// Current game pads state.
        /// </summary>
        public GamePadState[] CurrentGamePadsState
        {
            get
            {
                return currentGamePadsState;
            }
        }

        /// <summary>
        /// Last game pads state.
        /// </summary>
        public GamePadState[] LastGamePadsState
        {
            get
            {
                return lastGamePadsState;
            }
        }

        /// <summary>
        /// Checks for a "menu up" input action.
        /// </summary>
        public bool MenuUpInput
        {
            get
            {
                for (int i = 0; i < 4; ++i)
                {
                    if (((currentGamePadsState[i].DPad.Up == ButtonState.Pressed) &&
                        (lastGamePadsState[i].DPad.Up == ButtonState.Released)) ||
                        ((currentGamePadsState[i].ThumbSticks.Left.Y > 0) &&
                        (lastGamePadsState[i].ThumbSticks.Left.Y <= 0)))
                    {
                        return true;
                    }
                }

                if (HasKeyJustBeenPressed(Keys.Up))
                {
                    return true;
                }

                return false;
            }
        }

        /// <summary>
        /// Checks for a "menu down" input action.
        /// </summary>
        public bool MenuDownInput
        {
            get
            {
                for (int i = 0; i < 4; ++i)
                {
                    if (((currentGamePadsState[i].DPad.Down == ButtonState.Pressed) &&
                        (lastGamePadsState[i].DPad.Down == ButtonState.Released)) ||
                        ((currentGamePadsState[i].ThumbSticks.Left.Y < 0) &&
                        (lastGamePadsState[i].ThumbSticks.Left.Y >= 0)))
                    {
                        return true;
                    }
                }

                if (HasKeyJustBeenPressed(Keys.Down))
                {
                    return true;
                }

                return false;
            }
        }

        /// <summary>
        /// Checks for a "menu select" input action.
        /// </summary>
        public bool MenuSelectInput
        {
            get
            {
                for (int i = 0; i < 4; ++i)
                {
                    if (((currentGamePadsState[i].Buttons.A == ButtonState.Pressed) &&
                        (lastGamePadsState[i].Buttons.A == ButtonState.Released)) ||
                        ((currentGamePadsState[i].Buttons.Start == ButtonState.Pressed) &&
                        (lastGamePadsState[i].Buttons.Start == ButtonState.Released)))
                    {
                        return true;
                    }
                }

                if (HasKeyJustBeenPressed(Keys.Enter))
                {
                    return true;
                }

                if (HasKeyJustBeenPressed(Keys.Space))
                {
                    return true;
                }

                return false;
            }
        }

        /// <summary>
        /// Checks for a "menu cancel" input action.
        /// </summary>
        public bool MenuCancelInput
        {
            get
            {
                for (int i = 0; i < 4; ++i)
                {
                    if (((currentGamePadsState[i].Buttons.B == ButtonState.Pressed) &&
                        (lastGamePadsState[i].Buttons.B == ButtonState.Released)) ||
                        ((currentGamePadsState[i].Buttons.Back == ButtonState.Pressed) &&
                        (lastGamePadsState[i].Buttons.Back == ButtonState.Released)))
                    {
                        return true;
                    }
                }
                
                if (HasKeyJustBeenPressed(Keys.Escape))
                {
                    return true;
                }

                return false;
            }
        }

        /// <summary>
        /// Checks for a "pause the game" input action.
        /// </summary>
        public bool PauseGameInput
        {
            get
            {
                for (int i = 0; i < 4; ++i)
                {
                    if (((currentGamePadsState[i].Buttons.Start == ButtonState.Pressed) &&
                        (lastGamePadsState[i].Buttons.Start == ButtonState.Released)) ||
                        ((currentGamePadsState[i].Buttons.Back == ButtonState.Pressed) &&
                        (lastGamePadsState[i].Buttons.Back == ButtonState.Released)))
                    {
                        return true;
                    }
                }

                if (HasKeyJustBeenPressed(Keys.Escape))
                {
                    return true;
                }

                return false;
            }
        }

        /// <summary>
        /// The default X axis value.
        /// </summary>
        public float XAxisInput
        {
            get
            {
                float result = 0.0f;

                return result;
            }
        }

        #endregion

        #region Initialization / Cleanup

        /// <summary>
        /// Default constructor.
        /// </summary>
        public InputManagerDM()
        {
            currentGamePadsState = new GamePadState[4];
            lastGamePadsState = new GamePadState[4];

            for (int i = 0; i < 4; ++i)
            {
                currentGamePadsState[i] = GamePad.GetState((PlayerIndex)i);
                lastGamePadsState[i] = currentGamePadsState[i];
            }

            currentKeyboardState = Keyboard.GetState();
            lastKeyboardState = currentKeyboardState;

            currentMouseState = Mouse.GetState();
            lastMouseState = currentMouseState;
        }

        #endregion

        #region Update

        /// <summary>
        /// Update the input devices.
        /// </summary>
        public void Update()
        {
            for (int i = 0; i < 4; ++i)
            {
                lastGamePadsState[i] = currentGamePadsState[i];
                currentGamePadsState[i] = GamePad.GetState((PlayerIndex)i);
            }

            lastKeyboardState = currentKeyboardState;
            currentKeyboardState = Keyboard.GetState();

            lastMouseState = currentMouseState;
            currentMouseState = Mouse.GetState();
        }

        #endregion

        #region Methods

        /// <summary>
        /// Helper for checking if a key has just been pressed.
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool HasKeyJustBeenPressed(Keys key)
        {
            return (currentKeyboardState.IsKeyDown(key) && lastKeyboardState.IsKeyUp(key));
        }

        /// <summary>
        /// Helper that uses a pair of keys to simulate a positive or negative axis input.
        /// </summary>
        /// <param name="keyboardState"></param>
        /// <param name="downKey"></param>
        /// <param name="upKey"></param>
        /// <returns></returns>
        public static float ReadKeyboardAxis(KeyboardState keyboardState, Keys downKey, Keys upKey)
        {
            float result = 0.0f;

            if (keyboardState.IsKeyDown(downKey))
            {
                result -= 1.0f;
            }

            if (keyboardState.IsKeyDown(upKey))
            {
                result += 1.0f;
            }

            return result;
        }

        /// <summary>
        /// Power curve function. This curve makes small input values even smaller while preserving
        /// the full range when the stick is moved in an extreme position.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="power"></param>
        /// <returns></returns>
        public static float PowerCurve(float value, float power)
        {
            return (float)Math.Pow(Math.Abs(value), power) * Math.Sign(value);
        }

        #endregion

    }
}