TextureFragment.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. #if MONOGAME || FNA
  3. using Microsoft.Xna.Framework;
  4. using Microsoft.Xna.Framework.Graphics;
  5. #elif STRIDE
  6. using Stride.Core.Mathematics;
  7. using Stride.Graphics;
  8. using Texture2D = Stride.Graphics.Texture;
  9. #else
  10. using System.Numerics;
  11. using System.Drawing;
  12. using Texture2D = Veldrid.Texture;
  13. using Color = Veldrid.RgbaFloat;
  14. #endif
  15. namespace FontStashSharp.RichText
  16. {
  17. public class TextureFragment : IRenderable
  18. {
  19. public Texture2D Texture { get; private set; }
  20. public Rectangle Region { get; private set; }
  21. public Point Size
  22. {
  23. get
  24. {
  25. return new Point((int)(Region.Width * Scale.X + 0.5f), (int)(Region.Height * Scale.Y + 0.5f));
  26. }
  27. }
  28. public Vector2 Scale = Vector2.One;
  29. public TextureFragment(Texture2D texture, Rectangle region)
  30. {
  31. if (texture == null)
  32. {
  33. throw new ArgumentNullException(nameof(texture));
  34. }
  35. Texture = texture;
  36. Region = region;
  37. }
  38. #if MONOGAME || FNA || STRIDE
  39. public TextureFragment(Texture2D texture) :
  40. this(texture, new Rectangle(0, 0, texture.Width, texture.Height))
  41. {
  42. }
  43. #endif
  44. public void Draw(FSRenderContext context, Vector2 position, Color color)
  45. {
  46. context.DrawImage(Texture, Region, position, Scale, Color.White);
  47. }
  48. }
  49. }