FSRenderContext.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using FontStashSharp.Interfaces;
  2. using System;
  3. using Hebron.Runtime;
  4. #if MONOGAME || FNA
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Graphics;
  7. #elif STRIDE
  8. using Stride.Core.Mathematics;
  9. using Stride.Graphics;
  10. using Texture2D = Stride.Graphics.Texture;
  11. #else
  12. using System.Drawing;
  13. using System.Numerics;
  14. using Matrix = System.Numerics.Matrix3x2;
  15. using Texture2D = Veldrid.Texture;
  16. using Color = Veldrid.RgbaFloat;
  17. #endif
  18. namespace FontStashSharp.RichText
  19. {
  20. public class FSRenderContext
  21. {
  22. private IFontStashRenderer _renderer;
  23. private IFontStashRenderer2 _renderer2;
  24. private Matrix _transformation;
  25. private Vector2 _scale;
  26. private float _rotation;
  27. private float _layerDepth;
  28. public void SetRenderer(IFontStashRenderer renderer)
  29. {
  30. if (renderer == null)
  31. {
  32. throw new ArgumentNullException(nameof(renderer));
  33. }
  34. _renderer = renderer;
  35. _renderer2 = null;
  36. }
  37. public void SetRenderer(IFontStashRenderer2 renderer)
  38. {
  39. if (renderer == null)
  40. {
  41. throw new ArgumentNullException(nameof(renderer));
  42. }
  43. _renderer = null;
  44. _renderer2 = renderer;
  45. }
  46. public void Prepare(Vector2 position, Vector2 scale, float rotation, Vector2 origin, float layerDepth)
  47. {
  48. _scale = scale;
  49. _rotation = rotation;
  50. _layerDepth = layerDepth;
  51. Utility.BuildTransform(position, _scale, _rotation, origin, out _transformation);
  52. }
  53. public void DrawText(string text, SpriteFontBase font, Vector2 pos, Color color, TextStyle textStyle)
  54. {
  55. if (string.IsNullOrEmpty(text))
  56. {
  57. return;
  58. }
  59. pos = pos.Transform(ref _transformation);
  60. if (_renderer != null)
  61. {
  62. font.DrawText(_renderer, text, pos, color, _scale, _rotation, default(Vector2), _layerDepth, textStyle: textStyle);
  63. }
  64. else
  65. {
  66. font.DrawText(_renderer2, text, pos, color, _scale, _rotation, default(Vector2), _layerDepth, textStyle: textStyle);
  67. }
  68. }
  69. public void DrawImage(Texture2D texture, Rectangle sourceRegion, Vector2 position, Vector2 scale, Color color)
  70. {
  71. if (_renderer != null)
  72. {
  73. position = position.Transform(ref _transformation);
  74. _renderer.Draw(texture, position, sourceRegion, color, _rotation, _scale, _layerDepth);
  75. }
  76. else
  77. {
  78. var topLeft = new VertexPositionColorTexture();
  79. var topRight = new VertexPositionColorTexture();
  80. var bottomLeft = new VertexPositionColorTexture();
  81. var bottomRight = new VertexPositionColorTexture();
  82. var size = new Vector2(sourceRegion.Width, sourceRegion.Height) * _scale * scale;
  83. _renderer2.DrawQuad(texture, color, position, ref _transformation,
  84. _layerDepth, size, sourceRegion,
  85. ref topLeft, ref topRight, ref bottomLeft, ref bottomRight);
  86. }
  87. }
  88. }
  89. }