#region File Description
//-----------------------------------------------------------------------------
// Fonts.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
#region Using Statements
using System;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using System.Text;
using Microsoft.Xna.Framework;
using System.Collections.Generic;
#endregion
namespace RolePlaying
{
///
/// Static storage of SpriteFont objects and colors for use throughout the game.
///
static class Fonts
{
#region Fonts
private static SpriteFont headerFont;
public static SpriteFont HeaderFont
{
get { return headerFont; }
}
private static SpriteFont playerNameFont;
public static SpriteFont PlayerNameFont
{
get { return playerNameFont; }
}
private static SpriteFont debugFont;
public static SpriteFont DebugFont
{
get { return debugFont; }
}
private static SpriteFont buttonNamesFont;
public static SpriteFont ButtonNamesFont
{
get { return buttonNamesFont; }
}
private static SpriteFont descriptionFont;
public static SpriteFont DescriptionFont
{
get { return descriptionFont; }
}
private static SpriteFont gearInfoFont;
public static SpriteFont GearInfoFont
{
get { return gearInfoFont; }
}
private static SpriteFont damageFont;
public static SpriteFont DamageFont
{
get { return damageFont; }
}
private static SpriteFont playerStatisticsFont;
public static SpriteFont PlayerStatisticsFont
{
get { return playerStatisticsFont; }
}
private static SpriteFont hudDetailFont;
public static SpriteFont HudDetailFont
{
get { return hudDetailFont; }
}
private static SpriteFont captionFont;
public static SpriteFont CaptionFont
{
get { return captionFont; }
}
#endregion
#region Font Colors
public static readonly Color CountColor = new Color(79, 24, 44);
public static readonly Color TitleColor = new Color(59, 18, 6);
public static readonly Color CaptionColor = new Color(228, 168, 57);
public static readonly Color HighlightColor = new Color(223, 206, 148);
public static readonly Color DisplayColor = new Color(68, 32, 19);
public static readonly Color DescriptionColor = new Color(0, 0, 0);
public static readonly Color RestrictionColor = new Color(0, 0, 0);
public static readonly Color ModifierColor = new Color(0, 0, 0);
public static readonly Color MenuSelectedColor = new Color(248, 218, 127);
#endregion
#region Initialization
///
/// Load the fonts from the content pipeline.
///
public static void LoadContent(ContentManager contentManager)
{
// check the parameters
if (contentManager == null)
{
throw new ArgumentNullException("contentManager");
}
// load each font from the content pipeline
buttonNamesFont = contentManager.Load("Fonts/ButtonNamesFont");
captionFont = contentManager.Load("Fonts/CaptionFont");
damageFont = contentManager.Load("Fonts/DamageFont");
debugFont = contentManager.Load("Fonts/DebugFont");
descriptionFont = contentManager.Load("Fonts/DescriptionFont");
gearInfoFont = contentManager.Load("Fonts/GearInfoFont");
headerFont = contentManager.Load("Fonts/HeaderFont");
hudDetailFont = contentManager.Load("Fonts/HudDetailFont");
playerNameFont = contentManager.Load("Fonts/PlayerNameFont");
playerStatisticsFont =
contentManager.Load("Fonts/PlayerStatisticsFont");
}
///
/// Release all references to the fonts.
///
public static void UnloadContent()
{
buttonNamesFont = null;
captionFont = null;
damageFont = null;
debugFont = null;
descriptionFont = null;
gearInfoFont = null;
headerFont = null;
hudDetailFont = null;
playerNameFont = null;
playerStatisticsFont = null;
}
#endregion
#region Text Helper Methods
///
/// Adds newline characters to a string so that it fits within a certain size.
///
/// The text to be modified.
///
/// The maximum length of a single line of text.
///
/// The maximum number of lines to draw.
/// The new string, with newline characters if needed.
public static string BreakTextIntoLines(string text,
int maximumCharactersPerLine, int maximumLines)
{
if (maximumLines <= 0)
{
throw new ArgumentOutOfRangeException("maximumLines");
}
if (maximumCharactersPerLine <= 0)
{
throw new ArgumentOutOfRangeException("maximumCharactersPerLine");
}
// if the string is trivial, then this is really easy
if (String.IsNullOrEmpty(text))
{
return String.Empty;
}
// if the text is short enough to fit on one line, then this is still easy
if (text.Length < maximumCharactersPerLine)
{
return text;
}
// construct a new string with carriage returns
StringBuilder stringBuilder = new StringBuilder(text);
int currentLine = 0;
int newLineIndex = 0;
while (((text.Length - newLineIndex) > maximumCharactersPerLine) &&
(currentLine < maximumLines))
{
text.IndexOf(' ', 0);
int nextIndex = newLineIndex;
while ((nextIndex >= 0) && (nextIndex < maximumCharactersPerLine))
{
newLineIndex = nextIndex;
nextIndex = text.IndexOf(' ', newLineIndex + 1);
}
stringBuilder.Replace(' ', '\n', newLineIndex, 1);
currentLine++;
}
return stringBuilder.ToString();
}
///
/// Adds new-line characters to a string to make it fit.
///
/// The text to be drawn.
///
/// The maximum length of a single line of text.
///
public static string BreakTextIntoLines(string text,
int maximumCharactersPerLine)
{
// check the parameters
if (maximumCharactersPerLine <= 0)
{
throw new ArgumentOutOfRangeException("maximumCharactersPerLine");
}
// if the string is trivial, then this is really easy
if (String.IsNullOrEmpty(text))
{
return String.Empty;
}
// if the text is short enough to fit on one line, then this is still easy
if (text.Length < maximumCharactersPerLine)
{
return text;
}
// construct a new string with carriage returns
StringBuilder stringBuilder = new StringBuilder(text);
int currentLine = 0;
int newLineIndex = 0;
while (((text.Length - newLineIndex) > maximumCharactersPerLine))
{
text.IndexOf(' ', 0);
int nextIndex = newLineIndex;
while ((nextIndex >= 0) && (nextIndex < maximumCharactersPerLine))
{
newLineIndex = nextIndex;
nextIndex = text.IndexOf(' ', newLineIndex + 1);
}
stringBuilder.Replace(' ', '\n', newLineIndex, 1);
currentLine++;
}
return stringBuilder.ToString();
}
///
/// Break text up into separate lines to make it fit.
///
/// The text to be broken up.
/// The font used ot measure the width of the text.
/// The maximum width of each line, in pixels.
public static List BreakTextIntoList(string text, SpriteFont font,
int rowWidth)
{
// check parameters
if (font == null)
{
throw new ArgumentNullException("font");
}
if (rowWidth <= 0)
{
throw new ArgumentOutOfRangeException("rowWidth");
}
// create the list
List lines = new List();
// check for trivial text
if (String.IsNullOrEmpty("text"))
{
lines.Add(String.Empty);
return lines;
}
// check for text that fits on a single line
if (font.MeasureString(text).X <= rowWidth)
{
lines.Add(text);
return lines;
}
// break the text up into words
string[] words = text.Split(' ');
// add words until they go over the length
int currentWord = 0;
while (currentWord < words.Length)
{
int wordsThisLine = 0;
string line = String.Empty;
while (currentWord < words.Length)
{
string testLine = line;
if (testLine.Length < 1)
{
testLine += words[currentWord];
}
else if ((testLine[testLine.Length - 1] == '.') ||
(testLine[testLine.Length - 1] == '?') ||
(testLine[testLine.Length - 1] == '!'))
{
testLine += " " + words[currentWord];
}
else
{
testLine += " " + words[currentWord];
}
if ((wordsThisLine > 0) &&
(font.MeasureString(testLine).X > rowWidth))
{
break;
}
line = testLine;
wordsThisLine++;
currentWord++;
}
lines.Add(line);
}
return lines;
}
///
/// Returns a properly-formatted gold-quantity string.
///
public static string GetGoldString(int gold)
{
return String.Format("{0:n0}", gold);
}
#endregion
#region Drawing Helper Methods
///
/// Draws text centered at particular position.
///
/// The SpriteBatch object used to draw.
/// The font used to draw the text.
/// The text to be drawn
/// The center position of the text.
/// The color of the text.
public static void DrawCenteredText(SpriteBatch spriteBatch, SpriteFont font,
string text, Vector2 position, Color color)
{
// check the parameters
if (spriteBatch == null)
{
throw new ArgumentNullException("spriteBatch");
}
if (font == null)
{
throw new ArgumentNullException("font");
}
// check for trivial text
if (String.IsNullOrEmpty(text))
{
return;
}
// calculate the centered position
Vector2 textSize = font.MeasureString(text);
Vector2 centeredPosition = new Vector2(
position.X - (int)textSize.X / 2,
position.Y - (int)textSize.Y / 2);
// draw the string
spriteBatch.DrawString(font, text, centeredPosition, color, 0f,
Vector2.Zero, 1f, SpriteEffects.None, 1f - position.Y / 720f);
}
#endregion
}
}