Size.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. namespace MonoGame.Extended
  4. {
  5. /// <summary>
  6. /// A two dimensional size defined by two real numbers, a width and a height.
  7. /// </summary>
  8. /// <remarks>
  9. /// <para>
  10. /// A size is a subspace of two-dimensional space, the area of which is described in terms of a two-dimensional
  11. /// coordinate system, given by a reference point and two coordinate axes.
  12. /// </para>
  13. /// </remarks>
  14. /// <seealso cref="IEquatable{T}" />
  15. /// <seealso cref="IEquatableByRef{Size}" />
  16. public struct Size : IEquatable<Size>, IEquatableByRef<Size>
  17. {
  18. /// <summary>
  19. /// Returns a <see cref="Size" /> with <see cref="Width" /> and <see cref="Height" /> equal to <c>0.0f</c>.
  20. /// </summary>
  21. public static readonly Size Empty = new Size();
  22. /// <summary>
  23. /// The horizontal component of this <see cref="Size" />.
  24. /// </summary>
  25. public int Width;
  26. /// <summary>
  27. /// The vertical component of this <see cref="Size" />.
  28. /// </summary>
  29. public int Height;
  30. /// <summary>
  31. /// Gets a value that indicates whether this <see cref="Size" /> is empty.
  32. /// </summary>
  33. public bool IsEmpty => Width == 0 && Height == 0;
  34. /// <summary>
  35. /// Initializes a new instance of the <see cref="Size" /> structure from the specified dimensions.
  36. /// </summary>
  37. /// <param name="width">The width.</param>
  38. /// <param name="height">The height.</param>
  39. public Size(int width, int height)
  40. {
  41. Width = width;
  42. Height = height;
  43. }
  44. /// <summary>
  45. /// Compares two <see cref="Size" /> structures. The result specifies
  46. /// whether the values of the <see cref="Width" /> and <see cref="Height" />
  47. /// fields of the two <see cref="Point" /> structures are equal.
  48. /// </summary>
  49. /// <param name="first">The first size.</param>
  50. /// <param name="second">The second size.</param>
  51. /// <returns>
  52. /// <c>true</c> if the <see cref="Width" /> and <see cref="Height" />
  53. /// fields of the two <see cref="Point" /> structures are equal; otherwise, <c>false</c>.
  54. /// </returns>
  55. public static bool operator ==(Size first, Size second)
  56. {
  57. return first.Equals(ref second);
  58. }
  59. /// <summary>
  60. /// Indicates whether this <see cref="Size" /> is equal to another <see cref="Size" />.
  61. /// </summary>
  62. /// <param name="size">The size.</param>
  63. /// <returns>
  64. /// <c>true</c> if this <see cref="Point" /> is equal to the <paramref name="size" /> parameter; otherwise,
  65. /// <c>false</c>.
  66. /// </returns>
  67. public bool Equals(Size size)
  68. {
  69. return Equals(ref size);
  70. }
  71. /// <summary>
  72. /// Indicates whether this <see cref="Size" /> is equal to another <see cref="Size" />.
  73. /// </summary>
  74. /// <param name="size">The size.</param>
  75. /// <returns>
  76. /// <c>true</c> if this <see cref="Point" /> is equal to the <paramref name="size" />; otherwise,
  77. /// <c>false</c>.
  78. /// </returns>
  79. public bool Equals(ref Size size)
  80. {
  81. return Width == size.Width && Height == size.Height;
  82. }
  83. /// <summary>
  84. /// Returns a value indicating whether this <see cref="Size" /> is equal to a specified object.
  85. /// </summary>
  86. /// <param name="obj">The object to make the comparison with.</param>
  87. /// <returns>
  88. /// <c>true</c> if this <see cref="Size" /> is equal to <paramref name="obj" />; otherwise, <c>false</c>.
  89. /// </returns>
  90. public override bool Equals(object obj)
  91. {
  92. if (obj is Size)
  93. return Equals((Size) obj);
  94. return false;
  95. }
  96. /// <summary>
  97. /// Compares two <see cref="Size" /> structures. The result specifies
  98. /// whether the values of the <see cref="Width" /> or <see cref="Height" />
  99. /// fields of the two <see cref="Size" /> structures are unequal.
  100. /// </summary>
  101. /// <param name="first">The first size.</param>
  102. /// <param name="second">The second size.</param>
  103. /// <returns>
  104. /// <c>true</c> if the <see cref="Width" /> or <see cref="Height" />
  105. /// fields of the two <see cref="Size" /> structures are unequal; otherwise, <c>false</c>.
  106. /// </returns>
  107. public static bool operator !=(Size first, Size second)
  108. {
  109. return !(first == second);
  110. }
  111. /// <summary>
  112. /// Calculates the <see cref="Size" /> representing the vector addition of two <see cref="Size" /> structures as if
  113. /// they
  114. /// were <see cref="Vector2" /> structures.
  115. /// </summary>
  116. /// <param name="first">The first size.</param>
  117. /// <param name="second">The second size.</param>
  118. /// <returns>
  119. /// The <see cref="Size" /> representing the vector addition of two <see cref="Size" /> structures as if they
  120. /// were <see cref="Vector2" /> structures.
  121. /// </returns>
  122. public static Size operator +(Size first, Size second)
  123. {
  124. return Add(first, second);
  125. }
  126. /// <summary>
  127. /// Calculates the <see cref="Size" /> representing the vector addition of two <see cref="Size" /> structures.
  128. /// </summary>
  129. /// <param name="first">The first size.</param>
  130. /// <param name="second">The second size.</param>
  131. /// <returns>
  132. /// The <see cref="Size" /> representing the vector addition of two <see cref="Size" /> structures.
  133. /// </returns>
  134. public static Size Add(Size first, Size second)
  135. {
  136. Size size;
  137. size.Width = first.Width + second.Width;
  138. size.Height = first.Height + second.Height;
  139. return size;
  140. }
  141. /// <summary>
  142. /// Calculates the <see cref="Size" /> representing the vector subtraction of two <see cref="Size" /> structures.
  143. /// </summary>
  144. /// <param name="first">The first size.</param>
  145. /// <param name="second">The second size.</param>
  146. /// <returns>
  147. /// The <see cref="Size" /> representing the vector subtraction of two <see cref="Size" /> structures.
  148. /// </returns>
  149. public static Size operator -(Size first, Size second)
  150. {
  151. return Subtract(first, second);
  152. }
  153. public static Size operator /(Size size, int value)
  154. {
  155. return new Size(size.Width / value, size.Height / value);
  156. }
  157. public static Size operator *(Size size, int value)
  158. {
  159. return new Size(size.Width * value, size.Height * value);
  160. }
  161. /// <summary>
  162. /// Calculates the <see cref="Size" /> representing the vector subtraction of two <see cref="Size" /> structures.
  163. /// </summary>
  164. /// <param name="first">The first size.</param>
  165. /// <param name="second">The second size.</param>
  166. /// <returns>
  167. /// The <see cref="Size" /> representing the vector subtraction of two <see cref="Size" /> structures.
  168. /// </returns>
  169. public static Size Subtract(Size first, Size second)
  170. {
  171. Size size;
  172. size.Width = first.Width - second.Width;
  173. size.Height = first.Height - second.Height;
  174. return size;
  175. }
  176. /// <summary>
  177. /// Returns a hash code of this <see cref="Size" /> suitable for use in hashing algorithms and data
  178. /// structures like a hash table.
  179. /// </summary>
  180. /// <returns>
  181. /// A hash code of this <see cref="Point" />.
  182. /// </returns>
  183. public override int GetHashCode()
  184. {
  185. unchecked
  186. {
  187. // ReSharper disable NonReadonlyMemberInGetHashCode
  188. return (Width.GetHashCode()*397) ^ Height.GetHashCode();
  189. // ReSharper restore NonReadonlyMemberInGetHashCode
  190. }
  191. }
  192. /// <summary>
  193. /// Performs an implicit conversion from a <see cref="Point" /> to a <see cref="Size" />.
  194. /// </summary>
  195. /// <param name="point">The point.</param>
  196. /// <returns>
  197. /// The resulting <see cref="Size" />.
  198. /// </returns>
  199. public static implicit operator Size(Point point)
  200. {
  201. return new Size(point.X, point.Y);
  202. }
  203. /// <summary>
  204. /// Performs an implicit conversion from a <see cref="Point" /> to a <see cref="Size" />.
  205. /// </summary>
  206. /// <param name="size">The size.</param>
  207. /// <returns>
  208. /// The resulting <see cref="Point" />.
  209. /// </returns>
  210. public static implicit operator Point(Size size)
  211. {
  212. return new Point(size.Width, size.Height);
  213. }
  214. public static explicit operator Size(SizeF size)
  215. {
  216. return new Size((int) size.Width, (int) size.Height);
  217. }
  218. /// <summary>
  219. /// Returns a <see cref="string" /> that represents this <see cref="Size" />.
  220. /// </summary>
  221. /// <returns>
  222. /// A <see cref="string" /> that represents this <see cref="Size" />.
  223. /// </returns>
  224. public override string ToString()
  225. {
  226. return $"Width: {Width}, Height: {Height}";
  227. }
  228. internal string DebugDisplayString => ToString();
  229. }
  230. }