Esempio n. 1
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);
        }
Esempio n. 2
0
        /// <summary>
        /// Batch the draws required to display the provided text to the draw list.
        /// </summary>
        /// <param name="fontSize">The size to use when rendering the font</param>
        /// <param name="fontInternalScale">The size of the font glyphs to use in pixels</param>
        /// <param name="text">The text to draw on the screen</param>
        /// <param name="worldMatrix">The world matrix of the element</param>
        /// <param name="elementSize">The 2D size of the element to draw in virtual pixels</param>
        /// <param name="color">The color of the text to draw</param>
        /// <param name="alignment">The alignment of the text to draw</param>
        /// <param name="depthBias">The depth bias of the ui element</param>
        /// <param name="font">The fond to use to draw the text</param>
        /// <param name="snapText">Indicate if the rendered string should be snapped to the closed pixel.</param>
        public void DrawString(SpriteFont font, string text, float fontSize, ref Vector2 fontInternalScale, ref Matrix worldMatrix, ref Vector2 elementSize,
                               ref Color color, TextAlignment alignment, int depthBias, bool snapText)
        {
            var drawCommand = new SpriteFont.InternalUIDrawCommand
            {
                WorldMatrix = worldMatrix,
                Batch       = this,
                Color       = color,
                DepthBias   = depthBias,
                FontScale   = fontInternalScale,
                SnapText    = snapText,
                Alignment   = alignment,
                Size        = elementSize,
            };

            DrawString(font, text, ref drawCommand);
        }
Esempio n. 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;

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

            font.InternalUIDraw(ref proxy, ref drawCommand);
        }