{"id":11,"date":"2012-11-20T17:39:49","date_gmt":"2012-11-20T17:39:49","guid":{"rendered":"https:\/\/8bitroots.com\/?page_id=11"},"modified":"2013-04-21T21:02:53","modified_gmt":"2013-04-21T21:02:53","slug":"sample-code","status":"publish","type":"page","link":"https:\/\/8bitroots.com\/?page_id=11","title":{"rendered":"Sample Code"},"content":{"rendered":"<p>Here is Xbox controller input handling that I wrote for personal use to aid in rapid prototyping in XNA. More sample code is always available upon request!<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"brush: csharp\">\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\/\/  file :          CPlayerInput.cs\r\n\/\/\r\n\/\/  author :        Chase Cobb\r\n\/\/\r\n\/\/  description :   This class is responsible for gathering both buffered and unbuffered \r\n\/\/                  input from an xbox 360 controller\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\r\nusing System;\r\nusing Microsoft.Xna.Framework;\r\nusing Microsoft.Xna.Framework.Input;\r\n\r\nnamespace Xbox_Input\r\n{\r\n    class CPlayerInput\r\n    {\r\n        \/\/ This enum holds possible bit offsets for buttons\r\n        [Flags]\r\n        public enum EButtons\r\n        {\r\n            BUTTON_NONE = 0,\r\n            BUTTON_UP = 1,\r\n            BUTTON_DOWN = 2,\r\n            BUTTON_LEFT = 4,\r\n            BUTTON_RIGHT = 8,\r\n            BUTTON_A = 16,\r\n            BUTTON_B = 32,\r\n            BUTTON_X = 64,\r\n            BUTTON_Y = 128,\r\n            BUTTON_START = 256,\r\n            BUTTON_BACK = 512,\r\n            BUTTON_LEFT_SHOULDER = 1024,\r\n            BUTTON_RIGHT_SHOULDER = 2048,\r\n        }\r\n\r\n        \/***********************************************************\r\n         * Private Members ****************************************\/\r\n        private PlayerIndex m_eInputIndex;\r\n        private EButtons m_nCurrentState;\r\n        private EButtons m_nPreviousState;\r\n        private float m_fLeftTrigger;\r\n        private float m_fRightTrigger;\r\n        private Vector2 m_vLeftThumbstick;\r\n        private Vector2 m_vRightThumbstick;\r\n\r\n        \/\/ Constructor\r\n        public CPlayerInput(PlayerIndex eIndex)\r\n        {\r\n            m_eInputIndex = eIndex;\r\n            m_vLeftThumbstick = new Vector2();\r\n            m_vRightThumbstick = new Vector2();\r\n        }\r\n\r\n\r\n        \/***********************************************************\r\n\r\n        * Propterties *********************************************\/\r\n\r\n        public PlayerIndex ControllerIndex\r\n        {\r\n            get\r\n            {\r\n                return m_eInputIndex;\r\n            }\r\n        }\r\n\r\n        public float LeftTrigger\r\n        {\r\n            get\r\n            {\r\n                return m_fLeftTrigger;\r\n            }\r\n        }\r\n\r\n        public float RightTrigger\r\n        {\r\n            get\r\n            {\r\n                return m_fRightTrigger;\r\n            }\r\n        }\r\n\r\n        public Vector2 LeftThumbstick\r\n        {\r\n            get\r\n            {\r\n                return m_vLeftThumbstick;\r\n            }\r\n        }\r\n\r\n        public Vector2 RightThumbstick\r\n        {\r\n            get\r\n            {\r\n                return m_vRightThumbstick;\r\n            }\r\n        }\r\n\r\n\r\n        \/***********************************************************\r\n         * Public Functions ***************************************\/\r\n\r\n        \/\/\/ <summary>\r\n        \/\/\/ Resets all input variables to unused state\r\n        \/\/\/ <\/summary>\r\n        public void FlushInput()\r\n        {\r\n            m_nCurrentState = m_nPreviousState = EButtons.BUTTON_NONE;\r\n        }\r\n\r\n        \/\/\/ <summary>\r\n        \/\/\/ Takes in a button to and performs a buffered input check.\r\n        \/\/\/ <\/summary>\r\n        \/\/\/ <param name=\"eButton\">button to test against<\/param>\r\n        \/\/\/ <returns>returns true if the button was pressed this frame<\/returns>\r\n        public bool GetButtonPressed(EButtons eButton)\r\n        {\r\n            \/\/if currently down and not previously down\r\n            return ((m_nCurrentState & eButton) != EButtons.BUTTON_NONE && (m_nPreviousState & eButton) == EButtons.BUTTON_NONE);\r\n        }\r\n\r\n        \/\/\/ <summary>\r\n        \/\/\/ This input is NOT buffered - if you need buffered use GetButtonPressed\r\n        \/\/\/ <\/summary>\r\n        \/\/\/ <param name=\"eButton\">button to test against<\/param>\r\n        \/\/\/ <returns>true if the button passed in is currently held down<\/returns>\r\n        public bool GetButtonDown(EButtons eButton)\r\n        {\r\n            return ((m_nCurrentState & eButton) != EButtons.BUTTON_NONE);\r\n        }\r\n\r\n        \/\/\/ <summary>\r\n        \/\/\/ This should be called every frame after the input has been gathered for this controller\r\n        \/\/\/ <\/summary>\r\n        public void SetPreviousState()\r\n        {\r\n            \/\/set the previous state\r\n            m_nPreviousState = m_nCurrentState;\r\n        }\r\n\r\n        \/\/\/ <summary>\r\n        \/\/\/ Fills out all current input\r\n        \/\/\/ <\/summary>\r\n        public bool GetCurrentState()\r\n        {\r\n            \/\/make sure the controller is connected\r\n            if (GamePad.GetState(m_eInputIndex).IsConnected)\r\n            {\r\n                \/\/Set the previous state to the current state\r\n                SetPreviousState();\r\n\r\n                \/\/zero out the current button state\r\n                m_nCurrentState = EButtons.BUTTON_NONE;\r\n\r\n                \/\/set CurrentState flags for all input this frame\r\n                GetDPad();\r\n                GetButtons();\r\n                GetTriggers();\r\n                GetThumbsticks();\r\n\r\n                \/\/we successfully gathered our input\r\n                return true;\r\n            }\r\n            else\r\n            {\r\n                \/\/something went wrong!\r\n                return false;\r\n            }\r\n        }\r\n\r\n\r\n        \/***********************************************************\r\n         * Private Functions **************************************\/\r\n\r\n        \/\/\/ <summary>\r\n        \/\/\/ Responsible for storing D-Pad flags into the current state\r\n        \/\/\/ <\/summary>\r\n        private void GetDPad()\r\n        {\r\n            if (GamePad.GetState(m_eInputIndex).DPad.Up == ButtonState.Pressed)\r\n            {\r\n                m_nCurrentState |= EButtons.BUTTON_UP;\r\n            }\r\n            if (GamePad.GetState(m_eInputIndex).DPad.Down == ButtonState.Pressed)\r\n            {\r\n                m_nCurrentState |= EButtons.BUTTON_DOWN;\r\n            }\r\n            if (GamePad.GetState(m_eInputIndex).DPad.Left == ButtonState.Pressed)\r\n            {\r\n                m_nCurrentState |= EButtons.BUTTON_LEFT;\r\n            }\r\n            if (GamePad.GetState(m_eInputIndex).DPad.Right == ButtonState.Pressed)\r\n            {\r\n                m_nCurrentState |= EButtons.BUTTON_RIGHT;\r\n            }\r\n        }\r\n\r\n        \/\/\/ <summary>\r\n        \/\/\/ Responsible for storing Button flags into the current state\r\n        \/\/\/ <\/summary>\r\n        private void GetButtons()\r\n        {\r\n            if (GamePad.GetState(m_eInputIndex).Buttons.A == ButtonState.Pressed)\r\n            {\r\n                m_nCurrentState |= EButtons.BUTTON_A;\r\n            }\r\n            if (GamePad.GetState(m_eInputIndex).Buttons.B == ButtonState.Pressed)\r\n            {\r\n                m_nCurrentState |= EButtons.BUTTON_B;\r\n            }\r\n            if (GamePad.GetState(m_eInputIndex).Buttons.X == ButtonState.Pressed)\r\n            {\r\n                m_nCurrentState |= EButtons.BUTTON_X;\r\n            }\r\n            if (GamePad.GetState(m_eInputIndex).Buttons.Y == ButtonState.Pressed)\r\n            {\r\n                m_nCurrentState |= EButtons.BUTTON_Y;\r\n            }\r\n            if (GamePad.GetState(m_eInputIndex).Buttons.Start == ButtonState.Pressed)\r\n            {\r\n                m_nCurrentState |= EButtons.BUTTON_START;\r\n            }\r\n            if (GamePad.GetState(m_eInputIndex).Buttons.Back == ButtonState.Pressed)\r\n            {\r\n                m_nCurrentState |= EButtons.BUTTON_BACK;\r\n            }\r\n            if (GamePad.GetState(m_eInputIndex).Buttons.LeftShoulder == ButtonState.Pressed)\r\n            {\r\n                m_nCurrentState |= EButtons.BUTTON_LEFT_SHOULDER;\r\n            }\r\n            if (GamePad.GetState(m_eInputIndex).Buttons.RightShoulder == ButtonState.Pressed)\r\n            {\r\n                m_nCurrentState |= EButtons.BUTTON_RIGHT_SHOULDER;\r\n            }\r\n        }\r\n\r\n        \/\/\/ <summary>\r\n        \/\/\/ Responsible for reading and storing the current trigger state.\r\n        \/\/\/ <\/summary>\r\n        private void GetTriggers()\r\n        {\r\n            m_fLeftTrigger = GamePad.GetState(m_eInputIndex).Triggers.Left;\r\n            m_fRightTrigger = GamePad.GetState(m_eInputIndex).Triggers.Right;\r\n        }\r\n\r\n        \/\/\/ <summary>\r\n        \/\/\/ Responsible for reading and storing the current thumbstick state\r\n        \/\/\/ <\/summary>\r\n        private void GetThumbsticks()\r\n        {\r\n            m_vLeftThumbstick.X = GamePad.GetState(m_eInputIndex).ThumbSticks.Left.X;\r\n            m_vLeftThumbstick.Y = GamePad.GetState(m_eInputIndex).ThumbSticks.Left.Y;\r\n            m_vRightThumbstick.X = GamePad.GetState(m_eInputIndex).ThumbSticks.Right.X;\r\n            m_vRightThumbstick.X = GamePad.GetState(m_eInputIndex).ThumbSticks.Right.Y;\r\n        }\r\n    }\r\n}\r\n\r\n<\/pre>\n<pre class=\"brush: csharp\">\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\/\/  file :              CInputManager.cs\r\n\/\/\r\n\/\/  author :            Chase Cobb\r\n\/\/  \r\n\/\/  description :       This class is responsible for determining what controller the \r\n\/\/                      player is using(PlayerIndex) and gathering their input each frame.\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\r\nusing System.Linq;\r\nusing System.Collections.Generic;\r\nusing Microsoft.Xna.Framework;\r\nusing Microsoft.Xna.Framework.Input;\r\n\r\n\r\nnamespace Xbox_Input\r\n{\r\n    \/\/\/ <summary>\r\n    \/\/\/ Proper Singleton\r\n    \/\/\/ Gathers all input and stores it for querying\r\n    \/\/\/ Supports Xbox input unbuffered\/buffered\r\n    \/\/\/ <\/summary>\r\n    class CInputManager\r\n    {\r\n        \/***********************************************************\r\n         * Constants **********************************************\/\r\n        private const int NUM_CONTROLLER_SLOTS =     4;\r\n\r\n        \/***********************************************************\r\n         * Public Members *****************************************\/\r\n        public int m_nNumberOfPlayers;\r\n\r\n        \/***********************************************************\r\n         * Private Members ****************************************\/\r\n        private static CInputManager instance;\r\n        private List<CPlayerInput> m_cInputs;\r\n\r\n        \/\/\/ <summary>\r\n        \/\/\/ Private constructor\r\n        \/\/\/ <\/summary>\r\n        private CInputManager()\r\n        {\r\n            m_cInputs = new List<CPlayerInput>();\r\n        }\r\n\r\n        \/\/\/ <summary>\r\n        \/\/\/ Property for accessing the instance of the input\r\n        \/\/\/ manager\r\n        \/\/\/ <\/summary>\r\n        public static CInputManager GetInstance\r\n        {\r\n            get\r\n            {\r\n                if (instance == null)\r\n                {\r\n                    instance = new CInputManager();\r\n                }\r\n                return instance;\r\n            }\r\n        }\r\n\r\n        \/***********************************************************\r\n         * Accessors **********************************************\/\r\n        public CPlayerInput GetInput(PlayerIndex nIndex)\r\n        {\r\n            if ((int)nIndex >= 0 && (int)nIndex < m_nNumberOfPlayers)\r\n                return m_cInputs[(int)nIndex];\r\n\r\n            return null;\r\n        }\r\n\r\n\r\n        \/***********************************************************\r\n         * Public Functions ***************************************\/\r\n\r\n        \/\/\/ <summary>\r\n        \/\/\/ This function will tell us the index of the controller joining the game.\r\n        \/\/\/ <\/summary>\r\n        \/\/\/ <returns>The index of the controller currently joining<\/returns>\r\n        public int FindController()\r\n        {\r\n            \/\/iterate all unused controller indices for the player joining the game\r\n                \/\/when a controller is added increase the number of players\r\n            for (int i = 0; i < NUM_CONTROLLER_SLOTS; ++i)\r\n            {\r\n                if (GamePad.GetState((PlayerIndex)i).IsConnected)\r\n                {\r\n                    if (GamePad.GetState((PlayerIndex)i).Buttons.Start == ButtonState.Pressed)\r\n                    {\r\n                        \/\/add new controller\r\n                        CPlayerInput newInput = new CPlayerInput((PlayerIndex)i);\r\n                        m_cInputs.Add(newInput);\r\n                        ++m_nNumberOfPlayers;\r\n\r\n                        \/\/return a handle to its index in the list of CPlayerInput\r\n                        return m_nNumberOfPlayers - 1;\r\n                    }\r\n                }\r\n            }\r\n\r\n            \/\/something went wrong!\r\n            return -1;\r\n        }\r\n\r\n        \/\/\/ <summary>\r\n        \/\/\/ This function will remove the requested controller\r\n        \/\/\/ and decrease the number of players being tracked\r\n        \/\/\/ <\/summary>\r\n        \/\/\/ <param name=\"index\">Index handle to remove<\/param>\r\n        public void RemoveController(int index)\r\n        {\r\n            \/\/is this a valid index?\r\n            if (index < m_cInputs.Count &#038;&#038; index >= 0)\r\n            {\r\n                m_cInputs.RemoveAt(index);\r\n                --m_nNumberOfPlayers;\r\n            }\r\n        }\r\n\r\n        \/\/\/ <summary>\r\n        \/\/\/ should be called every frame before any input is handled\r\n        \/\/\/ <\/summary>\r\n        public bool GetCurrentInputState()\r\n        {\r\n            \/\/return false if any of the inputs.GetCurrentState() returns false \r\n            return m_cInputs.All(x => x.GetCurrentState() == true);\r\n        }\r\n\r\n        \/\/\/ <summary>\r\n        \/\/\/ should be called every frame after input has been collected and handled\r\n        \/\/\/ <\/summary>\r\n        public void SetPreviousInputState()\r\n        {\r\n            foreach (CPlayerInput input in m_cInputs)\r\n            {\r\n                input.SetPreviousState();\r\n            }\r\n        }\r\n\r\n        \/\/\/ <summary>\r\n        \/\/\/ This will flush the input currently stored in all CPlayerInput objects\r\n        \/\/\/ <\/summary>\r\n        public void FlushAllInput()\r\n        {\r\n            \/\/for each active CPlayerInput object call FlushInput()\r\n            foreach (CPlayerInput input in m_cInputs)\r\n            {\r\n                input.FlushInput();\r\n            }\r\n        }\r\n\r\n        \/\/\/ <summary>\r\n        \/\/\/ This will flush the input currently stored in CPlayerInput object for the passed in index\r\n        \/\/\/ <\/summary>\r\n        \/\/\/ <param name=\"playerIndex\">index of the player needing their input flushed<\/param>\r\n        public void FlushCPlayerInput(int playerIndex)\r\n        {\r\n            \/\/flu-flush it real good!\r\n            m_cInputs[playerIndex].FlushInput();\r\n        }\r\n    }\r\n}\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here is Xbox controller input handling that I wrote for personal use to aid in rapid prototyping in XNA. More sample code is always available upon request! &nbsp; \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ \/\/ file : CPlayerInput.cs \/\/ \/\/ author : Chase Cobb \/\/ &hellip; <a href=\"https:\/\/8bitroots.com\/?page_id=11\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"class_list":["post-11","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/8bitroots.com\/index.php?rest_route=\/wp\/v2\/pages\/11","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/8bitroots.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/8bitroots.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/8bitroots.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/8bitroots.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=11"}],"version-history":[{"count":10,"href":"https:\/\/8bitroots.com\/index.php?rest_route=\/wp\/v2\/pages\/11\/revisions"}],"predecessor-version":[{"id":74,"href":"https:\/\/8bitroots.com\/index.php?rest_route=\/wp\/v2\/pages\/11\/revisions\/74"}],"wp:attachment":[{"href":"https:\/\/8bitroots.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}