add SpriteBatch text rendering method overloads accepting a StringBuilder directly
This commit is contained in:
parent
ab33f3bfe4
commit
8831788f14
|
@ -518,6 +518,61 @@ namespace Blarg.GameFramework.Graphics
|
|||
}
|
||||
}
|
||||
|
||||
public void Render(SpriteFont font, int x, int y, Color color, StringBuilder text)
|
||||
{
|
||||
Render(font, x, y, ref color, 1.0f, text);
|
||||
}
|
||||
|
||||
public void Render(SpriteFont font, int x, int y, ref Color color, StringBuilder text)
|
||||
{
|
||||
Render(font, x, y, ref color, 1.0f, text);
|
||||
}
|
||||
|
||||
public void Render(SpriteFont font, int x, int y, Color color, float scale, StringBuilder text)
|
||||
{
|
||||
Render(font, x, y, ref color, scale, text);
|
||||
}
|
||||
|
||||
public void Render(SpriteFont font, int x, int y, ref Color color, float scale, StringBuilder text)
|
||||
{
|
||||
float scaledLetterHeight = (float)font.LetterHeight * scale;
|
||||
|
||||
y = (int)FixYCoord(y, scaledLetterHeight);
|
||||
|
||||
float drawX = (float)x;
|
||||
float drawY = (float)y;
|
||||
|
||||
for (int i = 0; i < text.Length; ++i)
|
||||
{
|
||||
char c = text[i];
|
||||
if (c == '\n')
|
||||
{
|
||||
// new line
|
||||
drawX = (float)x;
|
||||
drawY -= scaledLetterHeight;
|
||||
}
|
||||
else
|
||||
{
|
||||
RectF texCoords;
|
||||
Rect dimensions;
|
||||
font.GetCharTexCoords(c, out texCoords);
|
||||
font.GetCharDimensions(c, out dimensions);
|
||||
|
||||
float scaledGlyphWidth = (float)dimensions.Width * scale;
|
||||
float scaledGlyphHeight = (float)dimensions.Height * scale;
|
||||
|
||||
AddSprite(
|
||||
font.Texture,
|
||||
drawX, drawY, drawX + scaledGlyphWidth, drawY + scaledGlyphHeight,
|
||||
texCoords.Left, texCoords.Top, texCoords.Right, texCoords.Bottom,
|
||||
ref color
|
||||
);
|
||||
|
||||
drawX += scaledGlyphWidth;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Render(SpriteFont font, Vector3 worldPosition, Color color, string text)
|
||||
{
|
||||
Render(font, ref worldPosition, ref color, 1.0f, text);
|
||||
|
@ -593,6 +648,35 @@ namespace Blarg.GameFramework.Graphics
|
|||
Render(font, ref worldPosition, ref color, scale, _buffer.ToString());
|
||||
}
|
||||
|
||||
public void Render(SpriteFont font, Vector3 worldPosition, Color color, StringBuilder text)
|
||||
{
|
||||
Render(font, ref worldPosition, ref color, 1.0f, text);
|
||||
}
|
||||
|
||||
public void Render(SpriteFont font, ref Vector3 worldPosition, ref Color color, StringBuilder text)
|
||||
{
|
||||
Render(font, ref worldPosition, ref color, 1.0f, text);
|
||||
}
|
||||
|
||||
public void Render(SpriteFont font, Vector3 worldPosition, Color color, float scale, StringBuilder text)
|
||||
{
|
||||
Render(font, ref worldPosition, ref color, scale, text);
|
||||
}
|
||||
|
||||
public void Render(SpriteFont font, ref Vector3 worldPosition, ref Color color, float scale, StringBuilder text)
|
||||
{
|
||||
var screenCoordinates = GraphicsDevice.ViewContext.Camera.Project(ref worldPosition, ref _previousModelView, ref _previousProjection);
|
||||
|
||||
int textWidth;
|
||||
int textHeight;
|
||||
font.MeasureString(out textWidth, out textHeight, scale, text);
|
||||
|
||||
screenCoordinates.X -= textWidth / 2;
|
||||
screenCoordinates.Y -= textHeight / 2;
|
||||
|
||||
Render(font, screenCoordinates.X, screenCoordinates.Y, ref color, scale, text);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Internal Sprite Addition / Management
|
||||
|
|
|
@ -97,36 +97,7 @@ namespace Blarg.GameFramework.Graphics
|
|||
|
||||
public void MeasureString(out int width, out int height, string format, params object[] args)
|
||||
{
|
||||
_buffer.Clear();
|
||||
_buffer.AppendFormat(format, args);
|
||||
|
||||
int textLength = _buffer.Length;
|
||||
|
||||
int currentMaxWidth = 0;
|
||||
int left = 0;
|
||||
int numLines = 1;
|
||||
|
||||
for (int i = 0; i < textLength; ++i)
|
||||
{
|
||||
char c = _buffer[i];
|
||||
if (c == '\n')
|
||||
{
|
||||
// new line
|
||||
left = 0;
|
||||
++numLines;
|
||||
}
|
||||
else
|
||||
{
|
||||
Rect charSize = new Rect();
|
||||
GetCharDimensions(c, out charSize);
|
||||
left += charSize.Width;
|
||||
}
|
||||
|
||||
currentMaxWidth = Math.Max(left, currentMaxWidth);
|
||||
}
|
||||
|
||||
width = currentMaxWidth;
|
||||
height = numLines * LetterHeight;
|
||||
MeasureString(out width, out height, 1.0f, format, args);
|
||||
}
|
||||
|
||||
public void MeasureString(out int width, out int height, float scale, string format, params object[] args)
|
||||
|
@ -164,5 +135,43 @@ namespace Blarg.GameFramework.Graphics
|
|||
width = (int)Math.Ceiling(currentMaxWidth);
|
||||
height = (int)(numLines * scaledLetterHeight);
|
||||
}
|
||||
|
||||
public void MeasureString(out int width, out int height, StringBuilder text)
|
||||
{
|
||||
MeasureString(out width, out height, 1.0f, text);
|
||||
}
|
||||
|
||||
public void MeasureString(out int width, out int height, float scale, StringBuilder text)
|
||||
{
|
||||
int textLength = text.Length;
|
||||
|
||||
float scaledLetterHeight = (float)LetterHeight * scale;
|
||||
|
||||
float currentMaxWidth = 0.0f;
|
||||
float left = 0.0f;
|
||||
int numLines = 1;
|
||||
|
||||
for (int i = 0; i < textLength; ++i)
|
||||
{
|
||||
char c = text[i];
|
||||
if (c == '\n')
|
||||
{
|
||||
// new line
|
||||
left = 0.0f;
|
||||
++numLines;
|
||||
}
|
||||
else
|
||||
{
|
||||
Rect charSize = new Rect();
|
||||
GetCharDimensions(c, out charSize);
|
||||
left += (float)charSize.Width * scale;
|
||||
}
|
||||
|
||||
currentMaxWidth = Math.Max(left, currentMaxWidth);
|
||||
}
|
||||
|
||||
width = (int)Math.Ceiling(currentMaxWidth);
|
||||
height = (int)(numLines * scaledLetterHeight);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue