forked from bang590/JSPatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJPCGGeometry.m
167 lines (140 loc) · 5.2 KB
/
JPCGGeometry.m
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
//
// JPCGGeometryHelper.m
// JSPatchDemo
//
// Created by Albert438 on 15/7/2.
// Copyright © 2015年 bang. All rights reserved.
//
#import "JPCGGeometry.h"
#import <CoreGraphics/CGGeometry.h>
@implementation JPCGGeometry
+ (void)main:(JSContext *)context
{
[JPEngine defineStruct:@{
@"name": @"CGVector",
@"types": @"FF",
@"keys": @[@"dx", @"dy"]
}];
[JPEngine defineStruct:@{
@"name": @"CGAffineTransform",
@"types": @"FFFFFF",
@"keys": @[@"a", @"b", @"c", @"d", @"tx", @"ty"]
}];
context[@"CGRectContainsPoint"] = ^BOOL(NSDictionary *rectDict, NSDictionary *pointDict) {
CGRect rect;
CGPoint point;
[JPCGGeometry rectStruct:&rect ofDict:rectDict];
[JPCGGeometry pointStruct:&point ofDict:pointDict];
return CGRectContainsPoint(rect, point);
};
context[@"CGRectEqualToRect"] = ^BOOL(NSDictionary *rectDict1, NSDictionary *rectDict2) {
CGRect rect1,rect2;
[JPCGGeometry rectStruct:&rect1 ofDict:rectDict1];
[JPCGGeometry rectStruct:&rect2 ofDict:rectDict2];
return CGRectEqualToRect(rect1, rect2);
};
context[@"CGRectGetMaxX"] = ^CGFloat(NSDictionary *rectDict) {
CGRect rect;
[JPCGGeometry rectStruct:&rect ofDict:rectDict];
return CGRectGetMaxX(rect);
};
context[@"CGRectGetMaxY"] = ^CGFloat(NSDictionary *rectDict) {
CGRect rect;
[JPCGGeometry rectStruct:&rect ofDict:rectDict];
return CGRectGetMaxY(rect);
};
context[@"CGRectGetMidX"] = ^CGFloat(NSDictionary *rectDict) {
CGRect rect;
[JPCGGeometry rectStruct:&rect ofDict:rectDict];
return CGRectGetMidX(rect);
};
context[@"CGRectGetMidY"] = ^CGFloat(NSDictionary *rectDict) {
CGRect rect;
[JPCGGeometry rectStruct:&rect ofDict:rectDict];
return CGRectGetMidY(rect);
};
context[@"CGRectGetMinX"] = ^CGFloat(NSDictionary *rectDict) {
CGRect rect;
[JPCGGeometry rectStruct:&rect ofDict:rectDict];
return CGRectGetMinX(rect);
};
context[@"CGRectGetMinY"] = ^CGFloat(NSDictionary *rectDict) {
CGRect rect;
[JPCGGeometry rectStruct:&rect ofDict:rectDict];
return CGRectGetMinY(rect);
};
context[@"CGRectInset"] = ^CGRect(NSDictionary *rectDict, CGFloat dx, CGFloat dy) {
CGRect rect;
[JPCGGeometry rectStruct:&rect ofDict:rectDict];
CGRect rectInset = CGRectInset(rect, dx, dy);
return rectInset;
};
context[@"CGRectIntegral"] = ^CGRect(NSDictionary *rectDict) {
CGRect rect;
[JPCGGeometry rectStruct:&rect ofDict:rectDict];
CGRect rectIntegral = CGRectIntegral(rect);
return rectIntegral;
};
context[@"CGRectIntersection"] = ^CGRect(NSDictionary *rectDict1, NSDictionary *rectDict2) {
CGRect rect1,rect2;
[JPCGGeometry rectStruct:&rect1 ofDict:rectDict1];
[JPCGGeometry rectStruct:&rect2 ofDict:rectDict2];
CGRect rectIntersection = CGRectIntersection(rect1, rect2);
return rectIntersection;
};
context[@"CGRectOffset"] = ^CGRect(NSDictionary *rectDict, CGFloat dx, CGFloat dy) {
CGRect rect;
[JPCGGeometry rectStruct:&rect ofDict:rectDict];
CGRect rectOffset = CGRectOffset(rect, dx, dy);
return rectOffset;
};
context[@"CGRectIntersectsRect"] = ^BOOL(NSDictionary *rectDict1, NSDictionary *rectDict2) {
CGRect rect1,rect2;
[JPCGGeometry rectStruct:&rect1 ofDict:rectDict1];
[JPCGGeometry rectStruct:&rect2 ofDict:rectDict2];
return CGRectIntersectsRect(rect1, rect2);
};
}
+ (void)rectStruct:(CGRect *)rect ofDict:(NSDictionary *)dict
{
CGPoint point;
CGSize size;
point.x = [dict[@"x"] doubleValue];
point.y = [dict[@"y"] doubleValue];
size.width = [dict[@"width"] doubleValue];
size.height = [dict[@"height"] doubleValue];
rect->origin = point;
rect->size = size;
}
+ (void)pointStruct:(CGPoint *)point ofDict:(NSDictionary *)dict
{
point->x = [dict[@"x"] doubleValue];
point->y = [dict[@"y"] doubleValue];
}
+ (void)sizeStruct:(CGSize *)size ofDict:(NSDictionary *)dict
{
size->width = [dict[@"width"] doubleValue];
size->height = [dict[@"height"] doubleValue];
}
+ (void)vectorStruct:(CGVector *)vector ofDict:(NSDictionary *)dict
{
vector->dx = [dict[@"dx"] doubleValue];
vector->dy = [dict[@"dy"] doubleValue];
}
+ (NSDictionary *)rectDictOfStruct:(CGRect *)rect
{
return @{@"x": @(rect->origin.x), @"y": @(rect->origin.y), @"width": @(rect->size.width), @"height": @(rect->size.height)};
}
+ (NSDictionary *)sizeDictOfStruct:(CGSize *)size
{
return @{@"width": @(size->width), @"height": @(size->height)};
}
+ (NSDictionary *)pointDictOfStruct:(CGPoint *)point
{
return @{@"x": @(point->x), @"y": @(point->y)};
}
+ (NSDictionary *)vectorDictOfStruct:(CGVector *)vector
{
return @{@"dx": @(vector->dx), @"dy": @(vector->dy)};
}
@end