-
Notifications
You must be signed in to change notification settings - Fork 520
/
Copy pathCGAffineTransform.cs
501 lines (454 loc) · 15.4 KB
/
CGAffineTransform.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
//
// CGAffineTransform.cs: Implements the managed side
//
// Authors: Mono Team
//
// Copyright 2009 Novell, Inc
// Copyright 2014 Xamarin Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#nullable enable
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using CoreFoundation;
using ObjCRuntime;
using Foundation;
namespace CoreGraphics {
// CGAffineTransform.h
#if NET
[SupportedOSPlatform ("ios")]
[SupportedOSPlatform ("maccatalyst")]
[SupportedOSPlatform ("macos")]
[SupportedOSPlatform ("tvos")]
#endif
[StructLayout (LayoutKind.Sequential)]
public struct CGAffineTransform {
#if NET
public /* CGFloat */ nfloat A;
public /* CGFloat */ nfloat B;
public /* CGFloat */ nfloat C;
public /* CGFloat */ nfloat D;
public /* CGFloat */ nfloat Tx;
public /* CGFloat */ nfloat Ty;
#if !XAMCORE_5_0
[EditorBrowsable (EditorBrowsableState.Never)]
[Obsolete ("Use 'A' instead.")]
public nfloat xx { get => A; set => A = value; }
[EditorBrowsable (EditorBrowsableState.Never)]
[Obsolete ("Use 'B' instead.")]
public nfloat yx { get => B; set => B = value; }
[EditorBrowsable (EditorBrowsableState.Never)]
[Obsolete ("Use 'C' instead.")]
public nfloat xy { get => C; set => C = value; }
[EditorBrowsable (EditorBrowsableState.Never)]
[Obsolete ("Use 'D' instead.")]
public nfloat yy { get => D; set => D = value; }
[EditorBrowsable (EditorBrowsableState.Never)]
[Obsolete ("Use 'Tx' instead.")]
public nfloat x0 { get => Tx; set => Tx = value; }
[EditorBrowsable (EditorBrowsableState.Never)]
[Obsolete ("Use 'Ty' instead.")]
public nfloat y0 { get => Ty; set => Ty = value; }
#endif // !XAMCORE_5_0
#else
[Obsolete ("Use 'A' instead.")]
public /* CGFloat */ nfloat xx; // a
[Obsolete ("Use 'B' instead.")]
public /* CGFloat */ nfloat yx; // b
[Obsolete ("Use 'C' instead.")]
public /* CGFloat */ nfloat xy; // c
[Obsolete ("Use 'D' instead.")]
public /* CGFloat */ nfloat yy; // d
[Obsolete ("Use 'Tx' instead.")]
public /* CGFloat */ nfloat x0; // tx
[Obsolete ("Use 'Ty' instead.")]
public /* CGFloat */ nfloat y0; // ty
#pragma warning disable CS0618 // Type or member is obsolete
public /* CGFloat */ nfloat A { get => xx; set => xx = value; }
public /* CGFloat */ nfloat B { get => yx; set => yx = value; }
public /* CGFloat */ nfloat C { get => xy; set => xy = value; }
public /* CGFloat */ nfloat D { get => yy; set => yy = value; }
public /* CGFloat */ nfloat Tx { get => x0; set => x0 = value; }
public /* CGFloat */ nfloat Ty { get => y0; set => y0 = value; }
#pragma warning restore CS0618 // Type or member is obsolete
#endif // NET
#if !COREBUILD
//
// Constructors
//
#if NET
public CGAffineTransform (nfloat a, nfloat b, nfloat c, nfloat d, nfloat tx, nfloat ty)
{
this.A = a;
this.B = b;
this.C = c;
this.D = d;
this.Tx = tx;
this.Ty = ty;
}
#else
public CGAffineTransform (nfloat xx, nfloat yx, nfloat xy, nfloat yy, nfloat x0, nfloat y0)
{
#pragma warning disable CS0618 // Type or member is obsolete
this.xx = xx;
this.yx = yx;
this.xy = xy;
this.yy = yy;
this.x0 = x0;
this.y0 = y0;
#pragma warning restore CS0618 // Type or member is obsolete
}
#endif // NET
// Identity
public static CGAffineTransform MakeIdentity ()
{
return new CGAffineTransform (1, 0, 0, 1, 0, 0);
}
public static CGAffineTransform MakeRotation (nfloat angle)
{
var cos = (nfloat) Math.Cos (angle);
var sin = (nfloat) Math.Sin (angle);
return new CGAffineTransform (
cos, sin,
-sin, cos,
0, 0);
}
public static CGAffineTransform MakeScale (nfloat sx, nfloat sy)
{
return new CGAffineTransform (sx, 0, 0, sy, 0, 0);
}
public static CGAffineTransform MakeTranslation (nfloat tx, nfloat ty)
{
return new CGAffineTransform (1, 0, 0, 1, tx, ty);
}
//
// Operations
//
public static CGAffineTransform Multiply (CGAffineTransform a, CGAffineTransform b)
{
#if NET
return new CGAffineTransform (a.A * b.A + a.B * b.C,
a.A * b.B + a.B * b.D,
a.C * b.A + a.D * b.C,
a.C * b.B + a.D * b.D,
a.Tx * b.A + a.Ty * b.C + b.Tx,
a.Tx * b.B + a.Ty * b.D + b.Ty);
#else
#pragma warning disable CS0618 // Type or member is obsolete
return new CGAffineTransform (a.xx * b.xx + a.yx * b.xy,
a.xx * b.yx + a.yx * b.yy,
a.xy * b.xx + a.yy * b.xy,
a.xy * b.yx + a.yy * b.yy,
a.x0 * b.xx + a.y0 * b.xy + b.x0,
a.x0 * b.yx + a.y0 * b.yy + b.y0);
#pragma warning restore CS0618 // Type or member is obsolete
#endif // NET
}
public void Multiply (CGAffineTransform b)
{
var a = this;
#if NET
A = a.A * b.A + a.B * b.C;
B = a.A * b.B + a.B * b.D;
C = a.C * b.A + a.D * b.C;
D = a.C * b.B + a.D * b.D;
Tx = a.Tx * b.A + a.Ty * b.C + b.Tx;
Ty = a.Tx * b.B + a.Ty * b.D + b.Ty;
#else
#pragma warning disable CS0618 // Type or member is obsolete
xx = a.xx * b.xx + a.yx * b.xy;
yx = a.xx * b.yx + a.yx * b.yy;
xy = a.xy * b.xx + a.yy * b.xy;
yy = a.xy * b.yx + a.yy * b.yy;
x0 = a.x0 * b.xx + a.y0 * b.xy + b.x0;
y0 = a.x0 * b.yx + a.y0 * b.yy + b.y0;
#pragma warning restore CS0618 // Type or member is obsolete
#endif // NET
}
public void Scale (nfloat sx, nfloat sy, MatrixOrder order)
{
switch (order) {
case MatrixOrder.Prepend: // The new operation is applied before the old operation.
this = Multiply (MakeScale (sx, sy), this); // t' = [ sx 0 0 sy 0 0 ] * t – Swift equivalent
break;
case MatrixOrder.Append: // The new operation is applied after the old operation.
this = Multiply (this, MakeScale (sx, sy)); // t' = t * [ sx 0 0 sy 0 0 ]
break;
default:
throw new ArgumentOutOfRangeException (nameof (order));
}
}
[Advice ("By default, the new operation is applied after the old operation: t' = t * [ sx 0 0 sy 0 0 ].\nTo have the same behavior as the native Swift API, pass 'MatrixOrder.Prepend' to 'Scale (nfloat, nfloat, MatrixOrder)'.")]
public void Scale (nfloat sx, nfloat sy)
{
Scale (sx, sy, MatrixOrder.Append);
}
public static CGAffineTransform Scale (CGAffineTransform transform, nfloat sx, nfloat sy)
{
#if NET
return new CGAffineTransform (
sx * transform.A,
sx * transform.B,
sy * transform.C,
sy * transform.D,
transform.Tx,
transform.Ty);
#else
#pragma warning disable CS0618 // Type or member is obsolete
return new CGAffineTransform (
sx * transform.xx,
sx * transform.yx,
sy * transform.xy,
sy * transform.yy,
transform.x0,
transform.y0);
#pragma warning restore CS0618 // Type or member is obsolete
#endif // NET
}
public void Translate (nfloat tx, nfloat ty, MatrixOrder order)
{
switch (order) {
case MatrixOrder.Prepend: // The new operation is applied before the old operation.
this = Multiply (MakeTranslation (tx, ty), this); // t' = [ 1 0 0 1 tx ty ] * t – Swift equivalent
break;
case MatrixOrder.Append: // The new operation is applied after the old operation.
this = Multiply (this, MakeTranslation (tx, ty)); // t' = t * [ 1 0 0 1 tx ty ]
break;
default:
throw new ArgumentOutOfRangeException (nameof (order));
}
}
[Advice ("By default, the new operation is applied after the old operation: t' = t * [ 1 0 0 1 tx ty ].\nTo have the same behavior as the native Swift API, pass 'MatrixOrder.Prepend' to 'Translate (nfloat, nfloat, MatrixOrder)'.")]
public void Translate (nfloat tx, nfloat ty)
{
Translate (tx, ty, MatrixOrder.Append);
}
public static CGAffineTransform Translate (CGAffineTransform transform, nfloat tx, nfloat ty)
{
#if NET
return new CGAffineTransform (
transform.A,
transform.B,
transform.C,
transform.D,
tx * transform.A + ty * transform.C + transform.Tx,
tx * transform.B + ty * transform.D + transform.Ty);
#else
#pragma warning disable CS0618 // Type or member is obsolete
return new CGAffineTransform (
transform.xx,
transform.yx,
transform.xy,
transform.yy,
tx * transform.xx + ty * transform.xy + transform.x0,
tx * transform.yx + ty * transform.yy + transform.y0);
#pragma warning disable CS0618 // Type or member is obsolete
#endif // NET
}
public void Rotate (nfloat angle, MatrixOrder order)
{
switch (order) {
case MatrixOrder.Prepend: // The new operation is applied before the old operation.
this = Multiply (MakeRotation (angle), this); // t' = [ cos(angle) sin(angle) -sin(angle) cos(angle) 0 0 ] * t – Swift equivalent
break;
case MatrixOrder.Append: // The new operation is applied after the old operation.
this = Multiply (this, MakeRotation (angle)); // t' = t * [ cos(angle) sin(angle) -sin(angle) cos(angle) 0 0 ]
break;
default:
throw new ArgumentOutOfRangeException (nameof (order));
}
}
[Advice ("By default, the new operation is applied after the old operation: t' = t * [ cos(angle) sin(angle) -sin(angle) cos(angle) 0 0 ].\nTo have the same behavior as the native Swift API, pass 'MatrixOrder.Prepend' to 'Rotate (nfloat, MatrixOrder)'.")]
public void Rotate (nfloat angle)
{
Rotate (angle, MatrixOrder.Append);
}
public static CGAffineTransform Rotate (CGAffineTransform transform, nfloat angle)
{
#if NET
var cos = (nfloat) Math.Cos (angle);
var sin = (nfloat) Math.Sin (angle);
return new CGAffineTransform (
cos * transform.A + sin * transform.C,
cos * transform.B + sin * transform.D,
cos * transform.C - sin * transform.A,
cos * transform.D - sin * transform.B,
transform.Tx,
transform.Ty);
#else
var cos = (nfloat) Math.Cos (angle);
var sin = (nfloat) Math.Sin (angle);
return new CGAffineTransform (
cos * transform.xx + sin * transform.xy,
cos * transform.yx + sin * transform.yy,
cos * transform.xy - sin * transform.xx,
cos * transform.yy - sin * transform.yx,
transform.x0,
transform.y0);
#endif
}
public bool IsIdentity {
get {
#if NET
return A == 1 && B == 0 && C == 0 && D == 1 && Tx == 0 && Ty == 0;
#else
return xx == 1 && yx == 0 && xy == 0 && yy == 1 && x0 == 0 && y0 == 0;
#endif
}
}
#if NET && !MONOMAC
// on macOS NSAffineTransform is an ObjC type
[DllImport (Constants.UIKitLibrary)]
extern static /* NSString */ IntPtr NSStringFromCGAffineTransform (CGAffineTransform transform);
#endif
public override String? ToString ()
{
#if NET
#if MONOMAC
var s = $"[{A}, {B}, {C}, {D}, {Tx}, {Ty}]";
#else
var s = CFString.FromHandle (NSStringFromCGAffineTransform (this));
#endif
#else
var s = String.Format ("xx:{0:##0.0#} yx:{1:##0.0#} xy:{2:##0.0#} yy:{3:##0.0#} x0:{4:##0.0#} y0:{5:##0.0#}", xx, yx, xy, yy, x0, y0);
#endif
return s;
}
public static bool operator == (CGAffineTransform lhs, CGAffineTransform rhs)
{
#if NET
return (lhs.A == rhs.A && lhs.C == rhs.C &&
lhs.B == rhs.B && lhs.D == rhs.D &&
lhs.Tx == rhs.Tx && lhs.Ty == rhs.Ty);
#else
return (lhs.xx == rhs.xx && lhs.xy == rhs.xy &&
lhs.yx == rhs.yx && lhs.yy == rhs.yy &&
lhs.x0 == rhs.x0 && lhs.y0 == rhs.y0);
#endif
}
public static bool operator != (CGAffineTransform lhs, CGAffineTransform rhs)
{
return !(lhs == rhs);
}
public static CGAffineTransform operator * (CGAffineTransform a, CGAffineTransform b)
{
#if NET
return new CGAffineTransform (a.A * b.A + a.B * b.C,
a.A * b.B + a.B * b.D,
a.C * b.A + a.D * b.C,
a.C * b.B + a.D * b.D,
a.Tx * b.A + a.Ty * b.C + b.Tx,
a.Tx * b.B + a.Ty * b.D + b.Ty);
#else
return new CGAffineTransform (a.xx * b.xx + a.yx * b.xy,
a.xx * b.yx + a.yx * b.yy,
a.xy * b.xx + a.yy * b.xy,
a.xy * b.yx + a.yy * b.yy,
a.x0 * b.xx + a.y0 * b.xy + b.x0,
a.x0 * b.yx + a.y0 * b.yy + b.y0);
#endif
}
public override bool Equals (object? o)
{
if (o is CGAffineTransform transform) {
return this == transform;
} else
return false;
}
public override int GetHashCode ()
{
#if NET
return HashCode.Combine (A, C, B, D, Tx, Ty);
#else
return (int) this.xx ^ (int) this.xy ^
(int) this.yx ^ (int) this.yy ^
(int) this.x0 ^ (int) this.y0;
#endif
}
public CGPoint TransformPoint (CGPoint point)
{
#if NET
return new CGPoint (A * point.X + C * point.Y + Tx,
B * point.X + D * point.Y + Ty);
#else
return new CGPoint (xx * point.X + xy * point.Y + x0,
yx * point.X + yy * point.Y + y0);
#endif
}
[DllImport (Constants.CoreGraphicsLibrary)]
public extern static CGRect CGRectApplyAffineTransform (CGRect rect, CGAffineTransform t);
public CGRect TransformRect (CGRect rect)
{
return CGRectApplyAffineTransform (rect, this);
}
[DllImport (Constants.CoreGraphicsLibrary)]
extern static CGSize CGSizeApplyAffineTransform (CGSize rect, CGAffineTransform t);
public CGSize TransformSize (CGSize size)
{
return CGSizeApplyAffineTransform (size, this);
}
[DllImport (Constants.CoreGraphicsLibrary)]
public extern static CGAffineTransform CGAffineTransformInvert (CGAffineTransform t);
public CGAffineTransform Invert ()
{
return CGAffineTransformInvert (this);
}
#if NET
[SupportedOSPlatform ("ios16.0")]
[SupportedOSPlatform ("maccatalyst16.0")]
[SupportedOSPlatform ("macos13.0")]
[SupportedOSPlatform ("tvos16.0")]
#else
[Mac (13, 0), iOS (16, 0), TV (16, 0), MacCatalyst (16, 0)]
#endif
[DllImport (Constants.CoreGraphicsLibrary)]
static extern CGAffineTransformComponents CGAffineTransformDecompose (CGAffineTransform transform);
[SupportedOSPlatform ("ios16.0")]
[SupportedOSPlatform ("maccatalyst16.0")]
[SupportedOSPlatform ("macos13.0")]
[SupportedOSPlatform ("tvos16.0")]
public CGAffineTransformComponents Decompose ()
{
return CGAffineTransformDecompose (this);
}
#if NET
[SupportedOSPlatform ("ios16.0")]
[SupportedOSPlatform ("maccatalyst16.0")]
[SupportedOSPlatform ("macos13.0")]
[SupportedOSPlatform ("tvos16.0")]
#else
[Mac (13, 0), iOS (16, 0), TV (16, 0), MacCatalyst (16, 0)]
#endif
[DllImport (Constants.CoreGraphicsLibrary)]
static extern CGAffineTransform CGAffineTransformMakeWithComponents (CGAffineTransformComponents components);
[SupportedOSPlatform ("ios16.0")]
[SupportedOSPlatform ("maccatalyst16.0")]
[SupportedOSPlatform ("macos13.0")]
[SupportedOSPlatform ("tvos16.0")]
public static CGAffineTransform MakeWithComponents (CGAffineTransformComponents components)
{
return CGAffineTransformMakeWithComponents (components);
}
#endif // !COREBUILD
}
}