source: trunk/macosx/PiecesView.m @ 6127

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

remember the previous color of the pieces view as much as possible to avoid excessive drawing

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