RelativePoint.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using Avalonia.Utilities;
  4. using System;
  5. using System.Globalization;
  6. using System.Linq;
  7. namespace Avalonia
  8. {
  9. /// <summary>
  10. /// Defines the reference point units of an <see cref="RelativePoint"/> or
  11. /// <see cref="RelativeRect"/>.
  12. /// </summary>
  13. public enum RelativeUnit
  14. {
  15. /// <summary>
  16. /// The point is expressed as a fraction of the containing element's size.
  17. /// </summary>
  18. Relative,
  19. /// <summary>
  20. /// The point is absolute (i.e. in pixels).
  21. /// </summary>
  22. Absolute,
  23. }
  24. /// <summary>
  25. /// Defines a point that may be defined relative to a containing element.
  26. /// </summary>
  27. public struct RelativePoint : IEquatable<RelativePoint>
  28. {
  29. /// <summary>
  30. /// A point at the top left of the containing element.
  31. /// </summary>
  32. public static readonly RelativePoint TopLeft = new RelativePoint(0, 0, RelativeUnit.Relative);
  33. /// <summary>
  34. /// A point at the center of the containing element.
  35. /// </summary>
  36. public static readonly RelativePoint Center = new RelativePoint(0.5, 0.5, RelativeUnit.Relative);
  37. /// <summary>
  38. /// A point at the bottom right of the containing element.
  39. /// </summary>
  40. public static readonly RelativePoint BottomRight = new RelativePoint(1, 1, RelativeUnit.Relative);
  41. private Point _point;
  42. private readonly RelativeUnit _unit;
  43. /// <summary>
  44. /// Initializes a new instance of the <see cref="RelativePoint"/> struct.
  45. /// </summary>
  46. /// <param name="x">The X point.</param>
  47. /// <param name="y">The Y point</param>
  48. /// <param name="unit">The unit.</param>
  49. public RelativePoint(double x, double y, RelativeUnit unit)
  50. : this(new Point(x, y), unit)
  51. {
  52. }
  53. /// <summary>
  54. /// Initializes a new instance of the <see cref="RelativePoint"/> struct.
  55. /// </summary>
  56. /// <param name="point">The point.</param>
  57. /// <param name="unit">The unit.</param>
  58. public RelativePoint(Point point, RelativeUnit unit)
  59. {
  60. _point = point;
  61. _unit = unit;
  62. }
  63. /// <summary>
  64. /// Gets the point.
  65. /// </summary>
  66. public Point Point => _point;
  67. /// <summary>
  68. /// Gets the unit.
  69. /// </summary>
  70. public RelativeUnit Unit => _unit;
  71. /// <summary>
  72. /// Checks for equality between two <see cref="RelativePoint"/>s.
  73. /// </summary>
  74. /// <param name="left">The first point.</param>
  75. /// <param name="right">The second point.</param>
  76. /// <returns>True if the points are equal; otherwise false.</returns>
  77. public static bool operator ==(RelativePoint left, RelativePoint right)
  78. {
  79. return left.Equals(right);
  80. }
  81. /// <summary>
  82. /// Checks for unequality between two <see cref="RelativePoint"/>s.
  83. /// </summary>
  84. /// <param name="left">The first point.</param>
  85. /// <param name="right">The second point.</param>
  86. /// <returns>True if the points are unequal; otherwise false.</returns>
  87. public static bool operator !=(RelativePoint left, RelativePoint right)
  88. {
  89. return !left.Equals(right);
  90. }
  91. /// <summary>
  92. /// Checks if the <see cref="RelativePoint"/> equals another object.
  93. /// </summary>
  94. /// <param name="obj">The other object.</param>
  95. /// <returns>True if the objects are equal, otherwise false.</returns>
  96. public override bool Equals(object obj)
  97. {
  98. return (obj is RelativePoint) && Equals((RelativePoint)obj);
  99. }
  100. /// <summary>
  101. /// Checks if the <see cref="RelativePoint"/> equals another point.
  102. /// </summary>
  103. /// <param name="p">The other point.</param>
  104. /// <returns>True if the objects are equal, otherwise false.</returns>
  105. public bool Equals(RelativePoint p)
  106. {
  107. return Unit == p.Unit && Point == p.Point;
  108. }
  109. /// <summary>
  110. /// Gets a hashcode for a <see cref="RelativePoint"/>.
  111. /// </summary>
  112. /// <returns>A hash code.</returns>
  113. public override int GetHashCode()
  114. {
  115. unchecked
  116. {
  117. int hash = 17;
  118. hash = (hash * 23) + Unit.GetHashCode();
  119. hash = (hash * 23) + Point.GetHashCode();
  120. return hash;
  121. }
  122. }
  123. /// <summary>
  124. /// Converts a <see cref="RelativePoint"/> into pixels.
  125. /// </summary>
  126. /// <param name="size">The size of the visual.</param>
  127. /// <returns>The origin point in pixels.</returns>
  128. public Point ToPixels(Size size)
  129. {
  130. return _unit == RelativeUnit.Absolute ?
  131. _point :
  132. new Point(_point.X * size.Width, _point.Y * size.Height);
  133. }
  134. /// <summary>
  135. /// Parses a <see cref="RelativePoint"/> string.
  136. /// </summary>
  137. /// <param name="s">The string.</param>
  138. /// <param name="culture">The current culture.</param>
  139. /// <returns>The parsed <see cref="RelativePoint"/>.</returns>
  140. public static RelativePoint Parse(string s, CultureInfo culture)
  141. {
  142. using (var tokenizer = new StringTokenizer(s, culture, exceptionMessage: "Invalid RelativePoint"))
  143. {
  144. var x = tokenizer.ReadString();
  145. var y = tokenizer.ReadString();
  146. var unit = RelativeUnit.Absolute;
  147. var scale = 1.0;
  148. if (x.EndsWith("%"))
  149. {
  150. if (!y.EndsWith("%"))
  151. {
  152. throw new FormatException("If one coordinate is relative, both must be.");
  153. }
  154. x = x.TrimEnd('%');
  155. y = y.TrimEnd('%');
  156. unit = RelativeUnit.Relative;
  157. scale = 0.01;
  158. }
  159. return new RelativePoint(
  160. double.Parse(x, culture) * scale,
  161. double.Parse(y, culture) * scale,
  162. unit);
  163. }
  164. }
  165. }
  166. }