source: trunk/macosx/PiecesView.m @ 980

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

Faster scrolling at the cost of a bit less detailed advanced bar (if you want more details, use the info window ;))

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