/// <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 = SceneManager.GetEmbeddedFont(mFontName).Font; //mVertexDeclaration = new VertexDeclaration(graphics.GraphicsDevice, // new VertexElement[] { // new VertexElement(0, 0, VertexElementFormat.Vector3, // VertexElementMethod.Default, VertexElementUsage.Position, 0) // }); mEffect = new BasicEffect(graphics.GraphicsDevice); mEffect.VertexColorEnabled = false; mEffect.LightingEnabled = false; mEffect.TextureEnabled = false; mEffect.View = Matrix.Identity; mEffect.World = Matrix.Identity; //mEffect.Texture = mTexture; // 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(); }
/// <summary> /// Updates any input to the console /// </summary> /// <param name="gameTime">The current game time</param> public override void Update(GameTime gameTime) { if (!isEnabled) { #if !XBOX //TODO: Needs refactoring IGameConsole console = (IGameConsole)Game.Services.GetService(typeof(IGameConsole)); if (!console.IsOpen && InputCore.IsNewKeyPress(Keys.OemTilde)) { console.Open(Keys.OemTilde); } #endif return; } 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)) if (InputCore.IsNewKeyPress(mCloseKey)) { isEnabled = 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, new char[] { 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; // 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 #if XBOX string[] command = input.Split(new char[] { ' ', '\t' }); #else string[] command = input.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); #endif // 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); // call any command handlers registered to the command if (mCommandHandlers.ContainsKey(command[0])) { string[] args = new string[] { input }; if (mCommandHandlers[command[0]].ArgumentSeparators.Length > 0) { #if XBOX args = input.Split(mCommandHandlers[command[0]].ArgumentSeparators); #else args = input.Split(mCommandHandlers[command[0]].ArgumentSeparators, StringSplitOptions.RemoveEmptyEntries); #endif } mCommandHandlers[command[0]].Handler(gameTime, args); } else if (mAlertOnUnrecognizedCommand) { Log(string.Format("Unrecognized Command: '{0}'", command[0]), 0); } } // save last keyboard state mLastKeyboardState = kb; base.Update(gameTime); }