コード例 #1
0
        internal void DrawString(SpriteFont font, string text, ref SpriteFont.InternalUIDrawCommand drawCommand)
        {
            if (font == null)
            {
                throw new ArgumentNullException(nameof(font));
            }
            if (text == null)
            {
                throw new ArgumentNullException(nameof(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 / 2f;

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

            // do not snap static fonts when real/virtual resolution does not match.
            if (!drawCommand.IsFullscreen)
            {
                // we are drawing in 3D, don't snap or scale
                drawCommand.SnapText = false;
                drawCommand.RealVirtualResolutionRatio.X = 1f;
                drawCommand.RealVirtualResolutionRatio.Y = 1f;
            }
            else 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);
        }
コード例 #2
0
        internal void DrawString(SpriteFont font, string text, ref SpriteFont.InternalUIDrawCommand drawCommand)
        {
            if (font == null)
            {
                throw new ArgumentNullException(nameof(font));
            }
            if (text == null)
            {
                throw new ArgumentNullException(nameof(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 / 2f;
            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);

            drawCommand.SnapText = false;
            float scaling = drawCommand.RequestedFontSize / font.Size;

            drawCommand.RealVirtualResolutionRatio = 1 / new Vector2(scaling, scaling);

            // 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);
        }