project_files/HedgewarsMobile/Classes/MapPreviewButtonView.m
author unc0rr
Mon, 10 Jun 2013 21:20:14 +0400
changeset 9170 7c75643d0b66
parent 8441 a00b0fa0dbd7
child 10108 c68cf030eded
permissions -rw-r--r--
More verbose initialization
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
6700
e04da46ee43c the most important commit of the year
koda
parents: 6246
diff changeset
     3
 * Copyright (c) 2009-2012 Vittorio Giovara <vittorio.giovara@gmail.com>
3910
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
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
#import "MapPreviewButtonView.h"
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    21
#import <pthread.h>
6109
f6726ec81e64 finally removed the white border glitch of the ipad preview map, moved initialization from IB to code
koda
parents: 6078
diff changeset
    22
#import <QuartzCore/QuartzCore.h>
f6726ec81e64 finally removed the white border glitch of the ipad preview map, moved initialization from IB to code
koda
parents: 6078
diff changeset
    23
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    24
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    25
#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
    26
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    27
@implementation MapPreviewButtonView
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    28
@synthesize delegate;
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    29
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    30
-(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
    31
    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
    32
        delegate = nil;
6109
f6726ec81e64 finally removed the white border glitch of the ipad preview map, moved initialization from IB to code
koda
parents: 6078
diff changeset
    33
        self.backgroundColor = [UIColor whiteColor];
f6726ec81e64 finally removed the white border glitch of the ipad preview map, moved initialization from IB to code
koda
parents: 6078
diff changeset
    34
        self.layer.cornerRadius = 12;
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    35
    }
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    36
    return self;
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    37
}
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
-(void) dealloc {
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    40
    self.delegate = nil;
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    41
    [super dealloc];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    42
}
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    43
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    44
#pragma mark -
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    45
#pragma mark image wrappers
6109
f6726ec81e64 finally removed the white border glitch of the ipad preview map, moved initialization from IB to code
koda
parents: 6078
diff changeset
    46
-(void) setImageRounded:(UIImage *)image forState:(UIControlState)controlState {
f6726ec81e64 finally removed the white border glitch of the ipad preview map, moved initialization from IB to code
koda
parents: 6078
diff changeset
    47
    [self setImage:[image makeRoundCornersOfSize:CGSizeMake(12, 12)] forState:controlState];
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    48
}
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    49
6109
f6726ec81e64 finally removed the white border glitch of the ipad preview map, moved initialization from IB to code
koda
parents: 6078
diff changeset
    50
-(void) setImageRounded:(UIImage *)image {
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    51
    [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
    52
}
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    53
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    54
#pragma mark -
8441
a00b0fa0dbd7 some whitespaces from ios files
koda
parents: 7248
diff changeset
    55
#pragma mark preview
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    56
-(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
    57
    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
    58
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    59
    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
    60
    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
    61
}
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    62
6841
3633928a3188 let's outsmart the compiler for no particular benefit
koda
parents: 6832
diff changeset
    63
-(void) engineProtocol:(uint8_t *)unpackedMap {
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    64
    IPaddress ip;
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    65
    BOOL serverQuit = NO;
6841
3633928a3188 let's outsmart the compiler for no particular benefit
koda
parents: 6832
diff changeset
    66
    uint8_t packedMap[128*32];
6246
6b2d19ed521a just moving ios server stuff around
koda
parents: 6219
diff changeset
    67
    int port = [HWUtils randomPort];
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    68
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    69
    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
    70
        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
    71
        serverQuit = YES;
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    72
    }
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    73
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    74
    // 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
    75
    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
    76
        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
    77
        serverQuit = YES;
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    78
    }
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    79
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    80
    // 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
    81
    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
    82
        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
    83
        serverQuit = YES;
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    84
    }
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
    // 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
    87
    pthread_t thread_id;
7248
0ee403b190b5 ios: disable some warnings
koda
parents: 6841
diff changeset
    88
    pthread_create(&thread_id, NULL, (void *(*)(void *))GenLandPreview, (void *)port);
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    89
    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
    90
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    91
    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
    92
    while (!serverQuit) {
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    93
        /* 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
    94
         * 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
    95
        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
    96
        if (NULL != csd) {
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    97
            DLog(@"Client found");
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
            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
   100
            [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
   101
            [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
   102
            [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
   103
            [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
   104
            [self sendToEngine:@"!"];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   105
6841
3633928a3188 let's outsmart the compiler for no particular benefit
koda
parents: 6832
diff changeset
   106
            memset(packedMap, 0, 128*32);
3633928a3188 let's outsmart the compiler for no particular benefit
koda
parents: 6832
diff changeset
   107
            SDLNet_TCP_Recv(csd, packedMap, 128*32);
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   108
            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
   109
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   110
            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
   111
            serverQuit = YES;
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   112
        }
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   113
    }
6822
206db098f8c5 ios headers cleanup, with tweaks to +randomPort and grenade timer handling in overlay
koda
parents: 6700
diff changeset
   114
    [HWUtils freePort:port];
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   115
    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
   116
    SDLNet_Quit();
6841
3633928a3188 let's outsmart the compiler for no particular benefit
koda
parents: 6832
diff changeset
   117
3633928a3188 let's outsmart the compiler for no particular benefit
koda
parents: 6832
diff changeset
   118
    // spread the packed bits in an array of bytes (one pixel per element, 0= transparent 1= color)
3633928a3188 let's outsmart the compiler for no particular benefit
koda
parents: 6832
diff changeset
   119
    int k = 0;
3633928a3188 let's outsmart the compiler for no particular benefit
koda
parents: 6832
diff changeset
   120
    memset(unpackedMap, 255, 128*32*8);     // 255 is white
3633928a3188 let's outsmart the compiler for no particular benefit
koda
parents: 6832
diff changeset
   121
    for (int i = 0; i < 32*128; i++) {
3633928a3188 let's outsmart the compiler for no particular benefit
koda
parents: 6832
diff changeset
   122
        for (int j = 7; j >= 0; j--) {
3633928a3188 let's outsmart the compiler for no particular benefit
koda
parents: 6832
diff changeset
   123
            if (((packedMap[i] >> j) & 0x01) != 0)
3633928a3188 let's outsmart the compiler for no particular benefit
koda
parents: 6832
diff changeset
   124
                unpackedMap[k] = 170;       // level of gray [0-255]
3633928a3188 let's outsmart the compiler for no particular benefit
koda
parents: 6832
diff changeset
   125
            k++;
3633928a3188 let's outsmart the compiler for no particular benefit
koda
parents: 6832
diff changeset
   126
        }
3633928a3188 let's outsmart the compiler for no particular benefit
koda
parents: 6832
diff changeset
   127
    }
3633928a3188 let's outsmart the compiler for no particular benefit
koda
parents: 6832
diff changeset
   128
    return;
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   129
}
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   130
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   131
-(void) drawingThread {
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   132
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
6841
3633928a3188 let's outsmart the compiler for no particular benefit
koda
parents: 6832
diff changeset
   133
    uint8_t unpackedMap[128*32*8];
3633928a3188 let's outsmart the compiler for no particular benefit
koda
parents: 6832
diff changeset
   134
    [self engineProtocol:unpackedMap];
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   135
6841
3633928a3188 let's outsmart the compiler for no particular benefit
koda
parents: 6832
diff changeset
   136
    // http://developer.apple.com/mac/library/qa/qa2001/qa1037.html
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   137
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray();
6841
3633928a3188 let's outsmart the compiler for no particular benefit
koda
parents: 6832
diff changeset
   138
    CGContextRef bitmapImage = CGBitmapContextCreate(unpackedMap, 256, 128, 8, 256, colorspace, kCGImageAlphaNone);
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   139
    CGColorSpaceRelease(colorspace);
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   140
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   141
    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
   142
    CGContextRelease(bitmapImage);
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   143
    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
   144
    CGImageRelease(previewCGImage);
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   145
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   146
    // all these are performed on the main thread to prevent a leak
6109
f6726ec81e64 finally removed the white border glitch of the ipad preview map, moved initialization from IB to code
koda
parents: 6078
diff changeset
   147
    [self performSelectorOnMainThread:@selector(setImageRounded:)
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   148
                           withObject:previewImage
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   149
                        waitUntilDone:NO];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   150
    [previewImage release];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   151
    [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
   152
                           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
   153
                        waitUntilDone:NO];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   154
    [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
   155
                           withObject:nil
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   156
                        waitUntilDone:NO];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   157
    [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
   158
                           withObject:nil
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   159
                        waitUntilDone:NO];
8441
a00b0fa0dbd7 some whitespaces from ios files
koda
parents: 7248
diff changeset
   160
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   161
    [pool release];
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
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   164
-(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
   165
    // 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
   166
    [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
   167
    [self setTitle:nil forState:UIControlStateNormal];
8441
a00b0fa0dbd7 some whitespaces from ios files
koda
parents: 7248
diff changeset
   168
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   169
    // don't display preview on slower device, too slow and memory hog
6078
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5207
diff changeset
   170
    if (IS_NOT_POWERFUL([HWUtils modelType])) {
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   171
        [self setTitle:NSLocalizedString(@"Preview not available",@"") forState:UIControlStateNormal];
4082
bfe14b38dad1 better support for wimpier i-devices
koda
parents: 3910
diff changeset
   172
        [self turnOnWidgets];
8441
a00b0fa0dbd7 some whitespaces from ios files
koda
parents: 7248
diff changeset
   173
    } else {
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   174
        // 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
   175
        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
   176
                                              initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   177
        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
   178
        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
   179
        [indicator startAnimating];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   180
        [self addSubview:indicator];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   181
        [indicator release];
8441
a00b0fa0dbd7 some whitespaces from ios files
koda
parents: 7248
diff changeset
   182
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   183
        // 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
   184
        [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
   185
    }
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   186
}
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   187
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   188
-(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
   189
    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
   190
    [self setImageRounded:image forState:UIControlStateNormal];
6219
c193881389c1 restore game config on iphone
koda
parents: 6115
diff changeset
   191
    self.backgroundColor = [UIColor whiteColor];
c193881389c1 restore game config on iphone
koda
parents: 6115
diff changeset
   192
    self.layer.cornerRadius = 12;
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   193
    [image release];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   194
}
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   195
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   196
-(void) removeIndicator {
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   197
    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
   198
    if (indicator) {
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   199
        [indicator stopAnimating];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   200
        [indicator removeFromSuperview];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   201
    }
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   202
}
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   203
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   204
#pragma mark -
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   205
#pragma mark delegate
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   206
-(void) turnOnWidgets {
6115
485cfecadc9a HUGE refactoring of the ipad interface, finally understood how to use interface builder with custom uiviewcontrollers, as well as converted some uitableviewcontrollers to uiviewcontrollers for simplicity
koda
parents: 6109
diff changeset
   207
    if ([self.delegate respondsToSelector:@selector(turnOnWidgets)])
485cfecadc9a HUGE refactoring of the ipad interface, finally understood how to use interface builder with custom uiviewcontrollers, as well as converted some uitableviewcontrollers to uiviewcontrollers for simplicity
koda
parents: 6109
diff changeset
   208
        [self.delegate turnOnWidgets];
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   209
}
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   210
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   211
-(void) setLabelText:(NSString *)string {
6115
485cfecadc9a HUGE refactoring of the ipad interface, finally understood how to use interface builder with custom uiviewcontrollers, as well as converted some uitableviewcontrollers to uiviewcontrollers for simplicity
koda
parents: 6109
diff changeset
   212
    if ([self.delegate respondsToSelector:@selector(setMaxLabelText:)])
485cfecadc9a HUGE refactoring of the ipad interface, finally understood how to use interface builder with custom uiviewcontrollers, as well as converted some uitableviewcontrollers to uiviewcontrollers for simplicity
koda
parents: 6109
diff changeset
   213
        [self.delegate setMaxLabelText:string];
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   214
}
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
-(NSDictionary *)getDataForEngine {
6115
485cfecadc9a HUGE refactoring of the ipad interface, finally understood how to use interface builder with custom uiviewcontrollers, as well as converted some uitableviewcontrollers to uiviewcontrollers for simplicity
koda
parents: 6109
diff changeset
   217
    if ([self.delegate respondsToSelector:@selector(getDataForEngine)])
485cfecadc9a HUGE refactoring of the ipad interface, finally understood how to use interface builder with custom uiviewcontrollers, as well as converted some uitableviewcontrollers to uiviewcontrollers for simplicity
koda
parents: 6109
diff changeset
   218
        return [self.delegate getDataForEngine];
485cfecadc9a HUGE refactoring of the ipad interface, finally understood how to use interface builder with custom uiviewcontrollers, as well as converted some uitableviewcontrollers to uiviewcontrollers for simplicity
koda
parents: 6109
diff changeset
   219
    return nil;
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   220
}
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   221
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   222
@end