Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 149 lines (122 sloc) 4.451 kb
a516cc8 Initial check-in.
uli authored
1 //
2 // UKPaintLassoTool.m
3 // UKPaintView
4 //
5 // Created by Uli Kusterer on Fri Oct 31 2003.
6 // Copyright (c) 2003 M. Uli Kusterer. All rights reserved.
7 //
6a767a7 Uli Kusterer Big clean-up. Adopted new prefix. Fixed support for line and fill colors...
authored
8 // This software is provided 'as-is', without any express or implied
9 // warranty. In no event will the authors be held liable for any damages
10 // arising from the use of this software.
11 //
12 // Permission is granted to anyone to use this software for any purpose,
13 // including commercial applications, and to alter it and redistribute it
14 // freely, subject to the following restrictions:
15 //
16 // 1. The origin of this software must not be misrepresented; you must not
17 // claim that you wrote the original software. If you use this software
18 // in a product, an acknowledgment in the product documentation would be
19 // appreciated but is not required.
20 //
21 // 2. Altered source versions must be plainly marked as such, and must not be
22 // misrepresented as being the original software.
23 //
24 // 3. This notice may not be removed or altered from any source
25 // distribution.
26 //
a516cc8 Initial check-in.
uli authored
27
6a767a7 Uli Kusterer Big clean-up. Adopted new prefix. Fixed support for line and fill colors...
authored
28 #import "ULIPaintSelectionLassoTool.h"
29 #import "ULIPaintView.h"
a516cc8 Initial check-in.
uli authored
30 #import "NSCursor+CrossHair.h"
31
32
6a767a7 Uli Kusterer Big clean-up. Adopted new prefix. Fixed support for line and fill colors...
authored
33 @implementation ULIPaintSelectionLassoTool
a516cc8 Initial check-in.
uli authored
34
35 -(void) trackingWillStart
36 {
37 dragNotCreate = [[owner selectionPath] containsPoint: [owner initialPos]]; // Are we moving a selection or creating a new one?
38
39 // Remove old selection while user is drawing a new one with this tool:
40 // And don't show selection outline during drag for more exact positioning.
41 [owner setDrawSelectionHighlight: NO];
42 [owner setNeedsDisplay: YES];
43
44 if( !dragNotCreate )
45 [super trackingWillStart];
46 }
47
48 -(void) drawTemporaryTrackingToolShapeFrom: (NSPoint*)prevPos to: (NSPoint)currPos
49 {
50 if( dragNotCreate )
51 {
52 [owner moveSelectionByX: currPos.x -prevPos->x andY: currPos.y -prevPos->y];
53
54 *prevPos = currPos;
55 }
56 else
57 [super drawTemporaryTrackingToolShapeFrom: prevPos to: currPos];
58 }
59
60
61 -(void) drawFinalToolShapeFrom: (NSPoint*)prevPos to: (NSPoint)currPos
62 {
63 if( !dragNotCreate )
64 {
65 NSRect box = [currentPath bounds];
66 [currentPath closePath];
67
68 // Merge old selection into our image:
69 NSRect oldSelBounds = NSZeroRect, oldSelFrame;
70 NSImage* selection = [owner floatingSelectionImage];
71 oldSelBounds.size = [selection size];
72 oldSelFrame = [owner selectionFrame];
73
74 [selection drawInRect: oldSelFrame fromRect: oldSelBounds operation: NSCompositeSourceOver fraction: 1.0];
75
76 [selection lockFocus];
77 // Clear old selection:
78 [[NSColor colorWithCalibratedWhite: 1.0 alpha: 0.0] set];
79 [NSBezierPath setDefaultLineWidth: 1];
80 NSRectFill(oldSelBounds);
81 [selection unlockFocus];
82
83 if( box.size.width > 1 || box.size.height > 1 )
84 {
85 NSBezierPath* localPath = [[currentPath copy] autorelease];
86 NSAffineTransform* transform = [NSAffineTransform transform];
87 [transform translateXBy: -box.origin.x yBy: -box.origin.y];
88 [localPath transformUsingAffineTransform: transform];
89
90 [selection setSize: box.size];
91 [selection lockFocus];
92 // ... and copy the new one in:
93 NSRect boundsBox = box;
94 boundsBox.origin = NSZeroPoint;
95 [[NSGraphicsContext currentContext] setShouldAntialias: NO];
96 [localPath setClip]; // But only the selected area!
97 [[owner image] drawInRect: boundsBox fromRect: box operation: NSCompositeCopy fraction: 1.0];
98 [selection unlockFocus];
99 [owner setSelectionPath: currentPath];
100
101 // Erase where new selection used to be:
102 [[NSColor colorWithCalibratedWhite: 1.0 alpha: 0.0] set];
103 [currentPath setLineWidth: 0];
104 [[NSGraphicsContext currentContext] setShouldAntialias: NO];
105 [[NSGraphicsContext currentContext] setCompositingOperation: NSCompositeCopy];
106 [currentPath fill]; // But only the selected area!
107 }
108 else
109 {
110 [selection setSize: NSMakeSize(8,8)]; // Save us some memory.
111 box.size = NSZeroSize;
112 box.origin = [owner initialPos];
113 [owner setSelectionFrame: box];
114 }
115 }
116
117 [owner setDrawSelectionHighlight: YES]; // We turned it off, we turn it on again.
118 [owner setNeedsDisplay: YES];
119 [[owner window] invalidateCursorRectsForView: owner];
120
121 *prevPos = currPos;
122 }
123
124
125 // Return the name of an image to use as the icon for this tool in the tool palette.
126 -(NSString*) toolIconName
127 {
128 return @"UKPaintLassoTool";
129 }
130
131
132 -(BOOL) isSaveableTool
133 {
134 return NO;
135 }
136
137 -(NSCursor*) drawingCursor
138 {
139 return [NSCursor crossHairCursor];
140 }
141
142
143 -(NSCursor*) selectionCursor
144 {
145 return [NSCursor arrowCursor];
146 }
147
148 @end
Something went wrong with that request. Please try again.