1 | /****************************************************************************** |
---|
2 | * $Id: PiecesView.m 3383 2007-10-12 21:36:01Z livings124 $ |
---|
3 | * |
---|
4 | * Copyright (c) 2006-2007 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 | |
---|
28 | #define MAX_ACROSS 18 |
---|
29 | #define BETWEEN 1.0 |
---|
30 | |
---|
31 | @implementation PiecesView |
---|
32 | |
---|
33 | - (id) initWithCoder: (NSCoder *) decoder |
---|
34 | { |
---|
35 | if ((self = [super initWithCoder: decoder])) |
---|
36 | { |
---|
37 | fTorrent = nil; |
---|
38 | fPieces = malloc(MAX_ACROSS * MAX_ACROSS); |
---|
39 | } |
---|
40 | |
---|
41 | return self; |
---|
42 | } |
---|
43 | |
---|
44 | - (void) awakeFromNib |
---|
45 | { |
---|
46 | NSBezierPath * bp = [NSBezierPath bezierPathWithRect: [self bounds]]; |
---|
47 | |
---|
48 | //back image |
---|
49 | fBack = [[NSImage alloc] initWithSize: [self bounds].size]; |
---|
50 | |
---|
51 | [fBack lockFocus]; |
---|
52 | [[NSColor colorWithCalibratedWhite: 0.0 alpha: 0.4] set]; |
---|
53 | [bp fill]; |
---|
54 | [fBack unlockFocus]; |
---|
55 | |
---|
56 | //store box colors |
---|
57 | fWhiteColor = [[NSColor whiteColor] retain]; |
---|
58 | fOrangeColor = [[NSColor orangeColor] retain]; |
---|
59 | fGreen1Color = [[NSColor colorWithCalibratedRed: 0.6 green: 1.0 blue: 0.8 alpha: 1.0] retain]; |
---|
60 | fGreen2Color = [[NSColor colorWithCalibratedRed: 0.4 green: 1.0 blue: 0.6 alpha: 1.0] retain]; |
---|
61 | fGreen3Color = [[NSColor colorWithCalibratedRed: 0.0 green: 1.0 blue: 0.4 alpha: 1.0] retain]; |
---|
62 | fBlue1Color = [[NSColor colorWithCalibratedRed: 0.8 green: 1.0 blue: 1.0 alpha: 1.0] retain]; |
---|
63 | fBlue2Color = [[NSColor colorWithCalibratedRed: 0.6 green: 1.0 blue: 1.0 alpha: 1.0] retain]; |
---|
64 | fBlue3Color = [[NSColor colorWithCalibratedRed: 0.6 green: 0.8 blue: 1.0 alpha: 1.0] retain]; |
---|
65 | fBlue4Color = [[NSColor colorWithCalibratedRed: 0.4 green: 0.6 blue: 1.0 alpha: 1.0] retain]; |
---|
66 | fBlueColor = [[NSColor colorWithCalibratedRed: 0.0 green: 0.4 blue: 0.8 alpha: 1.0] retain]; |
---|
67 | |
---|
68 | //actually draw the box |
---|
69 | [self setTorrent: nil]; |
---|
70 | } |
---|
71 | |
---|
72 | - (void) dealloc |
---|
73 | { |
---|
74 | free(fPieces); |
---|
75 | |
---|
76 | [fBack release]; |
---|
77 | |
---|
78 | [fWhiteColor release]; |
---|
79 | [fOrangeColor release]; |
---|
80 | [fGreen1Color release]; |
---|
81 | [fGreen2Color release]; |
---|
82 | [fGreen3Color release]; |
---|
83 | [fBlue1Color release]; |
---|
84 | [fBlue2Color release]; |
---|
85 | [fBlue3Color release]; |
---|
86 | [fBlue4Color release]; |
---|
87 | [fBlueColor release]; |
---|
88 | |
---|
89 | [fTorrent release]; |
---|
90 | [super dealloc]; |
---|
91 | } |
---|
92 | |
---|
93 | - (void) setTorrent: (Torrent *) torrent |
---|
94 | { |
---|
95 | [fTorrent release]; |
---|
96 | |
---|
97 | if (torrent) |
---|
98 | { |
---|
99 | fTorrent = [torrent retain]; |
---|
100 | |
---|
101 | //determine relevant values |
---|
102 | fNumPieces = MAX_ACROSS * MAX_ACROSS; |
---|
103 | int pieceCount = [fTorrent pieceCount]; |
---|
104 | if (pieceCount < fNumPieces) |
---|
105 | { |
---|
106 | fNumPieces = pieceCount; |
---|
107 | |
---|
108 | fAcross = sqrt(fNumPieces); |
---|
109 | if (fAcross * fAcross < fNumPieces) |
---|
110 | fAcross++; |
---|
111 | } |
---|
112 | else |
---|
113 | fAcross = MAX_ACROSS; |
---|
114 | |
---|
115 | float width = [self bounds].size.width; |
---|
116 | fWidth = (width - (fAcross + 1) * BETWEEN) / fAcross; |
---|
117 | fExtraBorder = (width - ((fWidth + BETWEEN) * fAcross + BETWEEN)) / 2; |
---|
118 | |
---|
119 | [self updateView: YES]; |
---|
120 | } |
---|
121 | else |
---|
122 | { |
---|
123 | fTorrent = nil; |
---|
124 | |
---|
125 | NSImage * newBack = [fBack copy]; |
---|
126 | [self setImage: newBack]; |
---|
127 | [newBack release]; |
---|
128 | |
---|
129 | [self setNeedsDisplay]; |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | - (void) updateView: (BOOL) first |
---|
134 | { |
---|
135 | if (!fTorrent) |
---|
136 | return; |
---|
137 | |
---|
138 | if (first) |
---|
139 | { |
---|
140 | NSImage * newBack = [fBack copy]; |
---|
141 | [self setImage: newBack]; |
---|
142 | [newBack release]; |
---|
143 | } |
---|
144 | NSImage * image = [self image]; |
---|
145 | |
---|
146 | int8_t * pieces; |
---|
147 | float * piecesPercent; |
---|
148 | |
---|
149 | BOOL showAvailablity = [[NSUserDefaults standardUserDefaults] boolForKey: @"PiecesViewShowAvailability"]; |
---|
150 | if (showAvailablity) |
---|
151 | { |
---|
152 | pieces = malloc(fNumPieces); |
---|
153 | [fTorrent getAvailability: pieces size: fNumPieces]; |
---|
154 | } |
---|
155 | else |
---|
156 | { |
---|
157 | piecesPercent = malloc(fNumPieces * sizeof(float)); |
---|
158 | [fTorrent getAmountFinished: piecesPercent size: fNumPieces]; |
---|
159 | } |
---|
160 | |
---|
161 | int i, j, piece, index = -1; |
---|
162 | float piecePercent; |
---|
163 | NSRect rect = NSMakeRect(0, 0, fWidth, fWidth); |
---|
164 | NSColor * pieceColor; |
---|
165 | BOOL change = NO; |
---|
166 | |
---|
167 | for (i = 0; i < fAcross; i++) |
---|
168 | for (j = 0; j < fAcross; j++) |
---|
169 | { |
---|
170 | index++; |
---|
171 | if (index >= fNumPieces) |
---|
172 | { |
---|
173 | i = fAcross; |
---|
174 | break; |
---|
175 | } |
---|
176 | |
---|
177 | pieceColor = nil; |
---|
178 | |
---|
179 | if (showAvailablity) |
---|
180 | { |
---|
181 | piece = pieces[index]; |
---|
182 | if (piece < 0) |
---|
183 | { |
---|
184 | if (first || fPieces[index] == -2) |
---|
185 | { |
---|
186 | fPieces[index] = -1; |
---|
187 | pieceColor = fBlueColor; |
---|
188 | } |
---|
189 | else if (fPieces[index] != -1) |
---|
190 | { |
---|
191 | fPieces[index] = -2; |
---|
192 | pieceColor = fOrangeColor; |
---|
193 | } |
---|
194 | else; |
---|
195 | } |
---|
196 | else if (piece == 0) |
---|
197 | { |
---|
198 | if (first || fPieces[index] != 0) |
---|
199 | { |
---|
200 | fPieces[index] = 0; |
---|
201 | pieceColor = fWhiteColor; |
---|
202 | } |
---|
203 | } |
---|
204 | else if (piece <= 4) |
---|
205 | { |
---|
206 | if (first || fPieces[index] != 1) |
---|
207 | { |
---|
208 | fPieces[index] = 1; |
---|
209 | pieceColor = fGreen1Color; |
---|
210 | } |
---|
211 | } |
---|
212 | else if (piece <= 8) |
---|
213 | { |
---|
214 | if (first || fPieces[index] != 2) |
---|
215 | { |
---|
216 | fPieces[index] = 2; |
---|
217 | pieceColor = fGreen2Color; |
---|
218 | } |
---|
219 | } |
---|
220 | else |
---|
221 | { |
---|
222 | if (first || fPieces[index] != 3) |
---|
223 | { |
---|
224 | fPieces[index] = 3; |
---|
225 | pieceColor = fGreen3Color; |
---|
226 | } |
---|
227 | } |
---|
228 | } |
---|
229 | else |
---|
230 | { |
---|
231 | piecePercent = piecesPercent[index]; |
---|
232 | if (piecePercent >= 1.0) |
---|
233 | { |
---|
234 | if (first || fPieces[index] == -2) |
---|
235 | { |
---|
236 | fPieces[index] = -1; |
---|
237 | pieceColor = fBlueColor; |
---|
238 | } |
---|
239 | else if (fPieces[index] != -1) |
---|
240 | { |
---|
241 | fPieces[index] = -2; |
---|
242 | pieceColor = fOrangeColor; |
---|
243 | } |
---|
244 | else; |
---|
245 | } |
---|
246 | else if (piecePercent <= 0.0) |
---|
247 | { |
---|
248 | if (first || fPieces[index] != 0) |
---|
249 | { |
---|
250 | fPieces[index] = 0; |
---|
251 | pieceColor = fWhiteColor; |
---|
252 | } |
---|
253 | } |
---|
254 | else if (piecePercent < 0.25) |
---|
255 | { |
---|
256 | if (first || fPieces[index] != 1) |
---|
257 | { |
---|
258 | fPieces[index] = 1; |
---|
259 | pieceColor = fBlue1Color; |
---|
260 | } |
---|
261 | } |
---|
262 | else if (piecePercent < 0.5) |
---|
263 | { |
---|
264 | if (first || fPieces[index] != 2) |
---|
265 | { |
---|
266 | fPieces[index] = 2; |
---|
267 | pieceColor = fBlue2Color; |
---|
268 | } |
---|
269 | } |
---|
270 | else if (piecePercent < 0.75) |
---|
271 | { |
---|
272 | if (first || fPieces[index] != 3) |
---|
273 | { |
---|
274 | fPieces[index] = 3; |
---|
275 | pieceColor = fBlue3Color; |
---|
276 | } |
---|
277 | } |
---|
278 | else |
---|
279 | { |
---|
280 | if (first || fPieces[index] != 4) |
---|
281 | { |
---|
282 | fPieces[index] = 4; |
---|
283 | pieceColor = fBlue4Color; |
---|
284 | } |
---|
285 | } |
---|
286 | } |
---|
287 | |
---|
288 | if (pieceColor) |
---|
289 | { |
---|
290 | //drawing actually will occur |
---|
291 | if (!change) |
---|
292 | { |
---|
293 | [image lockFocus]; |
---|
294 | change = YES; |
---|
295 | } |
---|
296 | |
---|
297 | [pieceColor set]; |
---|
298 | |
---|
299 | rect.origin = NSMakePoint(j * (fWidth + BETWEEN) + BETWEEN + fExtraBorder, |
---|
300 | [image size].width - (i + 1) * (fWidth + BETWEEN) - fExtraBorder); |
---|
301 | NSRectFill(rect); |
---|
302 | } |
---|
303 | } |
---|
304 | |
---|
305 | if (change) |
---|
306 | { |
---|
307 | [image unlockFocus]; |
---|
308 | [self setNeedsDisplay]; |
---|
309 | } |
---|
310 | |
---|
311 | if (showAvailablity) |
---|
312 | free(pieces); |
---|
313 | else |
---|
314 | free(piecesPercent); |
---|
315 | } |
---|
316 | |
---|
317 | - (BOOL) acceptsFirstMouse: (NSEvent *) event |
---|
318 | { |
---|
319 | return YES; |
---|
320 | } |
---|
321 | |
---|
322 | - (void) mouseDown: (NSEvent *) event |
---|
323 | { |
---|
324 | if (fTorrent) |
---|
325 | [[[self window] windowController] setPiecesViewForAvailable: |
---|
326 | ![[NSUserDefaults standardUserDefaults] boolForKey: @"PiecesViewShowAvailability"]]; |
---|
327 | [super mouseDown: event]; |
---|
328 | } |
---|
329 | |
---|
330 | @end |
---|