Texture2DRegion.Extensions.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Copyright (c) Craftwork Games. All rights reserved.
  2. // Licensed under the MIT license.
  3. // See LICENSE file in the project root for full license information.
  4. using System;
  5. using Microsoft.Xna.Framework;
  6. namespace MonoGame.Extended.Graphics;
  7. /// <summary>
  8. /// Provides extension methods for the <see cref="Texture2DRegion"/> class.
  9. /// </summary>
  10. public static class Texture2DRegionExtensions
  11. {
  12. /// <summary>
  13. /// Gets a subregion of the specified texture region using the provided rectangle.
  14. /// </summary>
  15. /// <param name="textureRegion">The texture region to get the subregion from.</param>
  16. /// <param name="region">The rectangle defining the subregion.</param>
  17. /// <returns>A new <see cref="Texture2DRegion"/> representing the specified subregion.</returns>
  18. /// <exception cref="ArgumentNullException">
  19. /// Thrown if <paramref name="textureRegion"/> is <see langword="null"/>.
  20. /// </exception>
  21. /// <exception cref="ObjectDisposedException">
  22. /// Thrown if source texture of the <paramref name="textureRegion"/> has been disposed prior.
  23. /// </exception>
  24. public static Texture2DRegion GetSubregion(this Texture2DRegion textureRegion, Rectangle region) =>
  25. textureRegion.GetSubregion(region.X, region.Y, region.Width, region.Height, null);
  26. /// <summary>
  27. /// Gets a subregion of the specified texture region using the provided rectangle and name.
  28. /// </summary>
  29. /// <param name="textureRegion">The texture region to get the subregion from.</param>
  30. /// <param name="region">The rectangle defining the subregion.</param>
  31. /// <param name="name">The name of the new subregion.</param>
  32. /// <returns>A new <see cref="Texture2DRegion"/> representing the specified subregion.</returns>
  33. /// <exception cref="ArgumentNullException">
  34. /// Thrown if <paramref name="textureRegion"/> is <see langword="null"/>.
  35. /// </exception>
  36. /// <exception cref="ObjectDisposedException">
  37. /// Thrown if source texture of the <paramref name="textureRegion"/> has been disposed prior.
  38. /// </exception>
  39. public static Texture2DRegion GetSubregion(this Texture2DRegion textureRegion, string name, Rectangle region) =>
  40. textureRegion.GetSubregion(region.X, region.Y, region.Width, region.Height, name);
  41. /// <summary>
  42. /// Gets a subregion of the specified texture region using the provided coordinates and dimensions.
  43. /// </summary>
  44. /// <param name="textureRegion">The texture region to get the subregion from.</param>
  45. /// <param name="x">The top-left x-coordinate of the subregion within the texture region.</param>
  46. /// <param name="y">The top-left y-coordinate of the subregion within the texture region.</param>
  47. /// <param name="width">The width, in pixels, of the subregion.</param>
  48. /// <param name="height">The height, in pixels, of the subregion.</param>
  49. /// <returns>A new <see cref="Texture2DRegion"/> representing the specified subregion.</returns>
  50. /// <exception cref="ArgumentNullException">
  51. /// Thrown if <paramref name="textureRegion"/> is <see langword="null"/>.
  52. /// </exception>
  53. /// <exception cref="ObjectDisposedException">
  54. /// Thrown if source texture of the <paramref name="textureRegion"/> has been disposed prior.
  55. /// </exception>
  56. public static Texture2DRegion GetSubregion(this Texture2DRegion textureRegion, int x, int y, int width, int height) =>
  57. textureRegion.GetSubregion(x, y, width, height, null);
  58. /// <summary>
  59. /// Gets a subregion of the specified texture region using the provided name, coordinates, and dimensions.
  60. /// </summary>
  61. /// <param name="textureRegion">The texture region to get the subregion from.</param>
  62. /// <param name="x">The top-left x-coordinate of the subregion within the original sprite.</param>
  63. /// <param name="y">The top-left y-coordinate of the subregion within the original sprite.</param>
  64. /// <param name="width">The width, in pixels, of the subregion.</param>
  65. /// <param name="height">The height, in pixels, of the subregion.</param>
  66. /// <param name="name">The name of the new subregion.</param>
  67. /// <returns>A new <see cref="Texture2DRegion"/> representing the specified subregion.</returns>
  68. /// <exception cref="ArgumentNullException">
  69. /// Thrown if <paramref name="textureRegion"/> is <see langword="null"/>.
  70. /// </exception>
  71. /// <exception cref="ObjectDisposedException">
  72. /// Thrown if source texture of the <paramref name="textureRegion"/> has been disposed prior.
  73. /// </exception>
  74. public static Texture2DRegion GetSubregion(this Texture2DRegion textureRegion, int x, int y, int width, int height, string name)
  75. {
  76. ArgumentNullException.ThrowIfNull(textureRegion);
  77. if (string.IsNullOrEmpty(name))
  78. {
  79. name = $"{textureRegion.Texture.Name}({x}, {y}, {width}, {height})";
  80. }
  81. // The requested subregion is in original sprite coordinates
  82. Rectangle requestedRegion = new Rectangle(x, y, width, height);
  83. // Calculate the bounds of the trimmed region in original sprite coordinates
  84. Rectangle trimmedBounds = new Rectangle(
  85. (int)textureRegion.Offset.X,
  86. (int)textureRegion.Offset.Y,
  87. textureRegion.IsRotated ? textureRegion.Height : textureRegion.Width,
  88. textureRegion.IsRotated ? textureRegion.Width : textureRegion.Height);
  89. // Find intersection between requested subregion and the actual trimmed region
  90. Rectangle intersection = Rectangle.Intersect(requestedRegion, trimmedBounds);
  91. if (intersection.IsEmpty)
  92. {
  93. return null;
  94. }
  95. // The subregion offset must include the existing offset of the input region
  96. Vector2 subregionOffset = new Vector2(Math.Max(0, textureRegion.Offset.X - x),
  97. Math.Max(0, textureRegion.Offset.Y - y));
  98. if (textureRegion.IsRotated)
  99. {
  100. // Calculate the actual texture coordinates
  101. int textureX = textureRegion.X + (textureRegion.Width + (int)textureRegion.Offset.Y - intersection.Bottom);
  102. int textureY = textureRegion.Y + (intersection.X - (int)textureRegion.Offset.X);
  103. return new Texture2DRegion(textureRegion.Texture, textureX, textureY, intersection.Height, intersection.Width,
  104. textureRegion.IsRotated, new Size(width, height), subregionOffset, textureRegion.OriginNormalized, name);
  105. }
  106. else
  107. {
  108. // Calculate the actual texture coordinates
  109. int textureX = textureRegion.X + (intersection.X - (int)textureRegion.Offset.X);
  110. int textureY = textureRegion.Y + (intersection.Y - (int)textureRegion.Offset.Y);
  111. return new Texture2DRegion(textureRegion.Texture, textureX, textureY, intersection.Width, intersection.Height,
  112. textureRegion.IsRotated, new Size(width, height), subregionOffset, textureRegion.OriginNormalized, name);
  113. }
  114. }
  115. /// <summary>
  116. /// Creates a nine-patch from the specified texture region with the specified padding.
  117. /// </summary>
  118. /// <param name="textureRegion">The texture region to create the nine-patch from.</param>
  119. /// <param name="padding">The padding to apply to each side of the nine-patch.</param>
  120. /// <returns>A new <see cref="NinePatch"/> representing the created nine-patch.</returns>
  121. /// <exception cref="ArgumentNullException">
  122. /// Thrown if <paramref name="textureRegion"/> is <see langword="null"/>.
  123. /// </exception>
  124. /// <exception cref="ObjectDisposedException">
  125. /// Thrown if source texture of the <paramref name="textureRegion"/> has been disposed prior.
  126. /// </exception>
  127. public static NinePatch CreateNinePatch(this Texture2DRegion textureRegion, Thickness padding) =>
  128. textureRegion.CreateNinePatch(padding.Left, padding.Top, padding.Right, padding.Bottom);
  129. /// <summary>
  130. /// Creates a nine-patch from the specified texture region with uniform padding.
  131. /// </summary>
  132. /// <param name="textureRegion">The texture region to create the nine-patch from.</param>
  133. /// <param name="padding">The padding to apply uniformly to all sides of the nine-patch.</param>
  134. /// <returns>A new <see cref="NinePatch"/> representing the created nine-patch.</returns>
  135. /// <exception cref="ArgumentNullException">
  136. /// Thrown if <paramref name="textureRegion"/> is <see langword="null"/>.
  137. /// </exception>
  138. /// <exception cref="ObjectDisposedException">
  139. /// Thrown if source texture of the <paramref name="textureRegion"/> has been disposed prior.
  140. /// </exception>
  141. public static NinePatch CreateNinePatch(this Texture2DRegion textureRegion, int padding) =>
  142. textureRegion.CreateNinePatch(padding, padding, padding, padding);
  143. /// <summary>
  144. /// Creates a nine-patch from the specified texture region with non-uniform padding.
  145. /// </summary>
  146. /// <param name="textureRegion">The texture region to create the nine-patch from.</param>
  147. /// <param name="leftPadding">The padding on the left side of the nine-patch.</param>
  148. /// <param name="topPadding">The padding on the top side of the nine-patch.</param>
  149. /// <param name="rightPadding">The padding on the right side of the nine-patch.</param>
  150. /// <param name="bottomPadding">The padding on the bottom side of the nine-patch.</param>
  151. /// <returns>A new <see cref="NinePatch"/> representing the created nine-patch.</returns>
  152. /// <exception cref="ArgumentNullException">
  153. /// Thrown if <paramref name="textureRegion"/> is <see langword="null"/>.
  154. /// </exception>
  155. /// <exception cref="ObjectDisposedException">
  156. /// Thrown if source texture of the <paramref name="textureRegion"/> has been disposed prior.
  157. /// </exception>
  158. public static NinePatch CreateNinePatch(this Texture2DRegion textureRegion, int leftPadding, int topPadding, int rightPadding, int bottomPadding)
  159. {
  160. Texture2DRegion[] patches = new Texture2DRegion[9];
  161. // Use original sprite dimensions for calculations
  162. int middleWidth = textureRegion.OriginalSize.Width - leftPadding - rightPadding;
  163. int middleHeight = textureRegion.OriginalSize.Height - topPadding - bottomPadding;
  164. int rightX = textureRegion.OriginalSize.Width - rightPadding;
  165. int bottomY = textureRegion.OriginalSize.Height - bottomPadding;
  166. patches[NinePatch.TopLeft] = textureRegion.GetSubregion(0, 0, leftPadding, topPadding);
  167. patches[NinePatch.TopMiddle] = textureRegion.GetSubregion(leftPadding, 0, middleWidth, topPadding);
  168. patches[NinePatch.TopRight] = textureRegion.GetSubregion(rightX, 0, rightPadding, topPadding);
  169. patches[NinePatch.MiddleLeft] = textureRegion.GetSubregion(0, topPadding, leftPadding, middleHeight);
  170. patches[NinePatch.Middle] = textureRegion.GetSubregion(leftPadding, topPadding, middleWidth, middleHeight);
  171. patches[NinePatch.MiddleRight] = textureRegion.GetSubregion(rightX, topPadding, rightPadding, middleHeight);
  172. patches[NinePatch.BottomLeft] = textureRegion.GetSubregion(0, bottomY, leftPadding, bottomPadding);
  173. patches[NinePatch.BottomMiddle] = textureRegion.GetSubregion(leftPadding, bottomY, middleWidth, bottomPadding);
  174. patches[NinePatch.BottomRight] = textureRegion.GetSubregion(rightX, bottomY, rightPadding, bottomPadding);
  175. return new NinePatch(patches);
  176. }
  177. }