author | koda |
Sun, 29 Aug 2010 16:55:27 +0200 | |
changeset 3783 | 8e9daf967406 |
parent 3780 | 7c704e69242e |
child 3789 | c3eb56754e92 |
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, |
3705 | 20 |
tableView, maxLabel, sizeLabel, segmentedControl, slider, lastIndexPath, themeArray, mapArray, busy, delegate; |
3547 | 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]; |
|
3697 | 31 |
|
3547 | 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]; |
|
3697 | 40 |
|
3547 | 41 |
if (SDLNet_Init() < 0) { |
3660 | 42 |
DLog(@"SDLNet_Init: %s", SDLNet_GetError()); |
3547 | 43 |
serverQuit = YES; |
44 |
} |
|
3697 | 45 |
|
3547 | 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 |
} |
|
3697 | 51 |
|
3547 | 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 |
} |
|
3697 | 57 |
|
3547 | 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); |
|
3697 | 62 |
|
3547 | 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); |
|
3697 | 68 |
if (NULL != csd) { |
3547 | 69 |
DLog(@"Client found"); |
3697 | 70 |
|
3547 | 71 |
[self sendToEngine:self.seedCommand]; |
72 |
[self sendToEngine:self.templateFilterCommand]; |
|
73 |
[self sendToEngine:self.mapGenCommand]; |
|
74 |
[self sendToEngine:self.mazeSizeCommand]; |
|
75 |
[self sendToEngine:@"!"]; |
|
3697 | 76 |
|
3547 | 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 |
} |
|
3697 | 85 |
|
3547 | 86 |
SDLNet_TCP_Close(sd); |
87 |
SDLNet_Quit(); |
|
88 |
return map; |
|
89 |
} |
|
90 |
||
91 |
-(void) drawingThread { |
|
92 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
|
3697 | 93 |
|
3547 | 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); |
|
3697 | 117 |
|
3547 | 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]; |
|
3697 | 128 |
|
3547 | 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]; |
|
3697 | 131 |
|
3547 | 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 |
|
3697 | 138 |
UIGraphicsBeginImageContext(CGSizeMake(256,128)); |
139 |
CGContextRef context = UIGraphicsGetCurrentContext(); |
|
140 |
UIGraphicsPushContext(context); |
|
3547 | 141 |
|
142 |
CGContextSetRGBFillColor(context, 0.5, 0.5, 0.7, 1.0); |
|
143 |
CGContextFillRect(context,CGRectMake(xc,yc,1,1)); |
|
3697 | 144 |
|
3547 | 145 |
UIGraphicsPopContext(); |
146 |
UIImage *previewImage = UIGraphicsGetImageFromCurrentImageContext(); |
|
147 |
UIGraphicsEndImageContext(); |
|
148 |
*/ |
|
149 |
} |
|
150 |
||
3783 | 151 |
-(IBAction) mapButtonPressed { |
152 |
playSound(@"clickSound"); |
|
153 |
[self updatePreview]; |
|
154 |
} |
|
155 |
||
156 |
-(void) updatePreview { |
|
3547 | 157 |
// don't generate a new preview while it's already generating one |
158 |
if (busy) |
|
159 |
return; |
|
3697 | 160 |
|
3547 | 161 |
// generate a seed |
162 |
CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault); |
|
163 |
NSString *seed = (NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuid); |
|
164 |
CFRelease(uuid); |
|
165 |
NSString *seedCmd = [[NSString alloc] initWithFormat:@"eseed {%@}", seed]; |
|
166 |
[seed release]; |
|
167 |
self.seedCommand = seedCmd; |
|
168 |
[seedCmd release]; |
|
3697 | 169 |
|
3547 | 170 |
NSIndexPath *theIndex; |
171 |
if (segmentedControl.selectedSegmentIndex != 1) { |
|
3670
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
172 |
// remove the current preview and title |
3547 | 173 |
[self.previewButton setImage:nil forState:UIControlStateNormal]; |
3670
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
174 |
[self.previewButton setTitle:nil forState:UIControlStateNormal]; |
3697 | 175 |
|
3670
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
176 |
// don't display preview on slower device, too slow and memory hog |
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
177 |
NSString *modelId = modelType(); |
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
178 |
if ([modelId hasPrefix:@"iPhone1"] || [modelId hasPrefix:@"iPod1,1"] || [modelId hasPrefix:@"iPod2,1"]) { |
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
179 |
busy = NO; |
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
180 |
[self.previewButton setTitle:NSLocalizedString(@"Preview not available",@"") forState:UIControlStateNormal]; |
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
181 |
} else { |
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
182 |
// prevent other events and add an activity while the preview is beign generated |
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
183 |
[self turnOffWidgets]; |
3697 | 184 |
|
3670
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
185 |
// add a very nice spinning wheel |
3697 | 186 |
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] |
3670
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
187 |
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; |
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
188 |
indicator.center = CGPointMake(previewButton.bounds.size.width / 2, previewButton.bounds.size.height / 2); |
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
189 |
indicator.tag = INDICATOR_TAG; |
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
190 |
[indicator startAnimating]; |
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
191 |
[self.previewButton addSubview:indicator]; |
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
192 |
[indicator release]; |
3697 | 193 |
|
3670
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
194 |
// let's draw in a separate thread so the gui can work; at the end it restore other widgets |
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
195 |
[NSThread detachNewThreadSelector:@selector(drawingThread) toTarget:self withObject:nil]; |
4c673e57f0d7
use llvm to compile, don't preview map on wimpier devices, merge vsync, fix iphone launch image
koda
parents:
3662
diff
changeset
|
196 |
} |
3697 | 197 |
|
3547 | 198 |
theIndex = [NSIndexPath indexPathForRow:(random()%[self.themeArray count]) inSection:0]; |
199 |
} else { |
|
200 |
theIndex = [NSIndexPath indexPathForRow:(random()%[self.mapArray count]) inSection:0]; |
|
201 |
} |
|
202 |
[self.tableView reloadData]; |
|
203 |
[self tableView:self.tableView didSelectRowAtIndexPath:theIndex]; |
|
204 |
[self.tableView scrollToRowAtIndexPath:theIndex atScrollPosition:UITableViewScrollPositionNone animated:YES]; |
|
205 |
} |
|
206 |
||
3642 | 207 |
// instead of drawing a random map we load an image; this function is called by didSelectRowAtIndexPath only |
3547 | 208 |
-(void) updatePreviewWithMap:(NSInteger) index { |
209 |
// change the preview button |
|
210 |
NSString *fileImage = [[NSString alloc] initWithFormat:@"%@/%@/preview.png", MAPS_DIRECTORY(),[self.mapArray objectAtIndex:index]]; |
|
211 |
UIImage *image = [[UIImage alloc] initWithContentsOfFile:fileImage]; |
|
212 |
[fileImage release]; |
|
213 |
[self.previewButton setImage:[image makeRoundCornersOfSize:CGSizeMake(12, 12)] forState:UIControlStateNormal]; |
|
214 |
[image release]; |
|
3697 | 215 |
|
3547 | 216 |
// update label |
217 |
maxHogs = 18; |
|
218 |
NSString *fileCfg = [[NSString alloc] initWithFormat:@"%@/%@/map.cfg", MAPS_DIRECTORY(),[self.mapArray objectAtIndex:index]]; |
|
219 |
NSString *contents = [[NSString alloc] initWithContentsOfFile:fileCfg encoding:NSUTF8StringEncoding error:NULL]; |
|
220 |
[fileCfg release]; |
|
221 |
NSArray *split = [contents componentsSeparatedByString:@"\n"]; |
|
3642 | 222 |
[contents release]; |
3547 | 223 |
|
3642 | 224 |
// set the theme and map here |
225 |
self.themeCommand = [NSString stringWithFormat:@"etheme %@", [split objectAtIndex:0]]; |
|
226 |
self.staticMapCommand = [NSString stringWithFormat:@"emap %@", [self.mapArray objectAtIndex:index]]; |
|
3697 | 227 |
|
228 |
// if the number is not set we keep 18 standard; |
|
3547 | 229 |
// sometimes it's not set but there are trailing characters, we get around them with the second equation |
230 |
if ([split count] > 1 && [[split objectAtIndex:1] intValue] > 0) |
|
231 |
maxHogs = [[split objectAtIndex:1] intValue]; |
|
232 |
NSString *max = [[NSString alloc] initWithFormat:@"%d",maxHogs]; |
|
233 |
self.maxLabel.text = max; |
|
234 |
[max release]; |
|
235 |
} |
|
236 |
||
237 |
-(void) turnOffWidgets { |
|
238 |
busy = YES; |
|
239 |
self.previewButton.alpha = 0.5f; |
|
240 |
self.previewButton.enabled = NO; |
|
241 |
self.maxLabel.text = @"..."; |
|
242 |
self.segmentedControl.enabled = NO; |
|
243 |
self.slider.enabled = NO; |
|
244 |
} |
|
245 |
||
246 |
-(void) turnOnWidgets { |
|
247 |
self.previewButton.alpha = 1.0f; |
|
248 |
self.previewButton.enabled = YES; |
|
249 |
self.segmentedControl.enabled = YES; |
|
250 |
self.slider.enabled = YES; |
|
251 |
busy = NO; |
|
3697 | 252 |
|
3547 | 253 |
UIActivityIndicatorView *indicator = (UIActivityIndicatorView *)[self.previewButton viewWithTag:INDICATOR_TAG]; |
254 |
if (indicator) { |
|
255 |
[indicator stopAnimating]; |
|
256 |
[indicator removeFromSuperview]; |
|
257 |
} |
|
258 |
} |
|
3697 | 259 |
|
3547 | 260 |
-(void) setLabelText:(NSString *)str { |
261 |
self.maxLabel.text = str; |
|
262 |
} |
|
263 |
||
264 |
-(void) setButtonImage:(UIImage *)img { |
|
265 |
[self.previewButton setBackgroundImage:img forState:UIControlStateNormal]; |
|
266 |
} |
|
267 |
||
268 |
-(void) restoreBackgroundImage { |
|
269 |
// white rounded rectangle as background image for previewButton |
|
3697 | 270 |
UIGraphicsBeginImageContext(CGSizeMake(256,128)); |
271 |
CGContextRef context = UIGraphicsGetCurrentContext(); |
|
272 |
UIGraphicsPushContext(context); |
|
3547 | 273 |
|
274 |
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0); |
|
275 |
CGContextFillRect(context,CGRectMake(0,0,256,128)); |
|
3697 | 276 |
|
3547 | 277 |
UIGraphicsPopContext(); |
278 |
UIImage *bkgImg = UIGraphicsGetImageFromCurrentImageContext(); |
|
279 |
UIGraphicsEndImageContext(); |
|
280 |
[self.previewButton setBackgroundImage:[bkgImg makeRoundCornersOfSize:CGSizeMake(12, 12)] forState:UIControlStateNormal]; |
|
281 |
} |
|
282 |
||
283 |
#pragma mark - |
|
284 |
#pragma mark Table view data source |
|
285 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
|
286 |
return 1; |
|
287 |
} |
|
288 |
||
289 |
-(NSInteger) tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger) section { |
|
290 |
if (self.segmentedControl.selectedSegmentIndex != 1) |
|
291 |
return [themeArray count]; |
|
292 |
else |
|
293 |
return [mapArray count]; |
|
294 |
} |
|
295 |
||
296 |
-(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
297 |
static NSString *CellIdentifier = @"Cell"; |
|
298 |
NSInteger row = [indexPath row]; |
|
3697 | 299 |
|
3547 | 300 |
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
3697 | 301 |
if (cell == nil) |
3547 | 302 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
3697 | 303 |
|
3623 | 304 |
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { |
3703
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
305 |
cell.textLabel.textColor = UICOLOR_HW_YELLOW_TEXT; |
3623 | 306 |
} |
3697 | 307 |
|
3547 | 308 |
if (self.segmentedControl.selectedSegmentIndex != 1) { |
309 |
// the % prevents a strange bug that occurs sporadically |
|
310 |
NSString *themeName = [self.themeArray objectAtIndex:row % [self.themeArray count]]; |
|
311 |
cell.textLabel.text = themeName; |
|
312 |
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@/icon.png",THEMES_DIRECTORY(),themeName]]; |
|
313 |
cell.imageView.image = image; |
|
314 |
[image release]; |
|
315 |
} else { |
|
316 |
cell.textLabel.text = [self.mapArray objectAtIndex:row]; |
|
317 |
cell.imageView.image = nil; |
|
318 |
} |
|
3697 | 319 |
|
3703
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
320 |
if (row == [self.lastIndexPath row]) { |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
321 |
UIImageView *checkbox = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:@"checkbox.png"]]; |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
322 |
cell.accessoryView = checkbox; |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
323 |
[checkbox release]; |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
324 |
} else |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
325 |
cell.accessoryView = nil; |
3547 | 326 |
|
3703
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
327 |
cell.backgroundColor = [UIColor blackColor]; |
3547 | 328 |
return cell; |
329 |
} |
|
330 |
||
331 |
||
332 |
#pragma mark - |
|
333 |
#pragma mark Table view delegate |
|
334 |
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|
335 |
int newRow = [indexPath row]; |
|
336 |
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1; |
|
3697 | 337 |
|
3547 | 338 |
if (newRow != oldRow) { |
339 |
if (self.segmentedControl.selectedSegmentIndex != 1) { |
|
3697 | 340 |
NSString *theme = [self.themeArray objectAtIndex:newRow]; |
3642 | 341 |
self.themeCommand = [NSString stringWithFormat:@"etheme %@", theme]; |
342 |
} else { |
|
343 |
// theme and map are set in the function below |
|
3547 | 344 |
[self updatePreviewWithMap:newRow]; |
3642 | 345 |
} |
3697 | 346 |
|
347 |
UITableViewCell *newCell = [aTableView cellForRowAtIndexPath:indexPath]; |
|
3703
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
348 |
UIImageView *checkbox = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:@"checkbox.png"]]; |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
349 |
newCell.accessoryView = checkbox; |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
350 |
[checkbox release]; |
3547 | 351 |
UITableViewCell *oldCell = [aTableView cellForRowAtIndexPath:self.lastIndexPath]; |
3703
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
352 |
oldCell.accessoryView = nil; |
3547 | 353 |
|
354 |
self.lastIndexPath = indexPath; |
|
355 |
[aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; |
|
356 |
} |
|
357 |
[aTableView deselectRowAtIndexPath:indexPath animated:YES]; |
|
358 |
} |
|
359 |
||
360 |
#pragma mark - |
|
361 |
#pragma mark slider & segmentedControl |
|
362 |
// this updates the label and the command keys when the slider is moved, depending of the selection in segmentedControl |
|
363 |
// no methods are called by this routine and you can pass nil to it |
|
364 |
-(IBAction) sliderChanged:(id) sender { |
|
365 |
NSString *labelText; |
|
366 |
NSString *templateCommand; |
|
367 |
NSString *mazeCommand; |
|
3697 | 368 |
|
3547 | 369 |
switch ((int)(self.slider.value*100)) { |
370 |
case 0: |
|
371 |
if (self.segmentedControl.selectedSegmentIndex == 0) { |
|
372 |
labelText = NSLocalizedString(@"Wacky",@""); |
|
373 |
} else { |
|
374 |
labelText = NSLocalizedString(@"Large Floating Islands",@""); |
|
375 |
} |
|
376 |
templateCommand = @"e$template_filter 5"; |
|
377 |
mazeCommand = @"e$maze_size 5"; |
|
378 |
break; |
|
379 |
case 1: |
|
380 |
if (self.segmentedControl.selectedSegmentIndex == 0) { |
|
381 |
labelText = NSLocalizedString(@"Cavern",@""); |
|
382 |
} else { |
|
383 |
labelText = NSLocalizedString(@"Medium Floating Islands",@""); |
|
384 |
} |
|
385 |
templateCommand = @"e$template_filter 4"; |
|
386 |
mazeCommand = @"e$maze_size 4"; |
|
387 |
break; |
|
388 |
case 2: |
|
389 |
if (self.segmentedControl.selectedSegmentIndex == 0) { |
|
390 |
labelText = NSLocalizedString(@"Small",@""); |
|
391 |
} else { |
|
392 |
labelText = NSLocalizedString(@"Small Floating Islands",@""); |
|
393 |
} |
|
394 |
templateCommand = @"e$template_filter 1"; |
|
395 |
mazeCommand = @"e$maze_size 3"; |
|
396 |
break; |
|
397 |
case 3: |
|
398 |
if (self.segmentedControl.selectedSegmentIndex == 0) { |
|
399 |
labelText = NSLocalizedString(@"Medium",@""); |
|
400 |
} else { |
|
401 |
labelText = NSLocalizedString(@"Large Tunnels",@""); |
|
402 |
} |
|
403 |
templateCommand = @"e$template_filter 2"; |
|
404 |
mazeCommand = @"e$maze_size 2"; |
|
405 |
break; |
|
406 |
case 4: |
|
407 |
if (self.segmentedControl.selectedSegmentIndex == 0) { |
|
408 |
labelText = NSLocalizedString(@"Large",@""); |
|
409 |
} else { |
|
410 |
labelText = NSLocalizedString(@"Medium Tunnels",@""); |
|
411 |
} |
|
412 |
templateCommand = @"e$template_filter 3"; |
|
413 |
mazeCommand = @"e$maze_size 1"; |
|
414 |
break; |
|
415 |
case 5: |
|
416 |
if (self.segmentedControl.selectedSegmentIndex == 0) { |
|
417 |
labelText = NSLocalizedString(@"All",@""); |
|
418 |
} else { |
|
419 |
labelText = NSLocalizedString(@"Small Tunnels",@""); |
|
420 |
} |
|
421 |
templateCommand = @"e$template_filter 0"; |
|
422 |
mazeCommand = @"e$maze_size 0"; |
|
423 |
break; |
|
424 |
default: |
|
425 |
labelText = nil; |
|
426 |
templateCommand = nil; |
|
427 |
mazeCommand = nil; |
|
428 |
break; |
|
429 |
} |
|
3697 | 430 |
|
3547 | 431 |
self.sizeLabel.text = labelText; |
432 |
self.templateFilterCommand = templateCommand; |
|
433 |
self.mazeSizeCommand = mazeCommand; |
|
434 |
} |
|
435 |
||
436 |
// update preview (if not busy and if its value really changed) as soon as the user lifts its finger up |
|
437 |
-(IBAction) sliderEndedChanging:(id) sender { |
|
438 |
int num = (int) (self.slider.value * 100); |
|
439 |
if (oldValue != num) { |
|
440 |
[self updatePreview]; |
|
441 |
oldValue = num; |
|
442 |
} |
|
3783 | 443 |
playSound(@"clickSound"); |
3547 | 444 |
} |
445 |
||
3697 | 446 |
// perform actions based on the activated section, then call updatePreview to visually update the selection |
3547 | 447 |
// updatePreview will call didSelectRowAtIndexPath which will call the right update routine) |
448 |
// and if necessary update the table with a slide animation |
|
449 |
-(IBAction) segmentedControlChanged:(id) sender { |
|
3642 | 450 |
NSString *mapgen, *staticmap; |
3547 | 451 |
NSInteger newPage = self.segmentedControl.selectedSegmentIndex; |
3697 | 452 |
|
3783 | 453 |
playSound(@"selSound"); |
3547 | 454 |
switch (newPage) { |
455 |
case 0: // Random |
|
456 |
mapgen = @"e$mapgen 0"; |
|
3642 | 457 |
staticmap = @""; |
3547 | 458 |
[self sliderChanged:nil]; |
459 |
self.slider.enabled = YES; |
|
460 |
break; |
|
3697 | 461 |
|
3547 | 462 |
case 1: // Map |
463 |
mapgen = @"e$mapgen 0"; |
|
3642 | 464 |
// dummy value, everything is set by -updatePreview -> -didSelectRowAtIndexPath -> -updatePreviewWithMap |
465 |
staticmap = @"map Bamboo"; |
|
3547 | 466 |
self.slider.enabled = NO; |
3705 | 467 |
self.sizeLabel.text = @""; |
3547 | 468 |
[self restoreBackgroundImage]; |
469 |
break; |
|
3697 | 470 |
|
3547 | 471 |
case 2: // Maze |
472 |
mapgen = @"e$mapgen 1"; |
|
3642 | 473 |
staticmap = @""; |
3547 | 474 |
[self sliderChanged:nil]; |
475 |
self.slider.enabled = YES; |
|
476 |
break; |
|
3697 | 477 |
|
3547 | 478 |
default: |
479 |
mapgen = nil; |
|
3642 | 480 |
staticmap = nil; |
3547 | 481 |
break; |
482 |
} |
|
483 |
self.mapGenCommand = mapgen; |
|
3642 | 484 |
self.staticMapCommand = staticmap; |
3547 | 485 |
[self updatePreview]; |
3697 | 486 |
|
3547 | 487 |
// nice animation for updating the table when appropriate (on iphone) |
488 |
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) |
|
489 |
if (((oldPage == 0 || oldPage == 2) && newPage == 1) || |
|
490 |
(oldPage == 1 && (newPage == 0 || newPage == 2))) { |
|
491 |
[UIView beginAnimations:@"moving out table" context:NULL]; |
|
492 |
self.tableView.frame = CGRectMake(480, 0, 185, 276); |
|
493 |
[UIView commitAnimations]; |
|
494 |
[self performSelector:@selector(moveTable) withObject:nil afterDelay:0.2]; |
|
495 |
} |
|
496 |
oldPage = newPage; |
|
497 |
} |
|
498 |
||
499 |
// update data when table is not visible and then show it |
|
500 |
-(void) moveTable { |
|
501 |
[self.tableView reloadData]; |
|
3697 | 502 |
|
3547 | 503 |
[UIView beginAnimations:@"moving in table" context:NULL]; |
504 |
self.tableView.frame = CGRectMake(295, 0, 185, 276); |
|
505 |
[UIView commitAnimations]; |
|
506 |
} |
|
507 |
||
508 |
#pragma mark - |
|
509 |
#pragma mark view management |
|
510 |
-(void) viewDidLoad { |
|
511 |
[super viewDidLoad]; |
|
3697 | 512 |
|
3547 | 513 |
srandom(time(NULL)); |
3697 | 514 |
|
3547 | 515 |
CGSize screenSize = [[UIScreen mainScreen] bounds].size; |
516 |
self.view.frame = CGRectMake(0, 0, screenSize.height, screenSize.width - 44); |
|
517 |
||
518 |
// themes.cfg contains all the user-selectable themes |
|
519 |
NSString *string = [[NSString alloc] initWithContentsOfFile:[THEMES_DIRECTORY() stringByAppendingString:@"/themes.cfg"] |
|
3697 | 520 |
encoding:NSUTF8StringEncoding |
3547 | 521 |
error:NULL]; |
522 |
NSMutableArray *array = [[NSMutableArray alloc] initWithArray:[string componentsSeparatedByString:@"\n"]]; |
|
523 |
[string release]; |
|
524 |
// remove a trailing "" element |
|
525 |
[array removeLastObject]; |
|
526 |
self.themeArray = array; |
|
527 |
[array release]; |
|
528 |
self.mapArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:MAPS_DIRECTORY() error:NULL]; |
|
529 |
||
530 |
busy = NO; |
|
3697 | 531 |
|
3547 | 532 |
// draw a white background |
533 |
[self restoreBackgroundImage]; |
|
3697 | 534 |
|
3547 | 535 |
// initialize some "default" values |
536 |
self.sizeLabel.text = NSLocalizedString(@"All",@""); |
|
537 |
self.slider.value = 0.05f; |
|
3697 | 538 |
|
3780
7c704e69242e
fade in when returning from game, labels rewritten so that they are drawn at runtime (more flexibility with i18n and iphone support)
koda
parents:
3705
diff
changeset
|
539 |
// select a map at first because it's faster |
7c704e69242e
fade in when returning from game, labels rewritten so that they are drawn at runtime (more flexibility with i18n and iphone support)
koda
parents:
3705
diff
changeset
|
540 |
self.segmentedControl.selectedSegmentIndex = 1; |
3547 | 541 |
|
542 |
self.templateFilterCommand = @"e$template_filter 0"; |
|
543 |
self.mazeSizeCommand = @"e$maze_size 0"; |
|
544 |
self.mapGenCommand = @"e$mapgen 0"; |
|
3642 | 545 |
self.staticMapCommand = @""; |
3697 | 546 |
|
3547 | 547 |
self.lastIndexPath = [NSIndexPath indexPathForRow:0 inSection:0]; |
3697 | 548 |
|
3547 | 549 |
oldValue = 5; |
550 |
oldPage = 0; |
|
3703
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
551 |
|
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
552 |
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
553 |
[self.tableView setBackgroundView:nil]; |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
554 |
self.view.backgroundColor = [UIColor clearColor]; |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
555 |
self.tableView.separatorColor = UICOLOR_HW_YELLOW_BODER; |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
556 |
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
557 |
self.tableView.rowHeight = 45; |
12d17c6e8855
halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents:
3697
diff
changeset
|
558 |
} |
3547 | 559 |
} |
560 |
||
561 |
-(void) viewDidAppear:(BOOL) animated { |
|
562 |
[super viewDidAppear:animated]; |
|
563 |
[self updatePreview]; |
|
564 |
} |
|
565 |
||
566 |
#pragma mark - |
|
3705 | 567 |
#pragma mark delegate functions for iPad |
568 |
-(IBAction) buttonPressed:(id) sender { |
|
569 |
if (self.delegate != nil && [delegate respondsToSelector:@selector(buttonPressed:)]) |
|
570 |
[self.delegate buttonPressed:(UIButton *)sender]; |
|
571 |
} |
|
572 |
||
573 |
#pragma mark - |
|
3547 | 574 |
-(void) didReceiveMemoryWarning { |
575 |
[super didReceiveMemoryWarning]; |
|
3705 | 576 |
//[previewButton setImage:nil forState:UIControlStateNormal]; |
3547 | 577 |
} |
578 |
||
579 |
-(void) viewDidUnload { |
|
3705 | 580 |
self.delegate = nil; |
581 |
||
3547 | 582 |
self.previewButton = nil; |
583 |
self.seedCommand = nil; |
|
584 |
self.templateFilterCommand = nil; |
|
585 |
self.mapGenCommand = nil; |
|
586 |
self.mazeSizeCommand = nil; |
|
587 |
self.themeCommand = nil; |
|
3642 | 588 |
self.staticMapCommand = nil; |
3697 | 589 |
|
3547 | 590 |
self.previewButton = nil; |
591 |
self.tableView = nil; |
|
592 |
self.maxLabel = nil; |
|
593 |
self.sizeLabel = nil; |
|
594 |
self.segmentedControl = nil; |
|
595 |
self.slider = nil; |
|
3697 | 596 |
|
3547 | 597 |
self.lastIndexPath = nil; |
598 |
self.themeArray = nil; |
|
599 |
self.mapArray = nil; |
|
3697 | 600 |
|
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
|
601 |
MSG_DIDUNLOAD(); |
3547 | 602 |
[super viewDidUnload]; |
603 |
} |
|
604 |
||
605 |
-(void) dealloc { |
|
3705 | 606 |
self.delegate = nil; |
607 |
||
3547 | 608 |
[seedCommand release]; |
609 |
[templateFilterCommand release]; |
|
610 |
[mapGenCommand release]; |
|
611 |
[mazeSizeCommand release]; |
|
612 |
[themeCommand release]; |
|
3642 | 613 |
[staticMapCommand release]; |
3697 | 614 |
|
3547 | 615 |
[previewButton release]; |
616 |
[tableView release]; |
|
617 |
[maxLabel release]; |
|
618 |
[sizeLabel release]; |
|
619 |
[segmentedControl release]; |
|
620 |
[slider release]; |
|
3697 | 621 |
|
3547 | 622 |
[lastIndexPath release]; |
623 |
[themeArray release]; |
|
624 |
[mapArray release]; |
|
3697 | 625 |
|
3547 | 626 |
[super dealloc]; |
627 |
} |
|
628 |
||
629 |
||
630 |
@end |