public CachedStringRender(string text, BitmapFont font, Rectangle destinationRectangle, Color color, bool wrap, bool stroke, int strokeDistance = 1, DrawUtil.HorizontalAlignment horizontalAlignment = DrawUtil.HorizontalAlignment.Left, DrawUtil.VerticalAlignment verticalAlignment = DrawUtil.VerticalAlignment.Middle) { Text = text; Font = font; DestinationRectangle = new Rectangle(Point.Zero, destinationRectangle.Size); Color = color; Wrap = wrap; Stroke = stroke; StrokeDistance = strokeDistance; HorizontalAlignment = horizontalAlignment; VerticalAlignment = verticalAlignment; }
public static CachedStringRender GetCachedStringRender(string text, BitmapFont font, Rectangle destinationRectangle, Color color, bool wrap, bool stroke, int strokeDistance = 1, DrawUtil.HorizontalAlignment horizontalAlignment = DrawUtil.HorizontalAlignment.Left, DrawUtil.VerticalAlignment verticalAlignment = DrawUtil.VerticalAlignment.Middle) { var checkCSR = new CachedStringRender(text, font, destinationRectangle, color, wrap, stroke, strokeDistance, horizontalAlignment, verticalAlignment); int csrHash = checkCSR.GetHashCode(); if (!_cachedStringRenders.ContainsKey(csrHash)) { checkCSR.InitRender(); _cachedStringRenders.Add(csrHash, checkCSR); } return(_cachedStringRenders[csrHash]); }
public static void DrawStringOnCtrl( this SpriteBatch spriteBatch, Control ctrl, string text, BitmapFont font, Rectangle destinationRectangle, Color color, bool wrap = false, DrawUtil.HorizontalAlignment horizontalAlignment = DrawUtil.HorizontalAlignment.Left, DrawUtil.VerticalAlignment verticalAlignment = DrawUtil.VerticalAlignment.Middle ) { DrawStringOnCtrl(spriteBatch, ctrl, text, font, destinationRectangle, color, wrap, false, 1, horizontalAlignment, verticalAlignment); }
public static void DrawStringOnCtrl(this SpriteBatch spriteBatch, Control ctrl, string text, BitmapFont font, Rectangle destinationRectangle, Color color, bool wrap, bool stroke, int strokeDistance = 1, DrawUtil.HorizontalAlignment horizontalAlignment = DrawUtil.HorizontalAlignment.Left, DrawUtil.VerticalAlignment verticalAlignment = DrawUtil.VerticalAlignment.Middle) { if (string.IsNullOrEmpty(text)) { return; } text = wrap ? DrawUtil.WrapText(font, text, destinationRectangle.Width) : text; // TODO: This does not account for vertical alignment if (horizontalAlignment != DrawUtil.HorizontalAlignment.Left && (wrap || text.Contains("\n"))) { using (StringReader reader = new StringReader(text)) { string line; int lineHeightDiff = 0; while (destinationRectangle.Height - lineHeightDiff > 0 && (line = reader.ReadLine()) != null) { DrawStringOnCtrl(spriteBatch, ctrl, line, font, destinationRectangle.Add(0, lineHeightDiff, 0, -0), color, wrap, stroke, strokeDistance, horizontalAlignment, verticalAlignment); lineHeightDiff += font.LineHeight; } } return; } Vector2 textSize = font.MeasureString(text); destinationRectangle = destinationRectangle.ToBounds(ctrl.AbsoluteBounds); int xPos = destinationRectangle.X; int yPos = destinationRectangle.Y; switch (horizontalAlignment) { case DrawUtil.HorizontalAlignment.Center: xPos += destinationRectangle.Width / 2 - (int)textSize.X / 2; break; case DrawUtil.HorizontalAlignment.Right: xPos += destinationRectangle.Width - (int)textSize.X; break; } switch (verticalAlignment) { case DrawUtil.VerticalAlignment.Middle: yPos += destinationRectangle.Height / 2 - (int)textSize.Y / 2; break; case DrawUtil.VerticalAlignment.Bottom: yPos += destinationRectangle.Height - (int)textSize.Y; break; } var textPos = new Vector2(xPos, yPos); if (stroke) { spriteBatch.DrawString(font, text, textPos.OffsetBy(0, -strokeDistance), Color.Black * ctrl.AbsoluteOpacity()); spriteBatch.DrawString(font, text, textPos.OffsetBy(strokeDistance, -strokeDistance), Color.Black * ctrl.AbsoluteOpacity()); spriteBatch.DrawString(font, text, textPos.OffsetBy(strokeDistance, 0), Color.Black * ctrl.AbsoluteOpacity()); spriteBatch.DrawString(font, text, textPos.OffsetBy(strokeDistance, strokeDistance), Color.Black * ctrl.AbsoluteOpacity()); spriteBatch.DrawString(font, text, textPos.OffsetBy(0, strokeDistance), Color.Black * ctrl.AbsoluteOpacity()); spriteBatch.DrawString(font, text, textPos.OffsetBy(-strokeDistance, strokeDistance), Color.Black * ctrl.AbsoluteOpacity()); spriteBatch.DrawString(font, text, textPos.OffsetBy(-strokeDistance, 0), Color.Black * ctrl.AbsoluteOpacity()); spriteBatch.DrawString(font, text, textPos.OffsetBy(-strokeDistance, -strokeDistance), Color.Black * ctrl.AbsoluteOpacity()); } spriteBatch.DrawString(font, text, textPos, color * ctrl.AbsoluteOpacity()); }