-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSize.cs
152 lines (117 loc) · 5.36 KB
/
Size.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
using System;
using System.Runtime.Serialization;
namespace UnityEngine
{
[Serializable]
public readonly struct Size : IEquatableReadOnlyStruct<Size>, ISerializable
{
public readonly float Width;
public readonly float Height;
public float this[int index]
{
get
{
if (index == 0) return this.Width;
if (index == 1) return this.Height;
throw new IndexOutOfRangeException();
}
}
public Size(float width, float height)
{
this.Width = width;
this.Height = height;
}
public void Deconstruct(out float width, out float height)
{
width = this.Width;
height = this.Height;
}
public Size With(float? Width = null, float? 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.GetHashCode();
hashCode = hashCode * -1521134295 + this.Height.GetHashCode();
return hashCode;
#pragma warning restore CS0162 // Unreachable code detected
}
public override bool Equals(object obj)
=> obj is Size other &&
Mathf.Approximately(this.Width, other.Width) &&
Mathf.Approximately(this.Height, other.Height);
public bool Equals(Size other)
=> Mathf.Approximately(this.Width, other.Width) &&
Mathf.Approximately(this.Height, other.Height);
public bool Equals(in Size other)
=> Mathf.Approximately(this.Width, other.Width) &&
Mathf.Approximately(this.Height, other.Height);
public override string ToString()
=> $"({this.Width}, {this.Height})";
private Size(SerializationInfo info, StreamingContext context)
{
this.Width = info.GetSingleOrDefault(nameof(this.Width));
this.Height = info.GetSingleOrDefault(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="Size"/>(0, 0).
/// </summary>
public static Size Zero { get; } = new Size(0, 0);
public static implicit operator Size(in (float, float) value)
=> new Size(value.Item1, value.Item2);
public static implicit operator Size(Vector2 value)
=> new Size(value.x, value.y);
public static implicit operator Size(Vector2Int value)
=> new Size(value.x, value.y);
public static implicit operator Vector2(in Size value)
=> new Vector2(value.Width, value.Height);
public static explicit operator Vector2Int(in Size value)
=> new Vector2Int((int)value.Width, (int)value.Height);
public static implicit operator Size(Rect value)
=> new Size(value.width, value.height);
public static explicit operator Rect(in Size value)
=> new Rect(0, 0, value.Width, value.Height);
public static explicit operator RectInt(in Size value)
=> new RectInt(0, 0, (int)value.Width, (int)value.Height);
public static Size operator +(in Size lhs, in Size rhs)
=> new Size(lhs.Width + rhs.Width, lhs.Height + rhs.Height);
public static Size operator -(in Size lhs, in Size rhs)
=> new Size(lhs.Width - rhs.Width, lhs.Height - rhs.Height);
public static Size operator -(in Size a)
=> new Size(-a.Width, -a.Height);
public static Size operator *(in Size lhs, float rhs)
=> new Size(lhs.Width * rhs, lhs.Height * rhs);
public static Size operator *(float lhs, in Size rhs)
=> new Size(lhs * rhs.Width, lhs * rhs.Height);
public static Size operator *(in Size lhs, in Vector2 rhs)
=> new Size(lhs.Width * rhs.x, lhs.Height * rhs.y);
public static Size operator *(in Vector2 lhs, in Size rhs)
=> new Size(rhs.Width * lhs.x, rhs.Height * lhs.y);
public static Size operator *(in Size lhs, in Size rhs)
=> new Size(lhs.Width * rhs.Width, lhs.Height * rhs.Height);
public static Size operator /(in Size lhs, float rhs)
=> new Size(lhs.Width / rhs, lhs.Height / rhs);
public static Size operator /(in Size lhs, in Vector2 rhs)
=> new Size(lhs.Width / rhs.x, lhs.Height / rhs.y);
public static Size operator /(in Size lhs, in Size rhs)
=> new Size(lhs.Width / rhs.Width, lhs.Height / rhs.Height);
public static bool operator ==(in Size lhs, in Size rhs)
=> Mathf.Approximately(lhs.Width, rhs.Width) &&
Mathf.Approximately(lhs.Height, rhs.Height);
public static bool operator !=(in Size lhs, in Size rhs)
=> !Mathf.Approximately(lhs.Width, rhs.Width) ||
!Mathf.Approximately(lhs.Height, rhs.Height);
}
}