source: trunk/macosx/PiecesView.m @ 935

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

Hide pieces view if a single torrent is not selected.

File size: 5.1 KB
Line 
1//
2//  PiecesWindowController.m
3//  Transmission
4//
5//  Created by Livingston on 9/23/06.
6//  Copyright 2006 __MyCompanyName__. All rights reserved.
7//
8
9#import "PiecesView.h"
10
11#define MAX_ACROSS 18
12#define BETWEEN 1.0
13
14#define BLANK -99
15
16@implementation PiecesView
17
18- (id) init
19{
20    if ((self = [super init]))
21    {
22        fTorrent = nil;
23        int numPieces = MAX_ACROSS * MAX_ACROSS;
24        fPieces = malloc(numPieces);
25        int i;
26        for (i = 0; i < numPieces; i++)
27            fPieces[i] = BLANK;
28       
29        fBack = [NSImage imageNamed: @"PiecesBack.tiff"];
30        NSSize size = [fBack size];
31       
32        fWhitePiece = [NSImage imageNamed: @"BoxWhite.tiff"];
33        [fWhitePiece setScalesWhenResized: YES];
34        [fWhitePiece setSize: size];
35       
36        fGreenPiece = [NSImage imageNamed: @"BoxGreen.tiff"];
37        [fGreenPiece setScalesWhenResized: YES];
38        [fGreenPiece setSize: size];
39       
40        fBlue1Piece = [NSImage imageNamed: @"BoxBlue1.tiff"];
41        [fBlue1Piece setScalesWhenResized: YES];
42        [fBlue1Piece setSize: size];
43       
44        fBlue2Piece = [NSImage imageNamed: @"BoxBlue2.tiff"];
45        [fBlue2Piece setScalesWhenResized: YES];
46        [fBlue2Piece setSize: size];
47       
48        fBlue3Piece = [NSImage imageNamed: @"BoxBlue3.tiff"];
49        [fBlue3Piece setScalesWhenResized: YES];
50        [fBlue3Piece setSize: size];
51       
52        [self setTorrent: nil];
53    }
54   
55    return self;
56}
57
58- (void) dealloc
59{
60    free(fPieces);
61   
62    if (fTorrent)
63        [fTorrent release];
64    [super dealloc];
65}
66
67- (void) setTorrent: (Torrent *) torrent
68{
69    if (fTorrent)
70    {
71        [fTorrent release];
72       
73        if (!torrent)
74        {
75            fTorrent = nil;
76            [fImageView setImage: [[fBack copy] autorelease]];
77        }
78    }
79   
80    if (torrent)
81    {
82        fTorrent = [torrent retain];
83       
84        //determine relevant values
85        fNumPieces = MAX_ACROSS * MAX_ACROSS;
86        if ([fTorrent pieceCount] < fNumPieces)
87        {
88            fNumPieces = [fTorrent pieceCount];
89           
90            fAcross = sqrt(fNumPieces);
91            if (fAcross * fAcross < fNumPieces)
92                fAcross++;
93        }
94        else
95            fAcross = MAX_ACROSS;
96       
97        fWidth = ([[fImageView image] size].width - (fAcross + 1) * BETWEEN) / fAcross;
98        fExtraBorder = ([[fImageView image] size].width - ((fWidth + BETWEEN) * fAcross + BETWEEN)) / 2;
99       
100        [self updateView: YES];
101    }
102   
103    [fImageView setHidden: torrent == nil];
104}
105
106- (void) updateView: (BOOL) first
107{
108    if (!fTorrent)
109        return;
110   
111    if (first)
112        [fImageView setImage: [[fBack copy] autorelease]];
113   
114    NSImage * image = [fImageView image];
115   
116    int8_t * pieces = malloc(fNumPieces);
117    [fTorrent getAvailability: pieces size: fNumPieces];
118   
119    int i, j, piece, index = -1;
120    NSPoint point;
121    NSRect rect = NSMakeRect(0, 0, fWidth, fWidth);
122    NSImage * pieceImage;
123    BOOL change = NO;
124       
125    for (i = 0; i < fAcross; i++)
126        for (j = 0; j < fAcross; j++)
127        {
128            index++;
129            if (index >= fNumPieces)
130                break;
131           
132            pieceImage = nil;
133           
134            piece = pieces[index];
135            if (piece < 0)
136            {
137                if (first || fPieces[index] != -1)
138                {
139                    fPieces[index] = -1;
140                    pieceImage = fGreenPiece;
141                }
142            }
143            else if (piece == 0)
144            {
145                if (first || fPieces[index] != 0)
146                {
147                    fPieces[index] = 0;
148                    pieceImage = fWhitePiece;
149                }
150            }
151            else if (piece == 1)
152            {
153                if (first || fPieces[index] != 1)
154                {
155                    fPieces[index] = 1;
156                    pieceImage = fBlue1Piece;
157                }
158            }
159            else if (piece == 2)
160            {
161                if (first || fPieces[index] != 2)
162                {
163                    fPieces[index] = 2;
164                    pieceImage = fBlue2Piece;
165                }
166            }
167            else
168            {
169                if (first || fPieces[index] != 3)
170                {
171                    fPieces[index] = 3;
172                    pieceImage = fBlue3Piece;
173                }
174            }
175           
176            if (pieceImage)
177            {
178                //drawing actually will occur, so figure out values
179                if (!change)
180                {
181                    [image lockFocus];
182                    change = YES;
183                }
184               
185                point = NSMakePoint(j * (fWidth + BETWEEN) + BETWEEN + fExtraBorder,
186                                    [[fImageView image] size].width - (i + 1) * (fWidth + BETWEEN) - fExtraBorder);
187               
188                [pieceImage compositeToPoint: point fromRect: rect operation: NSCompositeSourceOver];
189            }
190        }
191   
192    if (change)
193    {
194        [image unlockFocus];
195        [fImageView setNeedsDisplay];
196    }
197   
198    free(pieces);
199}
200
201@end
Note: See TracBrowser for help on using the repository browser.