Example #1
0
        /// <summary>
        /// Draw a string with formatting.
        /// </summary>
        /// <param name="batch"></param>
        /// <param name="font"></param>
        /// <param name="label"></param>
        /// <param name="window"></param>
        /// <param name="color"></param>
        /// <param name="format"></param>
        public static void DrawAlignedString(this SpriteBatch batch, SpriteFont font, string text, Rectangle area, Color color, BlockAlignment format)
        {
            if (batch == null)
            {
                throw new ArgumentNullException("batch");
            }
            if (font == null)
            {
                throw new ArgumentNullException("font");
            }
            if (text == null)
            {
                throw new ArgumentNullException("text");
            }

            float spaceWidth = font.MeasureString(" ").X;
            int   lineCount = FormattedLineCount(font, text, area.Width);
            float y = area.Top, advance = font.LineSpacing;

            switch (format.VerticalAlignment)
            {
            case Alignment.Near:
                y = area.Top;
                break;

            case Alignment.Center:
                y = (area.Height - lineCount * advance) / 2 + area.Top;
                break;

            case Alignment.Far:
                y = area.Bottom - lineCount * advance;
                break;

            case Alignment.Full:
                if (lineCount > 1)
                {
                    advance = (area.Height - advance) / (lineCount - 1);
                }
                else
                {
                    y = (area.Height - advance) / 2 + area.Top;
                }
                break;

            default:
                throw new Exception("Unknown or invalid vertical alignment value " + format.VerticalAlignment);
            }

            for (int lineStart = 0; lineStart < text.Length;)
            {
                int       spaceCount  = 0;
                float     totalLength = 0;
                int       lineEnd     = lineStart;
                Alignment alignment   = format.HorizontalAlignment == Alignment.Full ? format.HorizontalPartialAlignment : format.HorizontalAlignment;

                while (lineEnd < text.Length && text[lineEnd] != '\n')
                {
                    int wordStart = lineEnd;
                    SkipWhite(text, ref lineEnd);
                    int spaceAdd = lineEnd - wordStart;
                    SkipNonWhite(text, ref lineEnd);

                    float add = font.MeasureString(text.Substring(wordStart, lineEnd - wordStart)).X;

                    if (totalLength > 0 && totalLength + add >= area.Width)
                    {
                        lineEnd = wordStart;
                        SkipWhite(text, ref lineEnd);
                        alignment = format.HorizontalAlignment;
                        break;
                    }
                    spaceCount  += spaceAdd;
                    totalLength += add;
                }

                DrawTextLine(batch, font, text.Substring(lineStart, lineEnd - lineStart), area, color, format, y, spaceCount, spaceWidth, totalLength, alignment);
                y += advance;

                lineStart = lineEnd;
                if (lineStart < text.Length && text[lineStart] == '\n')
                {
                    lineStart++;
                }
                lineCount++;
            }
        }
Example #2
0
        static void DrawTextLine(SpriteBatch batch, SpriteFont font, string text, Rectangle area, Color color, BlockAlignment format, float y, float spaceCount, float spaceWidth, float totalLength, Alignment alignment)
        {
            float x;

            switch (alignment)
            {
            case Alignment.Near:
                batch.DrawString(font, text, new Vector2(area.Left, y).Floor(), color);
                break;

            case Alignment.Far:
                x = area.Right - totalLength;
                batch.DrawString(font, text, new Vector2(x, y).Floor(), color);
                break;

            case Alignment.Center:
                x = (area.Width - totalLength) / 2 + area.Left;
                batch.DrawString(font, text, new Vector2(x, y).Floor(), color);
                break;

            default:
                throw new Exception("Unknown or invalid horizontal alignment value " + alignment);
            }
        }