project_files/HedgewarsMobile/Classes/MapPreviewButtonView.m
author koda
Sun, 26 Sep 2010 23:48:03 +0200
changeset 3910 dd47efbdec46
child 4082 bfe14b38dad1
permissions -rw-r--r--
move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
     1
/*
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
     2
 * Hedgewars-iOS, a Hedgewars port for iOS devices
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
     3
 * Copyright (c) 2009-2010 Vittorio Giovara <vittorio.giovara@gmail.com>
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
     4
 *
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
     8
 *
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    12
 * GNU General Public License for more details.
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    13
 *
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    17
 *
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    18
 * File created on 26/09/2010.
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    19
 */
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    20
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    21
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    22
#import "MapPreviewButtonView.h"
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    23
#import "MapConfigViewController.h"
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    24
#import "UIImageExtra.h"
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    25
#import <pthread.h>
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    26
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    27
#define INDICATOR_TAG 7654
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    28
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    29
@implementation MapPreviewButtonView
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    30
@synthesize delegate;
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    31
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    32
-(id) initWithFrame:(CGRect)frame {
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    33
    if ((self = [super initWithFrame:frame])) {
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    34
        delegate = nil;
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    35
        [self setBackgroundImageRounded:[UIImage whiteImage:frame.size] forState:UIControlStateNormal];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    36
    }
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    37
    return self;
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    38
}
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    39
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    40
-(id) initWithCoder:(NSCoder *)aDecoder {
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    41
    if ((self = [super initWithCoder:aDecoder])) {
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    42
        delegate = nil;
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    43
        [self setBackgroundImageRounded:[UIImage whiteImage:self.frame.size] forState:UIControlStateNormal];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    44
    }
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    45
    return self;
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    46
}
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    47
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    48
-(void) dealloc {
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    49
    self.delegate = nil;
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    50
    [super dealloc];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    51
}
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    52
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    53
#pragma mark -
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    54
#pragma mark image wrappers
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    55
-(void) setBackgroundImageRounded:(UIImage *)image forState:(UIControlState)state {
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    56
    [self setBackgroundImage:[image makeRoundCornersOfSize:CGSizeMake(12, 12)] forState:UIControlStateNormal];    
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    57
}
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    58
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    59
-(void) setImageRounded:(UIImage *)image forState:(UIControlState)state {
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    60
    [self setImage:[image makeRoundCornersOfSize:CGSizeMake(12, 12)] forState:UIControlStateNormal];    
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    61
}
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    62
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    63
-(void) setImageRoundedForNormalState:(UIImage *)image {
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    64
    [self setImageRounded:image forState:UIControlStateNormal];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    65
}
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    66
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    67
#pragma mark -
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    68
#pragma mark preview 
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    69
-(int) sendToEngine:(NSString *)string {
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    70
    unsigned char length = [string length];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    71
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    72
    SDLNet_TCP_Send(csd, &length, 1);
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    73
    return SDLNet_TCP_Send(csd, [string UTF8String], length);
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    74
}
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    75
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    76
-(const uint8_t *)engineProtocol {
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    77
    IPaddress ip;
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    78
    BOOL serverQuit = NO;
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    79
    static uint8_t map[128*32];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    80
    int port = randomPort();
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    81
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    82
    if (SDLNet_Init() < 0) {
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    83
        DLog(@"SDLNet_Init: %s", SDLNet_GetError());
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    84
        serverQuit = YES;
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    85
    }
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    86
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    87
    // Resolving the host using NULL make network interface to listen
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    88
    if (SDLNet_ResolveHost(&ip, NULL, port) < 0) {
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    89
        DLog(@"SDLNet_ResolveHost: %s\n", SDLNet_GetError());
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    90
        serverQuit = YES;
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    91
    }
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    92
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    93
    // Open a connection with the IP provided (listen on the host's port)
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    94
    if (!(sd = SDLNet_TCP_Open(&ip))) {
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    95
        DLog(@"SDLNet_TCP_Open: %s %\n", SDLNet_GetError(), port);
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    96
        serverQuit = YES;
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    97
    }
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    98
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    99
    // launch the preview here so that we're sure the tcp channel is open
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   100
    pthread_t thread_id;
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   101
    pthread_create(&thread_id, NULL, (void *)GenLandPreview, (void *)port);
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   102
    pthread_detach(thread_id);
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   103
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   104
    DLog(@"Waiting for a client on port %d", port);
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   105
    while (!serverQuit) {
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   106
        /* This check the sd if there is a pending connection.
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   107
         * If there is one, accept that, and open a new socket for communicating */
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   108
        csd = SDLNet_TCP_Accept(sd);
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   109
        if (NULL != csd) {
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   110
            DLog(@"Client found");
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   111
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   112
            NSDictionary *dictForEngine = [self getDataForEngine];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   113
            [self sendToEngine:[dictForEngine objectForKey:@"seedCommand"]];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   114
            [self sendToEngine:[dictForEngine objectForKey:@"templateFilterCommand"]];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   115
            [self sendToEngine:[dictForEngine objectForKey:@"mapGenCommand"]];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   116
            [self sendToEngine:[dictForEngine objectForKey:@"mazeSizeCommand"]];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   117
            [self sendToEngine:@"!"];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   118
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   119
            memset(map, 0, 128*32);
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   120
            SDLNet_TCP_Recv(csd, map, 128*32);
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   121
            SDLNet_TCP_Recv(csd, &maxHogs, sizeof(uint8_t));
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   122
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   123
            SDLNet_TCP_Close(csd);
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   124
            serverQuit = YES;
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   125
        }
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   126
    }
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   127
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   128
    SDLNet_TCP_Close(sd);
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   129
    SDLNet_Quit();
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   130
    return map;
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   131
}
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   132
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   133
-(void) drawingThread {
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   134
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   135
    const uint8_t *map = [self engineProtocol];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   136
    uint8_t mapExp[128*32*8];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   137
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   138
    // draw the buffer (1 pixel per component, 0= transparent 1= color)
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   139
    int k = 0;
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   140
    for (int i = 0; i < 32*128; i++) {
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   141
        unsigned char byte = map[i];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   142
        for (int j = 0; j < 8; j++) {
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   143
            // select the color based on the leftmost bit
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   144
            if ((byte & 0x80) != 0)
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   145
                mapExp[k] = 100;
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   146
            else
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   147
                mapExp[k] = 255;
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   148
            // shift to next bit
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   149
            byte <<= 1;
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   150
            k++;
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   151
        }
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   152
    }
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   153
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray();
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   154
    CGContextRef bitmapImage = CGBitmapContextCreate(mapExp, 256, 128, 8, 256, colorspace, kCGImageAlphaNone);
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   155
    CGColorSpaceRelease(colorspace);
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   156
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   157
    CGImageRef previewCGImage = CGBitmapContextCreateImage(bitmapImage);
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   158
    CGContextRelease(bitmapImage);
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   159
    UIImage *previewImage = [[UIImage alloc] initWithCGImage:previewCGImage];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   160
    CGImageRelease(previewCGImage);
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   161
    previewCGImage = nil;
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   162
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   163
    // all these are performed on the main thread to prevent a leak
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   164
    [self performSelectorOnMainThread:@selector(setImageRoundedForNormalState:)
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   165
                           withObject:previewImage
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   166
                        waitUntilDone:NO];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   167
    [previewImage release];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   168
    [self performSelectorOnMainThread:@selector(setLabelText:)
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   169
                           withObject:[NSString stringWithFormat:@"%d", maxHogs]
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   170
                        waitUntilDone:NO];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   171
    [self performSelectorOnMainThread:@selector(turnOnWidgets)
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   172
                           withObject:nil
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   173
                        waitUntilDone:NO];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   174
    [self performSelectorOnMainThread:@selector(removeIndicator)
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   175
                           withObject:nil
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   176
                        waitUntilDone:NO];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   177
    
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   178
    [pool release];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   179
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   180
    /*
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   181
    // http://developer.apple.com/mac/library/qa/qa2001/qa1037.html
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   182
    UIGraphicsBeginImageContext(CGSizeMake(256,128));
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   183
    CGContextRef context = UIGraphicsGetCurrentContext();
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   184
    UIGraphicsPushContext(context);
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   185
    CGContextSetRGBFillColor(context, 0.5, 0.5, 0.7, 1.0);
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   186
    CGContextFillRect(context,CGRectMake(xc,yc,1,1));
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   187
    UIGraphicsPopContext();
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   188
    UIImage *previewImage = UIGraphicsGetImageFromCurrentImageContext();
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   189
    UIGraphicsEndImageContext();
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   190
    */
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   191
}
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   192
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   193
-(void) updatePreviewWithSeed:(NSString *)seed {
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   194
    // remove the current preview and title
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   195
    [self setImage:nil forState:UIControlStateNormal];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   196
    [self setTitle:nil forState:UIControlStateNormal];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   197
    
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   198
    // don't display preview on slower device, too slow and memory hog
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   199
    NSString *modelId = modelType();
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   200
    if ([modelId hasPrefix:@"iPhone1"] || [modelId hasPrefix:@"iPod1,1"] || [modelId hasPrefix:@"iPod2,1"]) {
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   201
        //self.delegate.busy = NO;
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   202
        [self setTitle:NSLocalizedString(@"Preview not available",@"") forState:UIControlStateNormal];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   203
    } else {        
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   204
        // add a very nice spinning wheel
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   205
        UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc]
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   206
                                              initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   207
        indicator.center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   208
        indicator.tag = INDICATOR_TAG;
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   209
        [indicator startAnimating];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   210
        [self addSubview:indicator];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   211
        [indicator release];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   212
        
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   213
        // let's draw in a separate thread so the gui can work; at the end it restore other widgets
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   214
        [NSThread detachNewThreadSelector:@selector(drawingThread) toTarget:self withObject:nil];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   215
    }
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   216
}
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   217
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   218
-(void) updatePreviewWithFile:(NSString *)filePath {
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   219
    UIImage *image = [[UIImage alloc] initWithContentsOfFile:filePath];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   220
    [self setImageRounded:image forState:UIControlStateNormal];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   221
    [image release];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   222
}
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   223
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   224
-(void) removeIndicator {
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   225
    UIActivityIndicatorView *indicator = (UIActivityIndicatorView *)[self viewWithTag:INDICATOR_TAG];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   226
    if (indicator) {
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   227
        [indicator stopAnimating];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   228
        [indicator removeFromSuperview];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   229
    }
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   230
}
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   231
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   232
#pragma mark -
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   233
#pragma mark delegate
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   234
-(void) turnOnWidgets {
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   235
    [self.delegate turnOnWidgets];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   236
}
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   237
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   238
-(void) setLabelText:(NSString *)string {
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   239
    [self.delegate setLabelText:string];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   240
}
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   241
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   242
-(NSDictionary *)getDataForEngine {
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   243
    return [self.delegate getDataForEngine];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   244
}
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   245
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   246
@end