|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +namespace CommunityToolkit.WinUI.Controls; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// A struct representing a coordinate in UV adjusted space. |
| 9 | +/// </summary> |
| 10 | +[DebuggerDisplay("({U}u,{V}v)")] |
| 11 | +public struct UVCoord: IEquatable<UVCoord> |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Initializes a new instance of the <see cref="UVCoord"/> struct. |
| 15 | + /// </summary> |
| 16 | + public UVCoord(Orientation orientation) |
| 17 | + { |
| 18 | + Orientation = orientation; |
| 19 | + } |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Initializes a new instance of the <see cref="UVCoord"/> struct. |
| 23 | + /// </summary> |
| 24 | + public UVCoord(double x, double y, Orientation orientation) |
| 25 | + { |
| 26 | + X = x; |
| 27 | + Y = y; |
| 28 | + Orientation = orientation; |
| 29 | + } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Initializes a new instance of the <see cref="UVCoord"/> struct. |
| 33 | + /// </summary> |
| 34 | + public UVCoord(Point point, Orientation orientation) : this(point.X, point.Y, orientation) |
| 35 | + { |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Initializes a new instance of the <see cref="UVCoord"/> struct. |
| 40 | + /// </summary> |
| 41 | + public UVCoord(Size size, Orientation orientation) : this(size.Width, size.Height, orientation) |
| 42 | + { |
| 43 | + } |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Gets or sets the X coordinate. |
| 47 | + /// </summary> |
| 48 | + public double X { readonly get; set; } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Gets or sets the Y coordinate. |
| 52 | + /// </summary> |
| 53 | + public double Y { readonly get; set; } |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Gets or sets the orientation for translation between the XY and UV coordinate systems. |
| 57 | + /// </summary> |
| 58 | + public Orientation Orientation { get; set; } |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Gets or sets the U coordinate. |
| 62 | + /// </summary> |
| 63 | + public double U |
| 64 | + { |
| 65 | + readonly get => Orientation is Orientation.Horizontal ? X : Y; |
| 66 | + set |
| 67 | + { |
| 68 | + if (Orientation is Orientation.Horizontal) |
| 69 | + { |
| 70 | + X = value; |
| 71 | + } |
| 72 | + else |
| 73 | + { |
| 74 | + Y = value; |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /// <summary> |
| 80 | + /// Gets or sets the V coordinate. |
| 81 | + /// </summary> |
| 82 | + public double V |
| 83 | + { |
| 84 | + readonly get => Orientation is Orientation.Vertical ? X : Y; |
| 85 | + set |
| 86 | + { |
| 87 | + if (Orientation is Orientation.Vertical) |
| 88 | + { |
| 89 | + X = value; |
| 90 | + } |
| 91 | + else |
| 92 | + { |
| 93 | + Y = value; |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /// <summary> |
| 99 | + /// Implicitly casts a <see cref="UVCoord"/> to a <see cref="Point"/>. |
| 100 | + /// </summary> |
| 101 | + public static implicit operator Point(UVCoord uv) => new(uv.X, uv.Y); |
| 102 | + |
| 103 | + /// <summary> |
| 104 | + /// Implicitly casts a <see cref="UVCoord"/> to a <see cref="Size"/>. |
| 105 | + /// </summary> |
| 106 | + public static implicit operator Size(UVCoord uv) => new(uv.X, uv.Y); |
| 107 | + |
| 108 | + /// <inheritdoc/> |
| 109 | + public static UVCoord operator +(UVCoord addend1, UVCoord addend2) |
| 110 | + { |
| 111 | + if (addend1.Orientation != addend2.Orientation) |
| 112 | + { |
| 113 | + throw new InvalidOperationException($"Cannot add {nameof(UVCoord)} with mismatched {nameof(Orientation)}."); |
| 114 | + } |
| 115 | + |
| 116 | + var xSum = addend1.X + addend2.X; |
| 117 | + var ySum = addend1.Y + addend2.Y; |
| 118 | + var orientation = addend1.Orientation; |
| 119 | + return new UVCoord(xSum, ySum, orientation); |
| 120 | + } |
| 121 | + |
| 122 | + /// <inheritdoc/> |
| 123 | + public static bool operator ==(UVCoord coord1, UVCoord coord2) |
| 124 | + { |
| 125 | + return coord1.U == coord2.U && coord1.V == coord2.V; |
| 126 | + } |
| 127 | + |
| 128 | + |
| 129 | + /// <inheritdoc/> |
| 130 | + public static bool operator !=(UVCoord measure1, UVCoord measure2) |
| 131 | + { |
| 132 | + return !(measure1 == measure2); |
| 133 | + } |
| 134 | + |
| 135 | + /// <inheritdoc/> |
| 136 | + public override bool Equals(object? obj) |
| 137 | + { |
| 138 | + return obj is UVCoord other && Equals(other); |
| 139 | + } |
| 140 | + |
| 141 | + /// <inheritdoc/> |
| 142 | + public bool Equals(UVCoord other) |
| 143 | + { |
| 144 | + return this == other; |
| 145 | + } |
| 146 | + |
| 147 | + /// <inheritdoc/> |
| 148 | + public override int GetHashCode() |
| 149 | + { |
| 150 | + return HashCode.Combine(U, V); |
| 151 | + } |
| 152 | +} |
0 commit comments