cocoaTouch/MapConfigViewController.m
changeset 3369 c7289e42f0ee
parent 3366 f0e5ff24fb72
child 3373 c1ff724a5c34
equal deleted inserted replaced
3368:791fa4664209 3369:c7289e42f0ee
    11 #import "CommodityFunctions.h"
    11 #import "CommodityFunctions.h"
    12 #import "UIImageExtra.h"
    12 #import "UIImageExtra.h"
    13 #import "SDL_net.h"
    13 #import "SDL_net.h"
    14 #import <pthread.h>
    14 #import <pthread.h>
    15 
    15 
       
    16 #define INDICATOR_TAG 7654
       
    17 #define RANDOMSTR_LEN 36
    16 @implementation MapConfigViewController
    18 @implementation MapConfigViewController
    17 @synthesize previewButton, maxHogs, seedCommand;
    19 @synthesize previewButton, maxHogs, seedCommand, templateFilterCommand, mapGenCommand, mazeSizeCommand,
       
    20             tableView, maxLabel, sizeLabel, segmentedControl, slider;
    18 
    21 
    19 
    22 
    20 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    23 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    21     return rotationManager(interfaceOrientation);
    24     return rotationManager(interfaceOrientation);
    22 }
    25 }
    60 		csd = SDLNet_TCP_Accept(sd);
    63 		csd = SDLNet_TCP_Accept(sd);
    61 		if (NULL != csd) {			
    64 		if (NULL != csd) {			
    62 			NSLog(@"engineProtocol - Client found");
    65 			NSLog(@"engineProtocol - Client found");
    63             
    66             
    64             [self sendToEngine:self.seedCommand];
    67             [self sendToEngine:self.seedCommand];
    65             [self sendToEngine:@"e$template_filter 1"];
    68             [self sendToEngine:self.templateFilterCommand];
    66             [self sendToEngine:@"e$mapgen 0"];
    69             [self sendToEngine:self.mapGenCommand];
    67             [self sendToEngine:@"e$maze_size 1"];
    70             [self sendToEngine:self.mazeSizeCommand];
    68             [self sendToEngine:@"!"];
    71             [self sendToEngine:@"!"];
    69                 
    72                 
    70             memset(map, 0, 128*32);
    73             memset(map, 0, 128*32);
    71             SDLNet_TCP_Recv(csd, map, 128*32);
    74             SDLNet_TCP_Recv(csd, map, 128*32);
    72             SDLNet_TCP_Recv(csd, &maxHogs, sizeof(Uint8));
    75             SDLNet_TCP_Recv(csd, &maxHogs, sizeof(Uint8));
    78 	
    81 	
    79 	SDLNet_TCP_Close(sd);
    82 	SDLNet_TCP_Close(sd);
    80 	SDLNet_Quit();
    83 	SDLNet_Quit();
    81 }
    84 }
    82 
    85 
    83 
    86 -(void) drawingThread {
    84 -(void) updatePreview {
    87     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
       
    88     
       
    89     // select the port for IPC and launch the preview generation
       
    90     int port = randomPort();
    85     pthread_t thread_id;
    91     pthread_t thread_id;
    86     
       
    87     // generate a seed
       
    88     char randomStr[36];
       
    89     for (int i = 0; i<36; i++) {
       
    90          randomStr[i] = random()%255;
       
    91     }
       
    92     NSString *seedCmd = [[NSString alloc] initWithFormat:@"eseed {%s}", randomStr];
       
    93     self.seedCommand = seedCmd;
       
    94     [seedCmd release];
       
    95     
       
    96     // select the port for IPC
       
    97     int port = randomPort();
       
    98     pthread_create(&thread_id, NULL, (void *)GenLandPreview, (void *)port);
    92     pthread_create(&thread_id, NULL, (void *)GenLandPreview, (void *)port);
    99     [self engineProtocol:port];
    93     [self engineProtocol:port];
   100 
    94 
   101     // draw the buffer (1 pixel per component, 0= transparent 1= color)
    95     // draw the buffer (1 pixel per component, 0= transparent 1= color)
   102     int xc = 0;
    96     int xc = 0;
   103     int yc = 0;
    97     int yc = 0;
   104     UIGraphicsBeginImageContext(CGSizeMake(256,128));      
    98     UIGraphicsBeginImageContext(CGSizeMake(256,128));      
   105     CGContextRef context = UIGraphicsGetCurrentContext();       
    99     CGContextRef context = UIGraphicsGetCurrentContext();       
   106     UIGraphicsPushContext(context);  
   100     UIGraphicsPushContext(context);  
   107     for (int x = 0; x < 32*128; x++) {
   101     for (int i = 0; i < 32*128; i++) {
   108         unsigned char byte = map[x];
   102         unsigned char byte = map[i];
   109         for (int z = 0; z < 8; z++) {
   103         for (int j = 0; j < 8; j++) {
   110             // select the color based on the rightmost bit
   104             // select the color based on the rightmost bit
   111             if ((byte & 0x00000001) != 0)
   105             if ((byte & 0x00000001) != 0)
   112                 CGContextSetRGBFillColor(context, 0.5, 0.5, 0.7, 1.0);
   106                 CGContextSetRGBFillColor(context, 0.5, 0.5, 0.7, 1.0);
   113             else
   107             else
   114                 CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 0.0);
   108                 CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 0.0);
   115             
   109             
   116             // draw pixel
   110             // draw pixel
   117             CGContextFillRect(context,CGRectMake(xc,yc,1,1));
   111             CGContextFillRect(context,CGRectMake(xc,yc,1,1));
   118             // move coordinates
   112             // move coordinates
   119             xc = (xc+1)%256;
   113             xc = (xc + 1) % 256;
   120             if (xc == 0) yc++;
   114             if (xc == 0) yc++;
   121             
   115             
   122             // shift to next bit
   116             // shift to next bit
   123             byte = byte >> 1;
   117             byte = byte >> 1;
   124         }
   118         }
   125     }
   119     }
   126     UIGraphicsPopContext();
   120     UIGraphicsPopContext();
   127     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
   121     UIImage *previewImage = UIGraphicsGetImageFromCurrentImageContext();
   128     UIGraphicsEndImageContext();
   122     UIGraphicsEndImageContext();
   129     
   123     
   130     // set the image in the button
   124 
   131     [self.previewButton setImage:image forState:UIControlStateNormal];
   125     // set the preview image (autoreleased) in the button and the maxhog label
   132     
   126     [self.previewButton setBackgroundImage:previewImage forState:UIControlStateNormal];
       
   127     self.maxLabel.text = [NSString stringWithFormat:@"%d", maxHogs];
       
   128     
       
   129     // restore functionality of button and remove the spinning wheel
       
   130     [self turnOnWidgets];
       
   131     UIActivityIndicatorView *indicator = (UIActivityIndicatorView *)[self.previewButton viewWithTag:INDICATOR_TAG];
       
   132     [indicator stopAnimating];
       
   133     [indicator removeFromSuperview];
       
   134     
       
   135     [pool release];
       
   136     [NSThread exit];
       
   137 
   133     /*
   138     /*
   134     // http://developer.apple.com/mac/library/qa/qa2001/qa1037.html
   139     // http://developer.apple.com/mac/library/qa/qa2001/qa1037.html
   135     CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray();
   140     CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray();
   136     CGContextRef bitmapImage = CGBitmapContextCreate(mapExp, 128, 32, 8, 128, colorspace, kCGImageAlphaNone);
   141     CGContextRef bitmapImage = CGBitmapContextCreate(mapExp, 128, 32, 8, 128, colorspace, kCGImageAlphaNone);
   137     CGColorSpaceRelease(colorspace);
   142     CGColorSpaceRelease(colorspace);
   140     UIImage *previewImage = [[UIImage alloc] initWithCGImage:previewCGImage];
   145     UIImage *previewImage = [[UIImage alloc] initWithCGImage:previewCGImage];
   141     CGImageRelease(previewCGImage);
   146     CGImageRelease(previewCGImage);
   142     */
   147     */
   143 }
   148 }
   144 
   149 
       
   150 -(void) turnOffWidgets {
       
   151     self.previewButton.alpha = 0.5f;
       
   152     self.previewButton.enabled = NO;
       
   153     self.maxLabel.text = @"...";
       
   154     self.segmentedControl.enabled = NO;
       
   155     self.tableView.allowsSelection = NO;
       
   156     self.slider.enabled = NO;
       
   157 }
       
   158 
       
   159 -(void) turnOnWidgets {
       
   160     self.previewButton.alpha = 1.0f;
       
   161     self.previewButton.enabled = YES;
       
   162     self.segmentedControl.enabled = YES;
       
   163     self.tableView.allowsSelection = YES;
       
   164     self.slider.enabled = YES;
       
   165 }
       
   166 
       
   167 -(IBAction) updatePreview {
       
   168     // prevent other events and add an activity while the preview is beign generated
       
   169     [self turnOffWidgets];
       
   170     
       
   171     // remove the current preview
       
   172     [self.previewButton setImage:nil forState:UIControlStateNormal];
       
   173     
       
   174     // add a very nice spinning wheel
       
   175     UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] 
       
   176                                           initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
       
   177     indicator.center = CGPointMake(previewButton.bounds.size.width / 2, previewButton.bounds.size.height / 2);
       
   178     indicator.tag = INDICATOR_TAG;
       
   179     [indicator startAnimating];
       
   180     [self.previewButton addSubview:indicator];
       
   181     [indicator release];
       
   182 
       
   183     // generate a seed
       
   184     char randomStr[RANDOMSTR_LEN+1];
       
   185     for (int i = 0; i < RANDOMSTR_LEN; ) {
       
   186         randomStr[i] = random() % 255;
       
   187         if (randomStr[i] >= '0' && randomStr[i] <= '9' || randomStr[i] >= 'a' && randomStr[i] <= 'z') 
       
   188             i++;
       
   189     }
       
   190     randomStr[ 8] = '-';
       
   191     randomStr[13] = '-';
       
   192     randomStr[18] = '-';
       
   193     randomStr[23] = '-';
       
   194     randomStr[RANDOMSTR_LEN] = '\0';
       
   195     NSString *seedCmd = [[NSString alloc] initWithFormat:@"eseed {%s}", randomStr];
       
   196     self.seedCommand = seedCmd;
       
   197     [seedCmd release];
       
   198     
       
   199     // let's draw in a separate thread so the gui can work; also it restores the preview button
       
   200     [NSThread detachNewThreadSelector:@selector(drawingThread) toTarget:self withObject:nil];
       
   201 }
       
   202 
       
   203 #pragma mark -
       
   204 #pragma mark Table view data source
       
   205 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
       
   206     return 1;
       
   207 }
       
   208 
       
   209 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
       
   210     return 1;
       
   211 }
       
   212 
       
   213 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
       
   214     static NSString *CellIdentifier = @"Cell";
       
   215     
       
   216     UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
       
   217     if (cell == nil) 
       
   218         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
       
   219     
       
   220     return cell;
       
   221 }
       
   222 
       
   223 #pragma mark -
       
   224 #pragma mark slider & segmentedControl
       
   225 -(IBAction) sliderChanged:(id) sender {
       
   226     NSString *labelText;
       
   227     NSString *templateCommand;
       
   228     NSString *mazeCommand;
       
   229     
       
   230     switch ((int)(slider.value*100)) {
       
   231         case 0:
       
   232             if (self.segmentedControl.selectedSegmentIndex == 0) {
       
   233                 labelText = NSLocalizedString(@"Wacky",@"");
       
   234             } else {
       
   235                 labelText = NSLocalizedString(@"Large Floating Islands",@"");
       
   236             }
       
   237             templateCommand = @"e$template_filter 5";
       
   238             mazeCommand = @"e$maze_size 5";
       
   239             break;
       
   240         case 1:
       
   241             if (self.segmentedControl.selectedSegmentIndex == 0) {
       
   242                 labelText = NSLocalizedString(@"Cavern",@"");
       
   243             } else {
       
   244                 labelText = NSLocalizedString(@"Medium Floating Islands",@"");
       
   245             }
       
   246             templateCommand = @"e$template_filter 4";
       
   247             mazeCommand = @"e$maze_size 4";
       
   248             break;
       
   249         case 2:
       
   250             if (self.segmentedControl.selectedSegmentIndex == 0) {
       
   251                 labelText = NSLocalizedString(@"Small",@"");
       
   252             } else {
       
   253                 labelText = NSLocalizedString(@"Small Floating Islands",@"");
       
   254             }
       
   255             templateCommand = @"e$template_filter 1";
       
   256             mazeCommand = @"e$maze_size 3";
       
   257             break;
       
   258         case 3:
       
   259             if (self.segmentedControl.selectedSegmentIndex == 0) {
       
   260                 labelText = NSLocalizedString(@"Medium",@"");
       
   261             } else {
       
   262                 labelText = NSLocalizedString(@"Large Tunnels",@"");
       
   263             }
       
   264             templateCommand = @"e$template_filter 2";
       
   265             mazeCommand = @"e$maze_size 2";
       
   266             break;
       
   267         case 4:
       
   268             if (self.segmentedControl.selectedSegmentIndex == 0) {
       
   269                 labelText = NSLocalizedString(@"Large",@"");
       
   270             } else {
       
   271                 labelText = NSLocalizedString(@"Medium Tunnels",@"");
       
   272             }
       
   273             templateCommand = @"e$template_filter 3";
       
   274             mazeCommand = @"e$maze_size 1";
       
   275             break;
       
   276         case 5:
       
   277             if (self.segmentedControl.selectedSegmentIndex == 0) {
       
   278                 labelText = NSLocalizedString(@"All",@"");
       
   279             } else {
       
   280                 labelText = NSLocalizedString(@"Small Tunnels",@"");
       
   281             }
       
   282             templateCommand = @"e$template_filter 0";
       
   283             mazeCommand = @"e$maze_size 0";
       
   284             break;
       
   285         default:
       
   286             break;
       
   287     }
       
   288     self.sizeLabel.text = labelText;
       
   289     self.templateFilterCommand = templateCommand;
       
   290     self.mazeSizeCommand = mazeCommand;
       
   291 }
       
   292 
       
   293 // update preview as soon as the user lifts its finger
       
   294 -(IBAction) sliderEndedChanging:(id) sender {
       
   295     if (self.previewButton.enabled == YES)
       
   296         [self updatePreview];
       
   297 }
       
   298 
       
   299 -(IBAction) segmentedControlChanged:(id) sender {
       
   300     NSString *mapgen;
       
   301     
       
   302     switch (segmentedControl.selectedSegmentIndex) {
       
   303         case 0: // Random
       
   304             mapgen = @"e$mapgen 0";
       
   305             [self sliderChanged:nil];
       
   306             if (self.previewButton.enabled == YES)
       
   307                 [self updatePreview];
       
   308             break;
       
   309         case 1: // Map
       
   310             mapgen = @"e$mapgen 0";
       
   311             // other stuff
       
   312             break;
       
   313         case 2: // Maze
       
   314             mapgen = @"e$mapgen 1";
       
   315             [self sliderChanged:nil];
       
   316             if (self.previewButton.enabled == YES)
       
   317                 [self updatePreview];
       
   318 
       
   319             break;
       
   320     }
       
   321     self.mapGenCommand = mapgen;
       
   322 }
       
   323 
   145 #pragma mark -
   324 #pragma mark -
   146 #pragma mark view management
   325 #pragma mark view management
   147 -(void) viewDidLoad {
   326 -(void) viewDidLoad {
   148     srandom(time(NULL));
   327     srandom(time(NULL));
   149     [super viewDidLoad];
   328     [super viewDidLoad];
   150 
   329 
   151     CGSize screenSize = [[UIScreen mainScreen] bounds].size;
   330     CGSize screenSize = [[UIScreen mainScreen] bounds].size;
   152     self.view.frame = CGRectMake(0, 0, screenSize.height, screenSize.width - 44);
   331     self.view.frame = CGRectMake(0, 0, screenSize.height, screenSize.width - 44);
   153     
   332 
   154     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
   333     self.sizeLabel.text = NSLocalizedString(@"All",@"");
   155     button.frame = CGRectMake(32, 32, 256, 128);
   334     self.templateFilterCommand = @"e$template_filter 0";
   156     [button addTarget:self action:@selector(updatePreview) forControlEvents:UIControlEventTouchUpInside];
   335     self.segmentedControl.selectedSegmentIndex == 0;
   157     self.previewButton = button;
   336     self.mazeSizeCommand = @"e$maze_size 0";
   158     [button release];
   337     self.mapGenCommand = @"e$mapgen 0";
   159     [self.view addSubview:self.previewButton];
       
   160 }
   338 }
   161 
   339 
   162 -(void) viewWillAppear:(BOOL)animated {
   340 -(void) viewWillAppear:(BOOL)animated {
   163     [super viewWillAppear:animated];
   341     [super viewWillAppear:animated];
   164     [self updatePreview];
   342     [self updatePreview];
   173 #pragma mark -
   351 #pragma mark -
   174 #pragma mark memory
   352 #pragma mark memory
   175 -(void) viewDidUnload {
   353 -(void) viewDidUnload {
   176     self.previewButton = nil;
   354     self.previewButton = nil;
   177     self.seedCommand = nil;
   355     self.seedCommand = nil;
       
   356     self.seedCommand = nil;
       
   357     self.templateFilterCommand = nil;
       
   358     self.mapGenCommand = nil;
       
   359     self.mazeSizeCommand = nil;
       
   360     self.previewButton = nil;
       
   361     self.tableView = nil;
       
   362     self.maxLabel = nil;
       
   363     self.sizeLabel = nil;
       
   364     self.segmentedControl = nil;
       
   365     self.slider = nil;
   178     [super viewDidUnload];
   366     [super viewDidUnload];
   179     // Release any retained subviews of the main view.
       
   180     // e.g. self.myOutlet = nil;
       
   181 }
   367 }
   182 
   368 
   183 -(void) dealloc {
   369 -(void) dealloc {
       
   370     [seedCommand release];
       
   371     [templateFilterCommand release];
       
   372     [mapGenCommand release];
       
   373     [mazeSizeCommand release];
   184     [previewButton release];
   374     [previewButton release];
   185     [seedCommand release];
   375     [tableView release];
       
   376     [maxLabel release];
       
   377     [sizeLabel release];
       
   378     [segmentedControl release];
       
   379     [slider release];
   186     [super dealloc];
   380     [super dealloc];
   187 }
   381 }
   188 
   382 
   189 
   383 
   190 @end
   384 @end