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