source: branches/nat-traversal/PiecesWindowController.m @ 910

Last change on this file since 910 was 910, checked in by livings124, 15 years ago

Make boxes in pieces viewer larger if few enough to make them all fit.

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 "PiecesWindowController.h"
10
11#define MIN_WIDTH 4.0
12#define MAX_ACROSS 18
13
14#define BLANK -99
15
16@implementation PiecesWindowController
17
18- (id) initWithWindowNibName: (NSString *) name
19{
20    if ((self = [super initWithWindowNibName: name]))
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        fExistingImage = [fBack copy];
53    }
54   
55    return self;
56}
57
58- (void) awakeFromNib
59{
60    //window location and size
61    NSPanel * window = (NSPanel *)[self window];
62   
63    [window setBecomesKeyOnlyIfNeeded: YES];
64   
65    [window setFrameAutosaveName: @"PiecesWindowFrame"];
66    [window setFrameUsingName: @"PiecesWindowFrame"];
67}
68
69- (void) dealloc
70{
71    free(fPieces);
72   
73    if (fTorrent)
74        [fTorrent release];
75    [fExistingImage release];
76    [super dealloc];
77}
78
79- (void) setTorrent: (Torrent *) torrent
80{
81    if (fTorrent)
82    {
83        [fTorrent release];
84       
85        if (!torrent)
86        {
87            fTorrent = nil;
88            [fImageView setImage: fBack];
89        }
90    }
91   
92    if (torrent)
93    {
94        fTorrent = [torrent retain];
95        [self updateView: YES];
96    }
97}
98
99- (void) updateView: (BOOL) first
100{
101    if (!fTorrent)
102        return;
103   
104    if (first)
105    {
106        [fExistingImage release];
107        fExistingImage = [fBack copy];
108    }
109   
110    int numPieces = MAX_ACROSS * MAX_ACROSS;
111    if (numPieces > [fTorrent pieceCount])
112        numPieces = [fTorrent pieceCount];
113   
114    //determine how many boxes
115    int across = MAX_ACROSS;
116    float width = MIN_WIDTH, between;
117    while (numPieces <= (across / 2) * (across / 2))
118    {
119        across /= 2;
120        width *= 2.0;
121    }
122    between = ([fExistingImage size].width - (float)across * width) / (float)(across + 1);
123   
124    int8_t * pieces = malloc(numPieces);
125    [fTorrent getAvailability: pieces size: numPieces];
126   
127    int i, j, piece, index = -1;
128    NSPoint point;
129    NSRect rect = NSMakeRect(0, 0, width, width);
130    NSImage * pieceImage;
131    BOOL change = NO;
132   
133    for (i = 0; i < across; i++)
134        for (j = 0; j < across; j++)
135        {
136            pieceImage = nil;
137       
138            index++;
139            if (index >= numPieces)
140                break;
141           
142            piece = pieces[index];
143            if (piece < 0)
144            {
145                if (first || fPieces[index] != -1)
146                {
147                    fPieces[index] = -1;
148                    pieceImage = fGreenPiece;
149                }
150            }
151            else if (piece == 0)
152            {
153                if (first || fPieces[index] != 0)
154                {
155                    fPieces[index] = 0;
156                    pieceImage = fWhitePiece;
157                }
158            }
159            else if (piece == 1)
160            {
161                if (first || fPieces[index] != 1)
162                {
163                    fPieces[index] = 1;
164                    pieceImage = fBlue1Piece;
165                }
166            }
167            else if (piece == 2)
168            {
169                if (first || fPieces[index] != 2)
170                {
171                    fPieces[index] = 2;
172                    pieceImage = fBlue2Piece;
173                }
174            }
175            else
176            {
177                if (first || fPieces[index] != 3)
178                {
179                    fPieces[index] = 3;
180                    pieceImage = fBlue3Piece;
181                }
182            }
183           
184            if (pieceImage)
185            {
186                if (!change)
187                {
188                    [fExistingImage lockFocus];
189                    change = YES;
190                }
191               
192                point = NSMakePoint((float)j * (width + between) + between, (float)(across - i) * (width + between) - width);
193                [pieceImage compositeToPoint: point fromRect: rect operation: NSCompositeSourceOver];
194            }
195        }
196   
197    if (change)
198    {
199        [fExistingImage unlockFocus];
200       
201        [fImageView setImage: nil];
202        [fImageView setImage: fExistingImage];
203    }
204   
205    free(pieces);
206}
207
208@end
Note: See TracBrowser for help on using the repository browser.