source: trunk/macosx/PiecesView.m @ 936

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

Generate the pieces images in code rather than using files.

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