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

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

Make box size larger, as well as avoid overlap.

File size: 5.4 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 MAX_ACROSS 20
12#define BETWEEN 1.0
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       
96        //determine relevant values
97        fNumPieces = MAX_ACROSS * MAX_ACROSS;
98        if ([fTorrent pieceCount] < fNumPieces)
99        {
100            fNumPieces = [fTorrent pieceCount];
101           
102            fAcross = sqrt(fNumPieces);
103            if (fAcross * fAcross < fNumPieces)
104                fAcross++;
105        }
106        else
107            fAcross = MAX_ACROSS;
108       
109        fWidth = ([fExistingImage size].width - (fAcross + 1) * BETWEEN) / fAcross;
110        fExtraBorder = ([fExistingImage size].width - ((fWidth + BETWEEN) * fAcross + BETWEEN)) / 2;
111       
112        [self updateView: YES];
113    }
114}
115
116- (void) updateView: (BOOL) first
117{
118    if (!fTorrent)
119        return;
120   
121    if (first)
122    {
123        [fExistingImage release];
124        fExistingImage = [fBack copy];
125    }
126   
127    int8_t * pieces = malloc(fNumPieces);
128    [fTorrent getAvailability: pieces size: fNumPieces];
129   
130    int i, j, piece, index = -1;
131    NSPoint point;
132    NSRect rect = NSMakeRect(0, 0, fWidth, fWidth);
133    NSImage * pieceImage;
134    BOOL change = NO;
135       
136    for (i = 0; i < fAcross; i++)
137        for (j = 0; j < fAcross; j++)
138        {
139            index++;
140            if (index >= fNumPieces)
141                break;
142           
143            pieceImage = nil;
144           
145            piece = pieces[index];
146            if (piece < 0)
147            {
148                if (first || fPieces[index] != -1)
149                {
150                    fPieces[index] = -1;
151                    pieceImage = fGreenPiece;
152                }
153            }
154            else if (piece == 0)
155            {
156                if (first || fPieces[index] != 0)
157                {
158                    fPieces[index] = 0;
159                    pieceImage = fWhitePiece;
160                }
161            }
162            else if (piece == 1)
163            {
164                if (first || fPieces[index] != 1)
165                {
166                    fPieces[index] = 1;
167                    pieceImage = fBlue1Piece;
168                }
169            }
170            else if (piece == 2)
171            {
172                if (first || fPieces[index] != 2)
173                {
174                    fPieces[index] = 2;
175                    pieceImage = fBlue2Piece;
176                }
177            }
178            else
179            {
180                if (first || fPieces[index] != 3)
181                {
182                    fPieces[index] = 3;
183                    pieceImage = fBlue3Piece;
184                }
185            }
186           
187            if (pieceImage)
188            {
189                //drawing actually will occur, so figure out values
190                if (!change)
191                {
192                    [fExistingImage lockFocus];
193                    change = YES;
194                }
195               
196                point = NSMakePoint(j * (fWidth + BETWEEN) + BETWEEN + fExtraBorder,
197                                    [fExistingImage size].width - (i + 1) * (fWidth + BETWEEN) - fExtraBorder);
198               
199                [pieceImage compositeToPoint: point fromRect: rect operation: NSCompositeSourceOver];
200            }
201        }
202   
203    if (change)
204    {
205        [fExistingImage unlockFocus];
206       
207        [fImageView setImage: nil];
208        [fImageView setImage: fExistingImage];
209    }
210   
211    free(pieces);
212}
213
214@end
Note: See TracBrowser for help on using the repository browser.