-
Notifications
You must be signed in to change notification settings - Fork 597
Expand file tree
/
Copy pathRotationView.m
More file actions
79 lines (56 loc) · 2.41 KB
/
RotationView.m
File metadata and controls
79 lines (56 loc) · 2.41 KB
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
//
// 3DRotationView.m
// CPTTestApp
//
#import "RotationView.h"
static const CGFloat kMouseMovementScaleFactorForRotation = 1.0;
@interface RotationView()
@property (nonatomic, readwrite) NSPoint previousLocation;
@end
@implementation RotationView
#pragma mark -
#pragma mark Initialization and teardown
-(nonnull instancetype)initWithFrame:(NSRect)frame
{
if ((self = [super initWithFrame:frame])) {
rotationTransform = CATransform3DIdentity;
// Initialization code here.
}
return self;
}
#pragma mark -
#pragma mark Mouse handling methods
-(BOOL)acceptsFirstMouse:(nullable NSEvent *__unused)theEvent
{
return YES;
}
-(void)mouseDown:(nonnull NSEvent *)theEvent
{
self.previousLocation = [self convertPoint:theEvent.locationInWindow fromView:nil];
}
-(void)mouseDragged:(nonnull NSEvent *)theEvent
{
NSPoint currentLocation = [self convertPoint:theEvent.locationInWindow fromView:nil];
CGFloat displacementInX = kMouseMovementScaleFactorForRotation * (currentLocation.x - self.previousLocation.x);
CGFloat displacementInY = kMouseMovementScaleFactorForRotation * (self.previousLocation.y - currentLocation.y);
CGFloat totalRotation = sqrt(displacementInX * displacementInX + displacementInY * displacementInY);
CATransform3D oldTransform = self.rotationTransform;
CATransform3D newTransform = CATransform3DRotate(oldTransform, totalRotation * M_PI / 180.0,
((displacementInX / totalRotation) * oldTransform.m12 + (displacementInY / totalRotation) * oldTransform.m11),
((displacementInX / totalRotation) * oldTransform.m22 + (displacementInY / totalRotation) * oldTransform.m21),
((displacementInX / totalRotation) * oldTransform.m32 + (displacementInY / totalRotation) * oldTransform.m31));
id<CPTRotationDelegate> theDelegate = self.rotationDelegate;
[theDelegate rotateObjectUsingTransform:newTransform];
self.rotationTransform = newTransform;
self.previousLocation = [self convertPoint:theEvent.locationInWindow fromView:nil];
}
-(void)mouseUp:(nonnull NSEvent *)theEvent
{
self.previousLocation = [self convertPoint:theEvent.locationInWindow fromView:nil];
}
#pragma mark -
#pragma mark Accessors
@synthesize rotationTransform;
@synthesize rotationDelegate;
@synthesize previousLocation;
@end