/// <summary> /// Draws a text at the specified position nested in a begin/end. Suitable for quick text drawing. /// Automatically fits the text into the specifiec space. /// </summary> public static void DrawStringDirect(IRenderConfiguration renderConfig, SpriteFont font, string text, float x, float y, float width, float height, Color color, UITextAlignHorizontal horizontalAlignment = UITextAlignHorizontal.Center, UITextAlignVertical verticalAlignment = UITextAlignVertical.Center) { DrawStringDirectInternal(renderConfig, font, text, x, y, width, height, color, horizontalAlignment, verticalAlignment); }
private static void DrawStringDirectInternal(IRenderConfiguration renderConfig, SpriteFont font1, string text, float x, float y, float width, float height, Color color, UITextAlignHorizontal horizontalAlignment, UITextAlignVertical verticalAlignment) { if (renderConfig == null) { throw new ArgumentNullException(nameof(renderConfig)); } Vector2 Scale = renderConfig.Scale; Vector2 ScaledOffset = renderConfig.ScaledOffset; string[] lines = SplitText(text, (int)width, font1); int lineHeight = font1 != null ? font1.LineSpacing : 0; int totalHeight = lineHeight * lines.Length; renderConfig.PrimitiveRenderManager.SpriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.AnisotropicWrap, DepthStencilState.None, RasterizerState.CullCounterClockwise); float yGap = -2; float yPos = 0; switch (verticalAlignment) { case UITextAlignVertical.Top: yPos = y; break; case UITextAlignVertical.Center: yPos = y + (height / 2 - totalHeight / 2); break; case UITextAlignVertical.Bottom: yPos = y + (height - totalHeight); break; } for (int i = 0; i < lines.Length; i++) { Vector2 lineSize = MeasureString(lines[i], font1); float xPos = 0; switch (horizontalAlignment) { case UITextAlignHorizontal.Left: xPos = x; break; case UITextAlignHorizontal.Center: xPos = x + (width / 2 - lineSize.X / 2); break; case UITextAlignHorizontal.Right: xPos = x + (width - lineSize.X); break; } if (font1 != null) { renderConfig.PrimitiveRenderManager.SpriteBatch.DrawString(font1, lines[i], new Vector2(ScaledOffset.X + (xPos * Scale.X), ScaledOffset.Y + (yPos * Scale.Y)), color, 0, Vector2.Zero, new Vector2(Scale.X, Scale.Y), SpriteEffects.None, 1.0f); } yPos += lineHeight + yGap; } renderConfig.PrimitiveRenderManager.SpriteBatch.End(); }