private void ProcessText(string text, MessageTextAlignment alignment, ref int lineIndex) { var lineText = new StringBuilder(String.Join(" ", _wordsByLine[lineIndex].Select(arg => arg.Text))); _lineSizesByLine[lineIndex] = _font.MeasureString(lineText); string[] words = text.Split(_wordSeparator); for (int i = 0; i < words.Length; i++) { string word = words[i]; Vector2 wordSize = _font.MeasureString(word) + TextAdventure.Xna.Constants.MessageRenderer.ShadowOffset; Vector2 lineSize = _lineSizesByLine[lineIndex]; lineSize.Y = Math.Max(wordSize.Y, lineSize.Y); _lineSizesByLine[lineIndex] = lineSize; if (i == 0) { if (lineSize.X + wordSize.X <= _maximumLineWidth) { lineText.Append(word); _lineSizesByLine[lineIndex] = new Vector2(lineSize.X + wordSize.X, lineSize.Y); _wordsByLine[lineIndex].Add(new MessageTextWord(word, wordSize, false)); _alignmentsByLine[lineIndex] = alignment; continue; } lineIndex++; _lineSizesByLine[lineIndex] = Vector2.Zero; _wordsByLine[lineIndex] = new List <MessageTextWord>(); _alignmentsByLine[lineIndex] = alignment; } if (lineSize.X + _spaceWord.Size.X + wordSize.X <= _maximumLineWidth) { if (lineText.Length > 0) { lineText.Append(' '); } lineText.Append(word); _lineSizesByLine[lineIndex] = new Vector2(lineSize.X + _spaceWord.Size.X + wordSize.X, lineSize.Y); _wordsByLine[lineIndex].Add(new MessageTextWord(word, wordSize, true)); } else { lineText = new StringBuilder(word); lineIndex++; _lineSizesByLine[lineIndex] = new Vector2(wordSize.X, wordSize.Y); _wordsByLine[lineIndex] = new List <MessageTextWord> { new MessageTextWord(word, wordSize, false) }; _alignmentsByLine[lineIndex] = alignment; } } }
private void RenderWords( RendererParameters parameters, SpriteFont font, Matrix transformMatrix, int lineIndex, IList <MessageTextWord> words, Color shadowColor, ref Vector2 position, ref Color textColor) { parameters.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.None, new ScissoringRasterizerState(), null, transformMatrix); MessageTextAlignment alignment = _formatter.GetAlignmentByLine(lineIndex); Vector2 lineSize = _formatter.GetLineSizeByLine(lineIndex); if (alignment == MessageTextAlignment.Center) { position.X += (Window.AbsoluteClientRectangle.Width - lineSize.X) / 2; } for (int wordIndex = 0; wordIndex < words.Count; wordIndex++) { MessageTextWord word = words[wordIndex]; Engine.Common.Color color; if (_formatter.TryGetColorByWordCoordinate(new Coordinate(wordIndex, lineIndex), out color)) { textColor = color.ToXnaColor() * Alpha; } if (word.PrependSpace) { position.X += _formatter.SpaceWord.Size.X; } parameters.SpriteBatch.DrawStringWithShadow(font, word.Text, position.Round(), textColor, shadowColor, Vector2.One); position.X += word.Size.X; } position.X = Window.AbsoluteClientRectangle.X; position.Y += lineSize.Y; parameters.SpriteBatch.End(); }
private void ProcessText(string text, MessageTextAlignment alignment, ref int lineIndex) { var lineText = new StringBuilder(String.Join(" ", _wordsByLine[lineIndex].Select(arg => arg.Text))); _lineSizesByLine[lineIndex] = _font.MeasureString(lineText); string[] words = text.Split(_wordSeparator); for (int i = 0; i < words.Length; i++) { string word = words[i]; Vector2 wordSize = _font.MeasureString(word) + TextAdventure.Xna.Constants.MessageRenderer.ShadowOffset; Vector2 lineSize = _lineSizesByLine[lineIndex]; lineSize.Y = Math.Max(wordSize.Y, lineSize.Y); _lineSizesByLine[lineIndex] = lineSize; if (i == 0) { if (lineSize.X + wordSize.X <= _maximumLineWidth) { lineText.Append(word); _lineSizesByLine[lineIndex] = new Vector2(lineSize.X + wordSize.X, lineSize.Y); _wordsByLine[lineIndex].Add(new MessageTextWord(word, wordSize, false)); _alignmentsByLine[lineIndex] = alignment; continue; } lineIndex++; _lineSizesByLine[lineIndex] = Vector2.Zero; _wordsByLine[lineIndex] = new List<MessageTextWord>(); _alignmentsByLine[lineIndex] = alignment; } if (lineSize.X + _spaceWord.Size.X + wordSize.X <= _maximumLineWidth) { if (lineText.Length > 0) { lineText.Append(' '); } lineText.Append(word); _lineSizesByLine[lineIndex] = new Vector2(lineSize.X + _spaceWord.Size.X + wordSize.X, lineSize.Y); _wordsByLine[lineIndex].Add(new MessageTextWord(word, wordSize, true)); } else { lineText = new StringBuilder(word); lineIndex++; _lineSizesByLine[lineIndex] = new Vector2(wordSize.X, wordSize.Y); _wordsByLine[lineIndex] = new List<MessageTextWord> { new MessageTextWord(word, wordSize, false) }; _alignmentsByLine[lineIndex] = alignment; } } }