-
Notifications
You must be signed in to change notification settings - Fork 43
/
ECVCropCell.m
executable file
·201 lines (172 loc) · 7.98 KB
/
ECVCropCell.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
/* Copyright (c) 2009, Ben Trask
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY BEN TRASK ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL BEN TRASK BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
#import "ECVCropCell.h"
// Other Sources
#import "ECVAppKitAdditions.h"
#import "ECVDebug.h"
#import "ECVOpenGLAdditions.h"
#define ECVHandleSize 10
#define ECVMinimumCropSize 0.05f
static ECVRectEdgeMask const ECVHandlePositions[] = {
ECVMinYMask | ECVMinXMask,
ECVMinYMask | ECVRectMidX,
ECVMinYMask | ECVMaxXMask,
ECVRectMidY | ECVMinXMask,
ECVRectMidY | ECVMaxXMask,
ECVMaxYMask | ECVMinXMask,
ECVMaxYMask | ECVRectMidX,
ECVMaxYMask | ECVMaxXMask,
};
@implementation ECVCropCell
#pragma mark +NSCell
+ (BOOL)prefersTrackingUntilMouseUp
{
return YES;
}
#pragma mark -ECVCropCell
- (id)initWithOpenGLContext:(NSOpenGLContext *)context
{
if((self = [super init])) {
_cropRect = ECVUncroppedRect;
_handleRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL pixelsWide:ECVHandleSize pixelsHigh:ECVHandleSize bitsPerSample:8 samplesPerPixel:4 hasAlpha:YES isPlanar:NO colorSpaceName:NSCalibratedRGBColorSpace bytesPerRow:ECVHandleSize * 4 bitsPerPixel:0];
NSGraphicsContext *const graphicsContext = [NSGraphicsContext graphicsContextWithBitmapImageRep:_handleRep];
[NSGraphicsContext setCurrentContext:graphicsContext];
NSRect const r = NSInsetRect(NSMakeRect(0.0f, 0.0f, ECVHandleSize, ECVHandleSize), 1.0f, 1.0f);
NSBezierPath *const p = [NSBezierPath bezierPathWithOvalInRect:r];
[p setLineWidth:2.0f];
[p ECV_fillWithGradientFromColor:[NSColor colorWithCalibratedWhite:0.7f alpha:1.0f] atPoint:ECVRectPoint(r, ECVMinXMaxYCorner) toColor:[NSColor colorWithCalibratedWhite:0.2f alpha:1.0f] atPoint:ECVRectPoint(r, ECVMinXMinYCorner)];
[[NSColor whiteColor] set];
[p stroke];
[graphicsContext flushGraphics];
CGLContextObj const contextObj = ECVLockContext(context);
_handleTextureName = [_handleRep ECV_textureName];
ECVUnlockContext(contextObj);
}
return self;
}
@synthesize delegate;
- (NSRect)cropRect
{
return _cropRect;
}
- (void)setCropRect:(NSRect)aRect
{
_cropRect = aRect;
_tempCropRect = aRect;
}
#pragma mark -
- (NSRect)maskRectWithCropRect:(NSRect)crop frame:(NSRect)frame
{
NSRect r = NSOffsetRect(ECVScaledRect(crop, frame.size), NSMinX(frame), NSMinY(frame));
r.origin.x = round(NSMinX(r));
r.origin.y = round(NSMinY(r));
r.size.width = round(NSWidth(r));
r.size.height = round(NSHeight(r));
return r;
}
- (NSRect)frameForHandlePosition:(ECVRectEdgeMask)pos maskRect:(NSRect)mask inFrame:(NSRect)frame
{
NSPoint const c = ECVRectPoint(NSIntersectionRect(frame, NSInsetRect(mask, ECVHandleSize / -2.0f, ECVHandleSize / -2.0f)), pos);
NSPoint const p = ECVRectPoint(NSMakeRect(0.0f, 0.0f, ECVHandleSize, ECVHandleSize), pos);
return NSMakeRect(round(c.x - p.x), round(c.y - p.y), ECVHandleSize, ECVHandleSize);
}
- (ECVRectEdgeMask)handlePositionForPoint:(NSPoint)point withMaskRect:(NSRect)mask inFrame:(NSRect)frame view:(NSView *)aView
{
NSUInteger i = numberof(ECVHandlePositions);
while(i--) if([aView mouse:point inRect:[self frameForHandlePosition:ECVHandlePositions[i] maskRect:mask inFrame:frame]]) return ECVHandlePositions[i];
return ECVRectCenter;
}
- (NSCursor *)cursorForHandlePosition:(ECVRectEdgeMask)pos
{
switch(pos) {
case ECVMinXMask:
case ECVMaxXMask:
return [NSCursor resizeLeftRightCursor];
case ECVMinYMask:
case ECVMaxYMask:
return [NSCursor resizeUpDownCursor];
case ECVMinXMinYCorner:
case ECVMaxXMaxYCorner:
return [[[NSCursor alloc] initWithImage:[NSImage imageNamed:@"Cursor-Resize-135"] hotSpot:NSMakePoint(8.0f, 8.0f)] autorelease];
case ECVMinXMaxYCorner:
case ECVMaxXMinYCorner:
return [[[NSCursor alloc] initWithImage:[NSImage imageNamed:@"Cursor-Resize-45"] hotSpot:NSMakePoint(8.0f, 8.0f)] autorelease];
default:
return [NSCursor arrowCursor];
}
}
#pragma mark -NSCell
- (BOOL)trackMouse:(NSEvent *)firstEvent inRect:(NSRect)aRect ofView:(NSView *)aView untilMouseUp:(BOOL)flag
{
NSPoint const firstLocation = [aView convertPoint:[firstEvent locationInWindow] fromView:nil];
NSRect const firstMaskRect = [self maskRectWithCropRect:_cropRect frame:aRect];
ECVRectEdgeMask const handle = [self handlePositionForPoint:firstLocation withMaskRect:firstMaskRect inFrame:aRect view:aView];
if(!handle) {
[[self delegate] cropCellDidFinishCropping:self];
return YES; // Claim the mouse is up.
}
NSPoint const handlePoint = ECVRectPoint(firstMaskRect, handle);
NSSize const handleOffset = NSMakeSize(handlePoint.x - firstLocation.x, handlePoint.y - firstLocation.y);
[[aView window] disableCursorRects];
NSEvent *latestEvent = nil;
while((latestEvent = [[aView window] nextEventMatchingMask:NSLeftMouseUpMask | NSLeftMouseDraggedMask untilDate:[NSDate distantFuture] inMode:NSEventTrackingRunLoopMode dequeue:YES]) && [latestEvent type] != NSLeftMouseUp) {
NSPoint const latestLocation = [aView convertPoint:[latestEvent locationInWindow] fromView:nil];
NSRect const maskRect = [self maskRectWithCropRect:_cropRect frame:aRect];
NSRect const r = ECVRectByScalingEdgeToPoint(maskRect, handle, NSMakePoint(latestLocation.x + handleOffset.width, latestLocation.y + handleOffset.height), NSMakeSize(ECVHandleSize * 2.0f, ECVHandleSize * 2.0f), aRect);
_tempCropRect = ECVScaledRect(NSOffsetRect(r, -NSMinX(aRect), -NSMinY(aRect)), NSMakeSize(1.0f / NSWidth(aRect), 1.0f / NSHeight(aRect)));
[aView setNeedsDisplay:YES];
}
[[aView window] discardEventsMatchingMask:NSAnyEventMask beforeEvent:latestEvent];
[[aView window] invalidateCursorRectsForView:aView];
[[aView window] enableCursorRects];
_cropRect = _tempCropRect;
return YES;
}
- (void)resetCursorRect:(NSRect)cellFrame inView:(NSView *)controlView
{
NSUInteger i = 0;
NSRect const maskRect = [self maskRectWithCropRect:_tempCropRect frame:cellFrame];
for(; i < numberof(ECVHandlePositions); i++) [controlView addCursorRect:[self frameForHandlePosition:ECVHandlePositions[i] maskRect:maskRect inFrame:cellFrame] cursor:[self cursorForHandlePosition:ECVHandlePositions[i]]];
}
#pragma mark -NSObject
- (void)dealloc
{
ECVGLError(glDeleteTextures(1, &_handleTextureName));
[_handleRep release];
[super dealloc];
}
#pragma mark -<ECVVideoViewCell>
- (void)drawWithFrame:(NSRect)aRect inVideoView:(ECVVideoView *)view playing:(BOOL)flag
{
NSRect const maskRect = [self maskRectWithCropRect:_tempCropRect frame:aRect];
glColor4f(0.0f, 0.0f, 0.0f, 0.5f);
ECVGLDrawBorder(maskRect, aRect);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
ECVGLDrawBorder(maskRect, NSInsetRect(maskRect, -1.0f, -1.0f));
ECVGLError(glEnable(GL_TEXTURE_RECTANGLE_EXT));
ECVGLError(glBindTexture(GL_TEXTURE_RECTANGLE_EXT, _handleTextureName));
NSUInteger i = 0;
for(; i < numberof(ECVHandlePositions); i++) ECVGLDrawTextureInRect([self frameForHandlePosition:ECVHandlePositions[i] maskRect:maskRect inFrame:aRect]);
ECVGLError(glDisable(GL_TEXTURE_RECTANGLE_EXT));
}
@end
@implementation NSObject(ECVCropCellDelegate)
- (void)cropCellDidFinishCropping:(ECVCropCell *)sender {}
@end