project_files/HedgewarsMobile/Classes/MapConfigViewController.m
author koda
Sat, 03 Jul 2010 05:51:24 +0200
changeset 3617 1df21e06b8ba
parent 3598 a8aa06bae895
child 3623 f14db208f2fa
permissions -rw-r--r--
a couple of fixes i missed previously
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     1
//
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     2
//  MapConfigViewController.m
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     3
//  HedgewarsMobile
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     4
//
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     5
//  Created by Vittorio on 22/04/10.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     6
//  Copyright 2010 __MyCompanyName__. All rights reserved.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     7
//
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     8
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     9
#import "MapConfigViewController.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    10
#import "PascalImports.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    11
#import "CommodityFunctions.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    12
#import "UIImageExtra.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    13
#import "SDL_net.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    14
#import <pthread.h>
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    15
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    16
#define INDICATOR_TAG 7654
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    17
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    18
@implementation MapConfigViewController
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    19
@synthesize previewButton, maxHogs, seedCommand, templateFilterCommand, mapGenCommand, mazeSizeCommand, themeCommand,
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    20
            tableView, maxLabel, sizeLabel, segmentedControl, slider, lastIndexPath, themeArray, mapArray, busy;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    21
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    22
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    23
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    24
    return rotationManager(interfaceOrientation);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    25
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    26
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    27
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    28
#pragma mark Preview Handling
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    29
-(int) sendToEngine: (NSString *)string {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    30
    unsigned char length = [string length];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    31
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    32
    SDLNet_TCP_Send(csd, &length , 1);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    33
    return SDLNet_TCP_Send(csd, [string UTF8String], length);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    34
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    35
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    36
-(const uint8_t *)engineProtocol:(NSInteger) port {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    37
    IPaddress ip;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    38
    BOOL serverQuit = NO;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    39
    static uint8_t map[128*32];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    40
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    41
    if (SDLNet_Init() < 0) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    42
        NSLog(@"SDLNet_Init: %s", SDLNet_GetError());
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    43
        serverQuit = YES;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    44
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    45
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    46
    // Resolving the host using NULL make network interface to listen
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    47
    if (SDLNet_ResolveHost(&ip, NULL, port) < 0) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    48
        NSLog(@"SDLNet_ResolveHost: %s\n", SDLNet_GetError());
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    49
        serverQuit = YES;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    50
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    51
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    52
    // Open a connection with the IP provided (listen on the host's port)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    53
    if (!(sd = SDLNet_TCP_Open(&ip))) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    54
        NSLog(@"SDLNet_TCP_Open: %s %\n", SDLNet_GetError(), port);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    55
        serverQuit = YES;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    56
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    57
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    58
    // launch the preview here so that we're sure the tcp channel is open
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    59
    pthread_t thread_id;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    60
    pthread_create(&thread_id, NULL, (void *)GenLandPreview, (void *)port);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    61
    pthread_detach(thread_id);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    62
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    63
    DLog(@"Waiting for a client on port %d", port);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    64
    while (!serverQuit) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    65
        /* This check the sd if there is a pending connection.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    66
         * If there is one, accept that, and open a new socket for communicating */
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    67
        csd = SDLNet_TCP_Accept(sd);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    68
        if (NULL != csd) {          
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    69
            DLog(@"Client found");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    70
            
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    71
            [self sendToEngine:self.seedCommand];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    72
            [self sendToEngine:self.templateFilterCommand];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    73
            [self sendToEngine:self.mapGenCommand];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    74
            [self sendToEngine:self.mazeSizeCommand];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    75
            [self sendToEngine:@"!"];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    76
                
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    77
            memset(map, 0, 128*32);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    78
            SDLNet_TCP_Recv(csd, map, 128*32);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    79
            SDLNet_TCP_Recv(csd, &maxHogs, sizeof(uint8_t));
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    80
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    81
            SDLNet_TCP_Close(csd);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    82
            serverQuit = YES;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    83
        }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    84
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    85
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    86
    SDLNet_TCP_Close(sd);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    87
    SDLNet_Quit();
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    88
    return map;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    89
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    90
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    91
-(void) drawingThread {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    92
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    93
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    94
    // select the port for IPC and launch the preview generation through engineProtocol:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    95
    int port = randomPort();
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    96
    const uint8_t *map = [self engineProtocol:port];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    97
    uint8_t mapExp[128*32*8];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    98
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    99
    // draw the buffer (1 pixel per component, 0= transparent 1= color)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   100
    int k = 0;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   101
    for (int i = 0; i < 32*128; i++) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   102
        unsigned char byte = map[i];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   103
        for (int j = 0; j < 8; j++) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   104
            // select the color based on the leftmost bit
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   105
            if ((byte & 0x80) != 0)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   106
                mapExp[k] = 100;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   107
            else
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   108
                mapExp[k] = 255;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   109
            // shift to next bit
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   110
            byte <<= 1;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   111
            k++;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   112
        }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   113
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   114
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray();
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   115
    CGContextRef bitmapImage = CGBitmapContextCreate(mapExp, 256, 128, 8, 256, colorspace, kCGImageAlphaNone);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   116
    CGColorSpaceRelease(colorspace);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   117
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   118
    CGImageRef previewCGImage = CGBitmapContextCreateImage(bitmapImage);
3598
a8aa06bae895 tiy new overlay graphics
koda
parents: 3547
diff changeset
   119
    CGContextRelease(bitmapImage);
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   120
    UIImage *previewImage = [[UIImage alloc] initWithCGImage:previewCGImage];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   121
    CGImageRelease(previewCGImage);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   122
    previewCGImage = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   123
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   124
    // set the preview image (autoreleased) in the button and the maxhog label on the main thread to prevent a leak
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   125
    [self performSelectorOnMainThread:@selector(setButtonImage:) withObject:[previewImage makeRoundCornersOfSize:CGSizeMake(12, 12)] waitUntilDone:NO];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   126
    [previewImage release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   127
    [self performSelectorOnMainThread:@selector(setLabelText:) withObject:[NSString stringWithFormat:@"%d", maxHogs] waitUntilDone:NO];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   128
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   129
    // restore functionality of button and remove the spinning wheel on the main thread to prevent a leak
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   130
    [self performSelectorOnMainThread:@selector(turnOnWidgets) withObject:nil waitUntilDone:NO];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   131
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   132
    [pool release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   133
    //Invoking this method should be avoided as it does not give your thread a chance to clean up any resources it allocated during its execution.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   134
    //[NSThread exit];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   135
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   136
    /*
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   137
    // http://developer.apple.com/mac/library/qa/qa2001/qa1037.html
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   138
    UIGraphicsBeginImageContext(CGSizeMake(256,128));      
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   139
    CGContextRef context = UIGraphicsGetCurrentContext();       
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   140
    UIGraphicsPushContext(context);  
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   141
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   142
    CGContextSetRGBFillColor(context, 0.5, 0.5, 0.7, 1.0);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   143
    CGContextFillRect(context,CGRectMake(xc,yc,1,1));
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   144
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   145
    UIGraphicsPopContext();
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   146
    UIImage *previewImage = UIGraphicsGetImageFromCurrentImageContext();
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   147
    UIGraphicsEndImageContext();
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   148
    */
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   149
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   150
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   151
-(IBAction) updatePreview {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   152
    // don't generate a new preview while it's already generating one
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   153
    if (busy)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   154
        return;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   155
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   156
    // generate a seed
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   157
    CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   158
    NSString *seed = (NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuid);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   159
    CFRelease(uuid);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   160
    NSString *seedCmd = [[NSString alloc] initWithFormat:@"eseed {%@}", seed];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   161
    [seed release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   162
    self.seedCommand = seedCmd;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   163
    [seedCmd release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   164
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   165
    NSIndexPath *theIndex;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   166
    if (segmentedControl.selectedSegmentIndex != 1) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   167
        // prevent other events and add an activity while the preview is beign generated
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   168
        [self turnOffWidgets];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   169
        
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   170
        // remove the current preview
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   171
        [self.previewButton setImage:nil forState:UIControlStateNormal];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   172
        
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   173
        // add a very nice spinning wheel
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   174
        UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   175
                                              initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   176
        indicator.center = CGPointMake(previewButton.bounds.size.width / 2, previewButton.bounds.size.height / 2);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   177
        indicator.tag = INDICATOR_TAG;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   178
        [indicator startAnimating];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   179
        [self.previewButton addSubview:indicator];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   180
        [indicator release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   181
        
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   182
        // let's draw in a separate thread so the gui can work; at the end it restore other widgets
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   183
        [NSThread detachNewThreadSelector:@selector(drawingThread) toTarget:self withObject:nil];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   184
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   185
        theIndex = [NSIndexPath indexPathForRow:(random()%[self.themeArray count]) inSection:0];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   186
    } else {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   187
        theIndex = [NSIndexPath indexPathForRow:(random()%[self.mapArray count]) inSection:0];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   188
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   189
    [self.tableView reloadData];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   190
    [self tableView:self.tableView didSelectRowAtIndexPath:theIndex];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   191
    [self.tableView scrollToRowAtIndexPath:theIndex atScrollPosition:UITableViewScrollPositionNone animated:YES];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   192
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   193
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   194
-(void) updatePreviewWithMap:(NSInteger) index {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   195
    // change the preview button
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   196
    NSString *fileImage = [[NSString alloc] initWithFormat:@"%@/%@/preview.png", MAPS_DIRECTORY(),[self.mapArray objectAtIndex:index]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   197
    UIImage *image = [[UIImage alloc] initWithContentsOfFile:fileImage];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   198
    [fileImage release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   199
    [self.previewButton setImage:[image makeRoundCornersOfSize:CGSizeMake(12, 12)] forState:UIControlStateNormal];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   200
    [image release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   201
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   202
    // update label
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   203
    maxHogs = 18;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   204
    NSString *fileCfg = [[NSString alloc] initWithFormat:@"%@/%@/map.cfg", MAPS_DIRECTORY(),[self.mapArray objectAtIndex:index]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   205
    NSString *contents = [[NSString alloc] initWithContentsOfFile:fileCfg encoding:NSUTF8StringEncoding error:NULL];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   206
    [fileCfg release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   207
    NSArray *split = [contents componentsSeparatedByString:@"\n"];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   208
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   209
    // if the number is not set we keep 18 standard; 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   210
    // sometimes it's not set but there are trailing characters, we get around them with the second equation
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   211
    if ([split count] > 1 && [[split objectAtIndex:1] intValue] > 0)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   212
        maxHogs = [[split objectAtIndex:1] intValue];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   213
    [contents release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   214
    NSString *max = [[NSString alloc] initWithFormat:@"%d",maxHogs];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   215
    self.maxLabel.text = max;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   216
    [max release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   217
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   218
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   219
-(void) turnOffWidgets {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   220
    busy = YES;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   221
    self.previewButton.alpha = 0.5f;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   222
    self.previewButton.enabled = NO;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   223
    self.maxLabel.text = @"...";
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   224
    self.segmentedControl.enabled = NO;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   225
    self.slider.enabled = NO;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   226
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   227
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   228
-(void) turnOnWidgets {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   229
    self.previewButton.alpha = 1.0f;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   230
    self.previewButton.enabled = YES;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   231
    self.segmentedControl.enabled = YES;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   232
    self.slider.enabled = YES;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   233
    busy = NO;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   234
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   235
    UIActivityIndicatorView *indicator = (UIActivityIndicatorView *)[self.previewButton viewWithTag:INDICATOR_TAG];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   236
    if (indicator) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   237
        [indicator stopAnimating];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   238
        [indicator removeFromSuperview];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   239
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   240
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   241
 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   242
-(void) setLabelText:(NSString *)str {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   243
    self.maxLabel.text = str;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   244
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   245
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   246
-(void) setButtonImage:(UIImage *)img {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   247
    [self.previewButton setBackgroundImage:img forState:UIControlStateNormal];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   248
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   249
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   250
-(void) restoreBackgroundImage {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   251
    // white rounded rectangle as background image for previewButton
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   252
    UIGraphicsBeginImageContext(CGSizeMake(256,128));      
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   253
    CGContextRef context = UIGraphicsGetCurrentContext();       
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   254
    UIGraphicsPushContext(context);  
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   255
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   256
    CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   257
    CGContextFillRect(context,CGRectMake(0,0,256,128));
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   258
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   259
    UIGraphicsPopContext();
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   260
    UIImage *bkgImg = UIGraphicsGetImageFromCurrentImageContext();
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   261
    UIGraphicsEndImageContext();
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   262
    [self.previewButton setBackgroundImage:[bkgImg makeRoundCornersOfSize:CGSizeMake(12, 12)] forState:UIControlStateNormal];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   263
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   264
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   265
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   266
#pragma mark Table view data source
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   267
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   268
    return 1;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   269
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   270
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   271
-(NSInteger) tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger) section {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   272
    if (self.segmentedControl.selectedSegmentIndex != 1)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   273
        return [themeArray count];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   274
    else
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   275
        return [mapArray count];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   276
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   277
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   278
-(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   279
    static NSString *CellIdentifier = @"Cell";
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   280
    NSInteger row = [indexPath row];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   281
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   282
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   283
    if (cell == nil) 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   284
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   285
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   286
    if (self.segmentedControl.selectedSegmentIndex != 1) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   287
        // the % prevents a strange bug that occurs sporadically
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   288
        NSString *themeName = [self.themeArray objectAtIndex:row % [self.themeArray count]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   289
        cell.textLabel.text = themeName;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   290
        UIImage *image = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@/icon.png",THEMES_DIRECTORY(),themeName]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   291
        cell.imageView.image = image;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   292
        [image release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   293
    } else {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   294
        cell.textLabel.text = [self.mapArray objectAtIndex:row];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   295
        cell.imageView.image = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   296
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   297
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   298
    if (row == [self.lastIndexPath row]) 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   299
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   300
    else
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   301
        cell.accessoryType = UITableViewCellAccessoryNone;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   302
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   303
    return cell;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   304
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   305
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   306
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   307
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   308
#pragma mark Table view delegate
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   309
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   310
    int newRow = [indexPath row];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   311
    int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   312
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   313
    if (newRow != oldRow) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   314
        if (self.segmentedControl.selectedSegmentIndex != 1) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   315
            NSString *theme = [self.themeArray objectAtIndex:newRow];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   316
            self.themeCommand =  [NSString stringWithFormat:@"etheme %@", theme];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   317
        } else
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   318
            [self updatePreviewWithMap:newRow];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   319
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   320
        UITableViewCell *newCell = [aTableView cellForRowAtIndexPath:indexPath]; 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   321
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   322
        UITableViewCell *oldCell = [aTableView cellForRowAtIndexPath:self.lastIndexPath];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   323
        oldCell.accessoryType = UITableViewCellAccessoryNone;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   324
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   325
        self.lastIndexPath = indexPath;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   326
        [aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   327
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   328
    [aTableView deselectRowAtIndexPath:indexPath animated:YES];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   329
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   330
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   331
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   332
#pragma mark slider & segmentedControl
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   333
// this updates the label and the command keys when the slider is moved, depending of the selection in segmentedControl
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   334
// no methods are called by this routine and you can pass nil to it
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   335
-(IBAction) sliderChanged:(id) sender {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   336
    NSString *labelText;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   337
    NSString *templateCommand;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   338
    NSString *mazeCommand;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   339
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   340
    switch ((int)(self.slider.value*100)) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   341
        case 0:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   342
            if (self.segmentedControl.selectedSegmentIndex == 0) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   343
                labelText = NSLocalizedString(@"Wacky",@"");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   344
            } else {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   345
                labelText = NSLocalizedString(@"Large Floating Islands",@"");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   346
            }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   347
            templateCommand = @"e$template_filter 5";
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   348
            mazeCommand = @"e$maze_size 5";
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   349
            break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   350
        case 1:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   351
            if (self.segmentedControl.selectedSegmentIndex == 0) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   352
                labelText = NSLocalizedString(@"Cavern",@"");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   353
            } else {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   354
                labelText = NSLocalizedString(@"Medium Floating Islands",@"");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   355
            }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   356
            templateCommand = @"e$template_filter 4";
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   357
            mazeCommand = @"e$maze_size 4";
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   358
            break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   359
        case 2:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   360
            if (self.segmentedControl.selectedSegmentIndex == 0) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   361
                labelText = NSLocalizedString(@"Small",@"");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   362
            } else {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   363
                labelText = NSLocalizedString(@"Small Floating Islands",@"");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   364
            }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   365
            templateCommand = @"e$template_filter 1";
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   366
            mazeCommand = @"e$maze_size 3";
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   367
            break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   368
        case 3:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   369
            if (self.segmentedControl.selectedSegmentIndex == 0) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   370
                labelText = NSLocalizedString(@"Medium",@"");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   371
            } else {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   372
                labelText = NSLocalizedString(@"Large Tunnels",@"");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   373
            }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   374
            templateCommand = @"e$template_filter 2";
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   375
            mazeCommand = @"e$maze_size 2";
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   376
            break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   377
        case 4:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   378
            if (self.segmentedControl.selectedSegmentIndex == 0) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   379
                labelText = NSLocalizedString(@"Large",@"");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   380
            } else {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   381
                labelText = NSLocalizedString(@"Medium Tunnels",@"");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   382
            }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   383
            templateCommand = @"e$template_filter 3";
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   384
            mazeCommand = @"e$maze_size 1";
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   385
            break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   386
        case 5:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   387
            if (self.segmentedControl.selectedSegmentIndex == 0) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   388
                labelText = NSLocalizedString(@"All",@"");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   389
            } else {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   390
                labelText = NSLocalizedString(@"Small Tunnels",@"");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   391
            }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   392
            templateCommand = @"e$template_filter 0";
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   393
            mazeCommand = @"e$maze_size 0";
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   394
            break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   395
        default:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   396
            labelText = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   397
            templateCommand = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   398
            mazeCommand = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   399
            break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   400
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   401
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   402
    self.sizeLabel.text = labelText;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   403
    self.templateFilterCommand = templateCommand;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   404
    self.mazeSizeCommand = mazeCommand;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   405
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   406
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   407
// update preview (if not busy and if its value really changed) as soon as the user lifts its finger up
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   408
-(IBAction) sliderEndedChanging:(id) sender {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   409
    int num = (int) (self.slider.value * 100);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   410
    if (oldValue != num) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   411
        [self updatePreview];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   412
        oldValue = num;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   413
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   414
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   415
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   416
// perform actions based on the activated section, then call updatePreview to visually update the selection 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   417
// updatePreview will call didSelectRowAtIndexPath which will call the right update routine)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   418
// and if necessary update the table with a slide animation
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   419
-(IBAction) segmentedControlChanged:(id) sender {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   420
    NSString *mapgen;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   421
    NSInteger newPage = self.segmentedControl.selectedSegmentIndex;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   422
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   423
    switch (newPage) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   424
        case 0: // Random
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   425
            mapgen = @"e$mapgen 0";
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   426
            [self sliderChanged:nil];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   427
            self.slider.enabled = YES;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   428
            break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   429
            
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   430
        case 1: // Map
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   431
            mapgen = @"e$mapgen 0";
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   432
            self.slider.enabled = NO;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   433
            self.sizeLabel.text = @".";
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   434
            [self restoreBackgroundImage];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   435
            break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   436
            
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   437
        case 2: // Maze
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   438
            mapgen = @"e$mapgen 1";
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   439
            [self sliderChanged:nil];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   440
            self.slider.enabled = YES;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   441
            break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   442
        
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   443
        default:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   444
            mapgen = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   445
            break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   446
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   447
    self.mapGenCommand = mapgen;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   448
    [self updatePreview];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   449
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   450
    // nice animation for updating the table when appropriate (on iphone)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   451
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   452
        if (((oldPage == 0 || oldPage == 2) && newPage == 1) ||
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   453
            (oldPage == 1 && (newPage == 0 || newPage == 2))) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   454
            [UIView beginAnimations:@"moving out table" context:NULL];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   455
            self.tableView.frame = CGRectMake(480, 0, 185, 276);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   456
            [UIView commitAnimations];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   457
            [self performSelector:@selector(moveTable) withObject:nil afterDelay:0.2];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   458
        }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   459
    oldPage = newPage;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   460
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   461
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   462
// update data when table is not visible and then show it
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   463
-(void) moveTable {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   464
    [self.tableView reloadData];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   465
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   466
    [UIView beginAnimations:@"moving in table" context:NULL];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   467
    self.tableView.frame = CGRectMake(295, 0, 185, 276);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   468
    [UIView commitAnimations];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   469
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   470
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   471
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   472
#pragma mark view management
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   473
-(void) viewDidLoad {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   474
    [super viewDidLoad];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   475
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   476
    srandom(time(NULL));
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   477
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   478
    CGSize screenSize = [[UIScreen mainScreen] bounds].size;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   479
    self.view.frame = CGRectMake(0, 0, screenSize.height, screenSize.width - 44);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   480
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   481
    // themes.cfg contains all the user-selectable themes
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   482
    NSString *string = [[NSString alloc] initWithContentsOfFile:[THEMES_DIRECTORY() stringByAppendingString:@"/themes.cfg"]
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   483
                                                       encoding:NSUTF8StringEncoding 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   484
                                                          error:NULL];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   485
    NSMutableArray *array = [[NSMutableArray alloc] initWithArray:[string componentsSeparatedByString:@"\n"]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   486
    [string release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   487
    // remove a trailing "" element
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   488
    [array removeLastObject];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   489
    self.themeArray = array;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   490
    [array release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   491
    self.mapArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:MAPS_DIRECTORY() error:NULL];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   492
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   493
    self.tableView.rowHeight = 42;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   494
    busy = NO;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   495
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   496
    // draw a white background
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   497
    [self restoreBackgroundImage];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   498
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   499
    // initialize some "default" values
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   500
    self.sizeLabel.text = NSLocalizedString(@"All",@"");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   501
    self.slider.value = 0.05f;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   502
    self.segmentedControl.selectedSegmentIndex = 0;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   503
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   504
    self.templateFilterCommand = @"e$template_filter 0";
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   505
    self.mazeSizeCommand = @"e$maze_size 0";
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   506
    self.mapGenCommand = @"e$mapgen 0";
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   507
    self.lastIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   508
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   509
    oldValue = 5;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   510
    oldPage = 0;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   511
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   512
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   513
-(void) viewDidAppear:(BOOL) animated {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   514
    [super viewDidAppear:animated];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   515
    [self updatePreview];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   516
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   517
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   518
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   519
#pragma mark memory
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   520
-(void) didReceiveMemoryWarning {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   521
    [super didReceiveMemoryWarning];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   522
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   523
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   524
-(void) viewDidUnload {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   525
    self.previewButton = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   526
    self.seedCommand = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   527
    self.templateFilterCommand = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   528
    self.mapGenCommand = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   529
    self.mazeSizeCommand = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   530
    self.themeCommand = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   531
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   532
    self.previewButton = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   533
    self.tableView = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   534
    self.maxLabel = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   535
    self.sizeLabel = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   536
    self.segmentedControl = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   537
    self.slider = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   538
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   539
    self.lastIndexPath = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   540
    self.themeArray = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   541
    self.mapArray = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   542
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   543
    [super viewDidUnload];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   544
    MSG_DIDUNLOAD();
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   545
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   546
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   547
-(void) dealloc {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   548
    [seedCommand release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   549
    [templateFilterCommand release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   550
    [mapGenCommand release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   551
    [mazeSizeCommand release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   552
    [themeCommand release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   553
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   554
    [previewButton release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   555
    [tableView release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   556
    [maxLabel release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   557
    [sizeLabel release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   558
    [segmentedControl release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   559
    [slider release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   560
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   561
    [lastIndexPath release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   562
    [themeArray release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   563
    [mapArray release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   564
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   565
    [super dealloc];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   566
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   567
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   568
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   569
@end