source: trunk/PiecesView.m @ 925

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

Use setNeedsDisplay rather than resetting the image on each refresh.

File size: 5.0 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        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   
53    return self;
54}
55
56- (void) dealloc
57{
58    free(fPieces);
59   
60    if (fTorrent)
61        [fTorrent 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 copy] autorelease]];
75        }
76    }
77   
78    if (torrent)
79    {
80        fTorrent = [torrent retain];
81       
82        //determine relevant values
83        fNumPieces = MAX_ACROSS * MAX_ACROSS;
84        if ([fTorrent pieceCount] < fNumPieces)
85        {
86            fNumPieces = [fTorrent pieceCount];
87           
88            fAcross = sqrt(fNumPieces);
89            if (fAcross * fAcross < fNumPieces)
90                fAcross++;
91        }
92        else
93            fAcross = MAX_ACROSS;
94       
95        fWidth = ([[fImageView image] size].width - (fAcross + 1) * BETWEEN) / fAcross;
96        fExtraBorder = ([[fImageView image] size].width - ((fWidth + BETWEEN) * fAcross + BETWEEN)) / 2;
97       
98        [self updateView: YES];
99    }
100}
101
102- (void) updateView: (BOOL) first
103{
104    if (!fTorrent)
105        return;
106   
107    if (first)
108        [fImageView setImage: [[fBack copy] autorelease]];
109   
110    NSImage * image = [fImageView image];
111   
112    int8_t * pieces = malloc(fNumPieces);
113    [fTorrent getAvailability: pieces size: fNumPieces];
114   
115    int i, j, piece, index = -1;
116    NSPoint point;
117    NSRect rect = NSMakeRect(0, 0, fWidth, fWidth);
118    NSImage * pieceImage;
119    BOOL change = NO;
120       
121    for (i = 0; i < fAcross; i++)
122        for (j = 0; j < fAcross; j++)
123        {
124            index++;
125            if (index >= fNumPieces)
126                break;
127           
128            pieceImage = nil;
129           
130            piece = pieces[index];
131            if (piece < 0)
132            {
133                if (first || fPieces[index] != -1)
134                {
135                    fPieces[index] = -1;
136                    pieceImage = fGreenPiece;
137                }
138            }
139            else if (piece == 0)
140            {
141                if (first || fPieces[index] != 0)
142                {
143                    fPieces[index] = 0;
144                    pieceImage = fWhitePiece;
145                }
146            }
147            else if (piece == 1)
148            {
149                if (first || fPieces[index] != 1)
150                {
151                    fPieces[index] = 1;
152                    pieceImage = fBlue1Piece;
153                }
154            }
155            else if (piece == 2)
156            {
157                if (first || fPieces[index] != 2)
158                {
159                    fPieces[index] = 2;
160                    pieceImage = fBlue2Piece;
161                }
162            }
163            else
164            {
165                if (first || fPieces[index] != 3)
166                {
167                    fPieces[index] = 3;
168                    pieceImage = fBlue3Piece;
169                }
170            }
171           
172            if (pieceImage)
173            {
174                //drawing actually will occur, so figure out values
175                if (!change)
176                {
177                    [image lockFocus];
178                    change = YES;
179                }
180               
181                point = NSMakePoint(j * (fWidth + BETWEEN) + BETWEEN + fExtraBorder,
182                                    [[fImageView image] size].width - (i + 1) * (fWidth + BETWEEN) - fExtraBorder);
183               
184                [pieceImage compositeToPoint: point fromRect: rect operation: NSCompositeSourceOver];
185            }
186        }
187   
188    if (change)
189    {
190        [image unlockFocus];
191        [fImageView setNeedsDisplay];
192    }
193   
194    free(pieces);
195}
196
197@end
Note: See TracBrowser for help on using the repository browser.