Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Viewport.cs
Go to the documentation of this file.
3
5
6[SuppressMessage("Microsoft.Performance", "CA1815:OverrideEqualsAndOperatorEqualsOnValueTypes")]
7public struct Viewport
8{
9 private int _x;
10
11 private int _y;
12
13 private int _width;
14
15 private int _height;
16
17 private float _minZ;
18
19 private float _maxZ;
20
21 public int X
22 {
23 get
24 {
25 return _x;
26 }
27 set
28 {
29 _x = value;
30 }
31 }
32
33 public int Y
34 {
35 get
36 {
37 return _y;
38 }
39 set
40 {
41 _y = value;
42 }
43 }
44
45 public int Width
46 {
47 get
48 {
49 return _width;
50 }
51 set
52 {
53 _width = value;
54 }
55 }
56
57 public int Height
58 {
59 get
60 {
61 return _height;
62 }
63 set
64 {
65 _height = value;
66 }
67 }
68
69 public float MinDepth
70 {
71 get
72 {
73 return _minZ;
74 }
75 set
76 {
77 _minZ = value;
78 }
79 }
80
81 public float MaxDepth
82 {
83 get
84 {
85 return _maxZ;
86 }
87 set
88 {
89 _maxZ = value;
90 }
91 }
92
94 {
95 get
96 {
97 Rectangle result = default(Rectangle);
98 result.X = _x;
99 result.Y = _y;
100 result.Width = _width;
101 result.Height = _height;
102 return result;
103 }
104 set
105 {
106 _x = value.X;
107 _y = value.Y;
108 _width = value.Width;
109 _height = value.Height;
110 }
111 }
112
113 public float AspectRatio
114 {
115 get
116 {
117 if (_height == 0 || _width == 0)
118 {
119 return 0f;
120 }
121 return (float)_width / (float)_height;
122 }
123 }
124
126
127 public Viewport(int x, int y, int width, int height)
128 {
129 _x = x;
130 _y = y;
131 _width = width;
132 _height = height;
133 _minZ = 0f;
134 _maxZ = 1f;
135 }
136
137 public Viewport(Rectangle bounds)
138 {
139 _x = bounds.X;
140 _y = bounds.Y;
141 _width = bounds.Width;
142 _height = bounds.Height;
143 _minZ = 0f;
144 _maxZ = 1f;
145 }
146
147 public override string ToString()
148 {
149 return string.Format(CultureInfo.CurrentCulture, "{{X:{0} Y:{1} Width:{2} Height:{3} MinDepth:{4} MaxDepth:{5}}}", X, Y, Width, Height, MinDepth, MaxDepth);
150 }
151
152 private static bool WithinEpsilon(float a, float b)
153 {
154 float num = a - b;
155 if (-1E-45f <= num)
156 {
157 return num <= float.Epsilon;
158 }
159 return false;
160 }
161
162 public Vector3 Project(Vector3 source, Matrix projection, Matrix view, Matrix world)
163 {
164 Matrix matrix = Matrix.Multiply(world, view);
165 matrix = Matrix.Multiply(matrix, projection);
166 Vector3 result = Vector3.Transform(source, matrix);
167 float num = source.X * matrix.M14 + source.Y * matrix.M24 + source.Z * matrix.M34 + matrix.M44;
168 if (!WithinEpsilon(num, 1f))
169 {
170 result /= num;
171 }
172 result.X = (result.X + 1f) * 0.5f * (float)Width + (float)X;
173 result.Y = (0f - result.Y + 1f) * 0.5f * (float)Height + (float)Y;
174 result.Z = result.Z * (MaxDepth - MinDepth) + MinDepth;
175 return result;
176 }
177
178 public Vector3 Unproject(Vector3 source, Matrix projection, Matrix view, Matrix world)
179 {
180 Matrix matrix = Matrix.Multiply(world, view);
181 matrix = Matrix.Multiply(matrix, projection);
182 matrix = Matrix.Invert(matrix);
183 source.X = (source.X - (float)X) / (float)Width * 2f - 1f;
184 source.Y = 0f - ((source.Y - (float)Y) / (float)Height * 2f - 1f);
185 source.Z = (source.Z - MinDepth) / (MaxDepth - MinDepth);
186 Vector3 result = Vector3.Transform(source, matrix);
187 float num = source.X * matrix.M14 + source.Y * matrix.M24 + source.Z * matrix.M34 + matrix.M44;
188 if (!WithinEpsilon(num, 1f))
189 {
190 result /= num;
191 }
192 return result;
193 }
194
195 internal static Rectangle GetTitleSafeArea(int x, int y, int w, int h)
196 {
197 return new Rectangle(x, y, w, h);
198 }
199}
static CultureInfo CurrentCulture
Vector3 Project(Vector3 source, Matrix projection, Matrix view, Matrix world)
Definition Viewport.cs:162
Viewport(int x, int y, int width, int height)
Definition Viewport.cs:127
static Rectangle GetTitleSafeArea(int x, int y, int w, int h)
Definition Viewport.cs:195
Vector3 Unproject(Vector3 source, Matrix projection, Matrix view, Matrix world)
Definition Viewport.cs:178
static bool WithinEpsilon(float a, float b)
Definition Viewport.cs:152
static Matrix Invert(Matrix matrix)
Definition Matrix.cs:1694
static Matrix Multiply(Matrix matrix1, Matrix matrix2)
Definition Matrix.cs:1982
static Vector3 Transform(Vector3 position, Matrix matrix)
Definition Vector3.cs:407