2006-09-28 23:12:14 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
2007-04-03 02:22:25 +00:00
|
|
|
* Copyright (c) 2006-2007 Transmission authors and contributors
|
2006-09-28 23:12:14 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*****************************************************************************/
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2006-09-25 20:53:10 +00:00
|
|
|
#import "PiecesView.h"
|
2007-06-21 23:21:19 +00:00
|
|
|
#import "InfoWindowController.h"
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2006-09-25 20:53:10 +00:00
|
|
|
#define MAX_ACROSS 18
|
2006-09-25 18:37:45 +00:00
|
|
|
#define BETWEEN 1.0
|
|
|
|
|
2006-09-25 20:53:10 +00:00
|
|
|
@implementation PiecesView
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2007-06-20 22:53:46 +00:00
|
|
|
- (id) initWithCoder: (NSCoder *) decoder
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2007-06-20 22:53:46 +00:00
|
|
|
if ((self = [super initWithCoder: decoder]))
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
|
|
|
fTorrent = nil;
|
|
|
|
int numPieces = MAX_ACROSS * MAX_ACROSS;
|
|
|
|
fPieces = malloc(numPieces);
|
2006-09-27 20:34:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) awakeFromNib
|
|
|
|
{
|
2007-06-20 22:53:46 +00:00
|
|
|
NSSize size = [self bounds].size;
|
|
|
|
NSBezierPath * bp = [NSBezierPath bezierPathWithRect: [self bounds]];
|
2006-09-27 20:34:17 +00:00
|
|
|
|
|
|
|
//back image
|
|
|
|
fBack = [[NSImage alloc] initWithSize: size];
|
|
|
|
|
|
|
|
[fBack lockFocus];
|
2007-06-21 00:12:03 +00:00
|
|
|
[[NSColor colorWithCalibratedWhite: 0.0 alpha: 0.4] set];
|
2006-09-27 20:34:17 +00:00
|
|
|
[bp fill];
|
|
|
|
[fBack unlockFocus];
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2007-08-16 02:11:08 +00:00
|
|
|
//store box colors
|
|
|
|
fWhiteColor = [[NSColor whiteColor] retain];
|
|
|
|
fOrangeColor = [[NSColor orangeColor] retain];
|
|
|
|
fGreen1Color = [[NSColor colorWithCalibratedRed: 0.6 green: 1.0 blue: 0.8 alpha: 1.0] retain];
|
|
|
|
fGreen2Color = [[NSColor colorWithCalibratedRed: 0.4 green: 1.0 blue: 0.6 alpha: 1.0] retain];
|
|
|
|
fGreen3Color = [[NSColor colorWithCalibratedRed: 0.0 green: 1.0 blue: 0.4 alpha: 1.0] retain];
|
|
|
|
fBlue1Color = [[NSColor colorWithCalibratedRed: 0.8 green: 1.0 blue: 1.0 alpha: 1.0] retain];
|
|
|
|
fBlue2Color = [[NSColor colorWithCalibratedRed: 0.6 green: 1.0 blue: 1.0 alpha: 1.0] retain];
|
|
|
|
fBlue3Color = [[NSColor colorWithCalibratedRed: 0.6 green: 0.8 blue: 1.0 alpha: 1.0] retain];
|
|
|
|
fBlue4Color = [[NSColor colorWithCalibratedRed: 0.4 green: 0.6 blue: 1.0 alpha: 1.0] retain];
|
|
|
|
fBlueColor = [[NSColor colorWithCalibratedRed: 0.0 green: 0.4 blue: 0.8 alpha: 1.0] retain];
|
2006-10-13 00:19:00 +00:00
|
|
|
|
2006-09-27 20:34:17 +00:00
|
|
|
//actually draw the box
|
2006-09-27 19:16:34 +00:00
|
|
|
[self setTorrent: nil];
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
free(fPieces);
|
|
|
|
|
2006-10-01 15:14:12 +00:00
|
|
|
[fBack release];
|
2007-08-16 02:11:08 +00:00
|
|
|
|
|
|
|
[fWhiteColor release];
|
|
|
|
[fOrangeColor release];
|
|
|
|
[fGreen1Color release];
|
|
|
|
[fGreen2Color release];
|
|
|
|
[fGreen3Color release];
|
|
|
|
[fBlue1Color release];
|
|
|
|
[fBlue2Color release];
|
|
|
|
[fBlue3Color release];
|
|
|
|
[fBlue4Color release];
|
|
|
|
[fBlueColor release];
|
2006-10-01 15:14:12 +00:00
|
|
|
|
2007-08-14 05:21:02 +00:00
|
|
|
[fTorrent release];
|
2006-09-25 18:37:45 +00:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setTorrent: (Torrent *) torrent
|
|
|
|
{
|
2007-08-14 05:21:02 +00:00
|
|
|
[fTorrent release];
|
2006-09-25 18:37:45 +00:00
|
|
|
|
|
|
|
if (torrent)
|
|
|
|
{
|
|
|
|
fTorrent = [torrent retain];
|
|
|
|
|
|
|
|
//determine relevant values
|
|
|
|
fNumPieces = MAX_ACROSS * MAX_ACROSS;
|
2006-10-01 16:12:46 +00:00
|
|
|
int pieceCount = [fTorrent pieceCount];
|
|
|
|
if (pieceCount < fNumPieces)
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2006-10-01 16:12:46 +00:00
|
|
|
fNumPieces = pieceCount;
|
2006-09-25 18:37:45 +00:00
|
|
|
|
|
|
|
fAcross = sqrt(fNumPieces);
|
|
|
|
if (fAcross * fAcross < fNumPieces)
|
|
|
|
fAcross++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
fAcross = MAX_ACROSS;
|
|
|
|
|
2007-06-20 22:53:46 +00:00
|
|
|
float width = [self bounds].size.width;
|
2006-10-04 20:07:36 +00:00
|
|
|
fWidth = (width - (fAcross + 1) * BETWEEN) / fAcross;
|
|
|
|
fExtraBorder = (width - ((fWidth + BETWEEN) * fAcross + BETWEEN)) / 2;
|
2006-09-25 18:37:45 +00:00
|
|
|
|
|
|
|
[self updateView: YES];
|
|
|
|
}
|
2007-06-20 23:53:09 +00:00
|
|
|
else
|
|
|
|
{
|
2007-08-14 05:21:02 +00:00
|
|
|
fTorrent = nil;
|
|
|
|
|
|
|
|
NSImage * newBack = [fBack copy];
|
|
|
|
[self setImage: newBack];
|
|
|
|
[newBack release];
|
|
|
|
|
2007-06-20 23:53:09 +00:00
|
|
|
[self setNeedsDisplay];
|
|
|
|
}
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) updateView: (BOOL) first
|
|
|
|
{
|
|
|
|
if (!fTorrent)
|
|
|
|
return;
|
|
|
|
|
2006-10-04 19:34:04 +00:00
|
|
|
if (first)
|
2007-08-14 05:21:02 +00:00
|
|
|
{
|
|
|
|
NSImage * newBack = [fBack copy];
|
|
|
|
[self setImage: newBack];
|
|
|
|
[newBack release];
|
|
|
|
}
|
2007-06-20 22:53:46 +00:00
|
|
|
NSImage * image = [self image];
|
2006-10-13 00:19:00 +00:00
|
|
|
|
2006-10-13 01:34:15 +00:00
|
|
|
int8_t * pieces;
|
|
|
|
float * piecesPercent;
|
2006-10-13 00:19:00 +00:00
|
|
|
|
|
|
|
BOOL showAvailablity = [[NSUserDefaults standardUserDefaults] boolForKey: @"PiecesViewShowAvailability"];
|
|
|
|
if (showAvailablity)
|
2006-10-13 01:34:15 +00:00
|
|
|
{
|
|
|
|
pieces = malloc(fNumPieces);
|
2006-10-13 00:19:00 +00:00
|
|
|
[fTorrent getAvailability: pieces size: fNumPieces];
|
2006-10-13 01:34:15 +00:00
|
|
|
}
|
2006-10-13 00:19:00 +00:00
|
|
|
else
|
2006-10-13 01:34:15 +00:00
|
|
|
{
|
|
|
|
piecesPercent = malloc(fNumPieces * sizeof(float));
|
|
|
|
[fTorrent getAmountFinished: piecesPercent size: fNumPieces];
|
|
|
|
}
|
2006-09-25 18:37:45 +00:00
|
|
|
|
|
|
|
int i, j, piece, index = -1;
|
2006-10-13 01:34:15 +00:00
|
|
|
float piecePercent;
|
2006-09-25 18:37:45 +00:00
|
|
|
NSPoint point;
|
|
|
|
NSRect rect = NSMakeRect(0, 0, fWidth, fWidth);
|
2007-08-16 02:11:08 +00:00
|
|
|
NSColor * pieceColor;
|
2006-09-25 18:37:45 +00:00
|
|
|
BOOL change = NO;
|
|
|
|
|
|
|
|
for (i = 0; i < fAcross; i++)
|
|
|
|
for (j = 0; j < fAcross; j++)
|
|
|
|
{
|
|
|
|
index++;
|
|
|
|
if (index >= fNumPieces)
|
2006-10-01 16:12:46 +00:00
|
|
|
{
|
|
|
|
i = fAcross;
|
2006-09-25 18:37:45 +00:00
|
|
|
break;
|
2006-10-01 16:12:46 +00:00
|
|
|
}
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2007-08-16 02:11:08 +00:00
|
|
|
pieceColor = nil;
|
2006-10-13 00:19:00 +00:00
|
|
|
|
|
|
|
if (showAvailablity)
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2006-10-13 01:34:15 +00:00
|
|
|
piece = pieces[index];
|
2006-10-13 00:19:00 +00:00
|
|
|
if (piece < 0)
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2006-10-13 00:19:00 +00:00
|
|
|
if (first || fPieces[index] == -2)
|
|
|
|
{
|
|
|
|
fPieces[index] = -1;
|
2007-08-16 02:11:08 +00:00
|
|
|
pieceColor = fBlueColor;
|
2006-10-13 00:19:00 +00:00
|
|
|
}
|
|
|
|
else if (fPieces[index] != -1)
|
|
|
|
{
|
|
|
|
fPieces[index] = -2;
|
2007-08-16 02:11:08 +00:00
|
|
|
pieceColor = fOrangeColor;
|
2006-10-13 00:19:00 +00:00
|
|
|
}
|
|
|
|
else;
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
2006-10-13 00:19:00 +00:00
|
|
|
else if (piece == 0)
|
2006-10-06 20:10:15 +00:00
|
|
|
{
|
2006-10-13 00:19:00 +00:00
|
|
|
if (first || fPieces[index] != 0)
|
|
|
|
{
|
|
|
|
fPieces[index] = 0;
|
2007-08-16 02:11:08 +00:00
|
|
|
pieceColor = fWhiteColor;
|
2006-10-13 00:19:00 +00:00
|
|
|
}
|
2006-10-06 20:10:15 +00:00
|
|
|
}
|
2007-05-07 17:27:27 +00:00
|
|
|
else if (piece <= 4)
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2006-10-13 00:19:00 +00:00
|
|
|
if (first || fPieces[index] != 1)
|
|
|
|
{
|
|
|
|
fPieces[index] = 1;
|
2007-08-16 02:11:08 +00:00
|
|
|
pieceColor = fGreen1Color;
|
2006-10-13 00:19:00 +00:00
|
|
|
}
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
2007-05-07 17:27:27 +00:00
|
|
|
else if (piece <= 8)
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2006-10-13 00:19:00 +00:00
|
|
|
if (first || fPieces[index] != 2)
|
|
|
|
{
|
|
|
|
fPieces[index] = 2;
|
2007-08-16 02:11:08 +00:00
|
|
|
pieceColor = fGreen2Color;
|
2006-10-13 00:19:00 +00:00
|
|
|
}
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
2006-10-13 00:19:00 +00:00
|
|
|
else
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2006-10-13 00:19:00 +00:00
|
|
|
if (first || fPieces[index] != 3)
|
|
|
|
{
|
|
|
|
fPieces[index] = 3;
|
2007-08-16 02:11:08 +00:00
|
|
|
pieceColor = fGreen3Color;
|
2006-10-13 00:19:00 +00:00
|
|
|
}
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-10-13 01:34:15 +00:00
|
|
|
piecePercent = piecesPercent[index];
|
2006-10-20 15:01:18 +00:00
|
|
|
if (piecePercent >= 1.0)
|
|
|
|
{
|
|
|
|
if (first || fPieces[index] == -2)
|
|
|
|
{
|
|
|
|
fPieces[index] = -1;
|
2007-08-16 02:11:08 +00:00
|
|
|
pieceColor = fBlueColor;
|
2006-10-20 15:01:18 +00:00
|
|
|
}
|
|
|
|
else if (fPieces[index] != -1)
|
|
|
|
{
|
|
|
|
fPieces[index] = -2;
|
2007-08-16 02:11:08 +00:00
|
|
|
pieceColor = fOrangeColor;
|
2006-10-20 15:01:18 +00:00
|
|
|
}
|
|
|
|
else;
|
|
|
|
}
|
|
|
|
else if (piecePercent <= 0.0)
|
2006-10-13 00:19:00 +00:00
|
|
|
{
|
|
|
|
if (first || fPieces[index] != 0)
|
|
|
|
{
|
|
|
|
fPieces[index] = 0;
|
2007-08-16 02:11:08 +00:00
|
|
|
pieceColor = fWhiteColor;
|
2006-10-13 00:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
2006-10-13 01:34:15 +00:00
|
|
|
else if (piecePercent < 0.25)
|
2006-10-13 00:19:00 +00:00
|
|
|
{
|
|
|
|
if (first || fPieces[index] != 1)
|
|
|
|
{
|
|
|
|
fPieces[index] = 1;
|
2007-08-16 02:11:08 +00:00
|
|
|
pieceColor = fBlue1Color;
|
2006-10-13 00:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
2007-04-02 21:28:09 +00:00
|
|
|
else if (piecePercent < 0.5)
|
2006-10-13 00:19:00 +00:00
|
|
|
{
|
|
|
|
if (first || fPieces[index] != 2)
|
|
|
|
{
|
|
|
|
fPieces[index] = 2;
|
2007-08-16 02:11:08 +00:00
|
|
|
pieceColor = fBlue2Color;
|
2006-10-13 00:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
2006-10-13 01:34:15 +00:00
|
|
|
else if (piecePercent < 0.75)
|
2006-10-13 00:19:00 +00:00
|
|
|
{
|
|
|
|
if (first || fPieces[index] != 3)
|
|
|
|
{
|
|
|
|
fPieces[index] = 3;
|
2007-08-16 02:11:08 +00:00
|
|
|
pieceColor = fBlue3Color;
|
2006-10-13 00:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
2006-10-20 15:01:18 +00:00
|
|
|
else
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2006-10-13 00:19:00 +00:00
|
|
|
if (first || fPieces[index] != 4)
|
|
|
|
{
|
|
|
|
fPieces[index] = 4;
|
2007-08-16 02:11:08 +00:00
|
|
|
pieceColor = fBlue4Color;
|
2006-10-13 00:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
2007-08-16 02:11:08 +00:00
|
|
|
if (pieceColor)
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2007-08-16 02:11:08 +00:00
|
|
|
//drawing actually will occur
|
2006-09-25 18:37:45 +00:00
|
|
|
if (!change)
|
|
|
|
{
|
2006-09-25 21:20:08 +00:00
|
|
|
[image lockFocus];
|
2006-09-25 18:37:45 +00:00
|
|
|
change = YES;
|
|
|
|
}
|
|
|
|
|
2007-08-16 02:11:08 +00:00
|
|
|
[pieceColor set];
|
|
|
|
|
|
|
|
rect.origin = NSMakePoint(j * (fWidth + BETWEEN) + BETWEEN + fExtraBorder,
|
2006-10-04 19:53:26 +00:00
|
|
|
[image size].width - (i + 1) * (fWidth + BETWEEN) - fExtraBorder);
|
2007-08-16 02:11:08 +00:00
|
|
|
NSRectFill(rect);
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (change)
|
|
|
|
{
|
2006-09-25 21:20:08 +00:00
|
|
|
[image unlockFocus];
|
2007-06-20 22:53:46 +00:00
|
|
|
[self setNeedsDisplay];
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
2007-08-16 03:58:50 +00:00
|
|
|
if (showAvailablity)
|
|
|
|
free(pieces);
|
|
|
|
else
|
|
|
|
free(piecesPercent);
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
2007-06-21 23:21:19 +00:00
|
|
|
- (BOOL) acceptsFirstMouse: (NSEvent *) event
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) mouseDown: (NSEvent *) event
|
|
|
|
{
|
|
|
|
if (fTorrent)
|
|
|
|
[[[self window] windowController] setPiecesViewForAvailable:
|
|
|
|
![[NSUserDefaults standardUserDefaults] boolForKey: @"PiecesViewShowAvailability"]];
|
|
|
|
[super mouseDown: event];
|
|
|
|
}
|
|
|
|
|
2006-09-25 18:37:45 +00:00
|
|
|
@end
|