2
0

SpriteTexture.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. Vector2 offset = Vector2.Zero;
  23. Vector2 scale = Vector2.One;
  24. Internal_CreateInstance(this, texture, ref offset, ref scale);
  25. }
  26. /// <summary>
  27. /// Creates a new sprite texture that references a sub-area of the provided texture.
  28. /// </summary>
  29. /// <param name="texture">Texture to wrap by the sprite texture.</param>
  30. /// <param name="uvOffset">Top-left position of the area used by the sprite texture, in normalized coordinates.
  31. /// </param>
  32. /// <param name="uvScale">Size of the area used by the sprite texture, in normalized coordinates.</param>
  33. public SpriteTexture(Texture2D texture, Vector2 uvOffset, Vector2 uvScale)
  34. {
  35. Internal_CreateInstance(this, texture, ref uvOffset, ref uvScale);
  36. }
  37. /// <summary>
  38. /// Texture that the sprite texture references.
  39. /// </summary>
  40. public Texture2D Texture
  41. {
  42. get { return Internal_GetTexture(mCachedPtr); }
  43. set
  44. {
  45. IntPtr texturePtr = IntPtr.Zero;
  46. if (value != null)
  47. texturePtr = value.GetCachedPtr();
  48. Internal_SetTexture(mCachedPtr, texturePtr);
  49. }
  50. }
  51. /// <summary>
  52. /// Offset into the referenced texture where the sprite starts. In UV coordinates, range [0, 1].
  53. /// </summary>
  54. public Vector2 Offset
  55. {
  56. get { Vector2 value; Internal_GetOffset(mCachedPtr, out value); return value; }
  57. set { Internal_SetOffset(mCachedPtr, ref value); }
  58. }
  59. /// <summary>
  60. /// Size of the sprite in the referenced texture. In UV coordinates, range [0, 1].
  61. /// </summary>
  62. public Vector2 Scale
  63. {
  64. get { Vector2 value; Internal_GetScale(mCachedPtr, out value); return value; }
  65. set { Internal_SetScale(mCachedPtr, ref value); }
  66. }
  67. /// <summary>
  68. /// Returns width of the sprite texture in pixels.
  69. /// </summary>
  70. public int Width
  71. {
  72. get { return Internal_GetWidth(mCachedPtr); }
  73. }
  74. /// <summary>
  75. /// Returns height of the sprite texture in pixels.
  76. /// </summary>
  77. public int Height
  78. {
  79. get { return Internal_GetHeight(mCachedPtr); }
  80. }
  81. [MethodImpl(MethodImplOptions.InternalCall)]
  82. private static extern void Internal_CreateInstance(SpriteTexture instance,
  83. Texture2D texture, ref Vector2 offset, ref Vector2 scale);
  84. [MethodImpl(MethodImplOptions.InternalCall)]
  85. private static extern Texture2D Internal_GetTexture(IntPtr thisPtr);
  86. [MethodImpl(MethodImplOptions.InternalCall)]
  87. private static extern void Internal_SetTexture(IntPtr thisPtr, IntPtr value);
  88. [MethodImpl(MethodImplOptions.InternalCall)]
  89. private static extern void Internal_GetOffset(IntPtr thisPtr, out Vector2 value);
  90. [MethodImpl(MethodImplOptions.InternalCall)]
  91. private static extern void Internal_SetOffset(IntPtr thisPtr, ref Vector2 value);
  92. [MethodImpl(MethodImplOptions.InternalCall)]
  93. private static extern void Internal_GetScale(IntPtr thisPtr, out Vector2 value);
  94. [MethodImpl(MethodImplOptions.InternalCall)]
  95. private static extern void Internal_SetScale(IntPtr thisPtr, ref Vector2 value);
  96. [MethodImpl(MethodImplOptions.InternalCall)]
  97. private static extern int Internal_GetWidth(IntPtr thisPtr);
  98. [MethodImpl(MethodImplOptions.InternalCall)]
  99. private static extern int Internal_GetHeight(IntPtr thisPtr);
  100. }
  101. }