forked from laicasaane/unity-supplements
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSizeInt.cs
154 lines (117 loc) · 5.54 KB
/
SizeInt.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
using System;
using System.Runtime.Serialization;
namespace UnityEngine
{
[Serializable]
public readonly struct SizeInt : IEquatableReadOnlyStruct<SizeInt>, ISerializable
{
public readonly int Width;
public readonly int Height;
public int this[int index]
{
get
{
if (index == 0) return this.Width;
if (index == 1) return this.Height;
throw new IndexOutOfRangeException();
}
}
public SizeInt(int width, int height)
{
this.Width = width;
this.Height = height;
}
public void Deconstruct(out int width, out int height)
{
width = this.Width;
height = this.Height;
}
public Size With(int? Width = null, int? Height = null)
=> new Size(
Width ?? this.Width,
Height ?? this.Height
);
public override int GetHashCode()
{
#if USE_SYSTEM_HASHCODE
return HashCode.Combine(this.Width, this.Height);
#endif
#pragma warning disable CS0162 // Unreachable code detected
var hashCode = 859600377;
hashCode = hashCode * -1521134295 + this.Width;
hashCode = hashCode * -1521134295 + this.Height;
return hashCode;
#pragma warning restore CS0162 // Unreachable code detected
}
public override bool Equals(object obj)
=> obj is SizeInt other &&
this.Width == other.Width &&
this.Height == other.Height;
public bool Equals(SizeInt other)
=> this.Width == other.Width && this.Height == other.Height;
public bool Equals(in SizeInt other)
=> this.Width == other.Width && this.Height == other.Height;
public override string ToString()
=> $"({this.Width}, {this.Height})";
private SizeInt(SerializationInfo info, StreamingContext context)
{
this.Width = info.GetInt32OrDefault(nameof(this.Width));
this.Height = info.GetInt32OrDefault(nameof(this.Height));
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue(nameof(this.Width), this.Width);
info.AddValue(nameof(this.Height), this.Height);
}
/// <summary>
/// Shorthand for writing <see cref="SizeInt"/>(0, 0).
/// </summary>
public static SizeInt Zero { get; } = new SizeInt(0, 0);
public static implicit operator SizeInt(in (int, int) value)
=> new SizeInt(value.Item1, value.Item2);
public static implicit operator SizeInt(Vector2Int value)
=> new SizeInt(value.x, value.y);
public static implicit operator Vector2Int(in SizeInt value)
=> new Vector2Int(value.Width, value.Height);
public static implicit operator Vector2(in SizeInt value)
=> new Vector2(value.Width, value.Height);
public static implicit operator SizeInt(RectInt value)
=> new SizeInt(value.width, value.height);
public static explicit operator RectInt(in SizeInt value)
=> new RectInt(0, 0, value.Width, value.Height);
public static explicit operator Rect(in SizeInt value)
=> new Rect(0, 0, value.Width, value.Height);
public static SizeInt operator +(in SizeInt lhs, in SizeInt rhs)
=> new SizeInt(lhs.Width + rhs.Width, lhs.Height + rhs.Height);
public static SizeInt operator -(in SizeInt lhs, in SizeInt rhs)
=> new SizeInt(lhs.Width - rhs.Width, lhs.Height - rhs.Height);
public static SizeInt operator -(in SizeInt a)
=> new SizeInt(-a.Width, -a.Height);
public static SizeInt operator *(in SizeInt lhs, int rhs)
=> new SizeInt(lhs.Width * rhs, lhs.Height * rhs);
public static SizeInt operator *(int lhs, in SizeInt rhs)
=> new SizeInt(lhs * rhs.Width, lhs * rhs.Height);
public static SizeInt operator *(in SizeInt lhs, in Vector2Int rhs)
=> new SizeInt(lhs.Width * rhs.x, lhs.Height * rhs.y);
public static SizeInt operator *(in Vector2Int lhs, in SizeInt rhs)
=> new SizeInt(rhs.Width * lhs.x, rhs.Height * lhs.y);
public static SizeInt operator *(in SizeInt lhs, in SizeInt rhs)
=> new SizeInt(lhs.Width * rhs.Width, lhs.Height * rhs.Height);
public static SizeInt operator /(in SizeInt lhs, int rhs)
=> new SizeInt(lhs.Width / rhs, lhs.Height / rhs);
public static SizeInt operator /(in SizeInt lhs, in Vector2Int rhs)
=> new SizeInt(lhs.Width / rhs.x, lhs.Height / rhs.y);
public static SizeInt operator /(in SizeInt lhs, in SizeInt rhs)
=> new SizeInt(lhs.Width / rhs.Width, lhs.Height / rhs.Height);
public static SizeInt operator %(in SizeInt lhs, int rhs)
=> new SizeInt(lhs.Width % rhs, lhs.Height % rhs);
public static SizeInt operator %(in SizeInt lhs, in Vector2Int rhs)
=> new SizeInt(lhs.Width % rhs.x, lhs.Height % rhs.y);
public static SizeInt operator %(in SizeInt lhs, in SizeInt rhs)
=> new SizeInt(lhs.Width % rhs.Width, lhs.Height % rhs.Height);
public static bool operator ==(in SizeInt lhs, in SizeInt rhs)
=> lhs.Width == rhs.Width && lhs.Height == rhs.Height;
public static bool operator !=(in SizeInt lhs, in SizeInt rhs)
=> lhs.Width != rhs.Width || lhs.Height != rhs.Height;
}
}