project_files/HedgewarsMobile/Classes/MapPreviewButtonView.m
author Wuzzy <Wuzzy2@mail.ru>
Wed, 25 Oct 2017 23:09:41 +0200
changeset 12763 ad67a3804981
parent 11240 57891137ffef
child 12872 00215a7ec5f5
permissions -rw-r--r--
Fix sometimes ammo schemes not being saved after changing before an ammo scheme got deleted in session This was because the bool isDeleting is not initialized, so its initial value is unpredictable. Which means there's chance it starts with true, confusing the frontend.
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
10108
c68cf030eded update FSF address. note: two sdl include files (by Sam Lantinga) still have the old FSF address in their copyright - but I ain't gonna touch their copyright headers
sheepluva
parents: 8441
diff changeset
    16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA.
3910
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
11138
05853b1df2a2 - Property for port in MapPreviewButtonView
antonc27 <antonc27@mail.ru>
parents: 11137
diff changeset
    27
@interface MapPreviewButtonView ()
11148
064a53861759 - Refactoring in order to remove some warning related to using of int instead of NSInteger
antonc27 <antonc27@mail.ru>
parents: 11138
diff changeset
    28
@property (nonatomic) NSInteger port;
11138
05853b1df2a2 - Property for port in MapPreviewButtonView
antonc27 <antonc27@mail.ru>
parents: 11137
diff changeset
    29
@end
05853b1df2a2 - Property for port in MapPreviewButtonView
antonc27 <antonc27@mail.ru>
parents: 11137
diff changeset
    30
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    31
@implementation MapPreviewButtonView
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    32
@synthesize delegate;
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    33
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    34
-(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
    35
    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
    36
        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
    37
        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
    38
        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
    39
    }
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    40
    return self;
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    41
}
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
-(void) dealloc {
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    44
    self.delegate = nil;
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    45
    [super dealloc];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    46
}
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    47
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    48
#pragma mark -
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    49
#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
    50
-(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
    51
    [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
    52
}
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    53
6109
f6726ec81e64 finally removed the white border glitch of the ipad preview map, moved initialization from IB to code
koda
parents: 6078
diff changeset
    54
-(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
    55
    [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
    56
}
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    57
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    58
#pragma mark -
8441
a00b0fa0dbd7 some whitespaces from ios files
koda
parents: 7248
diff changeset
    59
#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
    60
-(int) sendToEngine:(NSString *)string {
11240
57891137ffef - Fix for sending to engine non-english characters
antonc27 <antonc27@mail.ru>
parents: 11148
diff changeset
    61
    unsigned char length = [string lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    62
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    63
    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
    64
    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
    65
}
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    66
6841
3633928a3188 let's outsmart the compiler for no particular benefit
koda
parents: 6832
diff changeset
    67
-(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
    68
    IPaddress ip;
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    69
    BOOL serverQuit = NO;
6841
3633928a3188 let's outsmart the compiler for no particular benefit
koda
parents: 6832
diff changeset
    70
    uint8_t packedMap[128*32];
11138
05853b1df2a2 - Property for port in MapPreviewButtonView
antonc27 <antonc27@mail.ru>
parents: 11137
diff changeset
    71
    self.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
    72
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    73
    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
    74
        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
    75
        serverQuit = YES;
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    76
    }
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    77
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    78
    // Resolving the host using NULL make network interface to listen
11138
05853b1df2a2 - Property for port in MapPreviewButtonView
antonc27 <antonc27@mail.ru>
parents: 11137
diff changeset
    79
    if (SDLNet_ResolveHost(&ip, NULL, self.port) < 0) {
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    80
        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
    81
        serverQuit = YES;
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    82
    }
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    83
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    84
    // 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
    85
    if (!(sd = SDLNet_TCP_Open(&ip))) {
11148
064a53861759 - Refactoring in order to remove some warning related to using of int instead of NSInteger
antonc27 <antonc27@mail.ru>
parents: 11138
diff changeset
    86
        DLog(@"SDLNet_TCP_Open: %s %ld\n", SDLNet_GetError(), (long)self.port);
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    87
        serverQuit = YES;
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    88
    }
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
    89
11133
fa3d227fb308 - Fix for retrieving 'Maze' preview genration
antonc27 <antonc27@mail.ru>
parents: 10108
diff changeset
    90
    // launch the preview in background here so that we're sure the tcp channel is open
fa3d227fb308 - Fix for retrieving 'Maze' preview genration
antonc27 <antonc27@mail.ru>
parents: 10108
diff changeset
    91
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
11148
064a53861759 - Refactoring in order to remove some warning related to using of int instead of NSInteger
antonc27 <antonc27@mail.ru>
parents: 11138
diff changeset
    92
        NSString *ipcString = [[NSString alloc] initWithFormat:@"%ld", (long)self.port];
11133
fa3d227fb308 - Fix for retrieving 'Maze' preview genration
antonc27 <antonc27@mail.ru>
parents: 10108
diff changeset
    93
        NSString *documentsDirectory = DOCUMENTS_FOLDER();
fa3d227fb308 - Fix for retrieving 'Maze' preview genration
antonc27 <antonc27@mail.ru>
parents: 10108
diff changeset
    94
        
fa3d227fb308 - Fix for retrieving 'Maze' preview genration
antonc27 <antonc27@mail.ru>
parents: 10108
diff changeset
    95
        NSMutableArray *gameParameters = [[NSMutableArray alloc] initWithObjects:
fa3d227fb308 - Fix for retrieving 'Maze' preview genration
antonc27 <antonc27@mail.ru>
parents: 10108
diff changeset
    96
                                          @"--internal",
fa3d227fb308 - Fix for retrieving 'Maze' preview genration
antonc27 <antonc27@mail.ru>
parents: 10108
diff changeset
    97
                                          @"--port", ipcString,
fa3d227fb308 - Fix for retrieving 'Maze' preview genration
antonc27 <antonc27@mail.ru>
parents: 10108
diff changeset
    98
                                          @"--user-prefix", documentsDirectory,
fa3d227fb308 - Fix for retrieving 'Maze' preview genration
antonc27 <antonc27@mail.ru>
parents: 10108
diff changeset
    99
                                          @"--landpreview",
fa3d227fb308 - Fix for retrieving 'Maze' preview genration
antonc27 <antonc27@mail.ru>
parents: 10108
diff changeset
   100
                                          nil];
fa3d227fb308 - Fix for retrieving 'Maze' preview genration
antonc27 <antonc27@mail.ru>
parents: 10108
diff changeset
   101
        [ipcString release];
fa3d227fb308 - Fix for retrieving 'Maze' preview genration
antonc27 <antonc27@mail.ru>
parents: 10108
diff changeset
   102
        
fa3d227fb308 - Fix for retrieving 'Maze' preview genration
antonc27 <antonc27@mail.ru>
parents: 10108
diff changeset
   103
        int argc = [gameParameters count];
fa3d227fb308 - Fix for retrieving 'Maze' preview genration
antonc27 <antonc27@mail.ru>
parents: 10108
diff changeset
   104
        const char **argv = (const char **)malloc(sizeof(const char*)*argc);
fa3d227fb308 - Fix for retrieving 'Maze' preview genration
antonc27 <antonc27@mail.ru>
parents: 10108
diff changeset
   105
        for (int i = 0; i < argc; i++)
fa3d227fb308 - Fix for retrieving 'Maze' preview genration
antonc27 <antonc27@mail.ru>
parents: 10108
diff changeset
   106
            argv[i] = strdup([[gameParameters objectAtIndex:i] UTF8String]);
fa3d227fb308 - Fix for retrieving 'Maze' preview genration
antonc27 <antonc27@mail.ru>
parents: 10108
diff changeset
   107
        [gameParameters release];
fa3d227fb308 - Fix for retrieving 'Maze' preview genration
antonc27 <antonc27@mail.ru>
parents: 10108
diff changeset
   108
        
fa3d227fb308 - Fix for retrieving 'Maze' preview genration
antonc27 <antonc27@mail.ru>
parents: 10108
diff changeset
   109
        RunEngine(argc, argv);
fa3d227fb308 - Fix for retrieving 'Maze' preview genration
antonc27 <antonc27@mail.ru>
parents: 10108
diff changeset
   110
        
fa3d227fb308 - Fix for retrieving 'Maze' preview genration
antonc27 <antonc27@mail.ru>
parents: 10108
diff changeset
   111
        // cleanup
fa3d227fb308 - Fix for retrieving 'Maze' preview genration
antonc27 <antonc27@mail.ru>
parents: 10108
diff changeset
   112
        for (int i = 0; i < argc; i++)
fa3d227fb308 - Fix for retrieving 'Maze' preview genration
antonc27 <antonc27@mail.ru>
parents: 10108
diff changeset
   113
            free((void *)argv[i]);
fa3d227fb308 - Fix for retrieving 'Maze' preview genration
antonc27 <antonc27@mail.ru>
parents: 10108
diff changeset
   114
        free(argv);
fa3d227fb308 - Fix for retrieving 'Maze' preview genration
antonc27 <antonc27@mail.ru>
parents: 10108
diff changeset
   115
    });
fa3d227fb308 - Fix for retrieving 'Maze' preview genration
antonc27 <antonc27@mail.ru>
parents: 10108
diff changeset
   116
    
11148
064a53861759 - Refactoring in order to remove some warning related to using of int instead of NSInteger
antonc27 <antonc27@mail.ru>
parents: 11138
diff changeset
   117
    DLog(@"Waiting for a client on port %ld", (long)self.port);
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   118
    while (!serverQuit) {
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   119
        /* 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
   120
         * 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
   121
        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
   122
        if (NULL != csd) {
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   123
            DLog(@"Client found");
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   124
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   125
            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
   126
            [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
   127
            [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
   128
            [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
   129
            [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
   130
            [self sendToEngine:@"!"];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   131
6841
3633928a3188 let's outsmart the compiler for no particular benefit
koda
parents: 6832
diff changeset
   132
            memset(packedMap, 0, 128*32);
3633928a3188 let's outsmart the compiler for no particular benefit
koda
parents: 6832
diff changeset
   133
            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
   134
            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
   135
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   136
            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
   137
            serverQuit = YES;
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   138
        }
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   139
    }
11138
05853b1df2a2 - Property for port in MapPreviewButtonView
antonc27 <antonc27@mail.ru>
parents: 11137
diff changeset
   140
    [HWUtils freePort:self.port];
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   141
    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
   142
    SDLNet_Quit();
6841
3633928a3188 let's outsmart the compiler for no particular benefit
koda
parents: 6832
diff changeset
   143
3633928a3188 let's outsmart the compiler for no particular benefit
koda
parents: 6832
diff changeset
   144
    // 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
   145
    int k = 0;
3633928a3188 let's outsmart the compiler for no particular benefit
koda
parents: 6832
diff changeset
   146
    memset(unpackedMap, 255, 128*32*8);     // 255 is white
3633928a3188 let's outsmart the compiler for no particular benefit
koda
parents: 6832
diff changeset
   147
    for (int i = 0; i < 32*128; i++) {
3633928a3188 let's outsmart the compiler for no particular benefit
koda
parents: 6832
diff changeset
   148
        for (int j = 7; j >= 0; j--) {
3633928a3188 let's outsmart the compiler for no particular benefit
koda
parents: 6832
diff changeset
   149
            if (((packedMap[i] >> j) & 0x01) != 0)
3633928a3188 let's outsmart the compiler for no particular benefit
koda
parents: 6832
diff changeset
   150
                unpackedMap[k] = 170;       // level of gray [0-255]
3633928a3188 let's outsmart the compiler for no particular benefit
koda
parents: 6832
diff changeset
   151
            k++;
3633928a3188 let's outsmart the compiler for no particular benefit
koda
parents: 6832
diff changeset
   152
        }
3633928a3188 let's outsmart the compiler for no particular benefit
koda
parents: 6832
diff changeset
   153
    }
3633928a3188 let's outsmart the compiler for no particular benefit
koda
parents: 6832
diff changeset
   154
    return;
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   155
}
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   156
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   157
-(void) drawingThread {
11137
14f50dde3e8c - Using of @autoreleasepool is better
Anton Malmygin <antonc27@mail.ru>
parents: 11133
diff changeset
   158
    @autoreleasepool {
14f50dde3e8c - Using of @autoreleasepool is better
Anton Malmygin <antonc27@mail.ru>
parents: 11133
diff changeset
   159
    
6841
3633928a3188 let's outsmart the compiler for no particular benefit
koda
parents: 6832
diff changeset
   160
    uint8_t unpackedMap[128*32*8];
3633928a3188 let's outsmart the compiler for no particular benefit
koda
parents: 6832
diff changeset
   161
    [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
   162
6841
3633928a3188 let's outsmart the compiler for no particular benefit
koda
parents: 6832
diff changeset
   163
    // 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
   164
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray();
11133
fa3d227fb308 - Fix for retrieving 'Maze' preview genration
antonc27 <antonc27@mail.ru>
parents: 10108
diff changeset
   165
    CGContextRef bitmapImage = CGBitmapContextCreate(unpackedMap, 256, 128, 8, 256, colorspace, (CGBitmapInfo)kCGImageAlphaNone);
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   166
    CGColorSpaceRelease(colorspace);
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   167
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   168
    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
   169
    CGContextRelease(bitmapImage);
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   170
    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
   171
    CGImageRelease(previewCGImage);
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   172
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   173
    // 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
   174
    [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
   175
                           withObject:previewImage
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   176
                        waitUntilDone:NO];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   177
    [previewImage release];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   178
    [self performSelectorOnMainThread:@selector(setLabelText:)
11148
064a53861759 - Refactoring in order to remove some warning related to using of int instead of NSInteger
antonc27 <antonc27@mail.ru>
parents: 11138
diff changeset
   179
                           withObject:[NSString stringWithFormat:@"%ld", (long)maxHogs]
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   180
                        waitUntilDone:NO];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   181
    [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
   182
                           withObject:nil
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   183
                        waitUntilDone:NO];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   184
    [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
   185
                           withObject:nil
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   186
                        waitUntilDone:NO];
11137
14f50dde3e8c - Using of @autoreleasepool is better
Anton Malmygin <antonc27@mail.ru>
parents: 11133
diff changeset
   187
    
14f50dde3e8c - Using of @autoreleasepool is better
Anton Malmygin <antonc27@mail.ru>
parents: 11133
diff changeset
   188
    }
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   189
}
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   190
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   191
-(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
   192
    // 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
   193
    [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
   194
    [self setTitle:nil forState:UIControlStateNormal];
8441
a00b0fa0dbd7 some whitespaces from ios files
koda
parents: 7248
diff changeset
   195
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   196
    // 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
   197
    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
   198
        [self setTitle:NSLocalizedString(@"Preview not available",@"") forState:UIControlStateNormal];
4082
bfe14b38dad1 better support for wimpier i-devices
koda
parents: 3910
diff changeset
   199
        [self turnOnWidgets];
8441
a00b0fa0dbd7 some whitespaces from ios files
koda
parents: 7248
diff changeset
   200
    } else {
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   201
        // 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
   202
        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
   203
                                              initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   204
        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
   205
        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
   206
        [indicator startAnimating];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   207
        [self addSubview:indicator];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   208
        [indicator release];
8441
a00b0fa0dbd7 some whitespaces from ios files
koda
parents: 7248
diff changeset
   209
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   210
        // 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
   211
        [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
   212
    }
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   213
}
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
-(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
   216
    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
   217
    [self setImageRounded:image forState:UIControlStateNormal];
6219
c193881389c1 restore game config on iphone
koda
parents: 6115
diff changeset
   218
    self.backgroundColor = [UIColor whiteColor];
c193881389c1 restore game config on iphone
koda
parents: 6115
diff changeset
   219
    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
   220
    [image release];
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
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   223
-(void) removeIndicator {
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   224
    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
   225
    if (indicator) {
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   226
        [indicator stopAnimating];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   227
        [indicator removeFromSuperview];
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   228
    }
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   229
}
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   230
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   231
#pragma mark -
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   232
#pragma mark delegate
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   233
-(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
   234
    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
   235
        [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
   236
}
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   237
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   238
-(void) setLabelText:(NSString *)string {
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
   239
    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
   240
        [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
   241
}
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   242
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   243
-(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
   244
    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
   245
        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
   246
    return nil;
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   247
}
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   248
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
diff changeset
   249
@end