Skip to content

Instantly share code, notes, and snippets.

@mike3k
Created May 27, 2011 19:56
Show Gist options
  • Save mike3k/996030 to your computer and use it in GitHub Desktop.
Save mike3k/996030 to your computer and use it in GitHub Desktop.
View that implements marching ants selection
@interface MarchingAntsView : NSView {
NSPoint clickLoc;
NSRect selection;
}
@end
@implementation MarchingAntsView
NSRect RectFromPoints(NSPoint pt1, NSPoint pt2)
{
float minX = MIN(pt1.x, pt2.x);
float minY = MIN(pt1.y, pt2.y);
float maxX = MAX(pt1.x, pt2.x);
float maxY = MAX(pt1.y, pt2.y);
float height = maxY - minY;
float width = maxX - minX;
return NSMakeRect(minX, minY, width, height);
}
- (void)mouseDown:(NSEvent *)theEvent
{
NSPoint eventLocation = [theEvent locationInWindow];
clickLoc = [self convertPoint:eventLocation fromView:nil];
selection = NSZeroRect;
}
- (void)mouseDragged:(NSEvent *)theEvent
{
NSPoint pt = [self convertPoint:[theEvent locationInWindow] fromView:nil];
NSRect oldSelection = selection;
selection = RectFromPoints(pt, clickLoc);
[self setNeedsDisplayInRect:NSInsetRect(NSUnionRect(selection, oldSelection), -1, -1)];
}
- (void)mouseUp:(NSEvent *)theEvent
{
Element *added = nil;
NSPoint pt = [self convertPoint:[theEvent locationInWindow] fromView:nil];
// do something here when the mouse is released
selection = NSZeroRect;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment