Esempio n. 1
0
 public Cat(string name, string type)
 {
     this.name = name; this._type = type;
 }
        public void Update(KeyboardState kbState, KeyboardState prevKBState)
        {
            // Assign the returned array of pressed keys for that frame to a variable.
            LiveStringArray = kbState.GetPressedKeys();



            //Need:
            //draw method.Look at Game1s draw fsm for things to copy paste.
            //to draw a pop up window first.Pop up because it is smaller than the rest of the window, so it looks
            //like it has been overlaid.Need to also determine when to show the pop up.
            //Button to submit the name.Also add functionality for enter key possibly.

            // TEXT INPUT TESTER CODE



            // For all intents and purposes, the following code works sufficiently well.
            // Mixing GetPressedKeys() with IsKeyUp/Down()
            // Loop through GetPressedKeys()'s returned array to see if the keys are pressed
            // If they are, do not add them to LiveStringArray - only add those that are not pressed.

            // Loop through the above returned array.
            for (int i = 0; i < LiveStringArray.Length; i++)
            {
                // Detect single presses of keys in the array. Only proceed if the current key
                //		being checked is in permittedKeys AND was up in the previous frame.
                //		Otherwise skip and check the next key in the array. This effectively
                //		allows for regular typing, save for the character repeat aspect. This
                //		is because a few keys are pressed simultaneously while typing normally,
                //		and this solution accommodates such a scenario.
                if (permittedKeys.Contains(LiveStringArray[i]) &&
                    kbState.IsKeyDown(LiveStringArray[i]) &&
                    prevKBState.IsKeyUp(LiveStringArray[i]))
                {
                    // If Backspace is pressed...
                    if (LiveStringArray[i] == Keys.Back)
                    {
                        // ...and if the string is empty, do nothing.
                        if (LiveString.Length == 0)
                        {
                            // Do nothing.
                            // This prevents an index out of range error from occurring.
                        }
                        // ...otherwise, add characters up to a max length of 3.
                        else if (LiveString.Length > 0 ||
                                 LiveString.Length <= 3)
                        {
                            // Strings are immutable, and so must be reassigned should they be modified,
                            //		similarly with how structs behave.
                            LiveString = LiveString.Remove(LiveString.Length - 1);
                        }
                    }
                    // If Space is pressed...
                    else if (LiveStringArray[i] == Keys.Space)
                    {
                        // ...add space up to a max length of 3.
                        if (LiveString.Length < 3)
                        {
                            LiveString += " ";
                        }
                    }
                    // For all other Keys, concatenate them in the order they were pressed i.e. the
                    //		order in which they appear in LiveStringArray up to a max length of 3.
                    else
                    {
                        if (LiveString.Length < 3)
                        {
                            LiveString += LiveStringArray[i];
                        }
                    }
                }
            }
        }