forked from laicasaane/unity-supplements
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOffset.cs
181 lines (144 loc) · 7.16 KB
/
Offset.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using System;
using System.Runtime.Serialization;
namespace UnityEngine
{
[Serializable]
public readonly struct Offset : IEquatableReadOnlyStruct<Offset>, ISerializable
{
public readonly float Left;
public readonly float Right;
public readonly float Top;
public readonly float Bottom;
/// <summary>
/// Shortcut for <see cref="Left"/> + <see cref="Right"/>.
/// </summary>
public float Horizontal
=> this.Left + this.Right;
/// <summary>
/// Shortcut for <see cref="Top"/> + <see cref="Bottom"/>.
/// </summary>
public float Vertical
=> this.Top + this.Bottom;
public float this[int index]
{
get
{
switch (index)
{
case 0: return this.Left;
case 1: return this.Right;
case 2: return this.Top;
case 3: return this.Bottom;
default: throw new IndexOutOfRangeException();
}
}
}
public Offset(float left, float right, float top, float bottom)
{
this.Left = left;
this.Right = right;
this.Top = top;
this.Bottom = bottom;
}
public void Deconstruct(out float left, out float right, out float top, out float bottom)
{
left = this.Left;
right = this.Right;
top = this.Top;
bottom = this.Bottom;
}
public Offset With(float? Left = null, float? Right = null, float? Top = null, float? Bottom = null)
=> new Offset(
Left ?? this.Left,
Right ?? this.Right,
Top ?? this.Top,
Bottom ?? this.Bottom
);
public override string ToString()
=> $"({this.Left}, {this.Right}, {this.Top}, {this.Bottom})";
public override int GetHashCode()
{
#if USE_SYSTEM_HASHCODE
return HashCode.Combine(this.Left, this.Right, this.Top, this.Bottom);
#endif
#pragma warning disable CS0162 // Unreachable code detected
var hashCode = 551583723;
hashCode = hashCode * -1521134295 + this.Left.GetHashCode();
hashCode = hashCode * -1521134295 + this.Right.GetHashCode();
hashCode = hashCode * -1521134295 + this.Top.GetHashCode();
hashCode = hashCode * -1521134295 + this.Bottom.GetHashCode();
return hashCode;
#pragma warning restore CS0162 // Unreachable code detected
}
public override bool Equals(object obj)
=> obj is Offset other &&
this.Left == other.Left && this.Right == other.Right &&
this.Top == other.Top && this.Bottom == other.Bottom;
public bool Equals(Offset other)
=> this.Left == other.Left && this.Right == other.Right &&
this.Top == other.Top && this.Bottom == other.Bottom;
public bool Equals(in Offset other)
=> this.Left == other.Left && this.Right == other.Right &&
this.Top == other.Top && this.Bottom == other.Bottom;
private Offset(SerializationInfo info, StreamingContext context)
{
this.Left = info.GetSingleOrDefault(nameof(this.Left));
this.Right = info.GetSingleOrDefault(nameof(this.Right));
this.Top = info.GetSingleOrDefault(nameof(this.Top));
this.Bottom = info.GetSingleOrDefault(nameof(this.Bottom));
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue(nameof(this.Left), this.Left);
info.AddValue(nameof(this.Right), this.Right);
info.AddValue(nameof(this.Top), this.Top);
info.AddValue(nameof(this.Bottom), this.Bottom);
}
/// <summary>
/// Shorthand for writing <see cref="Offset"/>(0, 0, 0, 0).
/// </summary>
public static Offset Zero { get; } = new Offset(0, 0, 0, 0);
public static implicit operator Offset(in (float, float, float, float) value)
=> new Offset(value.Item1, value.Item2, value.Item3, value.Item4);
public static implicit operator Offset(RectOffset value)
=> new Offset(value.left, value.right, value.top, value.bottom);
public static explicit operator RectOffset(in Offset value)
=> new RectOffset((int)value.Left, (int)value.Right, (int)value.Top, (int)value.Bottom);
public static implicit operator Offset(in Vector4 v)
=> new Offset(v.x, v.y, v.z, v.w);
public static implicit operator Vector4(in Offset c)
=> new Vector4(c.Left, c.Right, c.Top, c.Bottom);
public static Offset operator +(in Offset lhs, in Offset rhs)
=> new Offset(lhs.Left + rhs.Left, lhs.Right + rhs.Right, lhs.Top + rhs.Top, lhs.Bottom + rhs.Bottom);
public static Offset operator -(in Offset lhs, in Offset rhs)
=> new Offset(lhs.Left - rhs.Left, lhs.Right - rhs.Right, lhs.Top - rhs.Top, lhs.Bottom - rhs.Bottom);
public static Offset operator -(in Offset a)
=> new Offset(-a.Left, -a.Right, -a.Top, -a.Bottom);
public static Offset operator *(in Offset lhs, int rhs)
=> new Offset(lhs.Left * rhs, lhs.Right * rhs, lhs.Top * rhs, lhs.Bottom * rhs);
public static Offset operator *(int lhs, in Offset rhs)
=> new Offset(lhs * rhs.Left, lhs * rhs.Right, lhs * rhs.Top, lhs * rhs.Bottom);
public static Offset operator *(in Offset lhs, in Vector4 rhs)
=> new Offset(lhs.Left * rhs.x, lhs.Right * rhs.y, lhs.Top * rhs.z, lhs.Bottom * rhs.w);
public static Offset operator *(in Vector4 lhs, in Offset rhs)
=> new Offset(rhs.Left * lhs.x, rhs.Right * lhs.y, rhs.Top * lhs.z, rhs.Bottom * lhs.w);
public static Offset operator *(in Offset lhs, in Offset rhs)
=> new Offset(lhs.Left * rhs.Left, lhs.Right * rhs.Right, lhs.Top * rhs.Top, lhs.Bottom * rhs.Bottom);
public static Offset operator /(in Offset lhs, int rhs)
=> new Offset(lhs.Left / rhs, lhs.Right / rhs, lhs.Top / rhs, lhs.Bottom / rhs);
public static Offset operator /(in Offset lhs, in Vector4 rhs)
=> new Offset(lhs.Left / rhs.x, lhs.Right / rhs.y, lhs.Top / rhs.z, lhs.Bottom / rhs.w);
public static Offset operator /(in Offset lhs, in Offset rhs)
=> new Offset(lhs.Left / rhs.Left, lhs.Right / rhs.Right, lhs.Top / rhs.Top, lhs.Bottom / rhs.Bottom);
public static bool operator ==(in Offset lhs, in Offset rhs)
=> Mathf.Approximately(lhs.Left, rhs.Left) &&
Mathf.Approximately(lhs.Right, rhs.Right) &&
Mathf.Approximately(lhs.Top, rhs.Top) &&
Mathf.Approximately(lhs.Bottom, rhs.Bottom);
public static bool operator !=(in Offset lhs, in Offset rhs)
=> !Mathf.Approximately(lhs.Left, rhs.Left) ||
!Mathf.Approximately(lhs.Right, rhs.Right) ||
!Mathf.Approximately(lhs.Top, rhs.Top) ||
!Mathf.Approximately(lhs.Bottom, rhs.Bottom);
}
}