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

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

Use square root instead of a while loop.

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 MAX_ACROSS 18
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        [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, across;
111    if ([fTorrent pieceCount] < numPieces)
112    {
113        numPieces = [fTorrent pieceCount];
114       
115        //determine how many boxes
116        across = sqrt(numPieces);
117        if (across * across < numPieces)
118            across++;
119    }
120    else
121        across = MAX_ACROSS;
122   
123    float width = ([fExistingImage size].width - (float)(across + 1) * BETWEEN) / (float)across;
124   
125    int8_t * pieces = malloc(numPieces);
126    [fTorrent getAvailability: pieces size: numPieces];
127   
128    int i, j, piece, index = -1;
129    NSPoint point;
130    NSRect rect = NSMakeRect(0, 0, width, width);
131    NSImage * pieceImage;
132    BOOL change = NO;
133   
134    for (i = 0; i < across; i++)
135        for (j = 0; j < across; j++)
136        {
137            pieceImage = nil;
138       
139            index++;
140            if (index >= numPieces)
141                break;
142           
143            piece = pieces[index];
144            if (piece < 0)
145            {
146                if (first || fPieces[index] != -1)
147                {
148                    fPieces[index] = -1;
149                    pieceImage = fGreenPiece;
150                }
151            }
152            else if (piece == 0)
153            {
154                if (first || fPieces[index] != 0)
155                {
156                    fPieces[index] = 0;
157                    pieceImage = fWhitePiece;
158                }
159            }
160            else if (piece == 1)
161            {
162                if (first || fPieces[index] != 1)
163                {
164                    fPieces[index] = 1;
165                    pieceImage = fBlue1Piece;
166                }
167            }
168            else if (piece == 2)
169            {
170                if (first || fPieces[index] != 2)
171                {
172                    fPieces[index] = 2;
173                    pieceImage = fBlue2Piece;
174                }
175            }
176            else
177            {
178                if (first || fPieces[index] != 3)
179                {
180                    fPieces[index] = 3;
181                    pieceImage = fBlue3Piece;
182                }
183            }
184           
185            if (pieceImage)
186            {
187                if (!change)
188                {
189                    [fExistingImage lockFocus];
190                    change = YES;
191                }
192               
193                point = NSMakePoint((float)j * (width + BETWEEN) + BETWEEN, (float)(across - i) * (width + BETWEEN) - width);
194                [pieceImage compositeToPoint: point fromRect: rect operation: NSCompositeSourceOver];
195            }
196        }
197   
198    if (change)
199    {
200        [fExistingImage unlockFocus];
201       
202        [fImageView setImage: nil];
203        [fImageView setImage: fExistingImage];
204    }
205   
206    free(pieces);
207}
208
209@end
Note: See TracBrowser for help on using the repository browser.