Example #1
0
 public static void Transform(Vector2[] sourceArray, ref Matrix2D matrix, Vector2[] destinationArray)
 {
     Transform(sourceArray, 0, ref matrix, destinationArray, 0, sourceArray.Length);
 }
Example #2
0
        public void DrawInto(Batcher batcher, ref FontCharacterSource text, Vector2 position, Color color,
                             float rotation, Vector2 origin, Vector2 scale, SpriteEffects effect, float depth)
        {
            var flipAdjustment = Vector2.Zero;

            var flippedVert = (effect & SpriteEffects.FlipVertically) == SpriteEffects.FlipVertically;
            var flippedHorz = (effect & SpriteEffects.FlipHorizontally) == SpriteEffects.FlipHorizontally;

            if (flippedVert || flippedHorz)
            {
                Vector2 size;
                MeasureString(ref text, out size);

                if (flippedHorz)
                {
                    origin.X        *= -1;
                    flipAdjustment.X = -size.X;
                }

                if (flippedVert)
                {
                    origin.Y        *= -1;
                    flipAdjustment.Y = _font.LineSpacing - size.Y;
                }
            }

            // TODO: This looks excessive... i suspect we could do most of this with simple vector math and avoid this much matrix work.
            var requiresTransformation = flippedHorz || flippedVert || rotation != 0f || scale != Vector2.One;

            if (requiresTransformation)
            {
                Matrix2D temp;
                Matrix2D.CreateTranslation(-origin.X, -origin.Y, out _transformationMatrix);
                Matrix2D.CreateScale((flippedHorz ? -scale.X : scale.X), (flippedVert ? -scale.Y : scale.Y), out temp);
                Matrix2D.Multiply(ref _transformationMatrix, ref temp, out _transformationMatrix);
                Matrix2D.CreateTranslation(flipAdjustment.X, flipAdjustment.Y, out temp);
                Matrix2D.Multiply(ref temp, ref _transformationMatrix, out _transformationMatrix);
                Matrix2D.CreateRotation(rotation, out temp);
                Matrix2D.Multiply(ref _transformationMatrix, ref temp, out _transformationMatrix);
                Matrix2D.CreateTranslation(position.X, position.Y, out temp);
                Matrix2D.Multiply(ref _transformationMatrix, ref temp, out _transformationMatrix);
            }

            // Get the default glyph here once.
            SpriteFont.Glyph?defaultGlyph = null;
            if (_font.DefaultCharacter.HasValue)
            {
                defaultGlyph = _glyphs[_font.DefaultCharacter.Value];
            }

            var currentGlyph     = SpriteFont.Glyph.Empty;
            var offset           = requiresTransformation ? Vector2.Zero : position - origin;
            var firstGlyphOfLine = true;

            for (var i = 0; i < text.Length; ++i)
            {
                var c = text[i];

                if (c == '\r')
                {
                    continue;
                }

                if (c == '\n')
                {
                    offset.X         = requiresTransformation ? 0f : position.X - origin.X;
                    offset.Y        += _font.LineSpacing;
                    firstGlyphOfLine = true;
                    continue;
                }

                if (!_glyphs.TryGetValue(c, out currentGlyph))
                {
                    if (!defaultGlyph.HasValue)
                    {
                        throw new ArgumentException("Errors.TextContainsUnresolvableCharacters", "text");
                    }

                    currentGlyph = defaultGlyph.Value;
                }

                // The first character on a line might have a negative left side bearing.
                // In this scenario, SpriteBatch/SpriteFont normally offset the text to the right,
                // so that text does not hang off the left side of its rectangle.
                if (firstGlyphOfLine)
                {
                    offset.X        += Math.Max(currentGlyph.LeftSideBearing, 0);
                    firstGlyphOfLine = false;
                }
                else
                {
                    offset.X += _font.Spacing + currentGlyph.LeftSideBearing;
                }

                var p = offset;

                if (flippedHorz)
                {
                    p.X += currentGlyph.BoundsInTexture.Width;
                }
                p.X += currentGlyph.Cropping.X;

                if (flippedVert)
                {
                    p.Y += currentGlyph.BoundsInTexture.Height - _font.LineSpacing;
                }
                p.Y += currentGlyph.Cropping.Y;

                // transform our point if we need to
                if (requiresTransformation)
                {
                    Vector2Ext.Transform(ref p, ref _transformationMatrix, out p);
                }

                var destRect = RectangleExt.FromFloats(p.X, p.Y,
                                                       currentGlyph.BoundsInTexture.Width * scale.X,
                                                       currentGlyph.BoundsInTexture.Height * scale.Y);

                batcher.Draw(_font.Texture, destRect, currentGlyph.BoundsInTexture, color, rotation, Vector2.Zero,
                             effect, depth);

                offset.X += currentGlyph.Width + currentGlyph.RightSideBearing;
            }
        }
Example #3
0
 public static Vector2 Transform(Vector2 position, Matrix2D matrix)
 {
     return(new Vector2((position.X * matrix.M11) + (position.Y * matrix.M21) + matrix.M31,
                        (position.X * matrix.M12) + (position.Y * matrix.M22) + matrix.M32));
 }