SpriteTexture.cs 4.7 KB

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