1 | /****************************************************************************** |
---|
2 | * $Id$ |
---|
3 | * |
---|
4 | * Copyright (c) 2005-2006 Transmission authors and contributors |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
7 | * copy of this software and associated documentation files (the "Software"), |
---|
8 | * to deal in the Software without restriction, including without limitation |
---|
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
10 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
11 | * Software is furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
22 | * DEALINGS IN THE SOFTWARE. |
---|
23 | *****************************************************************************/ |
---|
24 | |
---|
25 | #import "PiecesView.h" |
---|
26 | |
---|
27 | #define MAX_ACROSS 18 |
---|
28 | #define BETWEEN 1.0 |
---|
29 | |
---|
30 | #define BLANK -99 |
---|
31 | |
---|
32 | @implementation PiecesView |
---|
33 | |
---|
34 | - (id) init |
---|
35 | { |
---|
36 | if ((self = [super init])) |
---|
37 | { |
---|
38 | fTorrent = nil; |
---|
39 | int numPieces = MAX_ACROSS * MAX_ACROSS; |
---|
40 | fPieces = malloc(numPieces); |
---|
41 | int i; |
---|
42 | for (i = 0; i < numPieces; i++) |
---|
43 | fPieces[i] = BLANK; |
---|
44 | } |
---|
45 | |
---|
46 | return self; |
---|
47 | } |
---|
48 | |
---|
49 | - (void) awakeFromNib |
---|
50 | { |
---|
51 | NSSize size = [fImageView frame].size; |
---|
52 | NSBezierPath * bp = [NSBezierPath bezierPathWithRect: [fImageView bounds]]; |
---|
53 | |
---|
54 | //back image |
---|
55 | fBack = [[NSImage alloc] initWithSize: size]; |
---|
56 | |
---|
57 | [fBack lockFocus]; |
---|
58 | [[NSColor blackColor] set]; |
---|
59 | [bp fill]; |
---|
60 | [fBack unlockFocus]; |
---|
61 | |
---|
62 | //white box image |
---|
63 | fWhitePiece = [[NSImage alloc] initWithSize: size]; |
---|
64 | |
---|
65 | [fWhitePiece lockFocus]; |
---|
66 | [[NSColor whiteColor] set]; |
---|
67 | [bp fill]; |
---|
68 | [fWhitePiece unlockFocus]; |
---|
69 | |
---|
70 | //green box image |
---|
71 | fGreenPiece = [[NSImage alloc] initWithSize: size]; |
---|
72 | |
---|
73 | [fGreenPiece lockFocus]; |
---|
74 | [[NSColor colorWithCalibratedRed: 0.557 green: 0.992 blue: 0.639 alpha: 1.0] set]; |
---|
75 | [bp fill]; |
---|
76 | [fGreenPiece unlockFocus]; |
---|
77 | |
---|
78 | //blue 1 box image |
---|
79 | fBlue1Piece = [[NSImage alloc] initWithSize: size]; |
---|
80 | |
---|
81 | [fBlue1Piece lockFocus]; |
---|
82 | [[NSColor colorWithCalibratedRed: 0.777 green: 0.906 blue: 1.0 alpha: 1.0] set]; |
---|
83 | [bp fill]; |
---|
84 | [fBlue1Piece unlockFocus]; |
---|
85 | |
---|
86 | //blue 2 box image |
---|
87 | fBlue2Piece = [[NSImage alloc] initWithSize: size]; |
---|
88 | |
---|
89 | [fBlue2Piece lockFocus]; |
---|
90 | [[NSColor colorWithCalibratedRed: 0.682 green: 0.839 blue: 1.0 alpha: 1.0] set]; |
---|
91 | [bp fill]; |
---|
92 | [fBlue2Piece unlockFocus]; |
---|
93 | |
---|
94 | //blue 3 box image |
---|
95 | fBlue3Piece = [[NSImage alloc] initWithSize: size]; |
---|
96 | |
---|
97 | [fBlue3Piece lockFocus]; |
---|
98 | [[NSColor colorWithCalibratedRed: 0.506 green: 0.745 blue: 1.0 alpha: 1.0] set]; |
---|
99 | [bp fill]; |
---|
100 | [fBlue3Piece unlockFocus]; |
---|
101 | |
---|
102 | //actually draw the box |
---|
103 | [self setTorrent: nil]; |
---|
104 | } |
---|
105 | |
---|
106 | - (void) dealloc |
---|
107 | { |
---|
108 | free(fPieces); |
---|
109 | |
---|
110 | if (fTorrent) |
---|
111 | [fTorrent release]; |
---|
112 | [super dealloc]; |
---|
113 | } |
---|
114 | |
---|
115 | - (void) setTorrent: (Torrent *) torrent |
---|
116 | { |
---|
117 | if (fTorrent) |
---|
118 | { |
---|
119 | [fTorrent release]; |
---|
120 | |
---|
121 | if (!torrent) |
---|
122 | fTorrent = nil; |
---|
123 | } |
---|
124 | |
---|
125 | if (torrent) |
---|
126 | { |
---|
127 | fTorrent = [torrent retain]; |
---|
128 | |
---|
129 | //determine relevant values |
---|
130 | fNumPieces = MAX_ACROSS * MAX_ACROSS; |
---|
131 | if ([fTorrent pieceCount] < fNumPieces) |
---|
132 | { |
---|
133 | fNumPieces = [fTorrent pieceCount]; |
---|
134 | |
---|
135 | fAcross = sqrt(fNumPieces); |
---|
136 | if (fAcross * fAcross < fNumPieces) |
---|
137 | fAcross++; |
---|
138 | } |
---|
139 | else |
---|
140 | fAcross = MAX_ACROSS; |
---|
141 | |
---|
142 | fWidth = ([[fImageView image] size].width - (fAcross + 1) * BETWEEN) / fAcross; |
---|
143 | fExtraBorder = ([[fImageView image] size].width - ((fWidth + BETWEEN) * fAcross + BETWEEN)) / 2; |
---|
144 | |
---|
145 | [self updateView: YES]; |
---|
146 | } |
---|
147 | |
---|
148 | [fImageView setHidden: torrent == nil]; |
---|
149 | } |
---|
150 | |
---|
151 | - (void) updateView: (BOOL) first |
---|
152 | { |
---|
153 | if (!fTorrent) |
---|
154 | return; |
---|
155 | |
---|
156 | if (first) |
---|
157 | [fImageView setImage: [[fBack copy] autorelease]]; |
---|
158 | |
---|
159 | NSImage * image = [fImageView image]; |
---|
160 | |
---|
161 | int8_t * pieces = malloc(fNumPieces); |
---|
162 | [fTorrent getAvailability: pieces size: fNumPieces]; |
---|
163 | |
---|
164 | int i, j, piece, index = -1; |
---|
165 | NSPoint point; |
---|
166 | NSRect rect = NSMakeRect(0, 0, fWidth, fWidth); |
---|
167 | NSImage * pieceImage; |
---|
168 | BOOL change = NO; |
---|
169 | |
---|
170 | for (i = 0; i < fAcross; i++) |
---|
171 | for (j = 0; j < fAcross; j++) |
---|
172 | { |
---|
173 | index++; |
---|
174 | if (index >= fNumPieces) |
---|
175 | break; |
---|
176 | |
---|
177 | pieceImage = nil; |
---|
178 | |
---|
179 | piece = pieces[index]; |
---|
180 | if (piece < 0) |
---|
181 | { |
---|
182 | if (first || fPieces[index] != -1) |
---|
183 | { |
---|
184 | fPieces[index] = -1; |
---|
185 | pieceImage = fGreenPiece; |
---|
186 | } |
---|
187 | } |
---|
188 | else if (piece == 0) |
---|
189 | { |
---|
190 | if (first || fPieces[index] != 0) |
---|
191 | { |
---|
192 | fPieces[index] = 0; |
---|
193 | pieceImage = fWhitePiece; |
---|
194 | } |
---|
195 | } |
---|
196 | else if (piece == 1) |
---|
197 | { |
---|
198 | if (first || fPieces[index] != 1) |
---|
199 | { |
---|
200 | fPieces[index] = 1; |
---|
201 | pieceImage = fBlue1Piece; |
---|
202 | } |
---|
203 | } |
---|
204 | else if (piece == 2) |
---|
205 | { |
---|
206 | if (first || fPieces[index] != 2) |
---|
207 | { |
---|
208 | fPieces[index] = 2; |
---|
209 | pieceImage = fBlue2Piece; |
---|
210 | } |
---|
211 | } |
---|
212 | else |
---|
213 | { |
---|
214 | if (first || fPieces[index] != 3) |
---|
215 | { |
---|
216 | fPieces[index] = 3; |
---|
217 | pieceImage = fBlue3Piece; |
---|
218 | } |
---|
219 | } |
---|
220 | |
---|
221 | if (pieceImage) |
---|
222 | { |
---|
223 | //drawing actually will occur, so figure out values |
---|
224 | if (!change) |
---|
225 | { |
---|
226 | [image lockFocus]; |
---|
227 | change = YES; |
---|
228 | } |
---|
229 | |
---|
230 | point = NSMakePoint(j * (fWidth + BETWEEN) + BETWEEN + fExtraBorder, |
---|
231 | [[fImageView image] size].width - (i + 1) * (fWidth + BETWEEN) - fExtraBorder); |
---|
232 | [pieceImage compositeToPoint: point fromRect: rect operation: NSCompositeSourceOver]; |
---|
233 | } |
---|
234 | } |
---|
235 | |
---|
236 | if (change) |
---|
237 | { |
---|
238 | [image unlockFocus]; |
---|
239 | [fImageView setNeedsDisplay]; |
---|
240 | } |
---|
241 | |
---|
242 | free(pieces); |
---|
243 | } |
---|
244 | |
---|
245 | @end |
---|