source: trunk/macosx/PiecesView.m @ 3464

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

trivial code cleanup

  • Property Id set to /Users/mitchell/Desktop/Transmission/macosx/ActionMenuRatioToDisplayRatioTransformer.h
  • Property svn:keywords set to Date Rev Author Id
File size: 9.9 KB
Line 
1/******************************************************************************
2 * $Id: PiecesView.m 3464 2007-10-19 01:24:42Z livings124 $
3 *
4 * Copyright (c) 2006-2007 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#import "InfoWindowController.h"
27
28#define MAX_ACROSS 18
29#define BETWEEN 1.0
30
31@implementation PiecesView
32
33- (id) initWithCoder: (NSCoder *) decoder
34{
35    if ((self = [super initWithCoder: decoder]))
36    {
37        fPieces = malloc(MAX_ACROSS * MAX_ACROSS);
38    }
39   
40    return self;
41}
42
43- (void) awakeFromNib
44{
45        NSBezierPath * bp = [NSBezierPath bezierPathWithRect: [self bounds]];
46       
47        //back image
48        fBack = [[NSImage alloc] initWithSize: [self bounds].size];
49       
50        [fBack lockFocus];
51        [[NSColor colorWithCalibratedWhite: 0.0 alpha: 0.4] set];
52        [bp fill];
53        [fBack unlockFocus];
54       
55        //store box colors
56        fWhiteColor = [[NSColor whiteColor] retain];
57        fOrangeColor = [[NSColor orangeColor] retain];
58        fGreen1Color = [[NSColor colorWithCalibratedRed: 0.6 green: 1.0 blue: 0.8 alpha: 1.0] retain];
59        fGreen2Color = [[NSColor colorWithCalibratedRed: 0.4 green: 1.0 blue: 0.6 alpha: 1.0] retain];
60        fGreen3Color = [[NSColor colorWithCalibratedRed: 0.0 green: 1.0 blue: 0.4 alpha: 1.0] retain];
61        fBlue1Color = [[NSColor colorWithCalibratedRed: 0.8 green: 1.0 blue: 1.0 alpha: 1.0] retain];
62        fBlue2Color = [[NSColor colorWithCalibratedRed: 0.6 green: 1.0 blue: 1.0 alpha: 1.0] retain];
63        fBlue3Color = [[NSColor colorWithCalibratedRed: 0.6 green: 0.8 blue: 1.0 alpha: 1.0] retain];
64        fBlue4Color = [[NSColor colorWithCalibratedRed: 0.4 green: 0.6 blue: 1.0 alpha: 1.0] retain];
65        fBlueColor = [[NSColor colorWithCalibratedRed: 0.0 green: 0.4 blue: 0.8 alpha: 1.0] retain];
66       
67        //actually draw the box
68        [self setTorrent: nil];
69}
70
71- (void) dealloc
72{
73    free(fPieces);
74   
75    [fBack release];
76   
77    [fWhiteColor release];
78    [fOrangeColor release];
79    [fGreen1Color release];
80    [fGreen2Color release];
81    [fGreen3Color release];
82    [fBlue1Color release];
83    [fBlue2Color release];
84    [fBlue3Color release];
85    [fBlue4Color release];
86    [fBlueColor release];
87   
88    [fTorrent release];
89    [super dealloc];
90}
91
92- (void) setTorrent: (Torrent *) torrent
93{
94    [fTorrent release];
95   
96    if (torrent)
97    {
98        fTorrent = [torrent retain];
99       
100        //determine relevant values
101        fNumPieces = MAX_ACROSS * MAX_ACROSS;
102        int pieceCount = [fTorrent pieceCount];
103        if (pieceCount < fNumPieces)
104        {
105            fNumPieces = pieceCount;
106           
107            fAcross = sqrt(fNumPieces);
108            if (fAcross * fAcross < fNumPieces)
109                fAcross++;
110        }
111        else
112            fAcross = MAX_ACROSS;
113       
114        float width = [self bounds].size.width;
115        fWidth = (width - (fAcross + 1) * BETWEEN) / fAcross;
116        fExtraBorder = (width - ((fWidth + BETWEEN) * fAcross + BETWEEN)) / 2;
117       
118        [self updateView: YES];
119    }
120    else
121    {
122        fTorrent = nil;
123       
124        NSImage * newBack = [fBack copy];
125        [self setImage: newBack];
126        [newBack release];
127       
128        [self setNeedsDisplay];
129    }
130}
131
132- (void) updateView: (BOOL) first
133{
134    if (!fTorrent)
135        return;
136   
137    if (first)
138    {
139        NSImage * newBack = [fBack copy];
140        [self setImage: newBack];
141        [newBack release];
142    }
143    NSImage * image = [self image];
144
145    int8_t * pieces;
146    float * piecesPercent;
147   
148    BOOL showAvailablity = [[NSUserDefaults standardUserDefaults] boolForKey: @"PiecesViewShowAvailability"];
149    if (showAvailablity)
150    {   
151        pieces = malloc(fNumPieces);
152        [fTorrent getAvailability: pieces size: fNumPieces];
153    }
154    else
155    {   
156        piecesPercent = malloc(fNumPieces * sizeof(float));
157        [fTorrent getAmountFinished: piecesPercent size: fNumPieces];
158    }
159   
160    int i, j, piece, index = -1;
161    float piecePercent;
162    NSRect rect = NSMakeRect(0, 0, fWidth, fWidth);
163    NSColor * pieceColor;
164    BOOL change = NO;
165       
166    for (i = 0; i < fAcross; i++)
167        for (j = 0; j < fAcross; j++)
168        {
169            index++;
170            if (index >= fNumPieces)
171            {
172                i = fAcross;
173                break;
174            }
175           
176            pieceColor = nil;
177           
178            if (showAvailablity)
179            {
180                piece = pieces[index];
181                if (piece < 0)
182                {
183                    if (first || fPieces[index] == -2)
184                    {
185                        fPieces[index] = -1;
186                        pieceColor = fBlueColor;
187                    }
188                    else if (fPieces[index] != -1)
189                    {
190                        fPieces[index] = -2;
191                        pieceColor = fOrangeColor;
192                    }
193                    else;
194                }
195                else if (piece == 0)
196                {
197                    if (first || fPieces[index] != 0)
198                    {
199                        fPieces[index] = 0;
200                        pieceColor = fWhiteColor;
201                    }
202                }
203                else if (piece <= 4)
204                {
205                    if (first || fPieces[index] != 1)
206                    {
207                        fPieces[index] = 1;
208                        pieceColor = fGreen1Color;
209                    }
210                }
211                else if (piece <= 8)
212                {
213                    if (first || fPieces[index] != 2)
214                    {
215                        fPieces[index] = 2;
216                        pieceColor = fGreen2Color;
217                    }
218                }
219                else
220                {
221                    if (first || fPieces[index] != 3)
222                    {
223                        fPieces[index] = 3;
224                        pieceColor = fGreen3Color;
225                    }
226                }
227            }
228            else
229            {
230                piecePercent = piecesPercent[index];
231                if (piecePercent >= 1.0)
232                {
233                    if (first || fPieces[index] == -2)
234                    {
235                        fPieces[index] = -1;
236                        pieceColor = fBlueColor;
237                    }
238                    else if (fPieces[index] != -1)
239                    {
240                        fPieces[index] = -2;
241                        pieceColor = fOrangeColor;
242                    }
243                    else;
244                }
245                else if (piecePercent <= 0.0)
246                {
247                    if (first || fPieces[index] != 0)
248                    {
249                        fPieces[index] = 0;
250                        pieceColor = fWhiteColor;
251                    }
252                }
253                else if (piecePercent < 0.25)
254                {
255                    if (first || fPieces[index] != 1)
256                    {
257                        fPieces[index] = 1;
258                        pieceColor = fBlue1Color;
259                    }
260                }
261                else if (piecePercent < 0.5)
262                {
263                    if (first || fPieces[index] != 2)
264                    {
265                        fPieces[index] = 2;
266                        pieceColor = fBlue2Color;
267                    }
268                }
269                else if (piecePercent < 0.75)
270                {
271                    if (first || fPieces[index] != 3)
272                    {
273                        fPieces[index] = 3;
274                        pieceColor = fBlue3Color;
275                    }
276                }
277                else
278                {
279                    if (first || fPieces[index] != 4)
280                    {
281                        fPieces[index] = 4;
282                        pieceColor = fBlue4Color;
283                    }
284                }
285            }
286           
287            if (pieceColor)
288            {
289                //drawing actually will occur
290                if (!change)
291                {
292                    [image lockFocus];
293                    change = YES;
294                }
295               
296                [pieceColor set];
297               
298                rect.origin = NSMakePoint(j * (fWidth + BETWEEN) + BETWEEN + fExtraBorder,
299                                    [image size].width - (i + 1) * (fWidth + BETWEEN) - fExtraBorder);
300                NSRectFill(rect);
301            }
302        }
303   
304    if (change)
305    {
306        [image unlockFocus];
307        [self setNeedsDisplay];
308    }
309   
310    if (showAvailablity)
311        free(pieces);
312    else
313        free(piecesPercent);
314}
315
316- (BOOL) acceptsFirstMouse: (NSEvent *) event
317{
318    return YES;
319}
320
321- (void) mouseDown: (NSEvent *) event
322{
323    if (fTorrent)
324        [[[self window] windowController] setPiecesViewForAvailable:
325            ![[NSUserDefaults standardUserDefaults] boolForKey: @"PiecesViewShowAvailability"]];
326    [super mouseDown: event];
327}
328
329@end
Note: See TracBrowser for help on using the repository browser.