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

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

Use the main gui's timer to update the pieces view rather than its own.

File size: 4.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 "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    [fTimer invalidate];
58
59    free(fPieces);
60   
61    if (fTorrent)
62        [fTorrent release];
63    if (fExistingImage)
64        [fExistingImage release];
65    [super dealloc];
66}
67
68- (void) setTorrent: (Torrent *) torrent
69{
70    BOOL first = YES;
71   
72    if (fTorrent)
73    {
74        [fTorrent release];
75       
76        if (!torrent)
77        {
78            fTorrent = nil;
79            [fImageView setImage: fBack];
80        }
81        else
82            first = NO;
83    }
84   
85    if (torrent)
86    {
87        fTorrent = [torrent retain];
88        [self updateView: first];
89    }
90}
91
92- (void) updateView: (BOOL) first
93{
94    if (!fTorrent)
95        return;
96   
97    int numPieces = ACROSS * DOWN;
98    int8_t * pieces = malloc(numPieces);
99    [fTorrent getAvailability: pieces size: numPieces];
100   
101    int i, j, piece, index = -1;
102    NSPoint point;
103    NSImage * pieceImage;
104    BOOL change = NO;
105   
106    [fExistingImage lockFocus];
107   
108    for (i = 0; i < DOWN; i++)
109        for (j = 0; j < ACROSS; j++)
110        {
111            pieceImage = nil;
112       
113            index++;
114            piece = pieces[index];
115            if (piece < 0)
116            {
117                if (fPieces[index] != -1)
118                {
119                    fPieces[index] = -1;
120                    pieceImage = fGreenPiece;
121                }
122            }
123            else if (piece == 0)
124            {
125                if (fPieces[index] != 0)
126                {
127                    fPieces[index] = 0;
128                    pieceImage = fWhitePiece;
129                }
130            }
131            else if (piece == 1)
132            {
133                if (fPieces[index] != 1)
134                {
135                    fPieces[index] = 1;
136                    pieceImage = fBlue1Piece;
137                }
138            }
139            else if (piece == 2)
140            {
141                if (fPieces[index] != 2)
142                {
143                    fPieces[index] = 2;
144                    pieceImage = fBlue2Piece;
145                }
146            }
147            else
148            {
149                if (fPieces[index] != 3)
150                {
151                    fPieces[index] = 3;
152                    pieceImage = fBlue3Piece;
153                }
154            }
155           
156            if (pieceImage)
157            {
158                point = NSMakePoint(j * (WIDTH + BETWEEN) + BETWEEN, i * (HEIGHT + BETWEEN) + BETWEEN);
159                [pieceImage compositeToPoint: point operation: NSCompositeSourceOver];
160               
161                change = YES;
162            }
163        }
164   
165    [fExistingImage unlockFocus];
166   
167    //reload the image regardless if it wasn't called by the timer
168    if (change || first)
169    {
170        [fImageView setImage: nil];
171        [fImageView setImage: fExistingImage];
172    }
173   
174    free(pieces);
175}
176
177@end
Note: See TracBrowser for help on using the repository browser.