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
|
|
19 |
@synthesize previewButton, maxHogs, seedCommand, templateFilterCommand, mapGenCommand, mazeSizeCommand, themeCommand,
|
|
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) {
|
|
42 |
NSLog(@"SDLNet_Init: %s", SDLNet_GetError());
|
|
43 |
serverQuit = YES;
|
|
44 |
}
|
|
45 |
|
|
46 |
// Resolving the host using NULL make network interface to listen
|
|
47 |
if (SDLNet_ResolveHost(&ip, NULL, port) < 0) {
|
|
48 |
NSLog(@"SDLNet_ResolveHost: %s\n", SDLNet_GetError());
|
|
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))) {
|
|
54 |
NSLog(@"SDLNet_TCP_Open: %s %\n", SDLNet_GetError(), port);
|
|
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 |
|
|
194 |
-(void) updatePreviewWithMap:(NSInteger) index {
|
|
195 |
// change the preview button
|
|
196 |
NSString *fileImage = [[NSString alloc] initWithFormat:@"%@/%@/preview.png", MAPS_DIRECTORY(),[self.mapArray objectAtIndex:index]];
|
|
197 |
UIImage *image = [[UIImage alloc] initWithContentsOfFile:fileImage];
|
|
198 |
[fileImage release];
|
|
199 |
[self.previewButton setImage:[image makeRoundCornersOfSize:CGSizeMake(12, 12)] forState:UIControlStateNormal];
|
|
200 |
[image release];
|
|
201 |
|
|
202 |
// update label
|
|
203 |
maxHogs = 18;
|
|
204 |
NSString *fileCfg = [[NSString alloc] initWithFormat:@"%@/%@/map.cfg", MAPS_DIRECTORY(),[self.mapArray objectAtIndex:index]];
|
|
205 |
NSString *contents = [[NSString alloc] initWithContentsOfFile:fileCfg encoding:NSUTF8StringEncoding error:NULL];
|
|
206 |
[fileCfg release];
|
|
207 |
NSArray *split = [contents componentsSeparatedByString:@"\n"];
|
|
208 |
|
|
209 |
// if the number is not set we keep 18 standard;
|
|
210 |
// sometimes it's not set but there are trailing characters, we get around them with the second equation
|
|
211 |
if ([split count] > 1 && [[split objectAtIndex:1] intValue] > 0)
|
|
212 |
maxHogs = [[split objectAtIndex:1] intValue];
|
|
213 |
[contents release];
|
|
214 |
NSString *max = [[NSString alloc] initWithFormat:@"%d",maxHogs];
|
|
215 |
self.maxLabel.text = max;
|
|
216 |
[max release];
|
|
217 |
}
|
|
218 |
|
|
219 |
-(void) turnOffWidgets {
|
|
220 |
busy = YES;
|
|
221 |
self.previewButton.alpha = 0.5f;
|
|
222 |
self.previewButton.enabled = NO;
|
|
223 |
self.maxLabel.text = @"...";
|
|
224 |
self.segmentedControl.enabled = NO;
|
|
225 |
self.slider.enabled = NO;
|
|
226 |
}
|
|
227 |
|
|
228 |
-(void) turnOnWidgets {
|
|
229 |
self.previewButton.alpha = 1.0f;
|
|
230 |
self.previewButton.enabled = YES;
|
|
231 |
self.segmentedControl.enabled = YES;
|
|
232 |
self.slider.enabled = YES;
|
|
233 |
busy = NO;
|
|
234 |
|
|
235 |
UIActivityIndicatorView *indicator = (UIActivityIndicatorView *)[self.previewButton viewWithTag:INDICATOR_TAG];
|
|
236 |
if (indicator) {
|
|
237 |
[indicator stopAnimating];
|
|
238 |
[indicator removeFromSuperview];
|
|
239 |
}
|
|
240 |
}
|
|
241 |
|
|
242 |
-(void) setLabelText:(NSString *)str {
|
|
243 |
self.maxLabel.text = str;
|
|
244 |
}
|
|
245 |
|
|
246 |
-(void) setButtonImage:(UIImage *)img {
|
|
247 |
[self.previewButton setBackgroundImage:img forState:UIControlStateNormal];
|
|
248 |
}
|
|
249 |
|
|
250 |
-(void) restoreBackgroundImage {
|
|
251 |
// white rounded rectangle as background image for previewButton
|
|
252 |
UIGraphicsBeginImageContext(CGSizeMake(256,128));
|
|
253 |
CGContextRef context = UIGraphicsGetCurrentContext();
|
|
254 |
UIGraphicsPushContext(context);
|
|
255 |
|
|
256 |
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
|
|
257 |
CGContextFillRect(context,CGRectMake(0,0,256,128));
|
|
258 |
|
|
259 |
UIGraphicsPopContext();
|
|
260 |
UIImage *bkgImg = UIGraphicsGetImageFromCurrentImageContext();
|
|
261 |
UIGraphicsEndImageContext();
|
|
262 |
[self.previewButton setBackgroundImage:[bkgImg makeRoundCornersOfSize:CGSizeMake(12, 12)] forState:UIControlStateNormal];
|
|
263 |
}
|
|
264 |
|
|
265 |
#pragma mark -
|
|
266 |
#pragma mark Table view data source
|
|
267 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
|
|
268 |
return 1;
|
|
269 |
}
|
|
270 |
|
|
271 |
-(NSInteger) tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger) section {
|
|
272 |
if (self.segmentedControl.selectedSegmentIndex != 1)
|
|
273 |
return [themeArray count];
|
|
274 |
else
|
|
275 |
return [mapArray count];
|
|
276 |
}
|
|
277 |
|
|
278 |
-(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
279 |
static NSString *CellIdentifier = @"Cell";
|
|
280 |
NSInteger row = [indexPath row];
|
|
281 |
|
|
282 |
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
|
|
283 |
if (cell == nil)
|
|
284 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
|
|
285 |
|
|
286 |
if (self.segmentedControl.selectedSegmentIndex != 1) {
|
|
287 |
// the % prevents a strange bug that occurs sporadically
|
|
288 |
NSString *themeName = [self.themeArray objectAtIndex:row % [self.themeArray count]];
|
|
289 |
cell.textLabel.text = themeName;
|
|
290 |
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@/icon.png",THEMES_DIRECTORY(),themeName]];
|
|
291 |
cell.imageView.image = image;
|
|
292 |
[image release];
|
|
293 |
} else {
|
|
294 |
cell.textLabel.text = [self.mapArray objectAtIndex:row];
|
|
295 |
cell.imageView.image = nil;
|
|
296 |
}
|
|
297 |
|
|
298 |
if (row == [self.lastIndexPath row])
|
|
299 |
cell.accessoryType = UITableViewCellAccessoryCheckmark;
|
|
300 |
else
|
|
301 |
cell.accessoryType = UITableViewCellAccessoryNone;
|
|
302 |
|
|
303 |
return cell;
|
|
304 |
}
|
|
305 |
|
|
306 |
|
|
307 |
#pragma mark -
|
|
308 |
#pragma mark Table view delegate
|
|
309 |
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
310 |
int newRow = [indexPath row];
|
|
311 |
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
|
|
312 |
|
|
313 |
if (newRow != oldRow) {
|
|
314 |
if (self.segmentedControl.selectedSegmentIndex != 1) {
|
|
315 |
NSString *theme = [self.themeArray objectAtIndex:newRow];
|
|
316 |
self.themeCommand = [NSString stringWithFormat:@"etheme %@", theme];
|
|
317 |
} else
|
|
318 |
[self updatePreviewWithMap:newRow];
|
|
319 |
|
|
320 |
UITableViewCell *newCell = [aTableView cellForRowAtIndexPath:indexPath];
|
|
321 |
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
|
|
322 |
UITableViewCell *oldCell = [aTableView cellForRowAtIndexPath:self.lastIndexPath];
|
|
323 |
oldCell.accessoryType = UITableViewCellAccessoryNone;
|
|
324 |
|
|
325 |
self.lastIndexPath = indexPath;
|
|
326 |
[aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
|
|
327 |
}
|
|
328 |
[aTableView deselectRowAtIndexPath:indexPath animated:YES];
|
|
329 |
}
|
|
330 |
|
|
331 |
#pragma mark -
|
|
332 |
#pragma mark slider & segmentedControl
|
|
333 |
// this updates the label and the command keys when the slider is moved, depending of the selection in segmentedControl
|
|
334 |
// no methods are called by this routine and you can pass nil to it
|
|
335 |
-(IBAction) sliderChanged:(id) sender {
|
|
336 |
NSString *labelText;
|
|
337 |
NSString *templateCommand;
|
|
338 |
NSString *mazeCommand;
|
|
339 |
|
|
340 |
switch ((int)(self.slider.value*100)) {
|
|
341 |
case 0:
|
|
342 |
if (self.segmentedControl.selectedSegmentIndex == 0) {
|
|
343 |
labelText = NSLocalizedString(@"Wacky",@"");
|
|
344 |
} else {
|
|
345 |
labelText = NSLocalizedString(@"Large Floating Islands",@"");
|
|
346 |
}
|
|
347 |
templateCommand = @"e$template_filter 5";
|
|
348 |
mazeCommand = @"e$maze_size 5";
|
|
349 |
break;
|
|
350 |
case 1:
|
|
351 |
if (self.segmentedControl.selectedSegmentIndex == 0) {
|
|
352 |
labelText = NSLocalizedString(@"Cavern",@"");
|
|
353 |
} else {
|
|
354 |
labelText = NSLocalizedString(@"Medium Floating Islands",@"");
|
|
355 |
}
|
|
356 |
templateCommand = @"e$template_filter 4";
|
|
357 |
mazeCommand = @"e$maze_size 4";
|
|
358 |
break;
|
|
359 |
case 2:
|
|
360 |
if (self.segmentedControl.selectedSegmentIndex == 0) {
|
|
361 |
labelText = NSLocalizedString(@"Small",@"");
|
|
362 |
} else {
|
|
363 |
labelText = NSLocalizedString(@"Small Floating Islands",@"");
|
|
364 |
}
|
|
365 |
templateCommand = @"e$template_filter 1";
|
|
366 |
mazeCommand = @"e$maze_size 3";
|
|
367 |
break;
|
|
368 |
case 3:
|
|
369 |
if (self.segmentedControl.selectedSegmentIndex == 0) {
|
|
370 |
labelText = NSLocalizedString(@"Medium",@"");
|
|
371 |
} else {
|
|
372 |
labelText = NSLocalizedString(@"Large Tunnels",@"");
|
|
373 |
}
|
|
374 |
templateCommand = @"e$template_filter 2";
|
|
375 |
mazeCommand = @"e$maze_size 2";
|
|
376 |
break;
|
|
377 |
case 4:
|
|
378 |
if (self.segmentedControl.selectedSegmentIndex == 0) {
|
|
379 |
labelText = NSLocalizedString(@"Large",@"");
|
|
380 |
} else {
|
|
381 |
labelText = NSLocalizedString(@"Medium Tunnels",@"");
|
|
382 |
}
|
|
383 |
templateCommand = @"e$template_filter 3";
|
|
384 |
mazeCommand = @"e$maze_size 1";
|
|
385 |
break;
|
|
386 |
case 5:
|
|
387 |
if (self.segmentedControl.selectedSegmentIndex == 0) {
|
|
388 |
labelText = NSLocalizedString(@"All",@"");
|
|
389 |
} else {
|
|
390 |
labelText = NSLocalizedString(@"Small Tunnels",@"");
|
|
391 |
}
|
|
392 |
templateCommand = @"e$template_filter 0";
|
|
393 |
mazeCommand = @"e$maze_size 0";
|
|
394 |
break;
|
|
395 |
default:
|
|
396 |
labelText = nil;
|
|
397 |
templateCommand = nil;
|
|
398 |
mazeCommand = nil;
|
|
399 |
break;
|
|
400 |
}
|
|
401 |
|
|
402 |
self.sizeLabel.text = labelText;
|
|
403 |
self.templateFilterCommand = templateCommand;
|
|
404 |
self.mazeSizeCommand = mazeCommand;
|
|
405 |
}
|
|
406 |
|
|
407 |
// update preview (if not busy and if its value really changed) as soon as the user lifts its finger up
|
|
408 |
-(IBAction) sliderEndedChanging:(id) sender {
|
|
409 |
int num = (int) (self.slider.value * 100);
|
|
410 |
if (oldValue != num) {
|
|
411 |
[self updatePreview];
|
|
412 |
oldValue = num;
|
|
413 |
}
|
|
414 |
}
|
|
415 |
|
|
416 |
// perform actions based on the activated section, then call updatePreview to visually update the selection
|
|
417 |
// updatePreview will call didSelectRowAtIndexPath which will call the right update routine)
|
|
418 |
// and if necessary update the table with a slide animation
|
|
419 |
-(IBAction) segmentedControlChanged:(id) sender {
|
|
420 |
NSString *mapgen;
|
|
421 |
NSInteger newPage = self.segmentedControl.selectedSegmentIndex;
|
|
422 |
|
|
423 |
switch (newPage) {
|
|
424 |
case 0: // Random
|
|
425 |
mapgen = @"e$mapgen 0";
|
|
426 |
[self sliderChanged:nil];
|
|
427 |
self.slider.enabled = YES;
|
|
428 |
break;
|
|
429 |
|
|
430 |
case 1: // Map
|
|
431 |
mapgen = @"e$mapgen 0";
|
|
432 |
self.slider.enabled = NO;
|
|
433 |
self.sizeLabel.text = @".";
|
|
434 |
[self restoreBackgroundImage];
|
|
435 |
break;
|
|
436 |
|
|
437 |
case 2: // Maze
|
|
438 |
mapgen = @"e$mapgen 1";
|
|
439 |
[self sliderChanged:nil];
|
|
440 |
self.slider.enabled = YES;
|
|
441 |
break;
|
|
442 |
|
|
443 |
default:
|
|
444 |
mapgen = nil;
|
|
445 |
break;
|
|
446 |
}
|
|
447 |
self.mapGenCommand = mapgen;
|
|
448 |
[self updatePreview];
|
|
449 |
|
|
450 |
// nice animation for updating the table when appropriate (on iphone)
|
|
451 |
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
|
|
452 |
if (((oldPage == 0 || oldPage == 2) && newPage == 1) ||
|
|
453 |
(oldPage == 1 && (newPage == 0 || newPage == 2))) {
|
|
454 |
[UIView beginAnimations:@"moving out table" context:NULL];
|
|
455 |
self.tableView.frame = CGRectMake(480, 0, 185, 276);
|
|
456 |
[UIView commitAnimations];
|
|
457 |
[self performSelector:@selector(moveTable) withObject:nil afterDelay:0.2];
|
|
458 |
}
|
|
459 |
oldPage = newPage;
|
|
460 |
}
|
|
461 |
|
|
462 |
// update data when table is not visible and then show it
|
|
463 |
-(void) moveTable {
|
|
464 |
[self.tableView reloadData];
|
|
465 |
|
|
466 |
[UIView beginAnimations:@"moving in table" context:NULL];
|
|
467 |
self.tableView.frame = CGRectMake(295, 0, 185, 276);
|
|
468 |
[UIView commitAnimations];
|
|
469 |
}
|
|
470 |
|
|
471 |
#pragma mark -
|
|
472 |
#pragma mark view management
|
|
473 |
-(void) viewDidLoad {
|
|
474 |
[super viewDidLoad];
|
|
475 |
|
|
476 |
srandom(time(NULL));
|
|
477 |
|
|
478 |
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
|
|
479 |
self.view.frame = CGRectMake(0, 0, screenSize.height, screenSize.width - 44);
|
|
480 |
|
|
481 |
// themes.cfg contains all the user-selectable themes
|
|
482 |
NSString *string = [[NSString alloc] initWithContentsOfFile:[THEMES_DIRECTORY() stringByAppendingString:@"/themes.cfg"]
|
|
483 |
encoding:NSUTF8StringEncoding
|
|
484 |
error:NULL];
|
|
485 |
NSMutableArray *array = [[NSMutableArray alloc] initWithArray:[string componentsSeparatedByString:@"\n"]];
|
|
486 |
[string release];
|
|
487 |
// remove a trailing "" element
|
|
488 |
[array removeLastObject];
|
|
489 |
self.themeArray = array;
|
|
490 |
[array release];
|
|
491 |
self.mapArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:MAPS_DIRECTORY() error:NULL];
|
|
492 |
|
|
493 |
self.tableView.rowHeight = 42;
|
|
494 |
busy = NO;
|
|
495 |
|
|
496 |
// draw a white background
|
|
497 |
[self restoreBackgroundImage];
|
|
498 |
|
|
499 |
// initialize some "default" values
|
|
500 |
self.sizeLabel.text = NSLocalizedString(@"All",@"");
|
|
501 |
self.slider.value = 0.05f;
|
|
502 |
self.segmentedControl.selectedSegmentIndex = 0;
|
|
503 |
|
|
504 |
self.templateFilterCommand = @"e$template_filter 0";
|
|
505 |
self.mazeSizeCommand = @"e$maze_size 0";
|
|
506 |
self.mapGenCommand = @"e$mapgen 0";
|
|
507 |
self.lastIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
|
|
508 |
|
|
509 |
oldValue = 5;
|
|
510 |
oldPage = 0;
|
|
511 |
}
|
|
512 |
|
|
513 |
-(void) viewDidAppear:(BOOL) animated {
|
|
514 |
[super viewDidAppear:animated];
|
|
515 |
[self updatePreview];
|
|
516 |
}
|
|
517 |
|
|
518 |
#pragma mark -
|
|
519 |
#pragma mark memory
|
|
520 |
-(void) didReceiveMemoryWarning {
|
|
521 |
[super didReceiveMemoryWarning];
|
|
522 |
}
|
|
523 |
|
|
524 |
-(void) viewDidUnload {
|
|
525 |
self.previewButton = nil;
|
|
526 |
self.seedCommand = nil;
|
|
527 |
self.templateFilterCommand = nil;
|
|
528 |
self.mapGenCommand = nil;
|
|
529 |
self.mazeSizeCommand = nil;
|
|
530 |
self.themeCommand = nil;
|
|
531 |
|
|
532 |
self.previewButton = nil;
|
|
533 |
self.tableView = nil;
|
|
534 |
self.maxLabel = nil;
|
|
535 |
self.sizeLabel = nil;
|
|
536 |
self.segmentedControl = nil;
|
|
537 |
self.slider = nil;
|
|
538 |
|
|
539 |
self.lastIndexPath = nil;
|
|
540 |
self.themeArray = nil;
|
|
541 |
self.mapArray = nil;
|
|
542 |
|
|
543 |
[super viewDidUnload];
|
|
544 |
MSG_DIDUNLOAD();
|
|
545 |
}
|
|
546 |
|
|
547 |
-(void) dealloc {
|
|
548 |
[seedCommand release];
|
|
549 |
[templateFilterCommand release];
|
|
550 |
[mapGenCommand release];
|
|
551 |
[mazeSizeCommand release];
|
|
552 |
[themeCommand release];
|
|
553 |
|
|
554 |
[previewButton release];
|
|
555 |
[tableView release];
|
|
556 |
[maxLabel release];
|
|
557 |
[sizeLabel release];
|
|
558 |
[segmentedControl release];
|
|
559 |
[slider release];
|
|
560 |
|
|
561 |
[lastIndexPath release];
|
|
562 |
[themeArray release];
|
|
563 |
[mapArray release];
|
|
564 |
|
|
565 |
[super dealloc];
|
|
566 |
}
|
|
567 |
|
|
568 |
|
|
569 |
@end
|