forked from laicasaane/unity-supplements
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOffsetInt.cs
175 lines (138 loc) · 6.9 KB
/
OffsetInt.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
using System;
using System.Runtime.Serialization;
namespace UnityEngine
{
[Serializable]
public readonly struct OffsetInt : IEquatableReadOnlyStruct<OffsetInt>, ISerializable
{
public readonly int Left;
public readonly int Right;
public readonly int Top;
public readonly int Bottom;
/// <summary>
/// Shortcut for <see cref="Left"/> + <see cref="Right"/>.
/// </summary>
public int Horizontal
=> this.Left + this.Right;
/// <summary>
/// Shortcut for <see cref="Top"/> + <see cref="Bottom"/>.
/// </summary>
public int Vertical
=> this.Top + this.Bottom;
public int 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 OffsetInt(int left, int right, int top, int bottom)
{
this.Left = left;
this.Right = right;
this.Top = top;
this.Bottom = bottom;
}
public void Deconstruct(out int left, out int right, out int top, out int bottom)
{
left = this.Left;
right = this.Right;
top = this.Top;
bottom = this.Bottom;
}
public OffsetInt With(int? Left = null, int? Right = null, int? Top = null, int? Bottom = null)
=> new OffsetInt(
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 OffsetInt other &&
this.Left == other.Left && this.Right == other.Right &&
this.Top == other.Top && this.Bottom == other.Bottom;
public bool Equals(OffsetInt other)
=> this.Left == other.Left && this.Right == other.Right &&
this.Top == other.Top && this.Bottom == other.Bottom;
public bool Equals(in OffsetInt other)
=> this.Left == other.Left && this.Right == other.Right &&
this.Top == other.Top && this.Bottom == other.Bottom;
private OffsetInt(SerializationInfo info, StreamingContext context)
{
this.Left = info.GetInt32OrDefault(nameof(this.Left));
this.Right = info.GetInt32OrDefault(nameof(this.Right));
this.Top = info.GetInt32OrDefault(nameof(this.Top));
this.Bottom = info.GetInt32OrDefault(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="OffsetInt"/>(0, 0, 0, 0).
/// </summary>
public static OffsetInt Zero { get; } = new OffsetInt(0, 0, 0, 0);
public static implicit operator OffsetInt(in (int, int, int, int) value)
=> new OffsetInt(value.Item1, value.Item2, value.Item3, value.Item4);
public static implicit operator OffsetInt(RectOffset value)
=> new OffsetInt(value.left, value.right, value.top, value.bottom);
public static explicit operator RectOffset(in OffsetInt value)
=> new RectOffset(value.Left, value.Right, value.Top, value.Bottom);
public static implicit operator OffsetInt(in Vector4 v)
=> new OffsetInt((int)v.x, (int)v.y, (int)v.z, (int)v.w);
public static implicit operator Vector4(in OffsetInt c)
=> new Vector4(c.Left, c.Right, c.Top, c.Bottom);
public static OffsetInt operator +(in OffsetInt lhs, in OffsetInt rhs)
=> new OffsetInt(lhs.Left + rhs.Left, lhs.Right + rhs.Right, lhs.Top + rhs.Top, lhs.Bottom + rhs.Bottom);
public static OffsetInt operator -(in OffsetInt lhs, in OffsetInt rhs)
=> new OffsetInt(lhs.Left - rhs.Left, lhs.Right - rhs.Right, lhs.Top - rhs.Top, lhs.Bottom - rhs.Bottom);
public static OffsetInt operator -(in OffsetInt a)
=> new OffsetInt(-a.Left, -a.Right, -a.Top, -a.Bottom);
public static OffsetInt operator *(in OffsetInt lhs, int rhs)
=> new OffsetInt(lhs.Left * rhs, lhs.Right * rhs, lhs.Top * rhs, lhs.Bottom * rhs);
public static OffsetInt operator *(int lhs, in OffsetInt rhs)
=> new OffsetInt(lhs * rhs.Left, lhs * rhs.Right, lhs * rhs.Top, lhs * rhs.Bottom);
public static OffsetInt operator *(in OffsetInt lhs, in OffsetInt rhs)
=> new OffsetInt(lhs.Left * rhs.Left, lhs.Right * rhs.Right, lhs.Top * rhs.Top, lhs.Bottom * rhs.Bottom);
public static OffsetInt operator /(in OffsetInt lhs, int rhs)
=> new OffsetInt(lhs.Left / rhs, lhs.Right / rhs, lhs.Top / rhs, lhs.Bottom / rhs);
public static OffsetInt operator /(in OffsetInt lhs, in OffsetInt rhs)
=> new OffsetInt(lhs.Left / rhs.Left, lhs.Right / rhs.Right, lhs.Top / rhs.Top, lhs.Bottom / rhs.Bottom);
public static OffsetInt operator %(in OffsetInt lhs, int rhs)
=> new OffsetInt(lhs.Left % rhs, lhs.Right % rhs, lhs.Top % rhs, lhs.Bottom % rhs);
public static OffsetInt operator %(in OffsetInt lhs, in OffsetInt rhs)
=> new OffsetInt(lhs.Left % rhs.Left, lhs.Right % rhs.Right, lhs.Top % rhs.Top, lhs.Bottom % rhs.Bottom);
public static bool operator ==(in OffsetInt lhs, in OffsetInt rhs)
=> lhs.Left == rhs.Left && lhs.Right == rhs.Right &&
lhs.Top == rhs.Top && lhs.Bottom == rhs.Bottom;
public static bool operator !=(in OffsetInt lhs, in OffsetInt rhs)
=> lhs.Left != rhs.Left && lhs.Right != rhs.Right &&
lhs.Top != rhs.Top && lhs.Bottom != rhs.Bottom;
}
}