1 | /****************************************************************************** |
---|
2 | * $Id: PiecesView.m 6100 2008-06-09 22:45:31Z livings124 $ |
---|
3 | * |
---|
4 | * Copyright (c) 2006-2008 Transmission authors and contributors |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
7 | * copy of this software and associated documentation files (the "Software"), |
---|
8 | * to deal in the Software without restriction, including without limitation |
---|
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
10 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
11 | * Software is furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
22 | * DEALINGS IN THE SOFTWARE. |
---|
23 | *****************************************************************************/ |
---|
24 | |
---|
25 | #import "PiecesView.h" |
---|
26 | #import "InfoWindowController.h" |
---|
27 | #import "CTGradient.h" |
---|
28 | |
---|
29 | #define MAX_ACROSS 18 |
---|
30 | #define BETWEEN 1.0 |
---|
31 | |
---|
32 | @implementation PiecesView |
---|
33 | |
---|
34 | - (void) awakeFromNib |
---|
35 | { |
---|
36 | //back image |
---|
37 | fBack = [[NSImage alloc] initWithSize: [self bounds].size]; |
---|
38 | |
---|
39 | [fBack lockFocus]; |
---|
40 | CTGradient * gradient = [CTGradient gradientWithBeginningColor: [NSColor colorWithCalibratedWhite: 0.0 alpha: 0.4] |
---|
41 | endingColor: [NSColor colorWithCalibratedWhite: 0.2 alpha: 0.4]]; |
---|
42 | [gradient fillRect: [self bounds] angle: 90.0]; |
---|
43 | [fBack unlockFocus]; |
---|
44 | |
---|
45 | //store box colors |
---|
46 | fWhiteColor = [[NSColor whiteColor] retain]; |
---|
47 | fOrangeColor = [[NSColor orangeColor] retain]; |
---|
48 | fGreen1Color = [[NSColor colorWithCalibratedRed: 0.6 green: 1.0 blue: 0.8 alpha: 1.0] retain]; |
---|
49 | fGreen2Color = [[NSColor colorWithCalibratedRed: 0.4 green: 1.0 blue: 0.6 alpha: 1.0] retain]; |
---|
50 | fGreen3Color = [[NSColor colorWithCalibratedRed: 0.0 green: 1.0 blue: 0.4 alpha: 1.0] retain]; |
---|
51 | fBlue1Color = [[NSColor colorWithCalibratedRed: 0.8 green: 1.0 blue: 1.0 alpha: 1.0] retain]; |
---|
52 | fBlue2Color = [[NSColor colorWithCalibratedRed: 0.6 green: 1.0 blue: 1.0 alpha: 1.0] retain]; |
---|
53 | fBlue3Color = [[NSColor colorWithCalibratedRed: 0.6 green: 0.8 blue: 1.0 alpha: 1.0] retain]; |
---|
54 | fBlue4Color = [[NSColor colorWithCalibratedRed: 0.4 green: 0.6 blue: 1.0 alpha: 1.0] retain]; |
---|
55 | fBlueColor = [[NSColor colorWithCalibratedRed: 0.0 green: 0.4 blue: 0.8 alpha: 1.0] retain]; |
---|
56 | |
---|
57 | //actually draw the box |
---|
58 | [self setTorrent: nil]; |
---|
59 | } |
---|
60 | |
---|
61 | - (void) dealloc |
---|
62 | { |
---|
63 | tr_free(fPieces); |
---|
64 | |
---|
65 | [fBack release]; |
---|
66 | |
---|
67 | [fWhiteColor release]; |
---|
68 | [fOrangeColor release]; |
---|
69 | [fGreen1Color release]; |
---|
70 | [fGreen2Color release]; |
---|
71 | [fGreen3Color release]; |
---|
72 | [fBlue1Color release]; |
---|
73 | [fBlue2Color release]; |
---|
74 | [fBlue3Color release]; |
---|
75 | [fBlue4Color release]; |
---|
76 | [fBlueColor release]; |
---|
77 | |
---|
78 | [super dealloc]; |
---|
79 | } |
---|
80 | |
---|
81 | - (void) setTorrent: (Torrent *) torrent |
---|
82 | { |
---|
83 | //reset the view to blank |
---|
84 | NSImage * newBack = [fBack copy]; |
---|
85 | [self setImage: newBack]; |
---|
86 | [newBack release]; |
---|
87 | |
---|
88 | tr_free(fPieces); |
---|
89 | fPieces = NULL; |
---|
90 | |
---|
91 | fTorrent = torrent; |
---|
92 | if (fTorrent) |
---|
93 | { |
---|
94 | //determine relevant values |
---|
95 | fNumPieces = MIN([fTorrent pieceCount], MAX_ACROSS * MAX_ACROSS); |
---|
96 | fAcross = ceil(sqrt(fNumPieces)); |
---|
97 | |
---|
98 | float width = [self bounds].size.width; |
---|
99 | fWidth = (width - (fAcross + 1) * BETWEEN) / fAcross; |
---|
100 | fExtraBorder = (width - ((fWidth + BETWEEN) * fAcross + BETWEEN)) / 2; |
---|
101 | |
---|
102 | [self updateView]; |
---|
103 | } |
---|
104 | else |
---|
105 | [self setNeedsDisplay]; |
---|
106 | } |
---|
107 | |
---|
108 | - (void) resetView |
---|
109 | { |
---|
110 | tr_free(fPieces); |
---|
111 | fPieces = NULL; |
---|
112 | |
---|
113 | [self updateView]; |
---|
114 | } |
---|
115 | |
---|
116 | - (void) updateView |
---|
117 | { |
---|
118 | if (!fTorrent) |
---|
119 | return; |
---|
120 | |
---|
121 | //determine if first time |
---|
122 | BOOL first = NO; |
---|
123 | if (!fPieces) |
---|
124 | { |
---|
125 | fPieces = (int8_t *)tr_malloc(fNumPieces * sizeof(int8_t)); |
---|
126 | first = YES; |
---|
127 | } |
---|
128 | |
---|
129 | NSImage * image = [self image]; |
---|
130 | |
---|
131 | int8_t * pieces = NULL; |
---|
132 | float * piecesPercent = NULL; |
---|
133 | |
---|
134 | BOOL showAvailablity = [[NSUserDefaults standardUserDefaults] boolForKey: @"PiecesViewShowAvailability"]; |
---|
135 | if (showAvailablity) |
---|
136 | { |
---|
137 | pieces = (int8_t *)tr_malloc(fNumPieces * sizeof(int8_t)); |
---|
138 | [fTorrent getAvailability: pieces size: fNumPieces]; |
---|
139 | } |
---|
140 | else |
---|
141 | { |
---|
142 | piecesPercent = (float *)tr_malloc(fNumPieces * sizeof(float)); |
---|
143 | [fTorrent getAmountFinished: piecesPercent size: fNumPieces]; |
---|
144 | } |
---|
145 | |
---|
146 | int i, j, index = -1; |
---|
147 | NSRect rect = NSMakeRect(0, 0, fWidth, fWidth); |
---|
148 | |
---|
149 | BOOL change = NO; |
---|
150 | |
---|
151 | for (i = 0; i < fAcross; i++) |
---|
152 | for (j = 0; j < fAcross; j++) |
---|
153 | { |
---|
154 | index++; |
---|
155 | if (index >= fNumPieces) |
---|
156 | { |
---|
157 | i = fAcross; |
---|
158 | break; |
---|
159 | } |
---|
160 | |
---|
161 | NSColor * pieceColor = nil; |
---|
162 | |
---|
163 | if (showAvailablity) |
---|
164 | { |
---|
165 | int piece = pieces[index]; |
---|
166 | if (piece == -1) |
---|
167 | { |
---|
168 | if (first || fPieces[index] == -2) |
---|
169 | { |
---|
170 | fPieces[index] = -1; |
---|
171 | pieceColor = fBlueColor; |
---|
172 | } |
---|
173 | else if (fPieces[index] != -1) |
---|
174 | { |
---|
175 | fPieces[index] = -2; |
---|
176 | pieceColor = fOrangeColor; |
---|
177 | } |
---|
178 | else; |
---|
179 | } |
---|
180 | else if (piece == 0) |
---|
181 | { |
---|
182 | if (first || fPieces[index] != 0) |
---|
183 | { |
---|
184 | fPieces[index] = 0; |
---|
185 | pieceColor = fWhiteColor; |
---|
186 | } |
---|
187 | } |
---|
188 | else if (piece <= 4) |
---|
189 | { |
---|
190 | if (first || fPieces[index] != 1) |
---|
191 | { |
---|
192 | fPieces[index] = 1; |
---|
193 | pieceColor = fGreen1Color; |
---|
194 | } |
---|
195 | } |
---|
196 | else if (piece <= 8) |
---|
197 | { |
---|
198 | if (first || fPieces[index] != 2) |
---|
199 | { |
---|
200 | fPieces[index] = 2; |
---|
201 | pieceColor = fGreen2Color; |
---|
202 | } |
---|
203 | } |
---|
204 | else |
---|
205 | { |
---|
206 | if (first || fPieces[index] != 3) |
---|
207 | { |
---|
208 | fPieces[index] = 3; |
---|
209 | pieceColor = fGreen3Color; |
---|
210 | } |
---|
211 | } |
---|
212 | } |
---|
213 | else |
---|
214 | { |
---|
215 | float piecePercent = piecesPercent[index]; |
---|
216 | if (piecePercent >= 1.0) |
---|
217 | { |
---|
218 | if (first || fPieces[index] == -2) |
---|
219 | { |
---|
220 | fPieces[index] = -1; |
---|
221 | pieceColor = fBlueColor; |
---|
222 | } |
---|
223 | else if (fPieces[index] != -1) |
---|
224 | { |
---|
225 | fPieces[index] = -2; |
---|
226 | pieceColor = fOrangeColor; |
---|
227 | } |
---|
228 | else; |
---|
229 | } |
---|
230 | else if (piecePercent == 0.0) |
---|
231 | { |
---|
232 | if (first || fPieces[index] != 0) |
---|
233 | { |
---|
234 | fPieces[index] = 0; |
---|
235 | pieceColor = fWhiteColor; |
---|
236 | } |
---|
237 | } |
---|
238 | else if (piecePercent < 0.25) |
---|
239 | { |
---|
240 | if (first || fPieces[index] != 1) |
---|
241 | { |
---|
242 | fPieces[index] = 1; |
---|
243 | pieceColor = fBlue1Color; |
---|
244 | } |
---|
245 | } |
---|
246 | else if (piecePercent < 0.5) |
---|
247 | { |
---|
248 | if (first || fPieces[index] != 2) |
---|
249 | { |
---|
250 | fPieces[index] = 2; |
---|
251 | pieceColor = fBlue2Color; |
---|
252 | } |
---|
253 | } |
---|
254 | else if (piecePercent < 0.75) |
---|
255 | { |
---|
256 | if (first || fPieces[index] != 3) |
---|
257 | { |
---|
258 | fPieces[index] = 3; |
---|
259 | pieceColor = fBlue3Color; |
---|
260 | } |
---|
261 | } |
---|
262 | else |
---|
263 | { |
---|
264 | if (first || fPieces[index] != 4) |
---|
265 | { |
---|
266 | fPieces[index] = 4; |
---|
267 | pieceColor = fBlue4Color; |
---|
268 | } |
---|
269 | } |
---|
270 | } |
---|
271 | |
---|
272 | if (pieceColor) |
---|
273 | { |
---|
274 | //drawing actually will occur |
---|
275 | if (!change) |
---|
276 | { |
---|
277 | [image lockFocus]; |
---|
278 | change = YES; |
---|
279 | } |
---|
280 | |
---|
281 | rect.origin = NSMakePoint(j * (fWidth + BETWEEN) + BETWEEN + fExtraBorder, |
---|
282 | [image size].width - (i + 1) * (fWidth + BETWEEN) - fExtraBorder); |
---|
283 | |
---|
284 | [pieceColor set]; |
---|
285 | NSRectFill(rect); |
---|
286 | } |
---|
287 | } |
---|
288 | |
---|
289 | if (change) |
---|
290 | { |
---|
291 | [image unlockFocus]; |
---|
292 | [self setNeedsDisplay]; |
---|
293 | } |
---|
294 | |
---|
295 | tr_free(pieces); |
---|
296 | tr_free(piecesPercent); |
---|
297 | } |
---|
298 | |
---|
299 | - (BOOL) acceptsFirstMouse: (NSEvent *) event |
---|
300 | { |
---|
301 | return YES; |
---|
302 | } |
---|
303 | |
---|
304 | - (void) mouseDown: (NSEvent *) event |
---|
305 | { |
---|
306 | if (fTorrent) |
---|
307 | [[[self window] windowController] setPiecesViewForAvailable: |
---|
308 | ![[NSUserDefaults standardUserDefaults] boolForKey: @"PiecesViewShowAvailability"]]; |
---|
309 | [super mouseDown: event]; |
---|
310 | } |
---|
311 | |
---|
312 | @end |
---|