SpriteTexture.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. namespace BansheeEngine
  4. {
  5. /// <summary>
  6. /// Texture interface that encapsulates underlying texture which allows us to create a sprite texture atlas
  7. /// (e.g. multiple sprite textures referencing different parts of a single texture).
  8. /// </summary>
  9. public sealed class SpriteTexture : Resource
  10. {
  11. /// <summary>
  12. /// Constructor for internal use by the runtime.
  13. /// </summary>
  14. private SpriteTexture()
  15. { }
  16. /// <summary>
  17. /// Creates a new sprite texture that references the entire area of the provided texture.
  18. /// </summary>
  19. /// <param name="texture">Texture to wrap by the sprite texture.</param>
  20. public SpriteTexture(Texture2D texture)
  21. {
  22. Internal_CreateInstance(this, texture, Vector2.Zero, Vector2.One);
  23. }
  24. /// <summary>
  25. /// Creates a new sprite texture that references a sub-area of the provided texture.
  26. /// </summary>
  27. /// <param name="texture">Texture to wrap by the sprite texture.</param>
  28. /// <param name="uvOffset">Top-left position of the area used by the sprite texture, in normalized coordinates.
  29. /// </param>
  30. /// <param name="uvScale">Size of the area used by the sprite texture, in normalized coordinates.</param>
  31. public SpriteTexture(Texture2D texture, Vector2 uvOffset, Vector2 uvScale)
  32. {
  33. Internal_CreateInstance(this, texture, uvOffset, uvScale);
  34. }
  35. /// <summary>
  36. /// Texture that the sprite texture references.
  37. /// </summary>
  38. public Texture2D Texture
  39. {
  40. get { return Internal_GetTexture(mCachedPtr); }
  41. set
  42. {
  43. IntPtr texturePtr = IntPtr.Zero;
  44. if (value != null)
  45. texturePtr = value.GetCachedPtr();
  46. Internal_SetTexture(mCachedPtr, texturePtr);
  47. }
  48. }
  49. /// <summary>
  50. /// Offset into the referenced texture where the sprite starts. In UV coordinates, range [0, 1].
  51. /// </summary>
  52. public Vector2 Offset
  53. {
  54. get { Vector2 value; Internal_GetOffset(mCachedPtr, out value); return value; }
  55. set { Internal_SetOffset(mCachedPtr, value); }
  56. }
  57. /// <summary>
  58. /// Size of the sprite in the referenced texture. In UV coordinates, range [0, 1].
  59. /// </summary>
  60. public Vector2 Scale
  61. {
  62. get { Vector2 value; Internal_GetScale(mCachedPtr, out value); return value; }
  63. set { Internal_SetScale(mCachedPtr, value); }
  64. }
  65. /// <summary>
  66. /// Returns width of the sprite texture in pixels.
  67. /// </summary>
  68. public int Width
  69. {
  70. get { return Internal_GetWidth(mCachedPtr); }
  71. }
  72. /// <summary>
  73. /// Returns height of the sprite texture in pixels.
  74. /// </summary>
  75. public int Height
  76. {
  77. get { return Internal_GetHeight(mCachedPtr); }
  78. }
  79. [MethodImpl(MethodImplOptions.InternalCall)]
  80. private static extern void Internal_CreateInstance(SpriteTexture instance,
  81. Texture2D teture, Vector2 offset, Vector2 scale);
  82. [MethodImpl(MethodImplOptions.InternalCall)]
  83. private static extern Texture2D Internal_GetTexture(IntPtr thisPtr);
  84. [MethodImpl(MethodImplOptions.InternalCall)]
  85. private static extern void Internal_SetTexture(IntPtr thisPtr, IntPtr value);
  86. [MethodImpl(MethodImplOptions.InternalCall)]
  87. private static extern void Internal_GetOffset(IntPtr thisPtr, out Vector2 value);
  88. [MethodImpl(MethodImplOptions.InternalCall)]
  89. private static extern void Internal_SetOffset(IntPtr thisPtr, Vector2 value);
  90. [MethodImpl(MethodImplOptions.InternalCall)]
  91. private static extern void Internal_GetScale(IntPtr thisPtr, out Vector2 value);
  92. [MethodImpl(MethodImplOptions.InternalCall)]
  93. private static extern void Internal_SetScale(IntPtr thisPtr, Vector2 value);
  94. [MethodImpl(MethodImplOptions.InternalCall)]
  95. private static extern int Internal_GetWidth(IntPtr thisPtr);
  96. [MethodImpl(MethodImplOptions.InternalCall)]
  97. private static extern int Internal_GetHeight(IntPtr thisPtr);
  98. }
  99. }