source: trunk/macosx/PiecesView.m @ 6126

Last change on this file since 6126 was 6126, checked in by livings124, 14 years ago

some cleanup of the pieces view code

  • Property Id set to /Users/mitchell/Desktop/Transmission/macosx/ActionMenuRatioToDisplayRatioTransformer.h
  • Property svn:keywords set to Date Rev Author Id
File size: 6.0 KB
Line 
1/******************************************************************************
2 * $Id: PiecesView.m 6126 2008-06-10 20:15:00Z livings124 $
3 *
4 * Copyright (c) 2006-2008 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#import "CTGradient.h"
28
29#define MAX_ACROSS 18
30#define BETWEEN 1.0
31
32#define HIGH_PEERS 15
33
34#define FINISHED 1
35
36@implementation PiecesView
37
38- (void) awakeFromNib
39{
40        //back image
41        fBack = [[NSImage alloc] initWithSize: [self bounds].size];
42       
43        [fBack lockFocus];
44        CTGradient * gradient = [CTGradient gradientWithBeginningColor: [NSColor colorWithCalibratedWhite: 0.0 alpha: 0.4]
45                                    endingColor: [NSColor colorWithCalibratedWhite: 0.2 alpha: 0.4]];
46        [gradient fillRect: [self bounds] angle: 90.0];
47        [fBack unlockFocus];
48       
49        //store box colors
50        fGreenAvailabilityColor = [[NSColor colorWithCalibratedRed: 0.0 green: 1.0 blue: 0.4 alpha: 1.0] retain];
51        fBluePieceColor = [[NSColor colorWithCalibratedRed: 0.0 green: 0.4 blue: 0.8 alpha: 1.0] retain];
52               
53        //actually draw the box
54        [self setTorrent: nil];
55}
56
57- (void) dealloc
58{
59    tr_free(fPieces);
60   
61    [fBack release];
62   
63    [fGreenAvailabilityColor release];
64    [fBluePieceColor release];
65   
66    [super dealloc];
67}
68
69- (void) setTorrent: (Torrent *) torrent
70{
71    [self clearView];
72   
73    fTorrent = torrent;
74    if (fTorrent)
75    {
76        //determine relevant values
77        fNumPieces = MIN([fTorrent pieceCount], MAX_ACROSS * MAX_ACROSS);
78        fAcross = ceil(sqrt(fNumPieces));
79       
80        float width = [self bounds].size.width;
81        fWidth = (width - (fAcross + 1) * BETWEEN) / fAcross;
82        fExtraBorder = (width - ((fWidth + BETWEEN) * fAcross + BETWEEN)) / 2;
83    }
84   
85    //reset the view to blank
86    NSImage * newBack = [fBack copy];
87    [self setImage: newBack];
88    [newBack release];
89   
90    [self setNeedsDisplay];
91}
92
93- (void) clearView
94{
95    tr_free(fPieces);
96    fPieces = NULL;
97}
98
99- (void) updateView
100{
101    if (!fTorrent)
102        return;
103   
104    //determine if first time
105    BOOL first = NO;
106    if (!fPieces)
107    {
108        fPieces = (int8_t *)tr_malloc(fNumPieces * sizeof(int8_t));
109        first = YES;
110    }
111
112    int8_t * pieces = NULL;
113    float * piecesPercent = NULL;
114   
115    BOOL showAvailablity = [[NSUserDefaults standardUserDefaults] boolForKey: @"PiecesViewShowAvailability"];
116    if (showAvailablity)
117    {   
118        pieces = (int8_t *)tr_malloc(fNumPieces * sizeof(int8_t));
119        [fTorrent getAvailability: pieces size: fNumPieces];
120    }
121    else
122    {   
123        piecesPercent = (float *)tr_malloc(fNumPieces * sizeof(float));
124        [fTorrent getAmountFinished: piecesPercent size: fNumPieces];
125    }
126   
127    int i, j, index = -1;
128    NSRect rect = NSMakeRect(0, 0, fWidth, fWidth);
129   
130    NSImage * image = [self image];
131    [image lockFocus];
132   
133    for (i = 0; i < fAcross; i++)
134        for (j = 0; j < fAcross; j++)
135        {
136            index++;
137            if (index >= fNumPieces)
138            {
139                i = fAcross;
140                break;
141            }
142           
143            NSColor * pieceColor;
144            if (showAvailablity)
145            {
146                if (pieces[index] == -1)
147                {
148                    pieceColor = !first && fPieces[index] != FINISHED ? [NSColor orangeColor] : fBluePieceColor;
149                    fPieces[index] = FINISHED;
150                }
151                else
152                {
153                    float percent = MIN(1.0, (float)pieces[index]/HIGH_PEERS);
154                    pieceColor = [[NSColor whiteColor] blendedColorWithFraction: percent ofColor: fGreenAvailabilityColor];
155                    fPieces[index] = 0;
156                }
157            }
158            else
159            {
160                if (piecesPercent[index] == 1.0)
161                {
162                    pieceColor = !first && fPieces[index] != FINISHED ? [NSColor orangeColor] : fBluePieceColor;
163                    fPieces[index] = FINISHED;
164                }
165                else
166                {
167                    pieceColor = [[NSColor whiteColor] blendedColorWithFraction: piecesPercent[index] ofColor: fBluePieceColor];
168                    fPieces[index] = 0;
169                }
170            }
171           
172            rect.origin = NSMakePoint(j * (fWidth + BETWEEN) + BETWEEN + fExtraBorder,
173                                    [image size].width - (i + 1) * (fWidth + BETWEEN) - fExtraBorder);
174           
175            [pieceColor set];
176            NSRectFill(rect);
177        }
178   
179    [image unlockFocus];
180    [self setNeedsDisplay];
181   
182    tr_free(pieces);
183    tr_free(piecesPercent);
184}
185
186- (BOOL) acceptsFirstMouse: (NSEvent *) event
187{
188    return YES;
189}
190
191- (void) mouseDown: (NSEvent *) event
192{
193    if (fTorrent)
194        [[[self window] windowController] setPiecesViewForAvailable:
195            ![[NSUserDefaults standardUserDefaults] boolForKey: @"PiecesViewShowAvailability"]];
196    [super mouseDown: event];
197}
198
199@end
Note: See TracBrowser for help on using the repository browser.