forked from fredrikw/Snac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SelectMarker.m
135 lines (110 loc) · 3.54 KB
/
SelectMarker.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
//
// SelectMarker.m
// selectInPic
//
// Created by Fredrik Wallner on 2006-08-01.
// Copyright 2006 Fredrik Wallner. All rights reserved.
//
#import "SelectMarker.h"
#define WHERE [target convertPoint:[theEvent locationInWindow] fromView:nil]
@implementation SelectMarker
+ selectMarkerForView:aView { return [[[self alloc] initWithView:aView] autorelease]; }
- initWithView:(NSView *) aView
{
if (self = [super init])
{
target = aView;
[self setColor:[NSColor blueColor]];
selectedPath = [[NSBezierPath bezierPath] retain];
[self setSelectedRect:NSZeroRect];
}
return self;
}
- (void) setColor:(NSColor *) aColor
{
[self setStrokeColor:aColor];
[self setFillColor:[strokeColor colorWithAlphaComponent:0.2]]; //This really shouldn't be hard-coded...
}
- (void) drawSelectMarker { [strokeColor set]; NSFrameRect(selectedRect); }
- (void) startMovingAtPoint:(NSPoint) where { trackingMode = trackMoving; lastLocation = where; }
- (void) startSelectingAtPoint:(NSPoint) where { trackingMode = trackSelecting; lastLocation = where; }
- (void) continueMovingAtPoint:(NSPoint) where
{
selectedRect.origin.x += where.x - lastLocation.x;
selectedRect.origin.y += where.y - lastLocation.y;
lastLocation = where;
}
- (void) stopMovingAtPoint:(NSPoint) where
{
[self continueMovingAtPoint:where];
trackingMode = trackNone;
}
- (void) continueSelectingAtPoint:(NSPoint) where { selectedRect = rectFromPoints(lastLocation,where); }
- (void) stopSelectingAtPoint:(NSPoint) where
{
selectedRect = rectFromPoints(lastLocation,where);
trackingMode = trackNone;
}
// CropMarker isn't an NSResponder subclass, but it still cares about mouse events.
- (void) mouseDown:(NSEvent *) theEvent
{
lastLocation = WHERE;
if (NSPointInRect(lastLocation, selectedRect))
{
[self startMovingAtPoint:lastLocation];
return;
}
[self startSelectingAtPoint:lastLocation];
}
- (void) mouseUp:(NSEvent *) theEvent
{
switch (trackingMode)
{
case trackSelecting:
[self stopSelectingAtPoint:WHERE];
break;
case trackMoving:
[self stopMovingAtPoint:WHERE];
break;
default:
NSLog (@"Bad tracking mode in [CropMarker mouseUp]");
}
}
- (void) mouseDragged:(NSEvent *) theEvent
{
switch (trackingMode)
{
case trackSelecting:
[self continueSelectingAtPoint:WHERE];
break;
case trackMoving:
[self continueMovingAtPoint:WHERE];
break;
default:
NSLog (@"Bad tracking mode in [CropMarker mouseDragged]");
}
}
- (void) dealloc
{
if (fillColor) [fillColor release];
if (strokeColor) [strokeColor release];
[super dealloc];
}
// Accessors and other one-liners.
- (void) setFillColor:(NSColor *) color { [color retain]; [fillColor release]; fillColor = color; }
- (void) setStrokeColor:(NSColor *) color { [color retain]; [strokeColor release]; strokeColor = color; }
- (NSBezierPath *) selectedPath { return [NSBezierPath bezierPathWithRect:selectedRect];}
- (NSRect) selectedRect { return selectedRect; }
- (void) setSelectedRect:(NSRect) rect { selectedRect = rect;}
- (void) setSelectedRectOrigin:(NSPoint) where { selectedRect.origin = where;}
- (void) setSelectedRectSize:(NSSize) size { selectedRect.size = size;}
- (void) moveSelectedRectBy:(NSSize) delta { selectedRect.origin.x += delta.width; selectedRect.origin.y += delta.height;}
@end
NSRect rectFromPoints(NSPoint p1, NSPoint p2) // Given two corners, make an NSRect.
{
return
NSMakeRect( MIN(p1.x, p2.x),
MIN(p1.y, p2.y),
fabs(p1.x - p2.x),
fabs(p1.y - p2.y));
}