-
Notifications
You must be signed in to change notification settings - Fork 1
/
ImageClip.m
183 lines (144 loc) · 3.91 KB
/
ImageClip.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
//
// ImageClip.m
// Crossbow
//
// Copyright (C) 2009 Roland Rabien
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#import "ImageClip.h"
#import "Viewer.h"
@interface NSEvent (DeviceDelta)
- (float)deviceDeltaY;
@end
@implementation ImageClip
@synthesize lastScroll;
- (id)initWithFrame:(NSRect)frame
{
if (self = [super initWithFrame:frame])
{
center = NSMakePoint(500, 500);
self.lastScroll = [NSDate distantPast];
}
return self;
}
- (void)setDelegate:(id)del
{
delegate = del;
}
- (void)drawRect:(NSRect)rect
{
[[NSColor blackColor] drawSwatchInRect:rect];
}
- (void)setDocument:(NSView*)view
{
document = view;
[self setSubviews:[NSArray arrayWithObject:document]];
}
- (void)centerImage
{
center = NSMakePoint(500, 500);
[self update];
}
- (NSPoint)normalizedCenter
{
NSSize sz = [document frame].size;
return NSMakePoint(center.x / 1000 * sz.width, center.y / 1000 * sz.height);
}
- (void)update
{
NSRect rc = [document frame];
NSRect bounds = [self bounds];
NSPoint nCenter = [self normalizedCenter];
rc.origin.x = -(nCenter.x - bounds.size.width / 2.0);
rc.origin.y = -(nCenter.y - bounds.size.height / 2.0);
[document setFrame:rc];
}
- (void)setFrame:(NSRect)frameRect
{
[super setFrame:frameRect];
[self update];
}
- (void)setFrameOrigin:(NSPoint)newOrigin
{
[super setFrameOrigin:newOrigin];
[self update];
}
- (void)setFrameSize:(NSSize)newSize
{
[super setFrameSize:newSize];
[self update];
}
- (BOOL)mouseDownCanMoveWindow
{
return NO;
}
- (void)mouseDown:(NSEvent*)event
{
if ([event clickCount] == 2)
{
[delegate imageClipClose:self];
}
else
{
dragStart = [event locationInWindow];
dragCenter = center;
}
}
- (void)panX:(int)deltaX Y:(int)deltaY
{
NSRect rc = [document frame];
center.x = center.x + deltaX / rc.size.width * 1000;
center.y = center.y + deltaY / rc.size.height * 1000;
if (center.y < 0) center.y = 0;
if (center.x < 0) center.x = 0;
if (center.x > 1000) center.x = 1000;
if (center.y > 1000) center.y = 1000;
[self update];
}
- (void)mouseDragged:(NSEvent*)event
{
NSPoint dragNow = [event locationInWindow];
NSRect rc = [document frame];
double deltaX = dragStart.x - dragNow.x;
center.x = dragCenter.x + deltaX / rc.size.width * 1000;
double deltaY = dragStart.y - dragNow.y;
center.y = dragCenter.y + deltaY / rc.size.height * 1000;
if (center.y < 0) center.y = 0;
if (center.x < 0) center.x = 0;
if (center.x > 1000) center.x = 1000;
if (center.y > 1000) center.y = 1000;
[self update];
}
- (void)mouseUp:(NSEvent*)event
{
}
- (void)scrollWheel:(NSEvent*)event
{
NSTimeInterval diff = [lastScroll timeIntervalSinceNow];
if (diff < -0.3)
{
float delta = [event deviceDeltaY];
if (delta > 0)
[delegate imageClipPrevious:self];
else if (delta < 0)
[delegate imageClipNext:self];
self.lastScroll = [NSDate date];
}
}
- (BOOL)isOpaque
{
return YES;
}
@end