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

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

better box sizing

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