Skip to content

Instantly share code, notes, and snippets.

@klern
Created August 19, 2010 02:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klern/536831 to your computer and use it in GitHub Desktop.
Save klern/536831 to your computer and use it in GitHub Desktop.
#import "EvfCropView.h"
@implementation EvfCropView
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
}
downPoint_ = NSZeroPoint;
lastRect_.size.width = 0;
lastRect_.size.height = 0;
lastRect_.origin.x = 0;
lastRect_.origin.y = 0;
return self;
}
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
NSRect CropImageRect = {0};
if (CropImageRect.origin.x == 0) {
CropImageRect.origin.x = dirtyRect.size.width / 5 * 2;
CropImageRect.origin.y = dirtyRect.size.width / 5;
CropImageRect.size.width = dirtyRect.size.width / 5;
CropImageRect.size.height = dirtyRect.size.height / 5;
if (lastRect_.size.width != 0) {
CropImageRect.size.width = lastRect_.size.width;
CropImageRect.size.height = lastRect_.size.height;
CropImageRect.origin.x = lastRect_.origin.x;
CropImageRect.origin.y = lastRect_.origin.y;
}
}
if (currentPoint_.x == 0) {
lastRect_.size.width = CropImageRect.size.width;
lastRect_.size.height = CropImageRect.size.height;
lastRect_.origin.x = CropImageRect.origin.x;
lastRect_.origin.y = CropImageRect.origin.y;
}
// Point In Rect
else if (NSPointInRect(downPoint_, CropImageRect) == YES) {
CropImageRect.size.width = lastRect_.size.width;
CropImageRect.size.height = lastRect_.size.height;
CropImageRect.origin.x = lastRect_.origin.x + (currentPoint_.x - downPoint_.x);
CropImageRect.origin.y = lastRect_.origin.y + (currentPoint_.y - downPoint_.y);
if (mouseDownUp_ == 1) {
lastRect_.origin.x = CropImageRect.origin.x;
lastRect_.origin.y = CropImageRect.origin.y;
lastRect_.size.width = CropImageRect.size.width;
lastRect_.size.height = CropImageRect.size.height;
}
}
// Point Not In Rect
else if (NSPointInRect(downPoint_, CropImageRect) == NO) {
CropImageRect.size.width = lastRect_.size.width;
CropImageRect.size.height = lastRect_.size.height;
CropImageRect.origin.x = lastRect_.origin.x;
CropImageRect.origin.y = lastRect_.origin.y;
// Adjust Crop
for (knobCount = 1; knobCount < 9;knobCount++){
knobHandle_ = [[[KnobPointHandle alloc] init] autorelease];
knobRect = [knobHandle_ knobGetOrigin:CropImageRect knobNum:knobCount];
CropImageRect = [knobHandle_ adjustedCrop:CropImageRect oldRect:lastRect_ downPoint:downPoint_ movePoint:currentPoint_];
if (mouseDownUp_ == 1)
lastRect_ = CropImageRect;
}
}
// Draw CropRect
[NSBezierPath setDefaultLineWidth:3];
[[NSColor colorWithCalibratedRed:0.0/255.0 green:0.0/255.0 blue: 0.0/255.0 alpha:0.5] setFill];
[NSBezierPath fillRect:CropImageRect];
[[NSColor colorWithCalibratedRed:0.0/255.0 green:0.0/255.0 blue: 0.0/255.0 alpha:1.0] set];
[NSBezierPath strokeRect:CropImageRect];
[[NSGraphicsContext currentContext] setCompositingOperation: NSCompositeSourceOver];
}
- (void)mouseDown:(NSEvent *)theEvent
{
mouseDownUp_ = 0;
NSPoint mousePoint = [theEvent locationInWindow];
downPoint_ = [self convertPoint:mousePoint fromView:nil];
currentPoint_ = downPoint_;
[self setNeedsDisplay:YES];
}
- (void)mouseDragged:(NSEvent *)theEvent
{
NSPoint mousePoint = [theEvent locationInWindow];
currentPoint_ = [self convertPoint:mousePoint fromView:nil];
[self setNeedsDisplay:YES];
}
- (void)mouseUp:(NSEvent *)theEvent
{
mouseDownUp_ = 1;
NSPoint mousePoint = [theEvent locationInWindow];
currentPoint_ = [self convertPoint:mousePoint fromView:nil];
[self setNeedsDisplay:YES];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment