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 |
|
3623
|
286 |
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
|
3625
|
287 |
cell.textLabel.textColor = [UIColor colorWithRed:(CGFloat)0xFE/255 green:(CGFloat)0xCB/255 blue:0 alpha:1 ];
|
3623
|
288 |
}
|
|
289 |
|
3547
|
290 |
if (self.segmentedControl.selectedSegmentIndex != 1) {
|
|
291 |
// the % prevents a strange bug that occurs sporadically
|
|
292 |
NSString *themeName = [self.themeArray objectAtIndex:row % [self.themeArray count]];
|
|
293 |
cell.textLabel.text = themeName;
|
|
294 |
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@/icon.png",THEMES_DIRECTORY(),themeName]];
|
|
295 |
cell.imageView.image = image;
|
|
296 |
[image release];
|
|
297 |
} else {
|
|
298 |
cell.textLabel.text = [self.mapArray objectAtIndex:row];
|
|
299 |
cell.imageView.image = nil;
|
|
300 |
}
|
|
301 |
|
|
302 |
if (row == [self.lastIndexPath row])
|
|
303 |
cell.accessoryType = UITableViewCellAccessoryCheckmark;
|
|
304 |
else
|
|
305 |
cell.accessoryType = UITableViewCellAccessoryNone;
|
|
306 |
|
|
307 |
return cell;
|
|
308 |
}
|
|
309 |
|
|
310 |
|
|
311 |
#pragma mark -
|
|
312 |
#pragma mark Table view delegate
|
|
313 |
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
314 |
int newRow = [indexPath row];
|
|
315 |
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
|
|
316 |
|
|
317 |
if (newRow != oldRow) {
|
|
318 |
if (self.segmentedControl.selectedSegmentIndex != 1) {
|
|
319 |
NSString *theme = [self.themeArray objectAtIndex:newRow];
|
|
320 |
self.themeCommand = [NSString stringWithFormat:@"etheme %@", theme];
|
|
321 |
} else
|
|
322 |
[self updatePreviewWithMap:newRow];
|
|
323 |
|
|
324 |
UITableViewCell *newCell = [aTableView cellForRowAtIndexPath:indexPath];
|
|
325 |
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
|
|
326 |
UITableViewCell *oldCell = [aTableView cellForRowAtIndexPath:self.lastIndexPath];
|
|
327 |
oldCell.accessoryType = UITableViewCellAccessoryNone;
|
|
328 |
|
|
329 |
self.lastIndexPath = indexPath;
|
|
330 |
[aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
|
|
331 |
}
|
|
332 |
[aTableView deselectRowAtIndexPath:indexPath animated:YES];
|
|
333 |
}
|
|
334 |
|
|
335 |
#pragma mark -
|
|
336 |
#pragma mark slider & segmentedControl
|
|
337 |
// this updates the label and the command keys when the slider is moved, depending of the selection in segmentedControl
|
|
338 |
// no methods are called by this routine and you can pass nil to it
|
|
339 |
-(IBAction) sliderChanged:(id) sender {
|
|
340 |
NSString *labelText;
|
|
341 |
NSString *templateCommand;
|
|
342 |
NSString *mazeCommand;
|
|
343 |
|
|
344 |
switch ((int)(self.slider.value*100)) {
|
|
345 |
case 0:
|
|
346 |
if (self.segmentedControl.selectedSegmentIndex == 0) {
|
|
347 |
labelText = NSLocalizedString(@"Wacky",@"");
|
|
348 |
} else {
|
|
349 |
labelText = NSLocalizedString(@"Large Floating Islands",@"");
|
|
350 |
}
|
|
351 |
templateCommand = @"e$template_filter 5";
|
|
352 |
mazeCommand = @"e$maze_size 5";
|
|
353 |
break;
|
|
354 |
case 1:
|
|
355 |
if (self.segmentedControl.selectedSegmentIndex == 0) {
|
|
356 |
labelText = NSLocalizedString(@"Cavern",@"");
|
|
357 |
} else {
|
|
358 |
labelText = NSLocalizedString(@"Medium Floating Islands",@"");
|
|
359 |
}
|
|
360 |
templateCommand = @"e$template_filter 4";
|
|
361 |
mazeCommand = @"e$maze_size 4";
|
|
362 |
break;
|
|
363 |
case 2:
|
|
364 |
if (self.segmentedControl.selectedSegmentIndex == 0) {
|
|
365 |
labelText = NSLocalizedString(@"Small",@"");
|
|
366 |
} else {
|
|
367 |
labelText = NSLocalizedString(@"Small Floating Islands",@"");
|
|
368 |
}
|
|
369 |
templateCommand = @"e$template_filter 1";
|
|
370 |
mazeCommand = @"e$maze_size 3";
|
|
371 |
break;
|
|
372 |
case 3:
|
|
373 |
if (self.segmentedControl.selectedSegmentIndex == 0) {
|
|
374 |
labelText = NSLocalizedString(@"Medium",@"");
|
|
375 |
} else {
|
|
376 |
labelText = NSLocalizedString(@"Large Tunnels",@"");
|
|
377 |
}
|
|
378 |
templateCommand = @"e$template_filter 2";
|
|
379 |
mazeCommand = @"e$maze_size 2";
|
|
380 |
break;
|
|
381 |
case 4:
|
|
382 |
if (self.segmentedControl.selectedSegmentIndex == 0) {
|
|
383 |
labelText = NSLocalizedString(@"Large",@"");
|
|
384 |
} else {
|
|
385 |
labelText = NSLocalizedString(@"Medium Tunnels",@"");
|
|
386 |
}
|
|
387 |
templateCommand = @"e$template_filter 3";
|
|
388 |
mazeCommand = @"e$maze_size 1";
|
|
389 |
break;
|
|
390 |
case 5:
|
|
391 |
if (self.segmentedControl.selectedSegmentIndex == 0) {
|
|
392 |
labelText = NSLocalizedString(@"All",@"");
|
|
393 |
} else {
|
|
394 |
labelText = NSLocalizedString(@"Small Tunnels",@"");
|
|
395 |
}
|
|
396 |
templateCommand = @"e$template_filter 0";
|
|
397 |
mazeCommand = @"e$maze_size 0";
|
|
398 |
break;
|
|
399 |
default:
|
|
400 |
labelText = nil;
|
|
401 |
templateCommand = nil;
|
|
402 |
mazeCommand = nil;
|
|
403 |
break;
|
|
404 |
}
|
|
405 |
|
|
406 |
self.sizeLabel.text = labelText;
|
|
407 |
self.templateFilterCommand = templateCommand;
|
|
408 |
self.mazeSizeCommand = mazeCommand;
|
|
409 |
}
|
|
410 |
|
|
411 |
// update preview (if not busy and if its value really changed) as soon as the user lifts its finger up
|
|
412 |
-(IBAction) sliderEndedChanging:(id) sender {
|
|
413 |
int num = (int) (self.slider.value * 100);
|
|
414 |
if (oldValue != num) {
|
|
415 |
[self updatePreview];
|
|
416 |
oldValue = num;
|
|
417 |
}
|
|
418 |
}
|
|
419 |
|
|
420 |
// perform actions based on the activated section, then call updatePreview to visually update the selection
|
|
421 |
// updatePreview will call didSelectRowAtIndexPath which will call the right update routine)
|
|
422 |
// and if necessary update the table with a slide animation
|
|
423 |
-(IBAction) segmentedControlChanged:(id) sender {
|
|
424 |
NSString *mapgen;
|
|
425 |
NSInteger newPage = self.segmentedControl.selectedSegmentIndex;
|
|
426 |
|
|
427 |
switch (newPage) {
|
|
428 |
case 0: // Random
|
|
429 |
mapgen = @"e$mapgen 0";
|
|
430 |
[self sliderChanged:nil];
|
|
431 |
self.slider.enabled = YES;
|
|
432 |
break;
|
|
433 |
|
|
434 |
case 1: // Map
|
|
435 |
mapgen = @"e$mapgen 0";
|
|
436 |
self.slider.enabled = NO;
|
|
437 |
self.sizeLabel.text = @".";
|
|
438 |
[self restoreBackgroundImage];
|
|
439 |
break;
|
|
440 |
|
|
441 |
case 2: // Maze
|
|
442 |
mapgen = @"e$mapgen 1";
|
|
443 |
[self sliderChanged:nil];
|
|
444 |
self.slider.enabled = YES;
|
|
445 |
break;
|
|
446 |
|
|
447 |
default:
|
|
448 |
mapgen = nil;
|
|
449 |
break;
|
|
450 |
}
|
|
451 |
self.mapGenCommand = mapgen;
|
|
452 |
[self updatePreview];
|
|
453 |
|
|
454 |
// nice animation for updating the table when appropriate (on iphone)
|
|
455 |
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
|
|
456 |
if (((oldPage == 0 || oldPage == 2) && newPage == 1) ||
|
|
457 |
(oldPage == 1 && (newPage == 0 || newPage == 2))) {
|
|
458 |
[UIView beginAnimations:@"moving out table" context:NULL];
|
|
459 |
self.tableView.frame = CGRectMake(480, 0, 185, 276);
|
|
460 |
[UIView commitAnimations];
|
|
461 |
[self performSelector:@selector(moveTable) withObject:nil afterDelay:0.2];
|
|
462 |
}
|
|
463 |
oldPage = newPage;
|
|
464 |
}
|
|
465 |
|
|
466 |
// update data when table is not visible and then show it
|
|
467 |
-(void) moveTable {
|
|
468 |
[self.tableView reloadData];
|
|
469 |
|
|
470 |
[UIView beginAnimations:@"moving in table" context:NULL];
|
|
471 |
self.tableView.frame = CGRectMake(295, 0, 185, 276);
|
|
472 |
[UIView commitAnimations];
|
|
473 |
}
|
|
474 |
|
|
475 |
#pragma mark -
|
|
476 |
#pragma mark view management
|
|
477 |
-(void) viewDidLoad {
|
|
478 |
[super viewDidLoad];
|
|
479 |
|
|
480 |
srandom(time(NULL));
|
|
481 |
|
|
482 |
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
|
|
483 |
self.view.frame = CGRectMake(0, 0, screenSize.height, screenSize.width - 44);
|
|
484 |
|
|
485 |
// themes.cfg contains all the user-selectable themes
|
|
486 |
NSString *string = [[NSString alloc] initWithContentsOfFile:[THEMES_DIRECTORY() stringByAppendingString:@"/themes.cfg"]
|
|
487 |
encoding:NSUTF8StringEncoding
|
|
488 |
error:NULL];
|
|
489 |
NSMutableArray *array = [[NSMutableArray alloc] initWithArray:[string componentsSeparatedByString:@"\n"]];
|
|
490 |
[string release];
|
|
491 |
// remove a trailing "" element
|
|
492 |
[array removeLastObject];
|
|
493 |
self.themeArray = array;
|
|
494 |
[array release];
|
|
495 |
self.mapArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:MAPS_DIRECTORY() error:NULL];
|
|
496 |
|
|
497 |
self.tableView.rowHeight = 42;
|
|
498 |
busy = NO;
|
|
499 |
|
|
500 |
// draw a white background
|
|
501 |
[self restoreBackgroundImage];
|
|
502 |
|
|
503 |
// initialize some "default" values
|
|
504 |
self.sizeLabel.text = NSLocalizedString(@"All",@"");
|
|
505 |
self.slider.value = 0.05f;
|
|
506 |
self.segmentedControl.selectedSegmentIndex = 0;
|
|
507 |
|
|
508 |
self.templateFilterCommand = @"e$template_filter 0";
|
|
509 |
self.mazeSizeCommand = @"e$maze_size 0";
|
|
510 |
self.mapGenCommand = @"e$mapgen 0";
|
|
511 |
self.lastIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
|
|
512 |
|
|
513 |
oldValue = 5;
|
|
514 |
oldPage = 0;
|
|
515 |
}
|
|
516 |
|
|
517 |
-(void) viewDidAppear:(BOOL) animated {
|
|
518 |
[super viewDidAppear:animated];
|
|
519 |
[self updatePreview];
|
|
520 |
}
|
|
521 |
|
|
522 |
#pragma mark -
|
|
523 |
#pragma mark memory
|
|
524 |
-(void) didReceiveMemoryWarning {
|
|
525 |
[super didReceiveMemoryWarning];
|
|
526 |
}
|
|
527 |
|
|
528 |
-(void) viewDidUnload {
|
|
529 |
self.previewButton = nil;
|
|
530 |
self.seedCommand = nil;
|
|
531 |
self.templateFilterCommand = nil;
|
|
532 |
self.mapGenCommand = nil;
|
|
533 |
self.mazeSizeCommand = nil;
|
|
534 |
self.themeCommand = nil;
|
|
535 |
|
|
536 |
self.previewButton = nil;
|
|
537 |
self.tableView = nil;
|
|
538 |
self.maxLabel = nil;
|
|
539 |
self.sizeLabel = nil;
|
|
540 |
self.segmentedControl = nil;
|
|
541 |
self.slider = nil;
|
|
542 |
|
|
543 |
self.lastIndexPath = nil;
|
|
544 |
self.themeArray = nil;
|
|
545 |
self.mapArray = nil;
|
|
546 |
|
|
547 |
[super viewDidUnload];
|
|
548 |
MSG_DIDUNLOAD();
|
|
549 |
}
|
|
550 |
|
|
551 |
-(void) dealloc {
|
|
552 |
[seedCommand release];
|
|
553 |
[templateFilterCommand release];
|
|
554 |
[mapGenCommand release];
|
|
555 |
[mazeSizeCommand release];
|
|
556 |
[themeCommand release];
|
|
557 |
|
|
558 |
[previewButton release];
|
|
559 |
[tableView release];
|
|
560 |
[maxLabel release];
|
|
561 |
[sizeLabel release];
|
|
562 |
[segmentedControl release];
|
|
563 |
[slider release];
|
|
564 |
|
|
565 |
[lastIndexPath release];
|
|
566 |
[themeArray release];
|
|
567 |
[mapArray release];
|
|
568 |
|
|
569 |
[super dealloc];
|
|
570 |
}
|
|
571 |
|
|
572 |
|
|
573 |
@end
|