2
0

Fonts.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Fonts.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using Microsoft.Xna.Framework.Graphics;
  12. using Microsoft.Xna.Framework.Content;
  13. using System.Text;
  14. using Microsoft.Xna.Framework;
  15. using System.Collections.Generic;
  16. #endregion
  17. namespace RolePlaying
  18. {
  19. /// <summary>
  20. /// Static storage of SpriteFont objects and colors for use throughout the game.
  21. /// </summary>
  22. static class Fonts
  23. {
  24. #region Fonts
  25. private static SpriteFont headerFont;
  26. public static SpriteFont HeaderFont
  27. {
  28. get { return headerFont; }
  29. }
  30. private static SpriteFont playerNameFont;
  31. public static SpriteFont PlayerNameFont
  32. {
  33. get { return playerNameFont; }
  34. }
  35. private static SpriteFont debugFont;
  36. public static SpriteFont DebugFont
  37. {
  38. get { return debugFont; }
  39. }
  40. private static SpriteFont buttonNamesFont;
  41. public static SpriteFont ButtonNamesFont
  42. {
  43. get { return buttonNamesFont; }
  44. }
  45. private static SpriteFont descriptionFont;
  46. public static SpriteFont DescriptionFont
  47. {
  48. get { return descriptionFont; }
  49. }
  50. private static SpriteFont gearInfoFont;
  51. public static SpriteFont GearInfoFont
  52. {
  53. get { return gearInfoFont; }
  54. }
  55. private static SpriteFont damageFont;
  56. public static SpriteFont DamageFont
  57. {
  58. get { return damageFont; }
  59. }
  60. private static SpriteFont playerStatisticsFont;
  61. public static SpriteFont PlayerStatisticsFont
  62. {
  63. get { return playerStatisticsFont; }
  64. }
  65. private static SpriteFont hudDetailFont;
  66. public static SpriteFont HudDetailFont
  67. {
  68. get { return hudDetailFont; }
  69. }
  70. private static SpriteFont captionFont;
  71. public static SpriteFont CaptionFont
  72. {
  73. get { return captionFont; }
  74. }
  75. #endregion
  76. #region Font Colors
  77. public static readonly Color CountColor = new Color(79, 24, 44);
  78. public static readonly Color TitleColor = new Color(59, 18, 6);
  79. public static readonly Color CaptionColor = new Color(228, 168, 57);
  80. public static readonly Color HighlightColor = new Color(223, 206, 148);
  81. public static readonly Color DisplayColor = new Color(68, 32, 19);
  82. public static readonly Color DescriptionColor = new Color(0, 0, 0);
  83. public static readonly Color RestrictionColor = new Color(0, 0, 0);
  84. public static readonly Color ModifierColor = new Color(0, 0, 0);
  85. public static readonly Color MenuSelectedColor = new Color(248, 218, 127);
  86. #endregion
  87. #region Initialization
  88. /// <summary>
  89. /// Load the fonts from the content pipeline.
  90. /// </summary>
  91. public static void LoadContent(ContentManager contentManager)
  92. {
  93. // check the parameters
  94. if (contentManager == null)
  95. {
  96. throw new ArgumentNullException("contentManager");
  97. }
  98. // load each font from the content pipeline
  99. buttonNamesFont = contentManager.Load<SpriteFont>("Fonts/ButtonNamesFont");
  100. captionFont = contentManager.Load<SpriteFont>("Fonts/CaptionFont");
  101. damageFont = contentManager.Load<SpriteFont>("Fonts/DamageFont");
  102. debugFont = contentManager.Load<SpriteFont>("Fonts/DebugFont");
  103. descriptionFont = contentManager.Load<SpriteFont>("Fonts/DescriptionFont");
  104. gearInfoFont = contentManager.Load<SpriteFont>("Fonts/GearInfoFont");
  105. headerFont = contentManager.Load<SpriteFont>("Fonts/HeaderFont");
  106. hudDetailFont = contentManager.Load<SpriteFont>("Fonts/HudDetailFont");
  107. playerNameFont = contentManager.Load<SpriteFont>("Fonts/PlayerNameFont");
  108. playerStatisticsFont =
  109. contentManager.Load<SpriteFont>("Fonts/PlayerStatisticsFont");
  110. }
  111. /// <summary>
  112. /// Release all references to the fonts.
  113. /// </summary>
  114. public static void UnloadContent()
  115. {
  116. buttonNamesFont = null;
  117. captionFont = null;
  118. damageFont = null;
  119. debugFont = null;
  120. descriptionFont = null;
  121. gearInfoFont = null;
  122. headerFont = null;
  123. hudDetailFont = null;
  124. playerNameFont = null;
  125. playerStatisticsFont = null;
  126. }
  127. #endregion
  128. #region Text Helper Methods
  129. /// <summary>
  130. /// Adds newline characters to a string so that it fits within a certain size.
  131. /// </summary>
  132. /// <param name="text">The text to be modified.</param>
  133. /// <param name="maximumCharactersPerLine">
  134. /// The maximum length of a single line of text.
  135. /// </param>
  136. /// <param name="maximumLines">The maximum number of lines to draw.</param>
  137. /// <returns>The new string, with newline characters if needed.</returns>
  138. public static string BreakTextIntoLines(string text,
  139. int maximumCharactersPerLine, int maximumLines)
  140. {
  141. if (maximumLines <= 0)
  142. {
  143. throw new ArgumentOutOfRangeException("maximumLines");
  144. }
  145. if (maximumCharactersPerLine <= 0)
  146. {
  147. throw new ArgumentOutOfRangeException("maximumCharactersPerLine");
  148. }
  149. // if the string is trivial, then this is really easy
  150. if (String.IsNullOrEmpty(text))
  151. {
  152. return String.Empty;
  153. }
  154. // if the text is short enough to fit on one line, then this is still easy
  155. if (text.Length < maximumCharactersPerLine)
  156. {
  157. return text;
  158. }
  159. // construct a new string with carriage returns
  160. StringBuilder stringBuilder = new StringBuilder(text);
  161. int currentLine = 0;
  162. int newLineIndex = 0;
  163. while (((text.Length - newLineIndex) > maximumCharactersPerLine) &&
  164. (currentLine < maximumLines))
  165. {
  166. text.IndexOf(' ', 0);
  167. int nextIndex = newLineIndex;
  168. while ((nextIndex >= 0) && (nextIndex < maximumCharactersPerLine))
  169. {
  170. newLineIndex = nextIndex;
  171. nextIndex = text.IndexOf(' ', newLineIndex + 1);
  172. }
  173. stringBuilder.Replace(' ', '\n', newLineIndex, 1);
  174. currentLine++;
  175. }
  176. return stringBuilder.ToString();
  177. }
  178. /// <summary>
  179. /// Adds new-line characters to a string to make it fit.
  180. /// </summary>
  181. /// <param name="text">The text to be drawn.</param>
  182. /// <param name="maximumCharactersPerLine">
  183. /// The maximum length of a single line of text.
  184. /// </param>
  185. public static string BreakTextIntoLines(string text,
  186. int maximumCharactersPerLine)
  187. {
  188. // check the parameters
  189. if (maximumCharactersPerLine <= 0)
  190. {
  191. throw new ArgumentOutOfRangeException("maximumCharactersPerLine");
  192. }
  193. // if the string is trivial, then this is really easy
  194. if (String.IsNullOrEmpty(text))
  195. {
  196. return String.Empty;
  197. }
  198. // if the text is short enough to fit on one line, then this is still easy
  199. if (text.Length < maximumCharactersPerLine)
  200. {
  201. return text;
  202. }
  203. // construct a new string with carriage returns
  204. StringBuilder stringBuilder = new StringBuilder(text);
  205. int currentLine = 0;
  206. int newLineIndex = 0;
  207. while (((text.Length - newLineIndex) > maximumCharactersPerLine))
  208. {
  209. text.IndexOf(' ', 0);
  210. int nextIndex = newLineIndex;
  211. while ((nextIndex >= 0) && (nextIndex < maximumCharactersPerLine))
  212. {
  213. newLineIndex = nextIndex;
  214. nextIndex = text.IndexOf(' ', newLineIndex + 1);
  215. }
  216. stringBuilder.Replace(' ', '\n', newLineIndex, 1);
  217. currentLine++;
  218. }
  219. return stringBuilder.ToString();
  220. }
  221. /// <summary>
  222. /// Break text up into separate lines to make it fit.
  223. /// </summary>
  224. /// <param name="text">The text to be broken up.</param>
  225. /// <param name="font">The font used ot measure the width of the text.</param>
  226. /// <param name="rowWidth">The maximum width of each line, in pixels.</param>
  227. public static List<string> BreakTextIntoList(string text, SpriteFont font,
  228. int rowWidth)
  229. {
  230. // check parameters
  231. if (font == null)
  232. {
  233. throw new ArgumentNullException("font");
  234. }
  235. if (rowWidth <= 0)
  236. {
  237. throw new ArgumentOutOfRangeException("rowWidth");
  238. }
  239. // create the list
  240. List<string> lines = new List<string>();
  241. // check for trivial text
  242. if (String.IsNullOrEmpty("text"))
  243. {
  244. lines.Add(String.Empty);
  245. return lines;
  246. }
  247. // check for text that fits on a single line
  248. if (font.MeasureString(text).X <= rowWidth)
  249. {
  250. lines.Add(text);
  251. return lines;
  252. }
  253. // break the text up into words
  254. string[] words = text.Split(' ');
  255. // add words until they go over the length
  256. int currentWord = 0;
  257. while (currentWord < words.Length)
  258. {
  259. int wordsThisLine = 0;
  260. string line = String.Empty;
  261. while (currentWord < words.Length)
  262. {
  263. string testLine = line;
  264. if (testLine.Length < 1)
  265. {
  266. testLine += words[currentWord];
  267. }
  268. else if ((testLine[testLine.Length - 1] == '.') ||
  269. (testLine[testLine.Length - 1] == '?') ||
  270. (testLine[testLine.Length - 1] == '!'))
  271. {
  272. testLine += " " + words[currentWord];
  273. }
  274. else
  275. {
  276. testLine += " " + words[currentWord];
  277. }
  278. if ((wordsThisLine > 0) &&
  279. (font.MeasureString(testLine).X > rowWidth))
  280. {
  281. break;
  282. }
  283. line = testLine;
  284. wordsThisLine++;
  285. currentWord++;
  286. }
  287. lines.Add(line);
  288. }
  289. return lines;
  290. }
  291. /// <summary>
  292. /// Returns a properly-formatted gold-quantity string.
  293. /// </summary>
  294. public static string GetGoldString(int gold)
  295. {
  296. return String.Format("{0:n0}", gold);
  297. }
  298. #endregion
  299. #region Drawing Helper Methods
  300. /// <summary>
  301. /// Draws text centered at particular position.
  302. /// </summary>
  303. /// <param name="spriteBatch">The SpriteBatch object used to draw.</param>
  304. /// <param name="font">The font used to draw the text.</param>
  305. /// <param name="text">The text to be drawn</param>
  306. /// <param name="position">The center position of the text.</param>
  307. /// <param name="color">The color of the text.</param>
  308. public static void DrawCenteredText(SpriteBatch spriteBatch, SpriteFont font,
  309. string text, Vector2 position, Color color)
  310. {
  311. // check the parameters
  312. if (spriteBatch == null)
  313. {
  314. throw new ArgumentNullException("spriteBatch");
  315. }
  316. if (font == null)
  317. {
  318. throw new ArgumentNullException("font");
  319. }
  320. // check for trivial text
  321. if (String.IsNullOrEmpty(text))
  322. {
  323. return;
  324. }
  325. // calculate the centered position
  326. Vector2 textSize = font.MeasureString(text);
  327. Vector2 centeredPosition = new Vector2(
  328. position.X - (int)textSize.X / 2,
  329. position.Y - (int)textSize.Y / 2);
  330. // draw the string
  331. spriteBatch.DrawString(font, text, centeredPosition, color, 0f,
  332. Vector2.Zero, 1f, SpriteEffects.None, 1f - position.Y / 720f);
  333. }
  334. #endregion
  335. }
  336. }