this repo has no description
at develop 212 lines 8.1 kB view raw
1// Copyright (c) 2021 ezequias2d <ezequiasmoises@gmail.com> and the Peridot contributors 2// This code is licensed under MIT license (see LICENSE for details) 3 4using System.Drawing; 5using System.Numerics; 6 7namespace Peridot.Text 8{ 9 /// <summary> 10 /// Represents an interface to a text renderer. 11 /// </summary> 12 public class TextRenderer : IDisposable 13 { 14 private readonly ISpriteBatch m_spriteBatch; 15 private readonly FontStashRenderer _textRenderer; 16 17 /// <summary> 18 /// Creates a new instance of <see cref="TextRenderer"/>. 19 /// </summary> 20 /// <param name="peridot">Peridot implementation.</param> 21 /// <param name="spriteBatch">SpriteBatch to use to render.</param> 22 public TextRenderer(IPeridot peridot, ISpriteBatch spriteBatch) 23 { 24 m_spriteBatch = spriteBatch; 25 _textRenderer = new(peridot, spriteBatch); 26 } 27 28 /// <inheritdoc/> 29 ~TextRenderer() 30 { 31 Dispose(false); 32 } 33 34 /// <summary> 35 /// A bool indicating whether this instance has been disposed. 36 /// </summary> 37 public bool IsDisposed { get; private set; } 38 39 /// <summary> 40 /// Submit a text string for drawing. 41 /// </summary> 42 /// <param name="font">The font to draw.</param> 43 /// <param name="fontSize">The font size.</param> 44 /// <param name="text">The text which will be drawn.</param> 45 /// <param name="position">The drawing location on screen.</param> 46 /// <param name="color">The text color.</param> 47 /// <param name="rotation">The rotation of the drawing.</param> 48 /// <param name="origin">The center of the rotation. (0, 0) for default.</param> 49 /// <param name="scale">The scaling of this drawing.</param> 50 /// <param name="layerDepth">The layer depth of this drawing.</param> 51 public void DrawString( 52 Font font, 53 int fontSize, 54 string text, 55 Vector2 position, 56 Color color, 57 float rotation, 58 Vector2 origin, 59 Vector2 scale, 60 float layerDepth) 61 { 62 var rf = font.FontSystem.GetFont(fontSize); 63 var fcolor = new FontStashSharp.FSColor(color.R, color.G, color.B, color.A); 64 65 rf.DrawText(_textRenderer, text, position, fcolor, scale, rotation, origin, layerDepth); 66 } 67 68 /// <summary> 69 /// Submit a text string for drawing. 70 /// </summary> 71 /// <param name="font">The font to draw.</param> 72 /// <param name="fontSize">The font size.</param> 73 /// <param name="text">The text which will be drawn.</param> 74 /// <param name="position">The drawing location on screen.</param> 75 /// <param name="color">The text color.</param> 76 /// <param name="rotation">The rotation of the drawing.</param> 77 /// <param name="origin">The center of the rotation. (0, 0) for default.</param> 78 /// <param name="scale">The scaling of this drawing.</param> 79 /// <param name="layerDepth">The layer depth of this drawing.</param> 80 /// <param name="scissor">The scissor rectangle.</param> 81 public void DrawString( 82 Font font, 83 int fontSize, 84 string text, 85 Vector2 position, 86 Color color, 87 float rotation, 88 Vector2 origin, 89 Vector2 scale, 90 float layerDepth, 91 Rectangle scissor) 92 { 93 var rf = font.FontSystem.GetFont(fontSize); 94 var fcolor = new FontStashSharp.FSColor(color.R, color.G, color.B, color.A); 95 96 _textRenderer.Scissor = scissor; 97 rf.DrawText(_textRenderer, text, position, fcolor, scale, rotation, origin, layerDepth); 98 _textRenderer.ResetScissor(); 99 } 100 101 /// <summary> 102 /// Submit a text string for drawing. 103 /// </summary> 104 /// <param name="font">The font to draw.</param> 105 /// <param name="fontSize">The font size.</param> 106 /// <param name="text">The text which will be drawn.</param> 107 /// <param name="position">The drawing location on screen</param> 108 /// <param name="color">The text color.</param> 109 public void DrawString( 110 Font font, 111 int fontSize, 112 string text, 113 Vector2 position, 114 Color color) 115 { 116 DrawString(font, fontSize, text, position, color, 0, Vector2.Zero, Vector2.One, 0); 117 } 118 119 /// <summary> 120 /// Submit a text string for drawing. 121 /// </summary> 122 /// <param name="font">The font to draw.</param> 123 /// <param name="fontSize">The font size.</param> 124 /// <param name="text">The text which will be drawn.</param> 125 /// <param name="position">The drawing location on screen</param> 126 /// <param name="color">The text color.</param> 127 /// <param name="scissor">The scissor rectangle.</param> 128 public void DrawString( 129 Font font, 130 int fontSize, 131 string text, 132 Vector2 position, 133 Color color, 134 Rectangle scissor) 135 { 136 DrawString(font, fontSize, text, position, color, 0, Vector2.Zero, Vector2.One, 0, scissor); 137 } 138 139 /// <summary> 140 /// Submit a text string for drawing. 141 /// </summary> 142 /// <param name="font">The font to draw.</param> 143 /// <param name="fontSize">The font size.</param> 144 /// <param name="text">The text which will be drawn.</param> 145 /// <param name="position">The drawing location on screen</param> 146 /// <param name="color">The text color.</param> 147 /// <param name="rotation">The rotation of the drawing.</param> 148 /// <param name="origin">The center of the rotation. (0, 0) for default.</param> 149 /// <param name="scale">The scaling of this drawing.</param> 150 /// <param name="layerDepth">The layer depth of this drawing.</param> 151 public void DrawString( 152 Font font, 153 int fontSize, 154 string text, 155 Vector2 position, 156 Color color, 157 float rotation, 158 Vector2 origin, 159 float scale, 160 float layerDepth) 161 { 162 DrawString(font, fontSize, text, position, color, rotation, origin, new Vector2(scale), layerDepth); 163 } 164 165 /// <summary> 166 /// Submit a text string for drawing. 167 /// </summary> 168 /// <param name="font">The font to draw.</param> 169 /// <param name="fontSize">The font size.</param> 170 /// <param name="text">The text which will be drawn.</param> 171 /// <param name="position">The drawing location on screen</param> 172 /// <param name="color">The text color.</param> 173 /// <param name="rotation">The rotation of the drawing.</param> 174 /// <param name="origin">The center of the rotation. (0, 0) for default.</param> 175 /// <param name="scale">The scaling of this drawing.</param> 176 /// <param name="layerDepth">The layer depth of this drawing.</param> 177 /// <param name="scissor">The scissor rectangle.</param> 178 public void DrawString( 179 Font font, 180 int fontSize, 181 string text, 182 Vector2 position, 183 Color color, 184 float rotation, 185 Vector2 origin, 186 float scale, 187 float layerDepth, 188 Rectangle scissor) 189 { 190 DrawString(font, fontSize, text, position, color, rotation, origin, new Vector2(scale), layerDepth, scissor); 191 } 192 193 /// <inheritdoc/> 194 public void Dispose() 195 { 196 GC.SuppressFinalize(this); 197 Dispose(true); 198 } 199 200 private void Dispose(bool disposing) 201 { 202 if (IsDisposed) 203 return; 204 IsDisposed = true; 205 206 if (disposing) 207 { 208 209 } 210 } 211 } 212}