-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIView+b2BodyBackedView.m
215 lines (161 loc) · 7.35 KB
/
UIView+b2BodyBackedView.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
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
//
// UIView+b2BodyBackedView.m
// GraceWorld
//
// Created by John Detloff on 4/21/13.
// Copyright (c) 2013 Uebie. All rights reserved.
//
#import "UIView+b2BodyBackedView.h"
#import "DiagonalView.h"
#import "OrbitalSurface.h"
#import "PolygonView.h"
#include <objc/runtime.h>
#import <QuartzCore/QuartzCore.h>
static const char* kGTPhysicsBackedView = "kGTPhysicsBackedView";
static const char* kGTPhysicsBackedViewBodyOffset = "kGTPhysicsBackedViewBodyOffset";
static const char* kGTSurfaceView = "kGTSurfaceView";
@implementation UIView (b2BodyBackedView)
- (id)initWithb2Fixture:(b2Fixture *)fixture orbitsToPixelsRatio:(CGFloat)orbitsToPixelsRatio {
self = [self init];
if (self) {
[self setPhysicsFixture:fixture];
CGSize fixtureSize;
CGPoint bodyOffset;
switch (fixture->GetType()) {
case 0: //circle
NSLog(@"not ready for circle!");
break;
case 1: { // edge
b2EdgeShape *edgeShape = (b2EdgeShape *)fixture->GetShape();
b2Vec2 vertexA = edgeShape->m_vertex1;
b2Vec2 vertexB = edgeShape->m_vertex2;
vertexA.x /= orbitsToPixelsRatio;
vertexA.y /= orbitsToPixelsRatio;
vertexB.x /= orbitsToPixelsRatio;
vertexB.y /= orbitsToPixelsRatio;
BOOL positive;
CGRect diagRect = [self rectForVertexA:vertexA vertexB:vertexB positive:&positive];
diagRect.size.width = MAX(4, diagRect.size.width);
diagRect.size.height = MAX(4, diagRect.size.height);
DiagonalView *diagonalView = [[DiagonalView alloc] initWithFrame:diagRect];
diagonalView.lineColor = [UIColor greenColor];
diagonalView.positive = positive;
[self addSubview:diagonalView];
[self setSurfaceView:diagonalView];
bodyOffset = CGPointZero;
fixtureSize = CGSizeZero;
break;
}
case 2: { // polygon
b2PolygonShape *polygonShape = (b2PolygonShape *)fixture->GetShape();
CGRect fixtureRect = [self rectFromVerticesOnShape:polygonShape];
fixtureSize = fixtureRect.size;
bodyOffset = CGPointMake(fixtureRect.origin.x+fixtureSize.width/2, fixtureRect.origin.y+fixtureSize.height/2);
NSMutableArray *points = [[NSMutableArray alloc] init];
for (int i = 0; i < polygonShape->GetVertexCount(); i++) {
b2Vec2 vertex = polygonShape->GetVertex(i);
CGPoint point = CGPointMake(vertex.x, vertex.y);
point.y = -point.y;
point.x += -fixtureRect.origin.x;
point.y += -fixtureRect.origin.y;
point.x /= orbitsToPixelsRatio;
point.y /= orbitsToPixelsRatio;
[points addObject:[NSValue valueWithCGPoint:point]];
}
fixtureSize.width /= orbitsToPixelsRatio;
fixtureSize.height /= orbitsToPixelsRatio;
bodyOffset.x /= orbitsToPixelsRatio;
bodyOffset.y /= -orbitsToPixelsRatio;
fixtureRect.origin.x /= orbitsToPixelsRatio;
fixtureRect.origin.y /= orbitsToPixelsRatio;
PolygonView *polyView = [[PolygonView alloc] initWithFrame:CGRectMake(fixtureRect.origin.x + fixtureSize.width/2 , fixtureRect.origin.y + fixtureSize.height/2, fixtureSize.width, fixtureSize.height) points:points];
polyView.backgroundColor = [UIColor clearColor];
polyView.layer.masksToBounds = NO;
polyView.userInteractionEnabled = NO;
polyView.color = (fixture->IsSensor() ? [[UIColor blueColor] colorWithAlphaComponent:0.3] : [UIColor redColor]);
[self addSubview:polyView];
self.backgroundColor = [UIColor clearColor];
// self.layer.borderColor = [UIColor blackColor].CGColor;
// self.layer.borderWidth = 1;
break;
}
default:
break;
}
[self setBodyOffset:bodyOffset];
self.layer.masksToBounds = NO;
self.frame = CGRectMake(0, 0, fixtureSize.width, fixtureSize.height);
}
return self;
}
#pragma mark - getters/setters
- (void)setSurfaceView:(UIView *)surfaceView {
objc_setAssociatedObject(self, kGTSurfaceView, surfaceView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIView *)surfaceView {
return (UIView *)objc_getAssociatedObject(self, kGTSurfaceView);
}
- (void)setPhysicsFixture:(b2Fixture *)fixture {
NSValue *val = [NSValue valueWithPointer:fixture];
objc_setAssociatedObject(self, kGTPhysicsBackedView, val, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (b2Fixture *)physicsFixture {
NSValue *val = objc_getAssociatedObject(self, kGTPhysicsBackedView);
return (b2Fixture *)[val pointerValue];
}
- (void)setBodyOffset:(CGPoint)bodyOffset {
NSValue *val = [NSValue valueWithCGPoint:bodyOffset];
objc_setAssociatedObject(self, kGTPhysicsBackedViewBodyOffset, val, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (CGPoint)bodyOffset {
NSValue *val = objc_getAssociatedObject(self, kGTPhysicsBackedViewBodyOffset);
return [val CGPointValue];
}
#pragma mark -
- (CGRect)rectFromVerticesOnShape:(b2PolygonShape *)polygonShape {
NSInteger vertexCount = polygonShape->GetVertexCount();
if (vertexCount == 0) {
return CGRectZero;
}
b2Vec2 firstVertex = polygonShape->GetVertex(0);
b2Vec2 leftMostVertex = firstVertex;
b2Vec2 rightMostVertex = firstVertex;
b2Vec2 lowestVertex = firstVertex;
b2Vec2 highestVertex = firstVertex;
for (int i = 1; i < vertexCount; i++) {
b2Vec2 vertex = polygonShape->GetVertex(i);
if (vertex.x < leftMostVertex.x) {
leftMostVertex = vertex;
}
if (vertex.x > rightMostVertex.x) {
rightMostVertex = vertex;
}
if (vertex.y > highestVertex.y) {
highestVertex = vertex;
}
if (vertex.y < lowestVertex.y) {
lowestVertex = vertex;
}
}
CGFloat width = rightMostVertex.x - leftMostVertex.x;
CGFloat height = highestVertex.y - lowestVertex.y;
return CGRectMake(leftMostVertex.x, -highestVertex.y, width, height);
}
- (CGRect)rectForVertexA:(b2Vec2)vertexA vertexB:(b2Vec2)vertexB positive:(BOOL *)positive {
if (vertexA.x > vertexB.x) {
b2Vec2 temp = vertexA;
vertexA = vertexB;
vertexB = temp;
}
CGSize size = CGSizeMake(vertexB.x - vertexA.x, fabsf(vertexB.y - vertexA.y));
CGPoint origin;
if (vertexA.y < vertexB.y) {
*positive = YES;
origin = CGPointMake(vertexA.x, -(vertexA.y + size.height));
} else {
*positive = NO;
origin = CGPointMake(vertexA.x, -vertexA.y);
}
return CGRectMake(origin.x, origin.y, size.width, size.height);
}
@end