28 @synthesize previewButton, maxHogs, seedCommand, templateFilterCommand, mapGenCommand, mazeSizeCommand, themeCommand, staticMapCommand, |
28 @synthesize previewButton, maxHogs, seedCommand, templateFilterCommand, mapGenCommand, mazeSizeCommand, themeCommand, staticMapCommand, |
29 missionCommand, tableView, maxLabel, segmentedControl, slider, lastIndexPath, dataSourceArray, busy, |
29 missionCommand, tableView, maxLabel, segmentedControl, slider, lastIndexPath, dataSourceArray, busy, |
30 oldPage, oldValue; |
30 oldPage, oldValue; |
31 |
31 |
32 |
32 |
33 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
33 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
34 return rotationManager(interfaceOrientation); |
34 return rotationManager(interfaceOrientation); |
35 } |
35 } |
36 |
36 |
37 -(IBAction) mapButtonPressed:(id) sender { |
37 - (IBAction)mapButtonPressed:(id)sender { |
38 [[AudioManagerController mainManager] playClickSound]; |
38 [[AudioManagerController mainManager] playClickSound]; |
39 [self updatePreview]; |
39 [self updatePreview]; |
40 } |
40 } |
41 |
41 |
42 -(void) updatePreview { |
42 - (void)updatePreview { |
43 // don't generate a new preview while it's already generating one |
43 // don't generate a new preview while it's already generating one |
44 if (self.busy) |
44 if (self.busy) |
45 return; |
45 return; |
46 |
46 |
47 // generate a seed |
47 // generate a seed |
48 NSString *seed = [HWUtils seed]; |
48 NSString *seed = [HWUtils seed]; |
49 NSString *seedCmd = [[NSString alloc] initWithFormat:@"eseed {%@}", seed]; |
49 NSString *seedCmd = [[NSString alloc] initWithFormat:@"eseed {%@}", seed]; |
50 self.seedCommand = seedCmd; |
50 self.seedCommand = seedCmd; |
51 [seedCmd release]; |
|
52 |
51 |
53 NSArray *source = [self.dataSourceArray objectAtIndex:scIndex]; |
52 NSArray *source = [self.dataSourceArray objectAtIndex:scIndex]; |
54 if (isRandomness()) { |
53 if (isRandomness()) { |
55 // prevent other events and add an activity while the preview is beign generated |
54 // prevent other events and add an activity while the preview is beign generated |
56 [self turnOffWidgets]; |
55 [self turnOffWidgets]; |
57 [self.previewButton updatePreviewWithSeed:seed]; |
56 [self.previewButton updatePreviewWithSeed:seed]; |
58 // the preview for static maps is loaded in didSelectRowAtIndexPath |
57 // the preview for static maps is loaded in didSelectRowAtIndexPath |
59 } |
58 } |
60 [seed release]; |
|
61 |
59 |
62 // perform as if user clicked on an entry |
60 // perform as if user clicked on an entry |
63 NSIndexPath *theIndex = [NSIndexPath indexPathForRow:arc4random_uniform((int)[source count]) inSection:0]; |
61 NSIndexPath *theIndex = [NSIndexPath indexPathForRow:arc4random_uniform((int)[source count]) inSection:0]; |
64 [self tableView:self.tableView didSelectRowAtIndexPath:theIndex]; |
62 [self tableView:self.tableView didSelectRowAtIndexPath:theIndex]; |
65 if (IS_NOT_POWERFUL([HWUtils modelType]) == NO) |
63 if (IS_NOT_POWERFUL([HWUtils modelType]) == NO) |
66 [self.tableView scrollToRowAtIndexPath:theIndex atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; |
64 [self.tableView scrollToRowAtIndexPath:theIndex atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; |
67 } |
65 } |
68 |
66 |
69 -(void) turnOffWidgets { |
67 - (void)turnOffWidgets { |
70 busy = YES; |
68 busy = YES; |
71 self.previewButton.alpha = 0.5f; |
69 self.previewButton.alpha = 0.5f; |
72 self.previewButton.enabled = NO; |
70 self.previewButton.enabled = NO; |
73 self.maxLabel.text = NSLocalizedString(@"Loading...",@"");; |
71 self.maxLabel.text = NSLocalizedString(@"Loading...",@"");; |
74 self.segmentedControl.enabled = NO; |
72 self.segmentedControl.enabled = NO; |
75 self.slider.enabled = NO; |
73 self.slider.enabled = NO; |
76 } |
74 } |
77 |
75 |
78 #pragma mark - |
76 #pragma mark - |
79 #pragma mark MapPreviewButtonView delegate methods |
77 #pragma mark MapPreviewButtonView delegate methods |
80 -(void) turnOnWidgets { |
78 - (void)turnOnWidgets { |
81 self.previewButton.alpha = 1.0f; |
79 self.previewButton.alpha = 1.0f; |
82 self.previewButton.enabled = YES; |
80 self.previewButton.enabled = YES; |
83 self.segmentedControl.enabled = YES; |
81 self.segmentedControl.enabled = YES; |
84 self.slider.enabled = YES; |
82 self.slider.enabled = YES; |
85 self.busy = NO; |
83 self.busy = NO; |
86 } |
84 } |
87 |
85 |
88 -(void) setMaxLabelText:(NSString *)str { |
86 - (void)setMaxLabelText:(NSString *)str { |
89 self.maxHogs = [str intValue]; |
87 self.maxHogs = [str intValue]; |
90 self.maxLabel.text = [NSString stringWithFormat:@"%@ %@",NSLocalizedString(@"Max Hogs:",@""),str]; |
88 self.maxLabel.text = [NSString stringWithFormat:@"%@ %@",NSLocalizedString(@"Max Hogs:",@""),str]; |
91 } |
89 } |
92 |
90 |
93 -(NSDictionary *)getDataForEngine { |
91 - (NSDictionary *)getDataForEngine { |
94 NSDictionary *dictForEngine = [NSDictionary dictionaryWithObjectsAndKeys: |
92 NSDictionary *dictForEngine = [NSDictionary dictionaryWithObjectsAndKeys: |
95 self.seedCommand,@"seedCommand", |
93 self.seedCommand,@"seedCommand", |
96 self.templateFilterCommand,@"templateFilterCommand", |
94 self.templateFilterCommand,@"templateFilterCommand", |
97 self.mapGenCommand,@"mapGenCommand", |
95 self.mapGenCommand,@"mapGenCommand", |
98 self.mazeSizeCommand,@"mazeSizeCommand", |
96 self.mazeSizeCommand,@"mazeSizeCommand", |
100 return dictForEngine; |
98 return dictForEngine; |
101 } |
99 } |
102 |
100 |
103 #pragma mark - |
101 #pragma mark - |
104 #pragma mark Table view data source |
102 #pragma mark Table view data source |
105 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
103 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { |
106 return 1; |
104 return 1; |
107 } |
105 } |
108 |
106 |
109 -(NSInteger) tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger) section { |
107 - (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section { |
110 return [[self.dataSourceArray objectAtIndex:scIndex] count]; |
108 return [[self.dataSourceArray objectAtIndex:scIndex] count]; |
111 } |
109 } |
112 |
110 |
113 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
111 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
114 static NSString *CellIdentifier = @"Cell"; |
112 static NSString *CellIdentifier = @"Cell"; |
115 NSUInteger row = [indexPath row]; |
113 NSUInteger row = [indexPath row]; |
116 |
114 |
117 UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
115 UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
118 if (cell == nil) |
116 if (cell == nil) |
119 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
117 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; |
120 |
118 |
121 NSArray *source = [self.dataSourceArray objectAtIndex:scIndex]; |
119 NSArray *source = [self.dataSourceArray objectAtIndex:scIndex]; |
122 |
120 |
123 NSString *labelString = [source objectAtIndex:row]; |
121 NSString *labelString = [source objectAtIndex:row]; |
124 cell.textLabel.text = labelString; |
122 cell.textLabel.text = labelString; |
128 cell.textLabel.backgroundColor = [UIColor clearColor]; |
126 cell.textLabel.backgroundColor = [UIColor clearColor]; |
129 |
127 |
130 if (isRandomness()) { |
128 if (isRandomness()) { |
131 UIImage *image = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@/icon.png",THEMES_DIRECTORY(),labelString]]; |
129 UIImage *image = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@/icon.png",THEMES_DIRECTORY(),labelString]]; |
132 cell.imageView.image = image; |
130 cell.imageView.image = image; |
133 [image release]; |
|
134 } else |
131 } else |
135 cell.imageView.image = nil; |
132 cell.imageView.image = nil; |
136 |
133 |
137 if (row == [self.lastIndexPath row]) { |
134 if (row == [self.lastIndexPath row]) { |
138 UIImageView *checkbox = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:@"checkbox.png"]]; |
135 UIImageView *checkbox = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:@"checkbox.png"]]; |
139 cell.accessoryView = checkbox; |
136 cell.accessoryView = checkbox; |
140 [checkbox release]; |
|
141 } else |
137 } else |
142 cell.accessoryView = nil; |
138 cell.accessoryView = nil; |
143 |
139 |
144 cell.backgroundColor = [UIColor blackColorTransparent]; |
140 cell.backgroundColor = [UIColor blackColorTransparent]; |
145 return cell; |
141 return cell; |
146 } |
142 } |
147 |
143 |
148 // this set details for a static map (called by didSelectRowAtIndexPath) |
144 // this set details for a static map (called by didSelectRowAtIndexPath) |
149 -(void) setDetailsForStaticMap:(NSInteger) index { |
145 - (void)setDetailsForStaticMap:(NSInteger)index { |
150 NSArray *source = [self.dataSourceArray objectAtIndex:scIndex]; |
146 NSArray *source = [self.dataSourceArray objectAtIndex:scIndex]; |
151 |
147 |
152 NSString *fileCfg = [[NSString alloc] initWithFormat:@"%@/%@/map.cfg", |
148 NSString *fileCfg = [[NSString alloc] initWithFormat:@"%@/%@/map.cfg", |
153 (scIndex == 1) ? MAPS_DIRECTORY() : MISSIONS_DIRECTORY(),[source objectAtIndex:index]]; |
149 (scIndex == 1) ? MAPS_DIRECTORY() : MISSIONS_DIRECTORY(),[source objectAtIndex:index]]; |
154 NSString *contents = [[NSString alloc] initWithContentsOfFile:fileCfg encoding:NSUTF8StringEncoding error:NULL]; |
150 NSString *contents = [[NSString alloc] initWithContentsOfFile:fileCfg encoding:NSUTF8StringEncoding error:NULL]; |
155 [fileCfg release]; |
|
156 NSArray *split = [contents componentsSeparatedByString:@"\n"]; |
151 NSArray *split = [contents componentsSeparatedByString:@"\n"]; |
157 [contents release]; |
|
158 |
152 |
159 // if the number is not set we keep 18 standard; |
153 // if the number is not set we keep 18 standard; |
160 // sometimes it's not set but there are trailing characters, we get around them with the second equation |
154 // sometimes it's not set but there are trailing characters, we get around them with the second equation |
161 NSString *max; |
155 NSString *max; |
162 if ([split count] > 1 && [[split objectAtIndex:1] intValue] > 0) |
156 if ([split count] > 1 && [[split objectAtIndex:1] intValue] > 0) |
280 self.templateFilterCommand = templateCommand; |
273 self.templateFilterCommand = templateCommand; |
281 self.mazeSizeCommand = mazeCommand; |
274 self.mazeSizeCommand = mazeCommand; |
282 } |
275 } |
283 |
276 |
284 // update preview (if not busy and if its value really changed) as soon as the user lifts its finger up |
277 // update preview (if not busy and if its value really changed) as soon as the user lifts its finger up |
285 -(IBAction) sliderEndedChanging:(id) sender { |
278 - (IBAction)sliderEndedChanging:(id)sender { |
286 int num = (int) (self.slider.value * 100); |
279 int num = (int) (self.slider.value * 100); |
287 if (oldValue != num) { |
280 if (oldValue != num) { |
288 [self updatePreview]; |
281 [self updatePreview]; |
289 oldValue = num; |
282 oldValue = num; |
290 } |
283 } |
291 [[AudioManagerController mainManager] playClickSound]; |
284 [[AudioManagerController mainManager] playClickSound]; |
292 } |
285 } |
293 |
286 |
294 // perform actions based on the activated section, then call updatePreview to visually update the selection |
287 // perform actions based on the activated section, then call updatePreview to visually update the selection |
295 // and if necessary update the table with a slide animation |
288 // and if necessary update the table with a slide animation |
296 -(IBAction) segmentedControlChanged:(id) sender { |
289 - (IBAction)segmentedControlChanged:(id)sender { |
297 NSString *mapgen, *staticmap, *mission; |
290 NSString *mapgen, *staticmap, *mission; |
298 NSInteger newPage = self.segmentedControl.selectedSegmentIndex; |
291 NSInteger newPage = self.segmentedControl.selectedSegmentIndex; |
299 |
292 |
300 [[AudioManagerController mainManager] playSelectSound]; |
293 [[AudioManagerController mainManager] playSelectSound]; |
301 switch (newPage) { |
294 switch (newPage) { |
434 self.tableView.contentInset = UIEdgeInsetsMake(10, 0, 10, 0); |
422 self.tableView.contentInset = UIEdgeInsetsMake(10, 0, 10, 0); |
435 |
423 |
436 UILabel *backLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 14, 300, 190) andTitle:nil withBorderWidth:2.3f]; |
424 UILabel *backLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 14, 300, 190) andTitle:nil withBorderWidth:2.3f]; |
437 backLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; |
425 backLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; |
438 [self.view insertSubview:backLabel belowSubview:self.segmentedControl]; |
426 [self.view insertSubview:backLabel belowSubview:self.segmentedControl]; |
439 [backLabel release]; |
|
440 } |
427 } |
441 self.tableView.separatorColor = [UIColor whiteColor]; |
428 self.tableView.separatorColor = [UIColor whiteColor]; |
442 self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; |
429 self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; |
443 } |
430 } |
444 |
431 |
445 -(void) viewWillAppear:(BOOL)animated { |
432 - (void)viewWillAppear:(BOOL)animated { |
446 [super viewWillAppear:animated]; |
433 [super viewWillAppear:animated]; |
447 } |
434 } |
448 |
435 |
449 -(void) viewDidAppear:(BOOL) animated { |
436 - (void)viewDidAppear:(BOOL) animated { |
450 [self updatePreview]; |
437 [self updatePreview]; |
451 [super viewDidAppear:animated]; |
438 [super viewDidAppear:animated]; |
452 } |
439 } |
453 |
440 |
454 -(void) viewDidUnload { |
441 - (void)didReceiveMemoryWarning { |
455 self.previewButton = nil; |
|
456 self.seedCommand = nil; |
|
457 self.templateFilterCommand = nil; |
|
458 self.mapGenCommand = nil; |
|
459 self.mazeSizeCommand = nil; |
|
460 self.themeCommand = nil; |
|
461 self.staticMapCommand = nil; |
|
462 self.missionCommand = nil; |
|
463 |
|
464 self.tableView = nil; |
|
465 self.maxLabel = nil; |
|
466 self.segmentedControl = nil; |
|
467 self.slider = nil; |
|
468 |
|
469 self.lastIndexPath = nil; |
|
470 self.dataSourceArray = nil; |
|
471 |
|
472 MSG_DIDUNLOAD(); |
|
473 [super viewDidUnload]; |
|
474 } |
|
475 |
|
476 -(void) didReceiveMemoryWarning { |
|
477 self.dataSourceArray = nil; |
442 self.dataSourceArray = nil; |
478 [super didReceiveMemoryWarning]; |
443 [super didReceiveMemoryWarning]; |
479 |
444 |
480 if (self.view.superview == nil) { |
445 if (self.view.superview == nil) { |
481 self.previewButton = nil; |
446 self.previewButton = nil; |