2006-09-28 23:12:14 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (c) 2005-2006 Transmission authors and contributors
|
|
|
|
*
|
|
|
|
* 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"
|
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
|
|
|
|
2006-09-25 20:53:10 +00:00
|
|
|
- (id) init
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2006-09-25 20:53:10 +00:00
|
|
|
if ((self = [super init]))
|
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
|
|
|
|
{
|
2006-10-04 19:53:26 +00:00
|
|
|
NSSize size = [fImageView bounds].size;
|
2006-09-27 20:34:17 +00:00
|
|
|
NSBezierPath * bp = [NSBezierPath bezierPathWithRect: [fImageView bounds]];
|
|
|
|
|
|
|
|
//back image
|
|
|
|
fBack = [[NSImage alloc] initWithSize: size];
|
|
|
|
|
|
|
|
[fBack lockFocus];
|
|
|
|
[[NSColor blackColor] set];
|
|
|
|
[bp fill];
|
|
|
|
[fBack unlockFocus];
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2006-09-27 20:34:17 +00:00
|
|
|
//white box image
|
|
|
|
fWhitePiece = [[NSImage alloc] initWithSize: size];
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2006-09-27 20:34:17 +00:00
|
|
|
[fWhitePiece lockFocus];
|
|
|
|
[[NSColor whiteColor] set];
|
|
|
|
[bp fill];
|
|
|
|
[fWhitePiece unlockFocus];
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2006-10-06 20:45:59 +00:00
|
|
|
//blue box image
|
|
|
|
fBluePiece = [[NSImage alloc] initWithSize: size];
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2006-10-06 20:45:59 +00:00
|
|
|
[fBluePiece lockFocus];
|
|
|
|
[[NSColor colorWithCalibratedRed: 0.35 green: 0.65 blue: 1.0 alpha: 1.0] set];
|
2006-09-27 20:34:17 +00:00
|
|
|
[bp fill];
|
2006-10-06 20:45:59 +00:00
|
|
|
[fBluePiece unlockFocus];
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2006-10-06 20:45:59 +00:00
|
|
|
//red box image
|
2006-10-06 20:10:15 +00:00
|
|
|
fRedPiece = [[NSImage alloc] initWithSize: size];
|
|
|
|
|
|
|
|
[fRedPiece lockFocus];
|
|
|
|
[[NSColor redColor] set];
|
|
|
|
[bp fill];
|
|
|
|
[fRedPiece unlockFocus];
|
|
|
|
|
2006-10-06 20:45:59 +00:00
|
|
|
//green 1 box image
|
|
|
|
fGreen1Piece = [[NSImage alloc] initWithSize: size];
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2006-10-06 20:45:59 +00:00
|
|
|
[fGreen1Piece lockFocus];
|
|
|
|
[[NSColor colorWithCalibratedRed: 0.6 green: 1.0 blue: 0.8 alpha: 1.0] set];
|
2006-09-27 20:34:17 +00:00
|
|
|
[bp fill];
|
2006-10-06 20:45:59 +00:00
|
|
|
[fGreen1Piece unlockFocus];
|
2006-09-27 19:16:34 +00:00
|
|
|
|
2006-10-06 20:45:59 +00:00
|
|
|
//green 2 box image
|
|
|
|
fGreen2Piece = [[NSImage alloc] initWithSize: size];
|
2006-09-27 20:34:17 +00:00
|
|
|
|
2006-10-06 20:45:59 +00:00
|
|
|
[fGreen2Piece lockFocus];
|
|
|
|
[[NSColor colorWithCalibratedRed: 0.4 green: 1.0 blue: 0.6 alpha: 1.0] set];
|
2006-09-27 20:34:17 +00:00
|
|
|
[bp fill];
|
2006-10-06 20:45:59 +00:00
|
|
|
[fGreen2Piece unlockFocus];
|
2006-09-27 20:34:17 +00:00
|
|
|
|
2006-10-06 20:45:59 +00:00
|
|
|
//green 3 box image
|
|
|
|
fGreen3Piece = [[NSImage alloc] initWithSize: size];
|
2006-09-27 20:34:17 +00:00
|
|
|
|
2006-10-06 20:45:59 +00:00
|
|
|
[fGreen3Piece lockFocus];
|
|
|
|
[[NSColor colorWithCalibratedRed: 0.0 green: 1.0 blue: 0.4 alpha: 1.0] set];
|
2006-09-27 20:34:17 +00:00
|
|
|
[bp fill];
|
2006-10-06 20:45:59 +00:00
|
|
|
[fGreen3Piece unlockFocus];
|
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];
|
|
|
|
[fWhitePiece release];
|
2006-10-06 20:10:15 +00:00
|
|
|
[fRedPiece release];
|
2006-10-06 20:45:59 +00:00
|
|
|
[fBluePiece release];
|
|
|
|
[fGreen1Piece release];
|
|
|
|
[fGreen2Piece release];
|
|
|
|
[fGreen3Piece release];
|
2006-10-01 15:14:12 +00:00
|
|
|
|
2006-09-25 18:37:45 +00:00
|
|
|
if (fTorrent)
|
|
|
|
[fTorrent release];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setTorrent: (Torrent *) torrent
|
|
|
|
{
|
|
|
|
if (fTorrent)
|
|
|
|
{
|
|
|
|
[fTorrent release];
|
|
|
|
|
|
|
|
if (!torrent)
|
|
|
|
fTorrent = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2006-10-04 20:07:36 +00:00
|
|
|
float width = [fImageView bounds].size.width;
|
|
|
|
fWidth = (width - (fAcross + 1) * BETWEEN) / fAcross;
|
|
|
|
fExtraBorder = (width - ((fWidth + BETWEEN) * fAcross + BETWEEN)) / 2;
|
2006-09-25 18:37:45 +00:00
|
|
|
|
|
|
|
[self updateView: YES];
|
|
|
|
}
|
2006-09-27 19:16:34 +00:00
|
|
|
|
|
|
|
[fImageView setHidden: torrent == nil];
|
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)
|
2006-10-04 19:53:26 +00:00
|
|
|
[fImageView setImage: [[fBack copy] autorelease]];
|
2006-10-04 19:34:04 +00:00
|
|
|
NSImage * image = [fImageView image];
|
2006-09-25 18:37:45 +00:00
|
|
|
|
|
|
|
int8_t * pieces = malloc(fNumPieces);
|
|
|
|
[fTorrent getAvailability: pieces size: fNumPieces];
|
|
|
|
|
|
|
|
int i, j, piece, index = -1;
|
|
|
|
NSPoint point;
|
|
|
|
NSRect rect = NSMakeRect(0, 0, fWidth, fWidth);
|
|
|
|
NSImage * pieceImage;
|
|
|
|
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
|
|
|
|
|
|
|
pieceImage = nil;
|
|
|
|
piece = pieces[index];
|
|
|
|
if (piece < 0)
|
|
|
|
{
|
2006-10-06 20:10:15 +00:00
|
|
|
if (first || fPieces[index] == -2)
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
|
|
|
fPieces[index] = -1;
|
2006-10-06 20:45:59 +00:00
|
|
|
pieceImage = fBluePiece;
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
2006-10-06 20:10:15 +00:00
|
|
|
else if (fPieces[index] != -1)
|
|
|
|
{
|
|
|
|
fPieces[index] = -2;
|
|
|
|
pieceImage = fRedPiece;
|
|
|
|
}
|
|
|
|
else;
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
else if (piece == 0)
|
|
|
|
{
|
|
|
|
if (first || fPieces[index] != 0)
|
|
|
|
{
|
|
|
|
fPieces[index] = 0;
|
|
|
|
pieceImage = fWhitePiece;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (piece == 1)
|
|
|
|
{
|
|
|
|
if (first || fPieces[index] != 1)
|
|
|
|
{
|
|
|
|
fPieces[index] = 1;
|
2006-10-06 20:45:59 +00:00
|
|
|
pieceImage = fGreen1Piece;
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (piece == 2)
|
|
|
|
{
|
|
|
|
if (first || fPieces[index] != 2)
|
|
|
|
{
|
|
|
|
fPieces[index] = 2;
|
2006-10-06 20:45:59 +00:00
|
|
|
pieceImage = fGreen2Piece;
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (first || fPieces[index] != 3)
|
|
|
|
{
|
|
|
|
fPieces[index] = 3;
|
2006-10-06 20:45:59 +00:00
|
|
|
pieceImage = fGreen3Piece;
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pieceImage)
|
|
|
|
{
|
|
|
|
//drawing actually will occur, so figure out values
|
|
|
|
if (!change)
|
|
|
|
{
|
2006-09-25 21:20:08 +00:00
|
|
|
[image lockFocus];
|
2006-09-25 18:37:45 +00:00
|
|
|
change = YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
point = NSMakePoint(j * (fWidth + BETWEEN) + BETWEEN + fExtraBorder,
|
2006-10-04 19:53:26 +00:00
|
|
|
[image size].width - (i + 1) * (fWidth + BETWEEN) - fExtraBorder);
|
2006-09-25 18:37:45 +00:00
|
|
|
[pieceImage compositeToPoint: point fromRect: rect operation: NSCompositeSourceOver];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (change)
|
|
|
|
{
|
2006-09-25 21:20:08 +00:00
|
|
|
[image unlockFocus];
|
2006-10-04 19:34:04 +00:00
|
|
|
[fImageView setNeedsDisplay];
|
2006-10-12 23:34:20 +00:00
|
|
|
|
|
|
|
/*int8_t * piecesC = malloc(fNumPieces);
|
|
|
|
[fTorrent getAmountFinished: piecesC size: fNumPieces];
|
|
|
|
for (i = 0; i < fNumPieces; i++)
|
|
|
|
NSLog(@"%d", piecesC[i]);
|
|
|
|
free(piecesC);*/
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
free(pieces);
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|