source: trunk/macosx/PiecesView.m @ 998

Last change on this file since 998 was 998, checked in by livings124, 16 years ago

Pieces View is working again

File size: 11.7 KB
Line 
1/******************************************************************************
2 * $Id$
3 *
4 * Copyright (c) 2005-2006 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
27#define MAX_ACROSS 18
28#define BETWEEN 1.0
29
30@implementation PiecesView
31
32- (id) init
33{
34    if ((self = [super init]))
35    {
36        fTorrent = nil;
37        int numPieces = MAX_ACROSS * MAX_ACROSS;
38        fPieces = malloc(numPieces);
39    }
40   
41    return self;
42}
43
44- (void) awakeFromNib
45{
46        NSSize size = [fImageView bounds].size;
47        NSBezierPath * bp = [NSBezierPath bezierPathWithRect: [fImageView bounds]];
48       
49        //back image
50        fBack = [[NSImage alloc] initWithSize: size];
51       
52        [fBack lockFocus];
53        [[NSColor blackColor] set];
54        [bp fill];
55        [fBack unlockFocus];
56       
57        //white box image
58        fWhitePiece = [[NSImage alloc] initWithSize: size];
59       
60        [fWhitePiece lockFocus];
61        [[NSColor whiteColor] set];
62        [bp fill];
63        [fWhitePiece unlockFocus];
64       
65        //red box image
66        fRedPiece = [[NSImage alloc] initWithSize: size];
67       
68        [fRedPiece lockFocus];
69        [[NSColor redColor] set];
70        [bp fill];
71        [fRedPiece unlockFocus];
72       
73        //green 1 box image
74        fGreen1Piece = [[NSImage alloc] initWithSize: size];
75       
76        [fGreen1Piece lockFocus];
77        [[NSColor colorWithCalibratedRed: 0.6 green: 1.0 blue: 0.8 alpha: 1.0] set];
78        [bp fill];
79        [fGreen1Piece unlockFocus];
80       
81        //green 2 box image
82        fGreen2Piece = [[NSImage alloc] initWithSize: size];
83       
84        [fGreen2Piece lockFocus];
85        [[NSColor colorWithCalibratedRed: 0.4 green: 1.0 blue: 0.6 alpha: 1.0] set];
86        [bp fill];
87        [fGreen2Piece unlockFocus];
88       
89        //green 3 box image
90        fGreen3Piece = [[NSImage alloc] initWithSize: size];
91       
92        [fGreen3Piece lockFocus];
93        [[NSColor colorWithCalibratedRed: 0.0 green: 1.0 blue: 0.4 alpha: 1.0] set];
94        [bp fill];
95        [fGreen3Piece unlockFocus];
96       
97        //blue 1 box image
98        fBlue1Piece = [[NSImage alloc] initWithSize: size];
99       
100        [fBlue1Piece lockFocus];
101        [[NSColor colorWithCalibratedRed: 0.8 green: 1.0 blue: 1.0 alpha: 1.0] set];
102        [bp fill];
103        [fBlue1Piece unlockFocus];
104       
105        //blue 2 box image
106        fBlue2Piece = [[NSImage alloc] initWithSize: size];
107       
108        [fBlue2Piece lockFocus];
109        [[NSColor colorWithCalibratedRed: 0.6 green: 1.0 blue: 1.0 alpha: 1.0] set];
110        [bp fill];
111        [fBlue2Piece unlockFocus];
112       
113        //blue 3 box image
114        fBlue3Piece = [[NSImage alloc] initWithSize: size];
115       
116        [fBlue3Piece lockFocus];
117        [[NSColor colorWithCalibratedRed: 0.6 green: 0.8 blue: 1.0 alpha: 1.0] set];
118        [bp fill];
119        [fBlue3Piece unlockFocus];
120       
121        //blue 4 box image
122        fBlue4Piece = [[NSImage alloc] initWithSize: size];
123       
124        [fBlue4Piece lockFocus];
125        [[NSColor colorWithCalibratedRed: 0.4 green: 0.6 blue: 1.0 alpha: 1.0] set];
126        [bp fill];
127        [fBlue4Piece unlockFocus];
128       
129        //blue box image
130        fBluePiece = [[NSImage alloc] initWithSize: size];
131       
132        [fBluePiece lockFocus];
133        [[NSColor colorWithCalibratedRed: 0.0 green: 0.4 blue: 0.8 alpha: 1.0] set];
134        [bp fill];
135        [fBluePiece unlockFocus];
136       
137        //actually draw the box
138        [self setTorrent: nil];
139}
140
141- (void) dealloc
142{
143    free(fPieces);
144   
145    [fBack release];
146    [fWhitePiece release];
147    [fRedPiece release];
148    [fBluePiece release];
149    [fGreen1Piece release];
150    [fGreen2Piece release];
151    [fGreen3Piece release];
152   
153    if (fTorrent)
154        [fTorrent release];
155    [super dealloc];
156}
157
158- (void) setTorrent: (Torrent *) torrent
159{
160    if (fTorrent)
161    {
162        [fTorrent release];
163       
164        if (!torrent)
165            fTorrent = nil;
166    }
167   
168    if (torrent)
169    {
170        fTorrent = [torrent retain];
171       
172        //determine relevant values
173        fNumPieces = MAX_ACROSS * MAX_ACROSS;
174        int pieceCount = [fTorrent pieceCount];
175        if (pieceCount < fNumPieces)
176        {
177            fNumPieces = pieceCount;
178           
179            fAcross = sqrt(fNumPieces);
180            if (fAcross * fAcross < fNumPieces)
181                fAcross++;
182        }
183        else
184            fAcross = MAX_ACROSS;
185       
186        float width = [fImageView bounds].size.width;
187        fWidth = (width - (fAcross + 1) * BETWEEN) / fAcross;
188        fExtraBorder = (width - ((fWidth + BETWEEN) * fAcross + BETWEEN)) / 2;
189       
190        [self updateView: YES];
191    }
192   
193    [fImageView setHidden: torrent == nil];
194}
195
196- (void) updateView: (BOOL) first
197{
198    if (!fTorrent)
199        return;
200   
201    if (first)
202        [fImageView setImage: [[fBack copy] autorelease]];
203    NSImage * image = [fImageView image];
204
205    int8_t * pieces;
206    float * piecesPercent;
207   
208    BOOL showAvailablity = [[NSUserDefaults standardUserDefaults] boolForKey: @"PiecesViewShowAvailability"];
209    if (showAvailablity)
210    {   
211        pieces = malloc(fNumPieces);
212        [fTorrent getAvailability: pieces size: fNumPieces];
213    }
214    else
215    {   
216        piecesPercent = malloc(fNumPieces * sizeof(float));
217        [fTorrent getAmountFinished: piecesPercent size: fNumPieces];
218       
219        int i;
220        for (i = 0; i < fNumPieces; i++)
221            NSLog(@"%f", piecesPercent[i]);
222    }
223   
224    int i, j, piece, index = -1;
225    float piecePercent;
226    NSPoint point;
227    NSRect rect = NSMakeRect(0, 0, fWidth, fWidth);
228    NSImage * pieceImage;
229    BOOL change = NO;
230       
231    for (i = 0; i < fAcross; i++)
232        for (j = 0; j < fAcross; j++)
233        {
234            index++;
235            if (index >= fNumPieces)
236            {
237                i = fAcross;
238                break;
239            }
240           
241            pieceImage = nil;
242           
243            if (showAvailablity)
244            {
245                piece = pieces[index];
246                if (piece < 0)
247                {
248                    if (first || fPieces[index] == -2)
249                    {
250                        fPieces[index] = -1;
251                        pieceImage = fBluePiece;
252                    }
253                    else if (fPieces[index] != -1)
254                    {
255                        fPieces[index] = -2;
256                        pieceImage = fRedPiece;
257                    }
258                    else;
259                }
260                else if (piece == 0)
261                {
262                    if (first || fPieces[index] != 0)
263                    {
264                        fPieces[index] = 0;
265                        pieceImage = fWhitePiece;
266                    }
267                }
268                else if (piece == 1)
269                {
270                    if (first || fPieces[index] != 1)
271                    {
272                        fPieces[index] = 1;
273                        pieceImage = fGreen1Piece;
274                    }
275                }
276                else if (piece == 2)
277                {
278                    if (first || fPieces[index] != 2)
279                    {
280                        fPieces[index] = 2;
281                        pieceImage = fGreen2Piece;
282                    }
283                }
284                else
285                {
286                    if (first || fPieces[index] != 3)
287                    {
288                        fPieces[index] = 3;
289                        pieceImage = fGreen3Piece;
290                    }
291                }
292            }
293            else
294            {
295                piecePercent = piecesPercent[index];
296                /*if (i==0)
297                    pieceImage = fBlue1Piece;
298                else if (i==1)
299                    pieceImage = fBlue2Piece;
300                else if (i==2)
301                    pieceImage = fBlue3Piece;
302                else if (i==3)
303                    pieceImage = fBlue4Piece;
304                else if (i==4)
305                    pieceImage = fBluePiece;
306               
307                else */if (piecePercent <= 0.0)
308                {
309                    if (first || fPieces[index] != 0)
310                    {
311                        fPieces[index] = 0;
312                        pieceImage = fWhitePiece;
313                    }
314                }
315                else if (piecePercent < 0.25)
316                {
317                    if (first || fPieces[index] != 1)
318                    {
319                        fPieces[index] = 1;
320                        pieceImage = fBlue1Piece;
321                    }
322                }
323                else if (piecePercent < 0.50)
324                {
325                    if (first || fPieces[index] != 2)
326                    {
327                        fPieces[index] = 2;
328                        pieceImage = fBlue2Piece;
329                    }
330                }
331                else if (piecePercent < 0.75)
332                {
333                    if (first || fPieces[index] != 3)
334                    {
335                        fPieces[index] = 3;
336                        pieceImage = fBlue3Piece;
337                    }
338                }
339                else if (piecePercent < 1.0)
340                {
341                    if (first || fPieces[index] != 4)
342                    {
343                        fPieces[index] = 4;
344                        pieceImage = fBlue4Piece;
345                    }
346                }
347                else
348                {
349                    if (first || fPieces[index] != 5)
350                    {
351                        fPieces[index] = 5;
352                        pieceImage = fBluePiece;
353                    }
354                }
355            }
356           
357            if (pieceImage)
358            {
359                //drawing actually will occur, so figure out values
360                if (!change)
361                {
362                    [image lockFocus];
363                    change = YES;
364                }
365               
366                point = NSMakePoint(j * (fWidth + BETWEEN) + BETWEEN + fExtraBorder,
367                                    [image size].width - (i + 1) * (fWidth + BETWEEN) - fExtraBorder);
368                [pieceImage compositeToPoint: point fromRect: rect operation: NSCompositeSourceOver];
369            }
370        }
371   
372    if (change)
373    {
374        [image unlockFocus];
375        [fImageView setNeedsDisplay];
376    }
377   
378    if (showAvailablity)
379        free(pieces);
380    else
381        free(piecesPercent);
382}
383
384/*- (void) toggleView: (id) sender
385{
386    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
387    [defaults setBool: ![defaults boolForKey: @"PiecesViewShowAvailability"]
388                forKey: @"PiecesViewShowAvailability"];
389   
390    [self updateView: YES];
391}*/
392
393@end
Note: See TracBrowser for help on using the repository browser.