// Copyright (c) 2021 ezequias2d and the Peridot contributors // This code is licensed under MIT license (see LICENSE for details) using System.Drawing; using System.Numerics; namespace Peridot.Text { /// /// Represents an interface to a text renderer. /// public class TextRenderer : IDisposable { private readonly ISpriteBatch m_spriteBatch; private readonly FontStashRenderer _textRenderer; /// /// Creates a new instance of . /// /// Peridot implementation. /// SpriteBatch to use to render. public TextRenderer(IPeridot peridot, ISpriteBatch spriteBatch) { m_spriteBatch = spriteBatch; _textRenderer = new(peridot, spriteBatch); } /// ~TextRenderer() { Dispose(false); } /// /// A bool indicating whether this instance has been disposed. /// public bool IsDisposed { get; private set; } /// /// Submit a text string for drawing. /// /// The font to draw. /// The font size. /// The text which will be drawn. /// The drawing location on screen. /// The text color. /// The rotation of the drawing. /// The center of the rotation. (0, 0) for default. /// The scaling of this drawing. /// The layer depth of this drawing. public void DrawString( Font font, int fontSize, string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, float layerDepth) { var rf = font.FontSystem.GetFont(fontSize); var fcolor = new FontStashSharp.FSColor(color.R, color.G, color.B, color.A); rf.DrawText(_textRenderer, text, position, fcolor, scale, rotation, origin, layerDepth); } /// /// Submit a text string for drawing. /// /// The font to draw. /// The font size. /// The text which will be drawn. /// The drawing location on screen. /// The text color. /// The rotation of the drawing. /// The center of the rotation. (0, 0) for default. /// The scaling of this drawing. /// The layer depth of this drawing. /// The scissor rectangle. public void DrawString( Font font, int fontSize, string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, float layerDepth, Rectangle scissor) { var rf = font.FontSystem.GetFont(fontSize); var fcolor = new FontStashSharp.FSColor(color.R, color.G, color.B, color.A); _textRenderer.Scissor = scissor; rf.DrawText(_textRenderer, text, position, fcolor, scale, rotation, origin, layerDepth); _textRenderer.ResetScissor(); } /// /// Submit a text string for drawing. /// /// The font to draw. /// The font size. /// The text which will be drawn. /// The drawing location on screen /// The text color. public void DrawString( Font font, int fontSize, string text, Vector2 position, Color color) { DrawString(font, fontSize, text, position, color, 0, Vector2.Zero, Vector2.One, 0); } /// /// Submit a text string for drawing. /// /// The font to draw. /// The font size. /// The text which will be drawn. /// The drawing location on screen /// The text color. /// The scissor rectangle. public void DrawString( Font font, int fontSize, string text, Vector2 position, Color color, Rectangle scissor) { DrawString(font, fontSize, text, position, color, 0, Vector2.Zero, Vector2.One, 0, scissor); } /// /// Submit a text string for drawing. /// /// The font to draw. /// The font size. /// The text which will be drawn. /// The drawing location on screen /// The text color. /// The rotation of the drawing. /// The center of the rotation. (0, 0) for default. /// The scaling of this drawing. /// The layer depth of this drawing. public void DrawString( Font font, int fontSize, string text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, float layerDepth) { DrawString(font, fontSize, text, position, color, rotation, origin, new Vector2(scale), layerDepth); } /// /// Submit a text string for drawing. /// /// The font to draw. /// The font size. /// The text which will be drawn. /// The drawing location on screen /// The text color. /// The rotation of the drawing. /// The center of the rotation. (0, 0) for default. /// The scaling of this drawing. /// The layer depth of this drawing. /// The scissor rectangle. public void DrawString( Font font, int fontSize, string text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, float layerDepth, Rectangle scissor) { DrawString(font, fontSize, text, position, color, rotation, origin, new Vector2(scale), layerDepth, scissor); } /// public void Dispose() { GC.SuppressFinalize(this); Dispose(true); } private void Dispose(bool disposing) { if (IsDisposed) return; IsDisposed = true; if (disposing) { } } } }