source: trunk/macosx/PiecesView.m @ 994

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

Initial attempt at against the completeness of a specific piece.

File size: 7.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        //blue box image
66        fBluePiece = [[NSImage alloc] initWithSize: size];
67       
68        [fBluePiece lockFocus];
69        [[NSColor colorWithCalibratedRed: 0.35 green: 0.65 blue: 1.0 alpha: 1.0] set];
70        [bp fill];
71        [fBluePiece unlockFocus];
72       
73        //red box image
74        fRedPiece = [[NSImage alloc] initWithSize: size];
75       
76        [fRedPiece lockFocus];
77        [[NSColor redColor] set];
78        [bp fill];
79        [fRedPiece unlockFocus];
80       
81        //green 1 box image
82        fGreen1Piece = [[NSImage alloc] initWithSize: size];
83       
84        [fGreen1Piece lockFocus];
85        [[NSColor colorWithCalibratedRed: 0.6 green: 1.0 blue: 0.8 alpha: 1.0] set];
86        [bp fill];
87        [fGreen1Piece unlockFocus];
88       
89        //green 2 box image
90        fGreen2Piece = [[NSImage alloc] initWithSize: size];
91       
92        [fGreen2Piece lockFocus];
93        [[NSColor colorWithCalibratedRed: 0.4 green: 1.0 blue: 0.6 alpha: 1.0] set];
94        [bp fill];
95        [fGreen2Piece unlockFocus];
96       
97        //green 3 box image
98        fGreen3Piece = [[NSImage alloc] initWithSize: size];
99       
100        [fGreen3Piece lockFocus];
101        [[NSColor colorWithCalibratedRed: 0.0 green: 1.0 blue: 0.4 alpha: 1.0] set];
102        [bp fill];
103        [fGreen3Piece unlockFocus];
104       
105        //actually draw the box
106        [self setTorrent: nil];
107}
108
109- (void) dealloc
110{
111    free(fPieces);
112   
113    [fBack release];
114    [fWhitePiece release];
115    [fRedPiece release];
116    [fBluePiece release];
117    [fGreen1Piece release];
118    [fGreen2Piece release];
119    [fGreen3Piece release];
120   
121    if (fTorrent)
122        [fTorrent release];
123    [super dealloc];
124}
125
126- (void) setTorrent: (Torrent *) torrent
127{
128    if (fTorrent)
129    {
130        [fTorrent release];
131       
132        if (!torrent)
133            fTorrent = nil;
134    }
135   
136    if (torrent)
137    {
138        fTorrent = [torrent retain];
139       
140        //determine relevant values
141        fNumPieces = MAX_ACROSS * MAX_ACROSS;
142        int pieceCount = [fTorrent pieceCount];
143        if (pieceCount < fNumPieces)
144        {
145            fNumPieces = pieceCount;
146           
147            fAcross = sqrt(fNumPieces);
148            if (fAcross * fAcross < fNumPieces)
149                fAcross++;
150        }
151        else
152            fAcross = MAX_ACROSS;
153       
154        float width = [fImageView bounds].size.width;
155        fWidth = (width - (fAcross + 1) * BETWEEN) / fAcross;
156        fExtraBorder = (width - ((fWidth + BETWEEN) * fAcross + BETWEEN)) / 2;
157       
158        [self updateView: YES];
159    }
160   
161    [fImageView setHidden: torrent == nil];
162}
163
164- (void) updateView: (BOOL) first
165{
166    if (!fTorrent)
167        return;
168   
169    if (first)
170        [fImageView setImage: [[fBack copy] autorelease]];
171    NSImage * image = [fImageView image];
172   
173    int8_t * pieces = malloc(fNumPieces);
174    [fTorrent getAvailability: pieces size: fNumPieces];
175   
176    int i, j, piece, index = -1;
177    NSPoint point;
178    NSRect rect = NSMakeRect(0, 0, fWidth, fWidth);
179    NSImage * pieceImage;
180    BOOL change = NO;
181       
182    for (i = 0; i < fAcross; i++)
183        for (j = 0; j < fAcross; j++)
184        {
185            index++;
186            if (index >= fNumPieces)
187            {
188                i = fAcross;
189                break;
190            }
191           
192            pieceImage = nil;
193            piece = pieces[index];
194            if (piece < 0)
195            {
196                if (first || fPieces[index] == -2)
197                {
198                    fPieces[index] = -1;
199                    pieceImage = fBluePiece;
200                }
201                else if (fPieces[index] != -1)
202                {
203                    fPieces[index] = -2;
204                    pieceImage = fRedPiece;
205                }
206                else;
207            }
208            else if (piece == 0)
209            {
210                if (first || fPieces[index] != 0)
211                {
212                    fPieces[index] = 0;
213                    pieceImage = fWhitePiece;
214                }
215            }
216            else if (piece == 1)
217            {
218                if (first || fPieces[index] != 1)
219                {
220                    fPieces[index] = 1;
221                    pieceImage = fGreen1Piece;
222                }
223            }
224            else if (piece == 2)
225            {
226                if (first || fPieces[index] != 2)
227                {
228                    fPieces[index] = 2;
229                    pieceImage = fGreen2Piece;
230                }
231            }
232            else
233            {
234                if (first || fPieces[index] != 3)
235                {
236                    fPieces[index] = 3;
237                    pieceImage = fGreen3Piece;
238                }
239            }
240           
241            if (pieceImage)
242            {
243                //drawing actually will occur, so figure out values
244                if (!change)
245                {
246                    [image lockFocus];
247                    change = YES;
248                }
249               
250                point = NSMakePoint(j * (fWidth + BETWEEN) + BETWEEN + fExtraBorder,
251                                    [image size].width - (i + 1) * (fWidth + BETWEEN) - fExtraBorder);
252                [pieceImage compositeToPoint: point fromRect: rect operation: NSCompositeSourceOver];
253            }
254        }
255   
256    if (change)
257    {
258        [image unlockFocus];
259        [fImageView setNeedsDisplay];
260       
261        /*int8_t * piecesC = malloc(fNumPieces);
262        [fTorrent getAmountFinished: piecesC size: fNumPieces];
263        for (i = 0; i < fNumPieces; i++)
264            NSLog(@"%d", piecesC[i]);
265        free(piecesC);*/
266    }
267   
268    free(pieces);
269}
270
271@end
Note: See TracBrowser for help on using the repository browser.