TextureFontBigNumbers.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. //-----------------------------------------------------------------------------
  2. // TextureFontBigNumbers.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using Microsoft.Xna.Framework;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Text;
  12. using RacingGame.Helpers;
  13. using RacingGame.GameLogic;
  14. using Microsoft.Xna.Framework.Input;
  15. namespace RacingGame.Graphics
  16. {
  17. /// <summary>
  18. /// TextureFontBigNumbers
  19. /// </summary>
  20. public static class TextureFontBigNumbers
  21. {
  22. /// <summary>
  23. /// Big numbers in the Ingame.png graphic
  24. /// </summary>
  25. private static readonly Rectangle[] BigNumberRects =
  26. {
  27. // 0
  28. new Rectangle(2, 342, 80, 133),
  29. // 1
  30. new Rectangle(84, 342, 80, 133),
  31. // 2
  32. new Rectangle(167, 342, 80, 133),
  33. // 3
  34. new Rectangle(247, 342, 78, 133),
  35. // 4
  36. new Rectangle(330, 342, 80, 133),
  37. // 5
  38. new Rectangle(411, 342, 80, 133),
  39. // 6
  40. new Rectangle(495, 342, 80, 133),
  41. // 7
  42. new Rectangle(578, 342, 80, 133),
  43. // 8
  44. new Rectangle(659, 342, 80, 133),
  45. // 9
  46. new Rectangle(749, 342, 80, 133),
  47. };
  48. /// <summary>
  49. /// Write digit
  50. /// </summary>
  51. /// <param name="x">X</param>
  52. /// <param name="y">Y</param>
  53. /// <param name="digit">Digit</param>
  54. /// <returns>Int</returns>
  55. private static int WriteDigit(int x, int y, int digit)
  56. {
  57. if (digit < 0)
  58. return 0;
  59. float resScalingX = (float)BaseGame.Width / 1600.0f;
  60. float resScalingY = (float)BaseGame.Height / 1200.0f;
  61. Rectangle rect = BigNumberRects[digit % BigNumberRects.Length];
  62. BaseGame.UI.Ingame.RenderOnScreen(new Rectangle(x, y,
  63. (int)Math.Round(rect.Width * resScalingX),
  64. (int)Math.Round(rect.Height * resScalingY)), rect);
  65. return (int)Math.Round(rect.Width * resScalingX);
  66. }
  67. /// <summary>
  68. /// Write digit
  69. /// </summary>
  70. /// <param name="x">X</param>
  71. /// <param name="y">Y</param>
  72. /// <param name="height">Height</param>
  73. /// <param name="digit">Digit</param>
  74. /// <returns>Int</returns>
  75. private static int WriteDigit(int x, int y, int height, int digit)
  76. {
  77. if (digit < 0)
  78. return 0;
  79. float resScalingX = (float)BaseGame.Width / 1600.0f;
  80. float resScalingY = (float)BaseGame.Height / 1200.0f;
  81. float scaleFactor = height / (float)BigNumberRects[0].Height;
  82. Rectangle rect = BigNumberRects[digit % BigNumberRects.Length];
  83. BaseGame.UI.Ingame.RenderOnScreen(new Rectangle(x, y,
  84. (int)Math.Round(rect.Width * resScalingX * scaleFactor),
  85. (int)Math.Round(rect.Height * resScalingY * scaleFactor)), rect);
  86. return (int)Math.Round(rect.Width * resScalingX * scaleFactor);
  87. }
  88. /// <summary>
  89. /// Write digit
  90. /// </summary>
  91. /// <param name="x">X</param>
  92. /// <param name="y">Y</param>
  93. /// <param name="digit">Digit</param>
  94. /// <param name="alpha">Alpha</param>
  95. /// <returns>Int</returns>
  96. private static int WriteDigit(int x, int y, int digit, float alpha)
  97. {
  98. float resScalingX = (float)BaseGame.Width / 1600.0f;
  99. float resScalingY = (float)BaseGame.Height / 1200.0f;
  100. Rectangle rect = BigNumberRects[digit % BigNumberRects.Length];
  101. BaseGame.UI.Ingame.RenderOnScreen(new Rectangle(x, y,
  102. (int)Math.Round(rect.Width * resScalingX),
  103. (int)Math.Round(rect.Height * resScalingY)), rect,
  104. ColorHelper.ApplyAlphaToColor(Color.White, alpha));
  105. return (int)Math.Round(rect.Width * resScalingX);
  106. }
  107. /// <summary>
  108. /// Write number
  109. /// </summary>
  110. /// <param name="x">X</param>
  111. /// <param name="y">Y</param>
  112. /// <param name="number">Number</param>
  113. /// <returns>Int</returns>
  114. public static int WriteNumber(int x, int y, int number)
  115. {
  116. // Convert to string
  117. string numberText = number.ToString();
  118. int width = 0;
  119. // And now process every letter
  120. //foreach (char numberChar in numberText.ToCharArray())
  121. char[] chars = numberText.ToCharArray();
  122. for (int num = 0; num < chars.Length; num++)
  123. {
  124. width += WriteDigit(x + width, y, (int)chars[num] - (int)'0');
  125. }
  126. return width;
  127. }
  128. /// <summary>
  129. /// Write number
  130. /// </summary>
  131. /// <param name="x">X</param>
  132. /// <param name="y">Y</param>
  133. /// <param name="number">Number</param>
  134. /// <param name="alpha">Alpha</param>
  135. /// <returns>Int</returns>
  136. public static int WriteNumber(int x, int y, int number, float alpha)
  137. {
  138. // Convert to string
  139. string numberText = number.ToString();
  140. int width = 0;
  141. // And now process every letter
  142. //foreach (char numberChar in numberText.ToCharArray())
  143. char[] chars = numberText.ToCharArray();
  144. for (int num = 0; num < chars.Length; num++)
  145. {
  146. width += WriteDigit(
  147. x + width, y, (int)chars[num] - (int)'0', alpha);
  148. }
  149. return width;
  150. }
  151. /// <summary>
  152. /// Write number
  153. /// </summary>
  154. /// <param name="x">X</param>
  155. /// <param name="y">Y</param>
  156. /// <param name="height">Height</param>
  157. /// <param name="number">Number</param>
  158. /// <returns>Int</returns>
  159. public static int WriteNumber(int x, int y, int height, int number)
  160. {
  161. // Convert to string
  162. string numberText = number.ToString();
  163. int width = 0;
  164. // And now process every letter
  165. //foreach (char numberChar in numberText.ToCharArray())
  166. char[] chars = numberText.ToCharArray();
  167. for (int num = 0; num < chars.Length; num++)
  168. {
  169. width += WriteDigit(
  170. x + width, y, height, (int)chars[num] - (int)'0');
  171. }
  172. return width;
  173. }
  174. /// <summary>
  175. /// Write number centered
  176. /// </summary>
  177. /// <param name="x">X</param>
  178. /// <param name="y">Y</param>
  179. /// <param name="number">Number</param>
  180. public static void WriteNumberCentered(int x, int y, int number)
  181. {
  182. WriteNumber(
  183. (int)(x - (number.ToString().Length * BigNumberRects[0].Width / 2) *
  184. ((float)BaseGame.Width / 1600.0f)),
  185. y, number);
  186. }
  187. /// <summary>
  188. /// Write number centered
  189. /// </summary>
  190. /// <param name="x">X</param>
  191. /// <param name="y">Y</param>
  192. /// <param name="number">Number</param>
  193. /// <param name="alpha">Alpha</param>
  194. public static void WriteNumberCentered(int x, int y, int number, float alpha)
  195. {
  196. WriteNumber(
  197. (int)(x - (number.ToString().Length * BigNumberRects[0].Width / 2) *
  198. ((float)BaseGame.Width / 1600.0f)),
  199. y, number, alpha);
  200. }
  201. }
  202. }