public static void DrawString(this SpriteBatch spriteBatch, BitmapFont bitmapFont, string text, Vector2 position, Color color)
        {
            var dx = position.X;
            var dy = position.Y;

            foreach (char character in text)
            {
                var fontRegion = bitmapFont.GetCharacterRegion(character);

                if (fontRegion != null)
                {
                    var fontChar = fontRegion.FontCharacter;
                    var charPosition = new Vector2(dx + fontChar.XOffset, dy + fontChar.YOffset);

                    spriteBatch.Draw(fontRegion.TextureRegion, charPosition, color);
                    dx += fontChar.XAdvance;
                }

                if (character == '\n')
                {
                    dy += bitmapFont.LineHeight;
                    dx = position.X;
                }
            }
        }
        public static void DrawString(this SpriteBatch spriteBatch, BitmapFont bitmapFont, string text, Vector2 position, Color color, int wrapWidth)
        {
            var dx        = position.X;
            var dy        = position.Y;
            var sentences = text.Split(new[] { '\n' }, StringSplitOptions.None);

            foreach (var sentence in sentences)
            {
                var words = sentence.Split(new[] { ' ' }, StringSplitOptions.None);

                for (var i = 0; i < words.Length; i++)
                {
                    var word = words[i];
                    var size = bitmapFont.GetStringRectangle(word, Vector2.Zero);

                    if (i != 0 && dx + size.Width >= wrapWidth)
                    {
                        dy += bitmapFont.LineHeight;
                        dx  = position.X;
                    }

                    DrawString(spriteBatch, bitmapFont, word, new Vector2(dx, dy), color);
                    dx += size.Width + bitmapFont.GetCharacterRegion(' ').XAdvance;
                }

                dx  = position.X;
                dy += bitmapFont.LineHeight;
            }
        }
        public static void DrawString(this SpriteBatch spriteBatch, BitmapFont bitmapFont, string text, Vector2 position, Color color, int wrapWidth)
        {
            var dx = position.X;
            var dy = position.Y;
            var sentences = text.Split(new[] {'\n'}, StringSplitOptions.None);

            foreach (var sentence in sentences)
            {
                var words = sentence.Split(new[] { ' ' }, StringSplitOptions.None);

                for (var i = 0; i < words.Length; i++)
                {
                    var word = words[i];
                    var size = bitmapFont.GetStringRectangle(word, Vector2.Zero);

                    if (i != 0 && dx + size.Width >= wrapWidth)
                    {
                        dy += bitmapFont.LineHeight;
                        dx = position.X;
                    }

                    DrawString(spriteBatch, bitmapFont, word, new Vector2(dx, dy), color);
                    dx += size.Width + bitmapFont.GetCharacterRegion(' ').FontCharacter.XAdvance;
                }

                dx = position.X;
                dy += bitmapFont.LineHeight;
            }
        }
示例#4
0
        public static void DrawString(this SpriteBatch spriteBatch, BitmapFont bitmapFont, string text, Vector2 position, Color color)
        {
            var dx = position.X;
            var dy = position.Y;

            foreach (char character in text)
            {
                var fontRegion = bitmapFont.GetCharacterRegion(character);

                if (fontRegion != null)
                {
                    var fontChar     = fontRegion.FontCharacter;
                    var charPosition = new Vector2(dx + fontChar.XOffset, dy + fontChar.YOffset);

                    spriteBatch.Draw(fontRegion.TextureRegion, charPosition, color);
                    dx += fontChar.XAdvance;
                }

                if (character == '\n')
                {
                    dy += bitmapFont.LineHeight;
                    dx  = position.X;
                }
            }
        }
示例#5
0
            public bool MoveNext()
            {
                if (++_index >= _text.Length)
                {
                    return(false);
                }

                var character = GetUnicodeCodePoint(_text, ref _index);

                _currentGlyph = new BitmapFontGlyph
                {
                    Character  = character,
                    FontRegion = _font.GetCharacterRegion(character),
                    Position   = _position + _positionDelta
                };

                if (_currentGlyph.FontRegion != null)
                {
                    _currentGlyph.Position.X += _currentGlyph.FontRegion.XOffset;
                    _currentGlyph.Position.Y += _currentGlyph.FontRegion.YOffset;
                    _positionDelta.X         += _currentGlyph.FontRegion.XAdvance + _font.LetterSpacing;
                }

                if (UseKernings && _previousGlyph.HasValue && _previousGlyph.Value.FontRegion != null)
                {
                    int amount;
                    if (_previousGlyph.Value.FontRegion.Kernings.TryGetValue(character, out amount))
                    {
                        _positionDelta.X         += amount;
                        _currentGlyph.Position.X += amount;
                    }
                }

                _previousGlyph = _currentGlyph;

                if (character != '\n')
                {
                    return(true);
                }

                _positionDelta.Y += _font.LineHeight;
                _positionDelta.X  = _position.X;
                _previousGlyph    = null;

                return(true);
            }
示例#6
0
        public bool MoveNext()
        {
            _index++;
            if (_index >= _iterator.Length)
            {
                return(false);
            }

            int character           = _iterator.GetCharacter32(ref _index);
            BitmapFontRegion region = _font.GetCharacterRegion(character);
            Vector2          newPos = _position + _positionDelta;

            if (region != null)
            {
                newPos.X         += region.XOffset;
                newPos.Y         += region.YOffset;
                _positionDelta.X += region.XAdvance + _font.LetterSpacing;
            }

            if (_hasPreviousGlyph)
            {
                if (BitmapFont.UseKernings && _previousGlyph.FontRegion != null)
                {
                    if (_previousGlyph.FontRegion.Kernings.TryGetValue(character, out int amount))
                    {
                        _positionDelta.X += amount;
                    }
                }
            }

            CurrentGlyph      = new Glyph(character, newPos, region);
            _previousGlyph    = CurrentGlyph;
            _hasPreviousGlyph = true;

            if (character == '\n')
            {
                _positionDelta.Y += _font.LineHeight;
                _positionDelta.X  = 0;
                _hasPreviousGlyph = false;
            }
            return(true);
        }
示例#7
0
        /// <summary>
        ///     Adds a string to a batch of sprites for rendering using the specified font, text, position, color, rotation,
        ///     origin, scale, effects and layer.
        /// </summary>
        /// <param name="spriteBatch"></param>
        /// <param name="bitmapFont">A font for displaying text.</param>
        /// <param name="text">The text message to display.</param>
        /// <param name="position">The location (in screen coordinates) to draw the text.</param>
        /// <param name="color">
        ///     The <see cref="Color" /> to tint a sprite. Use <see cref="Color.White" /> for full color with no
        ///     tinting.
        /// </param>
        /// <param name="rotation">Specifies the angle (in radians) to rotate the text about its origin.</param>
        /// <param name="origin">The origin for each letter; the default is (0,0) which represents the upper-left corner.</param>
        /// <param name="scale">Scale factor.</param>
        /// <param name="effect">Effects to apply.</param>
        /// <param name="layerDepth">
        ///     The depth of a layer. By default, 0 represents the front layer and 1 represents a back layer.
        ///     Use SpriteSortMode if you want sprites to be sorted during drawing.
        /// </param>
        public static void DrawString(this SpriteBatch spriteBatch, BitmapFont bitmapFont, string text, Vector2 position,
                                      Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effect, float layerDepth)
        {
            if (text == null)
            {
                throw new ArgumentNullException(nameof(text));
            }
            if (effect != SpriteEffects.None)
            {
                throw new NotSupportedException($"{effect} is not currently supported for {nameof(BitmapFont)}");
            }

            var dx = position.X;
            var dy = position.Y;

            for (var i = 0; i < text.Length; i++)
            {
                var character  = BitmapFont.GetUnicodeCodePoint(text, i);
                var fontRegion = bitmapFont.GetCharacterRegion(character);

                if (fontRegion != null)
                {
                    var characterPosition = new Vector2(dx + fontRegion.XOffset, dy + fontRegion.YOffset);
                    var characterOrigin   = position - characterPosition + origin;

                    spriteBatch.Draw(fontRegion.TextureRegion, position, color, rotation, characterOrigin, scale, effect, layerDepth);

                    dx += fontRegion.XAdvance + bitmapFont.LetterSpacing;
                }

                if (character == '\n')
                {
                    dy += bitmapFont.LineHeight;
                    dx  = position.X;
                }
            }
        }