Exemple #1
0
        private void DrawString(SpriteFont spriteFont, ref SpriteFont.StringProxy text, float fontSize, ref Vector2 position, ref Color4 color, float rotation, ref Vector2 origin, ref Vector2 scale, SpriteEffects effects, float layerDepth, TextAlignment alignment)
        {
            if (spriteFont == null)
            {
                throw new ArgumentNullException("spriteFont");
            }
            if (text.IsNull)
            {
                throw new ArgumentNullException("text");
            }
            if (fontSize < 0)
            {
                fontSize = spriteFont.Size;
            }

            // calculate the resolution ratio between the screen real size and the virtual resolution
            var viewportSize      = GraphicsDevice.Viewport;
            var virtualResolution = GetCurrentResolution();
            var resolutionRatio   = new Vector2(viewportSize.Width / virtualResolution.X, viewportSize.Height / virtualResolution.Y);

            scale.X = scale.X / resolutionRatio.X;
            scale.Y = scale.Y / resolutionRatio.Y;

            var fontSize2   = fontSize * (spriteFont.IsDynamic ? resolutionRatio : Vector2.One);
            var drawCommand = new SpriteFont.InternalDrawCommand(this, ref fontSize2, ref position, ref color, rotation, ref origin, ref scale, effects, layerDepth);

            // snap the position the closest 'real' pixel
            Vector2.Modulate(ref drawCommand.Position, ref resolutionRatio, out drawCommand.Position);
            drawCommand.Position.X  = (float)Math.Round(drawCommand.Position.X);
            drawCommand.Position.Y  = (float)Math.Round(drawCommand.Position.Y);
            drawCommand.Position.X /= resolutionRatio.X;
            drawCommand.Position.Y /= resolutionRatio.Y;

            spriteFont.InternalDraw(ref text, ref drawCommand, alignment);
        }
Exemple #2
0
        internal void DrawString(SpriteFont font, string text, ref SpriteFont.InternalUIDrawCommand drawCommand)
        {
            if (font == null)
            {
                throw new ArgumentNullException("font");
            }

            if (text == null)
            {
                throw new ArgumentNullException("text");
            }

            var proxy = new SpriteFont.StringProxy(text);

            // shift the string position so that it is written from the left/top corner of the element
            var leftTopCornerOffset = drawCommand.TextBoxSize / 2;
            var worldMatrix         = drawCommand.Matrix;

            worldMatrix.M41 -= worldMatrix.M11 * leftTopCornerOffset.X + worldMatrix.M21 * leftTopCornerOffset.Y;
            worldMatrix.M42 -= worldMatrix.M12 * leftTopCornerOffset.X + worldMatrix.M22 * leftTopCornerOffset.Y;
            worldMatrix.M43 -= worldMatrix.M13 * leftTopCornerOffset.X + worldMatrix.M23 * leftTopCornerOffset.Y;

            // transform the world matrix into the world view project matrix
            Matrix.MultiplyTo(ref worldMatrix, ref viewProjectionMatrix, out drawCommand.Matrix);

            // do not snap static fonts when real/virtual resolution does not match.
            if (font.FontType == SpriteFontType.SDF)
            {
                drawCommand.SnapText = false;
                float scaling = drawCommand.RequestedFontSize / font.Size;
                drawCommand.RealVirtualResolutionRatio = 1 / new Vector2(scaling, scaling);
            }
            else if ((font.FontType == SpriteFontType.Static))
            {
                if ((drawCommand.RealVirtualResolutionRatio.X != 1 || drawCommand.RealVirtualResolutionRatio.Y != 1))
                {
                    drawCommand.SnapText = false;                     // we don't want snapping of the resolution of the screen does not match virtual resolution. (character alignment problems)
                }
                drawCommand.RealVirtualResolutionRatio = Vector2.One; // ensure that static font are not scaled internally
            }

            // snap draw start position to prevent characters to be drawn in between two pixels
            if (drawCommand.SnapText)
            {
                var invW = 1.0f / drawCommand.Matrix.M44;
                var backBufferHalfWidth  = GraphicsContext.CommandList.RenderTarget.ViewWidth / 2;
                var backBufferHalfHeight = GraphicsContext.CommandList.RenderTarget.ViewHeight / 2;

                drawCommand.Matrix.M41 *= invW;
                drawCommand.Matrix.M42 *= invW;
                drawCommand.Matrix.M41  = (float)(Math.Round(drawCommand.Matrix.M41 * backBufferHalfWidth) / backBufferHalfWidth);
                drawCommand.Matrix.M42  = (float)(Math.Round(drawCommand.Matrix.M42 * backBufferHalfHeight) / backBufferHalfHeight);
                drawCommand.Matrix.M41 /= invW;
                drawCommand.Matrix.M42 /= invW;
            }

            font.InternalUIDraw(GraphicsContext.CommandList, ref proxy, ref drawCommand);
        }
Exemple #3
0
        internal void DrawString(SpriteFont font, string text, ref SpriteFont.InternalUIDrawCommand drawCommand)
        {
            if (font == null)
            {
                throw new ArgumentNullException("font");
            }

            if (text == null)
            {
                throw new ArgumentNullException("text");
            }

            var proxy = new SpriteFont.StringProxy(text);

            // shift the string position so that it is written from the left/top corner of the element
            var offsets     = drawCommand.Size / 2;
            var worldMatrix = drawCommand.Matrix;

            worldMatrix.M41 -= worldMatrix.M11 * offsets.X + worldMatrix.M21 * offsets.Y;
            worldMatrix.M42 -= worldMatrix.M12 * offsets.X + worldMatrix.M22 * offsets.Y;
            worldMatrix.M43 -= worldMatrix.M13 * offsets.X + worldMatrix.M23 * offsets.Y;

            // transform the world matrix into the world view project matrix
            Matrix.MultiplyTo(ref worldMatrix, ref viewProjectionMatrix, out drawCommand.Matrix);

            // do not snap static fonts when real/virtual resolution does not match.
            if (!font.IsDynamic && (drawCommand.FontScale.X != 1 || drawCommand.FontScale.Y != 1))
            {
                drawCommand.SnapText  = false;       // we don't want snapping of the resolution of the screen does not match virtual resolution. (character alignment problems)
                drawCommand.FontScale = Vector2.One; // ensure that static font are not scaled internally
            }

            // snap draw start position to prevent characters to be drawn in between two pixels
            if (drawCommand.SnapText)
            {
                var invW = 1.0f / drawCommand.Matrix.M44;
                var backBufferHalfWidth  = GraphicsDevice.BackBuffer.ViewWidth / 2;
                var backBufferHalfHeight = GraphicsDevice.BackBuffer.ViewHeight / 2;

                drawCommand.Matrix.M41 *= invW;
                drawCommand.Matrix.M42 *= invW;
                drawCommand.Matrix.M41  = (float)(Math.Round(drawCommand.Matrix.M41 * backBufferHalfWidth) / backBufferHalfWidth);
                drawCommand.Matrix.M42  = (float)(Math.Round(drawCommand.Matrix.M42 * backBufferHalfHeight) / backBufferHalfHeight);
                drawCommand.Matrix.M41 /= invW;
                drawCommand.Matrix.M42 /= invW;
            }

            font.InternalUIDraw(ref proxy, ref drawCommand);
        }
Exemple #4
0
 private void DrawString(SpriteFont spriteFont, ref SpriteFont.StringProxy text, float fontSize, ref Vector2 position, ref Color4 color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth, TextAlignment alignment)
 {
     DrawString(spriteFont, ref text, fontSize, ref position, ref color, rotation, ref origin, ref scale, effects, layerDepth, alignment);
 }
Exemple #5
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="spriteFont">A font for displaying text.</param>
        /// <param name="text">Text string.</param>
        /// <param name="fontSize">The font size in pixels (ignored in the case of static fonts)</param>
        /// <param name="position">The location (in screen coordinates) to draw the sprite.</param>
        /// <param name="color">The color to tint a sprite. Use Color.White for full color with no tinting.</param>
        /// <param name="rotation">Specifies the angle (in radians) to rotate the sprite about its center.</param>
        /// <param name="origin">The sprite origin in virtual pixels; the default is (0,0) which represents the upper-left corner.</param>
        /// <param name="scale">Scale factor.</param>
        /// <param name="effects">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>
        /// <param name="alignment">Describes how to align the text to draw</param>
        public void DrawString(SpriteFont spriteFont, StringBuilder text, float fontSize, Vector2 position, Color4 color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth, TextAlignment alignment)
        {
            var proxy = new SpriteFont.StringProxy(text);

            DrawString(spriteFont, ref proxy, fontSize, ref position, ref color, rotation, ref origin, ref scale, effects, layerDepth, alignment);
        }
Exemple #6
0
        /// <summary>Adds a string to a batch of sprites for rendering using the specified font, text, position, and color.</summary>
        /// <param name="spriteFont">A font for displaying text.</param>
        /// <param name="text">Text string.</param>
        /// <param name="fontSize">The font size in pixels (ignored in the case of static fonts)</param>
        /// <param name="position">The location (in screen coordinates) to draw the sprite.</param>
        /// <param name="color">The color to tint a sprite. Use Color.White for full color with no tinting.</param>
        /// <param name="alignment">Describes how to align the text to draw</param>
        public void DrawString(SpriteFont spriteFont, StringBuilder text, float fontSize, Vector2 position, Color4 color, TextAlignment alignment = TextAlignment.Left)
        {
            var proxy = new SpriteFont.StringProxy(text);

            DrawString(spriteFont, ref proxy, fontSize, ref position, ref color, 0, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f, alignment);
        }