BitmapFont.Extensions.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System;
  2. using System.Text;
  3. using Microsoft.Xna.Framework;
  4. using Microsoft.Xna.Framework.Graphics;
  5. using MonoGame.Extended.Graphics;
  6. namespace MonoGame.Extended.BitmapFonts
  7. {
  8. public static class BitmapFontExtensions
  9. {
  10. /// <summary>
  11. /// Adds a string to a batch of sprites for rendering using the specified font, text, position, color, rotation,
  12. /// origin, scale, effects and layer.
  13. /// </summary>
  14. /// <param name="spriteBatch"></param>
  15. /// <param name="bitmapFont">A font for displaying text.</param>
  16. /// <param name="text">The text message to display.</param>
  17. /// <param name="position">The location (in screen coordinates) to draw the text.</param>
  18. /// <param name="color">
  19. /// The <see cref="Color" /> to tint a sprite. Use <see cref="Color.White" /> for full color with no
  20. /// tinting.
  21. /// </param>
  22. /// <param name="rotation">Specifies the angle (in radians) to rotate the text about its origin.</param>
  23. /// <param name="origin">The origin for each letter; the default is (0,0) which represents the upper-left corner.</param>
  24. /// <param name="scale">Scale factor.</param>
  25. /// <param name="effect">Effects to apply.</param>
  26. /// <param name="layerDepth">
  27. /// The depth of a layer. By default, 0 represents the front layer and 1 represents a back layer.
  28. /// Use SpriteSortMode if you want sprites to be sorted during drawing.
  29. /// </param>
  30. /// <param name="clippingRectangle">Clips the boundaries of the text so that it's not drawn outside the clipping rectangle</param>
  31. public static void DrawString(this SpriteBatch spriteBatch, BitmapFont bitmapFont, string text, Vector2 position,
  32. Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effect, float layerDepth, Rectangle? clippingRectangle = null)
  33. {
  34. if (text == null)
  35. throw new ArgumentNullException(nameof(text));
  36. if (effect != SpriteEffects.None)
  37. throw new NotSupportedException($"{effect} is not currently supported for {nameof(BitmapFont)}");
  38. var glyphs = bitmapFont.GetGlyphs(text, position);
  39. foreach (var glyph in glyphs)
  40. {
  41. if (glyph.Character == null)
  42. continue;
  43. var characterOrigin = position - glyph.Position + origin;
  44. spriteBatch.Draw(glyph.Character.TextureRegion, position, color, rotation, characterOrigin, scale, effect, layerDepth, clippingRectangle);
  45. }
  46. }
  47. public static void DrawString(this SpriteBatch spriteBatch, BitmapFont bitmapFont, StringBuilder text, Vector2 position,
  48. Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effect, float layerDepth, Rectangle? clippingRectangle = null)
  49. {
  50. if (text == null)
  51. throw new ArgumentNullException(nameof(text));
  52. if (effect != SpriteEffects.None)
  53. throw new NotSupportedException($"{effect} is not currently supported for {nameof(BitmapFont)}");
  54. var glyphs = bitmapFont.GetGlyphs(text, position);
  55. foreach (var glyph in glyphs)
  56. {
  57. if (glyph.Character == null)
  58. continue;
  59. var characterOrigin = position - glyph.Position + origin;
  60. spriteBatch.Draw(glyph.Character.TextureRegion, position, color, rotation, characterOrigin, scale, effect, layerDepth, clippingRectangle);
  61. }
  62. }
  63. /// <summary>
  64. /// Adds a string to a batch of sprites for rendering using the specified font, text, position, color, rotation,
  65. /// origin, scale, effects and layer.
  66. /// </summary>
  67. /// <param name="spriteBatch"></param>
  68. /// <param name="font">A font for displaying text.</param>
  69. /// <param name="text">The text message to display.</param>
  70. /// <param name="position">The location (in screen coordinates) to draw the text.</param>
  71. /// <param name="color">
  72. /// The <see cref="Color" /> to tint a sprite. Use <see cref="Color.White" /> for full color with no
  73. /// tinting.
  74. /// </param>
  75. /// <param name="rotation">Specifies the angle (in radians) to rotate the text about its origin.</param>
  76. /// <param name="origin">The origin for each letter; the default is (0,0) which represents the upper-left corner.</param>
  77. /// <param name="scale">Scale factor.</param>
  78. /// <param name="effect">Effects to apply.</param>
  79. /// <param name="layerDepth">
  80. /// The depth of a layer. By default, 0 represents the front layer and 1 represents a back layer.
  81. /// Use SpriteSortMode if you want sprites to be sorted during drawing.
  82. /// </param>
  83. /// <param name="clippingRectangle">Clips the boundaries of the text so that it's not drawn outside the clipping rectangle</param>
  84. public static void DrawString(this SpriteBatch spriteBatch, BitmapFont font, string text, Vector2 position,
  85. Color color, float rotation, Vector2 origin, float scale, SpriteEffects effect, float layerDepth, Rectangle? clippingRectangle = null)
  86. {
  87. spriteBatch.DrawString(font, text, position, color, rotation, origin, new Vector2(scale, scale), effect, layerDepth, clippingRectangle);
  88. }
  89. public static void DrawString(this SpriteBatch spriteBatch, BitmapFont font, StringBuilder text, Vector2 position,
  90. Color color, float rotation, Vector2 origin, float scale, SpriteEffects effect, float layerDepth, Rectangle? clippingRectangle = null)
  91. {
  92. spriteBatch.DrawString(font, text, position, color, rotation, origin, new Vector2(scale, scale), effect, layerDepth, clippingRectangle);
  93. }
  94. /// <summary>
  95. /// Adds a string to a batch of sprites for rendering using the specified font, text, position, color, layer,
  96. /// and width (in pixels) where to wrap the text at.
  97. /// </summary>
  98. /// <remarks>
  99. /// <see cref="BitmapFont" /> objects are loaded from the Content Manager. See the <see cref="BitmapFont" /> class for
  100. /// more information.
  101. /// Before any calls to this method you must call <see cref="SpriteBatch.Begin" />. Once all calls
  102. /// are complete, call <see cref="SpriteBatch.End" />.
  103. /// Use a newline character (\n) to draw more than one line of text.
  104. /// </remarks>
  105. /// <param name="spriteBatch"></param>
  106. /// <param name="font">A font for displaying text.</param>
  107. /// <param name="text">The text message to display.</param>
  108. /// <param name="position">The location (in screen coordinates) to draw the text.</param>
  109. /// <param name="color">
  110. /// The <see cref="Color" /> to tint a sprite. Use <see cref="Color.White" /> for full color with no
  111. /// tinting.
  112. /// </param>
  113. /// <param name="layerDepth">
  114. /// The depth of a layer. By default, 0 represents the front layer and 1 represents a back layer.
  115. /// Use SpriteSortMode if you want sprites to be sorted during drawing.
  116. /// </param>
  117. /// <param name="clippingRectangle">Clips the boundaries of the text so that it's not drawn outside the clipping rectangle</param>
  118. public static void DrawString(this SpriteBatch spriteBatch, BitmapFont font, string text, Vector2 position, Color color, float layerDepth, Rectangle? clippingRectangle = null)
  119. {
  120. spriteBatch.DrawString(font, text, position, color, rotation: 0, origin: Vector2.Zero, scale: Vector2.One, effect: SpriteEffects.None,
  121. layerDepth: layerDepth, clippingRectangle: clippingRectangle);
  122. }
  123. /// <summary>
  124. /// Adds a string to a batch of sprites for rendering using the specified font, text, position, color,
  125. /// and width (in pixels) where to wrap the text at. The text is drawn on layer 0f.
  126. /// </summary>
  127. /// <param name="spriteBatch"></param>
  128. /// <param name="font">A font for displaying text.</param>
  129. /// <param name="text">The text message to display.</param>
  130. /// <param name="position">The location (in screen coordinates) to draw the text.</param>
  131. /// <param name="color">
  132. /// The <see cref="Color" /> to tint a sprite. Use <see cref="Color.White" /> for full color with no
  133. /// tinting.
  134. /// </param>
  135. /// <param name="clippingRectangle">Clips the boundaries of the text so that it's not drawn outside the clipping rectangle</param>
  136. public static void DrawString(this SpriteBatch spriteBatch, BitmapFont font, string text, Vector2 position, Color color, Rectangle? clippingRectangle = null)
  137. {
  138. spriteBatch.DrawString(font, text, position, color, rotation: 0, origin: Vector2.Zero, scale: Vector2.One, effect: SpriteEffects.None,
  139. layerDepth: 0, clippingRectangle: clippingRectangle);
  140. }
  141. public static void DrawString(this SpriteBatch spriteBatch, BitmapFont font, StringBuilder text, Vector2 position, Color color, Rectangle? clippingRectangle = null)
  142. {
  143. spriteBatch.DrawString(font, text, position, color, rotation: 0, origin: Vector2.Zero, scale: Vector2.One, effect: SpriteEffects.None,
  144. layerDepth: 0, clippingRectangle: clippingRectangle);
  145. }
  146. }
  147. }