source: trunk/macosx/PiecesView.m @ 956

Last change on this file since 956 was 956, checked in by livings124, 16 years ago

Status on ports in prefs window. Thanks to cluthi on the forums for this.

File size: 5.8 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 "PiecesView.h"
10
11#define MAX_ACROSS 18
12#define BETWEEN 1.0
13
14#define BLANK -99
15
16@implementation PiecesView
17
18- (id) init
19{
20    if ((self = [super init]))
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   
30    return self;
31}
32
33- (void) awakeFromNib
34{
35        NSSize size = [fImageView frame].size;
36        NSBezierPath * bp = [NSBezierPath bezierPathWithRect: [fImageView bounds]];
37       
38        //back image
39        fBack = [[NSImage alloc] initWithSize: size];
40       
41        [fBack lockFocus];
42        [[NSColor blackColor] set];
43        [bp fill];
44        [fBack unlockFocus];
45       
46        //white box image
47        fWhitePiece = [[NSImage alloc] initWithSize: size];
48       
49        [fWhitePiece lockFocus];
50        [[NSColor whiteColor] set];
51        [bp fill];
52        [fWhitePiece unlockFocus];
53       
54        //green box image
55        fGreenPiece = [[NSImage alloc] initWithSize: size];
56       
57        [fGreenPiece lockFocus];
58        [[NSColor colorWithCalibratedRed: 0.557 green: 0.992 blue: 0.639 alpha: 1.0] set];
59        [bp fill];
60        [fGreenPiece unlockFocus];
61       
62        //blue 1 box image
63        fBlue1Piece = [[NSImage alloc] initWithSize: size];
64       
65        [fBlue1Piece lockFocus];
66        [[NSColor colorWithCalibratedRed: 0.777 green: 0.906 blue: 1.0 alpha: 1.0] set];
67        [bp fill];
68        [fBlue1Piece unlockFocus];
69       
70        //blue 2 box image
71        fBlue2Piece = [[NSImage alloc] initWithSize: size];
72       
73        [fBlue2Piece lockFocus];
74        [[NSColor colorWithCalibratedRed: 0.682 green: 0.839 blue: 1.0 alpha: 1.0] set];
75        [bp fill];
76        [fBlue2Piece unlockFocus];
77       
78        //blue 3 box image
79        fBlue3Piece = [[NSImage alloc] initWithSize: size];
80       
81        [fBlue3Piece lockFocus];
82        [[NSColor colorWithCalibratedRed: 0.506 green: 0.745 blue: 1.0 alpha: 1.0] set];
83        [bp fill];
84        [fBlue3Piece unlockFocus];
85       
86        //actually draw the box
87        [self setTorrent: nil];
88}
89
90- (void) dealloc
91{
92    free(fPieces);
93   
94    if (fTorrent)
95        [fTorrent release];
96    [super dealloc];
97}
98
99- (void) setTorrent: (Torrent *) torrent
100{
101    if (fTorrent)
102    {
103        [fTorrent release];
104       
105        if (!torrent)
106            fTorrent = nil;
107    }
108   
109    if (torrent)
110    {
111        fTorrent = [torrent retain];
112       
113        //determine relevant values
114        fNumPieces = MAX_ACROSS * MAX_ACROSS;
115        if ([fTorrent pieceCount] < fNumPieces)
116        {
117            fNumPieces = [fTorrent pieceCount];
118           
119            fAcross = sqrt(fNumPieces);
120            if (fAcross * fAcross < fNumPieces)
121                fAcross++;
122        }
123        else
124            fAcross = MAX_ACROSS;
125       
126        fWidth = ([[fImageView image] size].width - (fAcross + 1) * BETWEEN) / fAcross;
127        fExtraBorder = ([[fImageView image] size].width - ((fWidth + BETWEEN) * fAcross + BETWEEN)) / 2;
128       
129        [self updateView: YES];
130    }
131   
132    [fImageView setHidden: torrent == nil];
133}
134
135- (void) updateView: (BOOL) first
136{
137    if (!fTorrent)
138        return;
139   
140    if (first)
141        [fImageView setImage: [[fBack copy] autorelease]];
142   
143    NSImage * image = [fImageView image];
144   
145    int8_t * pieces = malloc(fNumPieces);
146    [fTorrent getAvailability: pieces size: fNumPieces];
147   
148    int i, j, piece, index = -1;
149    NSPoint point;
150    NSRect rect = NSMakeRect(0, 0, fWidth, fWidth);
151    NSImage * pieceImage;
152    BOOL change = NO;
153       
154    for (i = 0; i < fAcross; i++)
155        for (j = 0; j < fAcross; j++)
156        {
157            index++;
158            if (index >= fNumPieces)
159                break;
160           
161            pieceImage = nil;
162           
163            piece = pieces[index];
164            if (piece < 0)
165            {
166                if (first || fPieces[index] != -1)
167                {
168                    fPieces[index] = -1;
169                    pieceImage = fGreenPiece;
170                }
171            }
172            else if (piece == 0)
173            {
174                if (first || fPieces[index] != 0)
175                {
176                    fPieces[index] = 0;
177                    pieceImage = fWhitePiece;
178                }
179            }
180            else if (piece == 1)
181            {
182                if (first || fPieces[index] != 1)
183                {
184                    fPieces[index] = 1;
185                    pieceImage = fBlue1Piece;
186                }
187            }
188            else if (piece == 2)
189            {
190                if (first || fPieces[index] != 2)
191                {
192                    fPieces[index] = 2;
193                    pieceImage = fBlue2Piece;
194                }
195            }
196            else
197            {
198                if (first || fPieces[index] != 3)
199                {
200                    fPieces[index] = 3;
201                    pieceImage = fBlue3Piece;
202                }
203            }
204           
205            if (pieceImage)
206            {
207                //drawing actually will occur, so figure out values
208                if (!change)
209                {
210                    [image lockFocus];
211                    change = YES;
212                }
213               
214                point = NSMakePoint(j * (fWidth + BETWEEN) + BETWEEN + fExtraBorder,
215                                    [[fImageView image] size].width - (i + 1) * (fWidth + BETWEEN) - fExtraBorder);
216                [pieceImage compositeToPoint: point fromRect: rect operation: NSCompositeSourceOver];
217            }
218        }
219   
220    if (change)
221    {
222        [image unlockFocus];
223        [fImageView setNeedsDisplay];
224    }
225   
226    free(pieces);
227}
228
229@end
Note: See TracBrowser for help on using the repository browser.