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