protected override void Initialize() { this.graphicsDevice.CreateDevice(); this.graphics = new GraphicsBatch(this.graphicsDevice); this.font = this.contentLoader.Load<TextureFont>(new LoadTextureFontArgs() { FileName = "GamePadReaderFont.xml" }); }
public TextBlock(TextureFont font, string text, Size size, TextAlignment alignment) : this(font, text, size, alignment, Vector2.One) { }
public TextBlock(TextureFont font, string text, Size size, TextAlignment alignment, Vector2 scale) { if (font == null) throw new ArgumentNullException("font"); if (text == null) throw new ArgumentNullException("text"); this.Font = font; this.Text = text; this.size = size; this.Alignment = alignment; this.scale = scale; List<Character> charactersList = new List<Character>(); float heightOfSingleLine = font.LineHeight * scale.Y; float heightOfAllLines = 0; if (heightOfSingleLine <= size.Height) { Vector2 cursor = Vector2.Zero; for (int i = 0; i < text.Length; i++) { // Skip characters we can't render. if (text[i] == '\r') continue; float widthOfChar = 0; if (text[i] == '\n' || cursor.X + (widthOfChar = font[text[i]].Width * scale.X) > this.size.Width) { cursor.X = 0; cursor.Y += heightOfSingleLine + font.LineSpacing; // If the next line extends past the destination, quit. if (cursor.Y + heightOfSingleLine > this.size.Height) break; heightOfAllLines = (int)(cursor.Y + heightOfSingleLine); // We can't render a new line. if (text[i] == '\n') continue; } Character character = new Character(); character.Source = font[text[i]]; character.Destination = new Rectangle((int)cursor.X, (int)cursor.Y, (int)widthOfChar, (int)heightOfSingleLine); charactersList.Add(character); cursor.X += widthOfChar + font.CharacterSpacing; } } this.characters = charactersList.ToArray(); int yPositionOfCurrentLine = -1; float widthOfCurrentLine = 0; if (alignment != TextAlignment.TopLeft) { for (int i = 0; i < this.characters.Length; i++) { Rectangle destination = this.characters[i].Destination; if (yPositionOfCurrentLine != destination.Y) // If we reach a new line, find the width of it. { yPositionOfCurrentLine = destination.Y; int j = i; while (j < this.characters.Length && this.characters[j].Destination.Y == yPositionOfCurrentLine) j++; widthOfCurrentLine = this.characters[j - 1].Destination.Right; } switch (this.Alignment) { case TextAlignment.TopCenter: case TextAlignment.MiddleCenter: case TextAlignment.BottomCenter: destination.X = (int)((this.size.Width / 2) - (widthOfCurrentLine / 2) + destination.X); break; case TextAlignment.TopRight: case TextAlignment.MiddleRight: case TextAlignment.BottomRight: destination.X = (int)(this.size.Width - widthOfCurrentLine + destination.X); break; } switch (this.Alignment) { case TextAlignment.MiddleLeft: case TextAlignment.MiddleCenter: case TextAlignment.MiddleRight: destination.Y = (int)((this.size.Height / 2) - (heightOfAllLines / 2) + destination.Y); break; case TextAlignment.BottomLeft: case TextAlignment.BottomCenter: case TextAlignment.BottomRight: destination.Y = (int)(this.size.Height - heightOfAllLines + destination.Y); break; } this.characters[i].Destination = destination; } } }
protected override void Initialize() { this.graphicsDevice.CreateDevice(); this.graphics = new GraphicsBatch(this.graphicsDevice); this.font = this.contentLoader.Load<TextureFont>(new LoadTextureFontArgs() { FileName = "TextBlockFont.xml" }); Size textBlockSize = new Size(this.graphicsDevice.BackBufferWidth / 3, this.graphicsDevice.BackBufferHeight / 3); this.topLeftTextBlock = new TextBlock(this.font, "Top\nLeft\nText\nBlock", textBlockSize, TextAlignment.TopLeft); this.topCenterTextBlock = new TextBlock(this.font, "Top\nCenter\nText\nBlock", textBlockSize, TextAlignment.TopCenter); this.topRightTextBlock = new TextBlock(this.font, "Top\nRight\nText\nBlock", textBlockSize, TextAlignment.TopRight); this.middleLeftTextBlock = new TextBlock(this.font, "Middle\nLeft\nText\nBlock", textBlockSize, TextAlignment.MiddleLeft); this.middleCenterTextBlock = new TextBlock(this.font, "Middle\nCenter\nText\nBlock", textBlockSize, TextAlignment.MiddleCenter); this.middleRightTextBlock = new TextBlock(this.font, "Middle\nRight\nText\nBlock", textBlockSize, TextAlignment.MiddleRight); this.bottomLeftTextBlock = new TextBlock(this.font, "Bottom\nLeft\nText\nBlock", textBlockSize, TextAlignment.BottomLeft); this.bottomCenterTextBlock = new TextBlock(this.font, "Bottom\nCenter\nText\nBlock", textBlockSize, TextAlignment.BottomCenter); this.bottomRightTextBlock = new TextBlock(this.font, "Bottom\nRight\nText\nBlock", textBlockSize, TextAlignment.BottomRight); }
protected override void Initialize() { this.graphicsDevice.CreateDevice(800, 600, false); this.soundDevice.CreateDevice(); this.console.Initialize(); this.console.BackgroundTexture = this.contentManager.Load<Texture>(DemoGameContent.ConsoleBackground); this.ship.LoadContent(this.contentManager); this.graphics = new GraphicsBatch(this.graphicsDevice); this.font = this.contentManager.Load<TextureFont>(DemoGameContent.Font); this.textBlock = new TextBlock(this.font, "This is text\nspanning multiple lines.", new Size(800, 600), TextAlignment.MiddleCenter, Vector2.One); this.starfield.Initialize(); this.ship.Initialize(); }
private void EnsureDrawStringParams(TextureFont font, string text) { if (font == null) throw new ArgumentNullException("font"); if (text == null) throw new ArgumentNullException("text"); }
public void DrawString(TextureFont font, string text, Rectangle destination, Vector2 scale, Color color) { this.EnsureDrawStringParams(font, text); if (text.Length == 0) return; float heightOfSingleLine = font.LineHeight * scale.Y; if (heightOfSingleLine > destination.Height) // We can't draw anything return; Vector2 cursor = new Vector2(destination.X, destination.Y); for (int i = 0; i < text.Length; i++) { // Skip characters we can't render. if (text[i] == '\r') continue; float widthOfChar = 0; if (text[i] == '\n' || cursor.X + (widthOfChar = font[text[i]].Width * scale.X) > destination.Right) { cursor.X = destination.X; cursor.Y += heightOfSingleLine + font.LineSpacing; // If the next line extends past the destination, quit. if (cursor.Y + heightOfSingleLine > destination.Bottom) return; // We can't render a new line. if (text[i] == '\n') continue; } Rectangle letterSource = font[text[i]]; Rectangle letterDestination = new Rectangle((int)cursor.X, (int)cursor.Y, (int)widthOfChar, (int)heightOfSingleLine); this.DrawTexture(font.Texture, letterDestination, letterSource, color); cursor.X += widthOfChar + font.CharacterSpacing; } }
public void DrawString(TextureFont font, string text, Rectangle destination, Color color) { this.DrawString(font, text, destination, Vector2.One, color); }
public void DrawString(TextureFont font, string text, Vector2 position, Vector2 scale, Color color) { this.EnsureDrawStringParams(font, text); Size textSize = font.MeasureString(text); this.DrawString(font, text, new Rectangle((int)position.X, (int)position.Y, textSize.Width, textSize.Height), scale, color); }
public void DrawString(TextureFont font, string text, Vector2 position, Color color) { this.DrawString(font, text, position, Vector2.One, color); }