Exemple #1
0
        /// <summary>
        /// Loads managed resources used by the console
        /// </summary>
        protected override void LoadContent()
        {
            IGraphicsDeviceService graphics =
                (IGraphicsDeviceService)Game.Services.GetService(typeof(IGraphicsDeviceService));

            mSpriteBatch = new SpriteBatch(graphics.GraphicsDevice);
            mFont        = Game.Content.Load <SpriteFont>(mFontName);

            mVertexDeclaration = new VertexDeclaration(graphics.GraphicsDevice,
                                                       new VertexElement[] {
                new VertexElement(0, 0, VertexElementFormat.Vector3,
                                  VertexElementMethod.Default, VertexElementUsage.Position, 0)
            });

            mEffect = new BasicEffect(graphics.GraphicsDevice, null);
            mEffect.VertexColorEnabled = false;
            mEffect.LightingEnabled    = false;
            mEffect.TextureEnabled     = false;
            mEffect.View  = Matrix.Identity;
            mEffect.World = Matrix.Identity;

            // measure the font height
            mFontHeight = 0f;

            List <char> chars = ConsoleKeyMap.GetRegisteredCharacters();

            foreach (char c in chars)
            {
                Vector2 size = mFont.MeasureString(c.ToString());

                if (size.Y > mFontHeight)
                {
                    mFontHeight = size.Y;
                }

                mCharSizeLut[c] = size;
            }

            base.LoadContent();
        }
Exemple #2
0
        /// <summary>
        /// Updates any input to the console
        /// </summary>
        /// <param name="gameTime">The current game time</param>
        public override void Update(GameTime gameTime)
        {
            mCurrentTime = gameTime;

            // handle cursor blinking
            mCursorTimer -= (float)gameTime.ElapsedGameTime.TotalSeconds;
            if (mCursorTimer <= 0)
            {
                mDrawCursor  = !mDrawCursor;
                mCursorTimer = mCursorBlinkSpeed + mCursorTimer;
            }

            // handle notify blinking
            mNotifyTimer -= (float)gameTime.ElapsedGameTime.TotalSeconds;
            if (mNotifyTimer <= 0)
            {
                mDrawNotify  = !mDrawNotify;
                mNotifyTimer = mNotifyBlinkSpeed + mNotifyTimer;
            }

            KeyboardState kb = Keyboard.GetState();

            // Close the console if the close key was pressed
            if ((kb[mCloseKey] == KeyState.Down) &&
                (mLastKeyboardState[mCloseKey] == KeyState.Up))
            {
                Enabled = false;
                return;
            }

            // handle input cursor movement if cursor is enabled
            if (mCursorEnabled)
            {
                if ((kb[Keys.Left] == KeyState.Down) && (mLastKeyboardState[Keys.Left] == KeyState.Up))
                {
                    if (mInputPosition > 0)
                    {
                        mInputPosition--;
                    }
                }

                if ((kb[Keys.Right] == KeyState.Down) && (mLastKeyboardState[Keys.Right] == KeyState.Up))
                {
                    if (mInputPosition < mCurrentText.Length)
                    {
                        mInputPosition++;
                    }
                }
            }

            // handle paging up and down
            if ((kb[Keys.PageUp] == KeyState.Down) && (mLastKeyboardState[Keys.PageUp] == KeyState.Up))
            {
                mCurrentLine = (int)MathHelper.Clamp(mCurrentLine - mVisibleLineCount, 0, mLog.Count - 1);
            }

            if ((kb[Keys.PageDown] == KeyState.Down) && (mLastKeyboardState[Keys.PageDown] == KeyState.Up))
            {
                mCurrentLine = (int)MathHelper.Clamp(mCurrentLine + mVisibleLineCount, 0, mLog.Count - 1);
            }

            /*if ((kb[Keys.Up] == KeyState.Down) && (mLastKeyboardState[Keys.Up] == KeyState.Up))
             *  mCurrentLine = (int)MathHelper.Clamp(mCurrentLine - 1, 0, mLog.Count - 1);
             *
             * if ((kb[Keys.Down] == KeyState.Down) && (mLastKeyboardState[Keys.Down] == KeyState.Up))
             *  mCurrentLine = (int)MathHelper.Clamp(mCurrentLine + 1, 0, mLog.Count - 1);
             */
            // Process each pressed key for a key down event
            foreach (Keys key in kb.GetPressedKeys())
            {
                // if its a repeat key, skip it
                if (mLastKeyboardState[key] != KeyState.Up)
                {
                    continue;
                }

                char        ch  = new char();
                KeyModifier mod = KeyModifier.None;

                // check for shift modifiers
                if ((kb[Keys.LeftShift] == KeyState.Down) ||
                    (kb[Keys.RightShift] == KeyState.Down))
                {
                    mod = KeyModifier.Shift;
                }

                if (ConsoleKeyMap.GetCharacter(key, mod, ref ch))
                {
                    mCurrentText.Insert(mInputPosition, ch);
                    mInputPosition++;
                }
            }

            // check for backspace
            if (kb[Keys.Back] == KeyState.Down && mLastKeyboardState[Keys.Back] == KeyState.Up)
            {
                if (mInputPosition > 0)
                {
                    mCurrentText.Remove(mInputPosition - 1, 1);
                }

                mInputPosition = (int)MathHelper.Clamp(mInputPosition - 1, 0, mCurrentText.Length);
            }

            // check for entered text
            if ((kb[Keys.Enter] == KeyState.Down) && (mLastKeyboardState[Keys.Enter] == KeyState.Up))
            {
                mInputPosition = 0;
                mHistoryOffset = 1;
                // if the text is length 0, we won't log it
                if (mCurrentText.Length == 0)
                {
                    return;
                }

                // build the current text input string
                string input = mCurrentText.ToString();

                // break the text into a command and arguments for any command handlers that
                // might be registered for it
                string[] command = input.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);

                // nothing useful...
                if (command.Length == 0)
                {
                    return;
                }

                // log it if echo is enabled
                if (mEchoEnabled)
                {
                    Log(input, mEchoLogLevel);
                }

                // let the raw input handlers do their thing
                if (TextEntered != null)
                {
                    TextEntered(input, gameTime);
                }

                // remove the command part from the input, leaving the arguments
                //input = input.Remove(0, command[0].Length + ((command.Length > 1) ? (1) : (0)));

                // clear the current text
                mCurrentText.Remove(0, mCurrentText.Length);

                Game1.interpreter.run(input);
                Game1.DebugLog.WriteLine(input);
                // call any command handlers registered to the command

                /*if (mCommandHandlers.ContainsKey(command[0]))
                 * {
                 *  string args = "";
                 *
                 *  if (mCommandHandlers[command[0]].ArgumentSeparators.Length > 0)
                 *  {
                 *      args = input;//.Split(mCommandHandlers[command[0]].ArgumentSeparators,
                 *                   //             StringSplitOptions.RemoveEmptyEntries);
                 *  }
                 *
                 *  mCommandHandlers[command[0]].Handler(gameTime, args);
                 * }
                 * /*Uelse if (mAlertOnUnrecognizedCommand)
                 * {
                 *  Log(string.Format("Unrecognized Command: '{0}'", command[0]), 0);
                 * }*/
            }
            //check for if history is requested
            if (kb[Keys.Up] == KeyState.Down && mLastKeyboardState[Keys.Up] == KeyState.Up)
            {
                if (MessageLog.Count > 0 && MessageLog.Count >= mHistoryOffset)
                {
                    mCurrentText = new StringBuilder();
                    this.mCurrentText.Append(this.MessageLog[MessageLog.Count - mHistoryOffset].Text);
                    mHistoryOffset++;
                }
            }

            // save last keyboard state
            mLastKeyboardState = kb;

            base.Update(gameTime);
        }