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

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

Show pieces top-down, and if there are less pieces than are shown, only show that many pieces.

File size: 4.3 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 HEIGHT 4.0
12#define WIDTH 4.0
13#define BETWEEN 1.0
14#define ACROSS 18
15#define DOWN 18
16
17#define BLANK -99
18
19@implementation PiecesWindowController
20
21- (id) initWithWindowNibName: (NSString *) name
22{
23    if ((self = [super initWithWindowNibName: name]))
24    {
25        fTorrent = nil;
26        fPieces = malloc(ACROSS * DOWN);
27        int i;
28        for (i = 0; i < DOWN * ACROSS; i++)
29            fPieces[i] = BLANK;
30       
31        fBack = [NSImage imageNamed: @"PiecesBack.tiff"];
32        fWhitePiece = [NSImage imageNamed: @"BoxWhite.tiff"];
33        fGreenPiece = [NSImage imageNamed: @"BoxGreen.tiff"];
34        fBlue1Piece = [NSImage imageNamed: @"BoxBlue1.tiff"];
35        fBlue2Piece = [NSImage imageNamed: @"BoxBlue2.tiff"];
36        fBlue3Piece = [NSImage imageNamed: @"BoxBlue3.tiff"];
37       
38        fExistingImage = [fBack copy];
39    }
40   
41    return self;
42}
43
44- (void) awakeFromNib
45{
46    //window location and size
47    NSPanel * window = (NSPanel *)[self window];
48   
49    [window setBecomesKeyOnlyIfNeeded: YES];
50   
51    [window setFrameAutosaveName: @"PiecesWindowFrame"];
52    [window setFrameUsingName: @"PiecesWindowFrame"];
53}
54
55- (void) dealloc
56{
57    free(fPieces);
58   
59    if (fTorrent)
60        [fTorrent release];
61    [fExistingImage release];
62    [super dealloc];
63}
64
65- (void) setTorrent: (Torrent *) torrent
66{
67    if (fTorrent)
68    {
69        [fTorrent release];
70       
71        if (!torrent)
72        {
73            fTorrent = nil;
74            [fImageView setImage: fBack];
75        }
76    }
77   
78    if (torrent)
79    {
80        fTorrent = [torrent retain];
81        [self updateView: YES];
82    }
83}
84
85- (void) updateView: (BOOL) first
86{
87    if (!fTorrent)
88        return;
89   
90    if (first)
91    {
92        [fExistingImage release];
93        fExistingImage = [fBack copy];
94    }
95   
96    int numPieces = ACROSS * DOWN;
97    if (numPieces > [fTorrent pieceCount])
98        numPieces = [fTorrent pieceCount];
99   
100    int8_t * pieces = malloc(numPieces);
101    [fTorrent getAvailability: pieces size: numPieces];
102   
103    int i, j, piece, index = -1;
104    NSPoint point;
105    NSImage * pieceImage;
106    BOOL change = NO;
107   
108    for (i = 0; i < DOWN; i++)
109        for (j = 0; j < ACROSS; j++)
110        {
111            pieceImage = nil;
112       
113            index++;
114            if (index >= numPieces)
115                break;
116           
117            piece = pieces[index];
118            if (piece < 0)
119            {
120                if (first || fPieces[index] != -1)
121                {
122                    fPieces[index] = -1;
123                    pieceImage = fGreenPiece;
124                }
125            }
126            else if (piece == 0)
127            {
128                if (first || fPieces[index] != 0)
129                {
130                    fPieces[index] = 0;
131                    pieceImage = fWhitePiece;
132                }
133            }
134            else if (piece == 1)
135            {
136                if (first || fPieces[index] != 1)
137                {
138                    fPieces[index] = 1;
139                    pieceImage = fBlue1Piece;
140                }
141            }
142            else if (piece == 2)
143            {
144                if (first || fPieces[index] != 2)
145                {
146                    fPieces[index] = 2;
147                    pieceImage = fBlue2Piece;
148                }
149            }
150            else
151            {
152                if (first || fPieces[index] != 3)
153                {
154                    fPieces[index] = 3;
155                    pieceImage = fBlue3Piece;
156                }
157            }
158           
159            if (pieceImage)
160            {
161                if (!change)
162                {
163                    [fExistingImage lockFocus];
164                    change = YES;
165                }
166                point = NSMakePoint(j * (WIDTH + BETWEEN) + BETWEEN, (DOWN - i - 1) * (HEIGHT + BETWEEN) + BETWEEN);
167                [pieceImage compositeToPoint: point operation: NSCompositeSourceOver];
168            }
169        }
170   
171    if (change)
172        [fExistingImage unlockFocus];
173   
174    //reload the image if changes were made or the torrent was loaded after it was blank
175    if (change || first)
176    {
177        [fImageView setImage: nil];
178        [fImageView setImage: fExistingImage];
179    }
180   
181    free(pieces);
182}
183
184@end
Note: See TracBrowser for help on using the repository browser.