Fonts.cs 13 KB

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