Changeset 910


Ignore:
Timestamp:
Sep 25, 2006, 12:54:04 AM (15 years ago)
Author:
livings124
Message:

Make boxes in pieces viewer larger if few enough to make them all fit.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/nat-traversal/PiecesWindowController.m

    r909 r910  
    99#import "PiecesWindowController.h"
    1010
    11 #define HEIGHT 4.0
    12 #define WIDTH 4.0
    13 #define BETWEEN 1.0
    14 #define ACROSS 18
    15 #define DOWN 18
     11#define MIN_WIDTH 4.0
     12#define MAX_ACROSS 18
    1613
    1714#define BLANK -99
     
    2421    {
    2522        fTorrent = nil;
    26         fPieces = malloc(ACROSS * DOWN);
     23        int numPieces = MAX_ACROSS * MAX_ACROSS;
     24        fPieces = malloc(numPieces);
    2725        int i;
    28         for (i = 0; i < DOWN * ACROSS; i++)
     26        for (i = 0; i < numPieces; i++)
    2927            fPieces[i] = BLANK;
    3028       
    3129        fBack = [NSImage imageNamed: @"PiecesBack.tiff"];
     30        NSSize size = [fBack size];
     31       
    3232        fWhitePiece = [NSImage imageNamed: @"BoxWhite.tiff"];
     33        [fWhitePiece setScalesWhenResized: YES];
     34        [fWhitePiece setSize: size];
     35       
    3336        fGreenPiece = [NSImage imageNamed: @"BoxGreen.tiff"];
     37        [fGreenPiece setScalesWhenResized: YES];
     38        [fGreenPiece setSize: size];
     39       
    3440        fBlue1Piece = [NSImage imageNamed: @"BoxBlue1.tiff"];
     41        [fBlue1Piece setScalesWhenResized: YES];
     42        [fBlue1Piece setSize: size];
     43       
    3544        fBlue2Piece = [NSImage imageNamed: @"BoxBlue2.tiff"];
     45        [fBlue2Piece setScalesWhenResized: YES];
     46        [fBlue2Piece setSize: size];
     47       
    3648        fBlue3Piece = [NSImage imageNamed: @"BoxBlue3.tiff"];
     49        [fBlue3Piece setScalesWhenResized: YES];
     50        [fBlue3Piece setSize: size];
    3751       
    3852        fExistingImage = [fBack copy];
     
    94108    }
    95109   
    96     int numPieces = ACROSS * DOWN;
     110    int numPieces = MAX_ACROSS * MAX_ACROSS;
    97111    if (numPieces > [fTorrent pieceCount])
    98112        numPieces = [fTorrent pieceCount];
    99113   
     114    //determine how many boxes
     115    int across = MAX_ACROSS;
     116    float width = MIN_WIDTH, between;
     117    while (numPieces <= (across / 2) * (across / 2))
     118    {
     119        across /= 2;
     120        width *= 2.0;
     121    }
     122    between = ([fExistingImage size].width - (float)across * width) / (float)(across + 1);
     123   
    100124    int8_t * pieces = malloc(numPieces);
    101125    [fTorrent getAvailability: pieces size: numPieces];
     
    103127    int i, j, piece, index = -1;
    104128    NSPoint point;
     129    NSRect rect = NSMakeRect(0, 0, width, width);
    105130    NSImage * pieceImage;
    106131    BOOL change = NO;
    107132   
    108     for (i = 0; i < DOWN; i++)
    109         for (j = 0; j < ACROSS; j++)
     133    for (i = 0; i < across; i++)
     134        for (j = 0; j < across; j++)
    110135        {
    111136            pieceImage = nil;
     
    164189                    change = YES;
    165190                }
    166                 point = NSMakePoint(j * (WIDTH + BETWEEN) + BETWEEN, (DOWN - i - 1) * (HEIGHT + BETWEEN) + BETWEEN);
    167                 [pieceImage compositeToPoint: point operation: NSCompositeSourceOver];
     191               
     192                point = NSMakePoint((float)j * (width + between) + between, (float)(across - i) * (width + between) - width);
     193                [pieceImage compositeToPoint: point fromRect: rect operation: NSCompositeSourceOver];
    168194            }
    169195        }
    170196   
    171197    if (change)
     198    {
    172199        [fExistingImage unlockFocus];
    173    
    174     //reload the image if changes were made or the torrent was loaded after it was blank
    175     if (change || first)
    176     {
     200       
    177201        [fImageView setImage: nil];
    178202        [fImageView setImage: fExistingImage];
Note: See TracChangeset for help on using the changeset viewer.