18 * File created on 22/04/2010. |
18 * File created on 22/04/2010. |
19 */ |
19 */ |
20 |
20 |
21 |
21 |
22 #import "MapConfigViewController.h" |
22 #import "MapConfigViewController.h" |
23 #import "PascalImports.h" |
23 #import <QuartzCore/QuartzCore.h> |
24 #import "CommodityFunctions.h" |
|
25 #import "UIImageExtra.h" |
|
26 #import "SchemeWeaponConfigViewController.h" |
24 #import "SchemeWeaponConfigViewController.h" |
27 #import "GameConfigViewController.h" |
25 #import "GameConfigViewController.h" |
|
26 |
28 |
27 |
29 #define scIndex self.segmentedControl.selectedSegmentIndex |
28 #define scIndex self.segmentedControl.selectedSegmentIndex |
30 #define isRandomness() (segmentedControl.selectedSegmentIndex == 0 || segmentedControl.selectedSegmentIndex == 2) |
29 #define isRandomness() (segmentedControl.selectedSegmentIndex == 0 || segmentedControl.selectedSegmentIndex == 2) |
31 |
30 |
32 @implementation MapConfigViewController |
31 @implementation MapConfigViewController |
33 @synthesize previewButton, maxHogs, seedCommand, templateFilterCommand, mapGenCommand, mazeSizeCommand, themeCommand, staticMapCommand, |
32 @synthesize previewButton, maxHogs, seedCommand, templateFilterCommand, mapGenCommand, mazeSizeCommand, themeCommand, staticMapCommand, |
34 missionCommand, tableView, maxLabel, sizeLabel, segmentedControl, slider, lastIndexPath, dataSourceArray, busy, |
33 missionCommand, tableView, maxLabel, sizeLabel, segmentedControl, slider, lastIndexPath, dataSourceArray, busy, |
35 externalController, parentController; |
34 oldPage, oldValue; |
36 |
35 |
37 |
36 |
38 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
37 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
39 return rotationManager(interfaceOrientation); |
38 return rotationManager(interfaceOrientation); |
40 } |
39 } |
41 |
40 |
42 -(IBAction) mapButtonPressed { |
41 -(IBAction) mapButtonPressed:(id) sender { |
43 [AudioManagerController playClickSound]; |
42 [AudioManagerController playClickSound]; |
44 [self updatePreview]; |
43 [self updatePreview]; |
45 } |
44 } |
46 |
45 |
47 -(void) updatePreview { |
46 -(void) updatePreview { |
48 // don't generate a new preview while it's already generating one |
47 // don't generate a new preview while it's already generating one |
49 if (busy) |
48 if (self.busy) |
50 return; |
49 return; |
51 |
50 |
52 // generate a seed |
51 // generate a seed |
53 CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault); |
52 CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault); |
54 NSString *seed = (NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuid); |
53 NSString *seed = (NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuid); |
55 CFRelease(uuid); |
54 CFRelease(uuid); |
56 NSString *seedCmd = [[NSString alloc] initWithFormat:@"eseed {%@}", seed]; |
55 NSString *seedCmd = [[NSString alloc] initWithFormat:@"eseed {%@}", seed]; |
57 self.seedCommand = seedCmd; |
56 self.seedCommand = seedCmd; |
58 [seedCmd release]; |
57 [seedCmd release]; |
59 |
58 |
60 if (self.dataSourceArray == nil) |
|
61 [self loadDataSourceArray]; |
|
62 NSArray *source = [self.dataSourceArray objectAtIndex:scIndex]; |
59 NSArray *source = [self.dataSourceArray objectAtIndex:scIndex]; |
63 NSIndexPath *theIndex; |
|
64 if (isRandomness()) { |
60 if (isRandomness()) { |
65 // prevent other events and add an activity while the preview is beign generated |
61 // prevent other events and add an activity while the preview is beign generated |
66 [self turnOffWidgets]; |
62 [self turnOffWidgets]; |
67 [self.previewButton updatePreviewWithSeed:seed]; |
63 [self.previewButton updatePreviewWithSeed:seed]; |
68 theIndex = [NSIndexPath indexPathForRow:(random()%[source count]) inSection:0]; |
|
69 } else { |
|
70 theIndex = [NSIndexPath indexPathForRow:(random()%[source count]) inSection:0]; |
|
71 // the preview for static maps is loaded in didSelectRowAtIndexPath |
64 // the preview for static maps is loaded in didSelectRowAtIndexPath |
72 } |
65 } |
73 [seed release]; |
66 [seed release]; |
74 |
67 |
75 // perform as if user clicked on an entry |
68 // perform as if user clicked on an entry |
|
69 NSIndexPath *theIndex = [NSIndexPath indexPathForRow:(random()%[source count]) inSection:0]; |
76 [self tableView:self.tableView didSelectRowAtIndexPath:theIndex]; |
70 [self tableView:self.tableView didSelectRowAtIndexPath:theIndex]; |
77 if (IS_NOT_POWERFUL(getModelType()) == NO) |
71 if (IS_NOT_POWERFUL([HWUtils modelType]) == NO) |
78 [self.tableView scrollToRowAtIndexPath:theIndex atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; |
72 [self.tableView scrollToRowAtIndexPath:theIndex atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; |
79 } |
73 } |
80 |
74 |
81 -(void) turnOffWidgets { |
75 -(void) turnOffWidgets { |
82 busy = YES; |
76 busy = YES; |
83 self.previewButton.alpha = 0.5f; |
77 self.previewButton.alpha = 0.5f; |
84 self.previewButton.enabled = NO; |
78 self.previewButton.enabled = NO; |
85 self.maxLabel.text = @"..."; |
79 self.maxLabel.text = NSLocalizedString(@"Loading...",@"");; |
86 self.segmentedControl.enabled = NO; |
80 self.segmentedControl.enabled = NO; |
87 self.slider.enabled = NO; |
81 self.slider.enabled = NO; |
88 } |
82 } |
89 |
83 |
|
84 #pragma mark - |
|
85 #pragma mark MapPreviewButtonView delegate methods |
90 -(void) turnOnWidgets { |
86 -(void) turnOnWidgets { |
91 self.previewButton.alpha = 1.0f; |
87 self.previewButton.alpha = 1.0f; |
92 self.previewButton.enabled = YES; |
88 self.previewButton.enabled = YES; |
93 self.segmentedControl.enabled = YES; |
89 self.segmentedControl.enabled = YES; |
94 self.slider.enabled = YES; |
90 self.slider.enabled = YES; |
95 busy = NO; |
91 self.busy = NO; |
96 } |
92 } |
97 |
93 |
98 -(void) setLabelText:(NSString *)str { |
94 -(void) setMaxLabelText:(NSString *)str { |
99 self.maxHogs = [str intValue]; |
95 self.maxHogs = [str intValue]; |
100 self.maxLabel.text = str; |
96 self.maxLabel.text = [NSString stringWithFormat:@"%@ %@",NSLocalizedString(@"Max Hogs:",@""),str]; |
101 } |
97 } |
102 |
98 |
103 -(NSDictionary *)getDataForEngine { |
99 -(NSDictionary *)getDataForEngine { |
104 NSDictionary *dictForEngine = [NSDictionary dictionaryWithObjectsAndKeys: |
100 NSDictionary *dictForEngine = [NSDictionary dictionaryWithObjectsAndKeys: |
105 self.seedCommand,@"seedCommand", |
101 self.seedCommand,@"seedCommand", |
128 |
122 |
129 UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
123 UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
130 if (cell == nil) |
124 if (cell == nil) |
131 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
125 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
132 |
126 |
133 if (self.dataSourceArray == nil) |
|
134 [self loadDataSourceArray]; |
|
135 NSArray *source = [self.dataSourceArray objectAtIndex:scIndex]; |
127 NSArray *source = [self.dataSourceArray objectAtIndex:scIndex]; |
136 |
128 |
137 NSString *labelString = [source objectAtIndex:row]; |
129 NSString *labelString = [source objectAtIndex:row]; |
138 cell.textLabel.text = labelString; |
130 cell.textLabel.text = labelString; |
139 cell.textLabel.adjustsFontSizeToFitWidth = YES; |
131 cell.textLabel.adjustsFontSizeToFitWidth = YES; |
140 cell.textLabel.minimumFontSize = 7; |
132 cell.textLabel.minimumFontSize = 7; |
141 cell.textLabel.textColor = UICOLOR_HW_YELLOW_TEXT; |
133 cell.textLabel.textColor = [UIColor lightYellowColor]; |
142 cell.textLabel.backgroundColor = [UIColor clearColor]; |
134 cell.textLabel.backgroundColor = [UIColor clearColor]; |
143 |
135 |
144 if (isRandomness()) { |
136 if (isRandomness()) { |
145 UIImage *image = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@/icon.png",THEMES_DIRECTORY(),labelString]]; |
137 UIImage *image = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@/icon.png",THEMES_DIRECTORY(),labelString]]; |
146 cell.imageView.image = image; |
138 cell.imageView.image = image; |
322 mapgen = @"e$mapgen 0"; |
309 mapgen = @"e$mapgen 0"; |
323 staticmap = @""; |
310 staticmap = @""; |
324 mission = @""; |
311 mission = @""; |
325 [self sliderChanged:nil]; |
312 [self sliderChanged:nil]; |
326 self.slider.enabled = YES; |
313 self.slider.enabled = YES; |
327 [externalController fillSections]; |
314 [SchemeWeaponConfigViewController fillInstanceSections]; |
328 break; |
315 break; |
329 |
316 |
330 case 1: // Map |
317 case 1: // Map |
331 mapgen = @"e$mapgen 0"; |
318 mapgen = @"e$mapgen 0"; |
332 // dummy values, these are set by -updatePreview -> -didSelectRowAtIndexPath -> -setDetailsForStaticMap |
319 // dummy values, these are set by -updatePreview -> -didSelectRowAtIndexPath -> -setDetailsForStaticMap |
333 staticmap = @"map Bamboo"; |
320 staticmap = @"map Bamboo"; |
334 mission = @""; |
321 mission = @""; |
335 self.slider.enabled = NO; |
322 self.slider.enabled = NO; |
336 self.sizeLabel.text = NSLocalizedString(@"No filter",@""); |
323 self.sizeLabel.text = NSLocalizedString(@"No filter",@""); |
337 [externalController fillSections]; |
324 [SchemeWeaponConfigViewController fillInstanceSections]; |
338 break; |
325 break; |
339 |
326 |
340 case 2: // Maze |
327 case 2: // Maze |
341 mapgen = @"e$mapgen 1"; |
328 mapgen = @"e$mapgen 1"; |
342 staticmap = @""; |
329 staticmap = @""; |
343 mission = @""; |
330 mission = @""; |
344 [self sliderChanged:nil]; |
331 [self sliderChanged:nil]; |
345 self.slider.enabled = YES; |
332 self.slider.enabled = YES; |
346 [externalController fillSections]; |
333 [SchemeWeaponConfigViewController fillInstanceSections]; |
347 break; |
334 break; |
348 |
335 |
349 case 3: // Mission |
336 case 3: // Mission |
350 mapgen = @"e$mapgen 0"; |
337 mapgen = @"e$mapgen 0"; |
351 // dummy values, these are set by -updatePreview -> -didSelectRowAtIndexPath -> -setDetailsForStaticMap |
338 // dummy values, these are set by -updatePreview -> -didSelectRowAtIndexPath -> -setDetailsForStaticMap |
352 staticmap = @"map Bamboo"; |
339 staticmap = @"map Bamboo"; |
353 mission = @""; |
340 mission = @""; |
354 self.slider.enabled = NO; |
341 self.slider.enabled = NO; |
355 self.sizeLabel.text = NSLocalizedString(@"No filter",@""); |
342 self.sizeLabel.text = NSLocalizedString(@"No filter",@""); |
356 [externalController emptySections]; |
343 [SchemeWeaponConfigViewController emptyInstanceSections]; |
357 break; |
344 break; |
358 |
345 |
359 default: |
346 default: |
360 mapgen = nil; |
347 mapgen = nil; |
361 staticmap = nil; |
348 staticmap = nil; |
369 [self.tableView reloadData]; |
356 [self.tableView reloadData]; |
370 [self updatePreview]; |
357 [self updatePreview]; |
371 oldPage = newPage; |
358 oldPage = newPage; |
372 } |
359 } |
373 |
360 |
374 -(IBAction) buttonPressed:(id) sender { |
|
375 [self.parentController buttonPressed:sender]; |
|
376 } |
|
377 |
|
378 #pragma mark - |
361 #pragma mark - |
379 #pragma mark view management |
362 #pragma mark view management |
380 -(void) loadDataSourceArray { |
363 -(NSArray *) dataSourceArray { |
381 NSString *model = getModelType(); |
364 if (dataSourceArray == nil) { |
382 |
365 NSString *model = [HWUtils modelType]; |
383 // only folders containing icon.png are a valid theme |
366 |
384 NSArray *themeArrayFull = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:THEMES_DIRECTORY() error:NULL]; |
367 // only folders containing icon.png are a valid theme |
385 NSMutableArray *themeArray = [[NSMutableArray alloc] init]; |
368 NSArray *themeArrayFull = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:THEMES_DIRECTORY() error:NULL]; |
386 for (NSString *themeName in themeArrayFull) { |
369 NSMutableArray *themeArray = [[NSMutableArray alloc] init]; |
387 NSString *checkPath = [[NSString alloc] initWithFormat:@"%@/%@/icon.png",THEMES_DIRECTORY(),themeName]; |
370 for (NSString *themeName in themeArrayFull) { |
388 if ([[NSFileManager defaultManager] fileExistsAtPath:checkPath]) |
371 NSString *checkPath = [[NSString alloc] initWithFormat:@"%@/%@/icon.png",THEMES_DIRECTORY(),themeName]; |
389 [themeArray addObject:themeName]; |
372 if ([[NSFileManager defaultManager] fileExistsAtPath:checkPath]) |
390 [checkPath release]; |
373 [themeArray addObject:themeName]; |
391 } |
374 [checkPath release]; |
392 |
375 } |
393 // remove images that are too big for certain devices without loading the whole image |
376 |
394 NSArray *mapArrayFull = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:MAPS_DIRECTORY() error:NULL]; |
377 // remove images that are too big for certain devices without loading the whole image |
395 NSMutableArray *mapArray = [[NSMutableArray alloc] init]; |
378 NSArray *mapArrayFull = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:MAPS_DIRECTORY() error:NULL]; |
396 for (NSString *str in mapArrayFull) { |
379 NSMutableArray *mapArray = [[NSMutableArray alloc] init]; |
397 CGSize imgSize = PSPNGSizeFromMetaData([MAPS_DIRECTORY() stringByAppendingFormat:@"%@/map.png",str]); |
380 for (NSString *str in mapArrayFull) { |
398 if (IS_NOT_POWERFUL(model) && imgSize.height > 1024.0f) |
381 CGSize imgSize = [UIImage imageSizeFromMetadataOf:[MAPS_DIRECTORY() stringByAppendingFormat:@"%@/map.png",str]]; |
399 continue; |
382 if (IS_NOT_POWERFUL(model) && imgSize.height > 1024.0f) |
400 if (IS_NOT_VERY_POWERFUL(model) && imgSize.height > 1280.0f) |
383 continue; |
401 continue; |
384 if (IS_NOT_VERY_POWERFUL(model) && imgSize.height > 1280.0f) |
402 [mapArray addObject:str]; |
385 continue; |
403 } |
386 [mapArray addObject:str]; |
404 |
387 } |
405 NSArray *missionArrayFull = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:MISSIONS_DIRECTORY() error:NULL]; |
388 |
406 NSMutableArray *missionArray = [[NSMutableArray alloc] init]; |
389 NSArray *missionArrayFull = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:MISSIONS_DIRECTORY() error:NULL]; |
407 for (NSString *str in missionArrayFull) { |
390 NSMutableArray *missionArray = [[NSMutableArray alloc] init]; |
408 CGSize imgSize = PSPNGSizeFromMetaData([MISSIONS_DIRECTORY() stringByAppendingFormat:@"%@/map.png",str]); |
391 for (NSString *str in missionArrayFull) { |
409 if (IS_NOT_POWERFUL(model) && imgSize.height > 1024.0f) |
392 CGSize imgSize = [UIImage imageSizeFromMetadataOf:[MISSIONS_DIRECTORY() stringByAppendingFormat:@"%@/map.png",str]]; |
410 continue; |
393 if (IS_NOT_POWERFUL(model) && imgSize.height > 1024.0f) |
411 if (IS_NOT_VERY_POWERFUL(model) && imgSize.height > 1280.0f) |
394 continue; |
412 continue; |
395 if (IS_NOT_VERY_POWERFUL(model) && imgSize.height > 1280.0f) |
413 [missionArray addObject:str]; |
396 continue; |
414 } |
397 [missionArray addObject:str]; |
415 NSArray *array = [[NSArray alloc] initWithObjects:themeArray,mapArray,themeArray,missionArray,nil]; |
398 } |
416 [missionArray release]; |
399 NSArray *array = [[NSArray alloc] initWithObjects:themeArray,mapArray,themeArray,missionArray,nil]; |
417 [themeArray release]; |
400 [missionArray release]; |
418 [mapArray release]; |
401 [themeArray release]; |
419 |
402 [mapArray release]; |
420 self.dataSourceArray = array; |
403 |
421 [array release]; |
404 self.dataSourceArray = array; |
|
405 [array release]; |
|
406 } |
|
407 return dataSourceArray; |
|
408 } |
|
409 |
|
410 -(MapPreviewButtonView *)previewButton { |
|
411 if (previewButton == nil) { |
|
412 MapPreviewButtonView *preview = [[MapPreviewButtonView alloc] initWithFrame:CGRectMake(32, 26, 256, 128)]; |
|
413 preview.delegate = self; |
|
414 [preview addTarget:self action:@selector(mapButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; |
|
415 [self.view addSubview:preview]; |
|
416 self.previewButton = preview; |
|
417 [preview release]; |
|
418 } |
|
419 return previewButton; |
422 } |
420 } |
423 |
421 |
424 -(void) viewDidLoad { |
422 -(void) viewDidLoad { |
425 [super viewDidLoad]; |
423 [super viewDidLoad]; |
426 |
424 |
427 srandom(time(NULL)); |
425 srandom(time(NULL)); |
428 |
426 |
|
427 /* |
429 CGSize screenSize = [[UIScreen mainScreen] bounds].size; |
428 CGSize screenSize = [[UIScreen mainScreen] bounds].size; |
430 self.view.frame = CGRectMake(0, 0, screenSize.height, screenSize.width - 44); |
429 self.view.frame = CGRectMake(0, 0, screenSize.height, screenSize.width - 44); |
|
430 */ |
431 |
431 |
432 // initialize some "default" values |
432 // initialize some "default" values |
433 self.sizeLabel.text = NSLocalizedString(@"All",@""); |
|
434 self.slider.value = 0.05f; |
433 self.slider.value = 0.05f; |
435 oldValue = 5; |
434 self.slider.enabled = NO; |
436 |
435 self.sizeLabel.text = NSLocalizedString(@"No filter",@""); |
437 busy = NO; |
436 self.oldValue = 5; |
438 [self loadDataSourceArray]; |
437 self.busy = NO; |
439 self.lastIndexPath = [NSIndexPath indexPathForRow:-1 inSection:0]; |
438 self.oldPage = self.segmentedControl.selectedSegmentIndex; |
440 |
|
441 // select a map at first because it's faster - done in IB |
|
442 oldPage = 1; |
|
443 if (self.segmentedControl.selectedSegmentIndex == 1) { |
|
444 self.slider.enabled = NO; |
|
445 self.sizeLabel.text = NSLocalizedString(@"No filter",@""); |
|
446 } |
|
447 |
439 |
448 self.templateFilterCommand = @"e$template_filter 0"; |
440 self.templateFilterCommand = @"e$template_filter 0"; |
449 self.mazeSizeCommand = @"e$maze_size 0"; |
441 self.mazeSizeCommand = @"e$maze_size 0"; |
450 self.mapGenCommand = @"e$mapgen 0"; |
442 self.mapGenCommand = @"e$mapgen 0"; |
451 self.staticMapCommand = @""; |
443 self.staticMapCommand = @""; |
452 self.missionCommand = @""; |
444 self.missionCommand = @""; |
453 |
445 |
454 if ([self.tableView respondsToSelector:@selector(setBackgroundView:)]) |
446 if (IS_IPAD()) { |
455 [self.tableView setBackgroundView:nil]; |
447 [self.tableView setBackgroundColorForAnyTable:[UIColor darkBlueColorTransparent]]; |
456 self.tableView.backgroundColor = [UIColor clearColor]; |
448 self.tableView.layer.borderColor = [[UIColor darkYellowColor] CGColor]; |
457 self.tableView.separatorColor = UICOLOR_HW_YELLOW_BODER; |
449 self.tableView.layer.borderWidth = 2.7f; |
|
450 self.tableView.layer.cornerRadius = 8; |
|
451 self.tableView.contentInset = UIEdgeInsetsMake(10, 0, 10, 0); |
|
452 |
|
453 UILabel *backLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 14, 300, 190) andTitle:nil withBorderWidth:2.3f]; |
|
454 [self.view insertSubview:backLabel belowSubview:self.segmentedControl]; |
|
455 [backLabel release]; |
|
456 } |
|
457 self.tableView.separatorColor = [UIColor whiteColor]; |
458 self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; |
458 self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; |
459 } |
459 } |
460 |
460 |
461 -(void) viewWillAppear:(BOOL)animated { |
461 -(void) viewWillAppear:(BOOL)animated { |
462 if (self.dataSourceArray == nil) |
|
463 [self loadDataSourceArray]; |
|
464 [super viewWillAppear:animated]; |
462 [super viewWillAppear:animated]; |
465 } |
463 } |
466 |
464 |
467 -(void) viewDidAppear:(BOOL) animated { |
465 -(void) viewDidAppear:(BOOL) animated { |
468 [self updatePreview]; |
466 [self updatePreview]; |